// SLAM main JavaScript Document

var current = "";

$(document).ready(function() {
	
	if($.address.baseURL() == "http://slam.com.au" || $.address.baseURL() == "http://www.slam.com.au") {return} else {$.address.value($.address.baseURL().split('slam.com.au')[1])};
	initSlider($('#banner-content').children()[0]);
	$('#content-area').jScrollPane();

})

//service functions

	var IsText       = function (str) { return /[0-9_;:'!~?=+<|>]/g.test(str); };
	var IsNumeric    = function (str) { return /^[0-9-\+\(\)\s]+z/.test(str + "z"); };
	var IsValidEmail = function (str) { return /^[a-zA-Z0-9_\.]+@[a-z0-9_\.]+.[a-z]{2,3}$/.test(str); };

function initSlider(id) {
			$(id).nivoSlider({
				effect:'fade',
				controlNav:true,
				animSpeed:500, // Slide transition speed
        		pauseTime:5000, 
				directionNav:false
			});
			
			if(document.getElementById('about_slider')) {
				$(".nivo-controlNav").css('left', '450px')
			};
}

function validInput(){
	
		$('input#file').uniform();
		
		$('input, textarea').focus(function() {$(this).addClass('focus')}).blur(function () {$(this).removeClass('focus');});
		$('.required,').parent().next().find('input, textarea').blur(function () {
			
			if($(this).val() == "") {$(this).addClass('mandatory')} else
			{$(this).removeClass('mandatory')}
		
		});
}



function initFancy() {
	if(document.getElementById("gallery_content")){
		$("div.gallery_container a:not(.y_project)").fancybox({
				'titlePosition'	: 'over',
				'width'				: '100%',
				'height'			: '100%',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'padding' : '0'
		});
	$('.image_shadow').hover(
		function () {
    		$(this).addClass('hover')}, 
		function () {
			$(this).removeClass('hover')}
	);
	}
}

function initGooglemap() {
	$(document).ready(function(){
		var map = new GMap2(document.getElementById('googlemap'));
		var SLAM = new GLatLng(-37.797318,144.976406);
		map.setCenter(SLAM, 15);
		map.setUIToDefault();
		map.addOverlay(new GMarker(SLAM));

	});
}

function AjaxLoad(href, type, instance) {
	//alert(href);
	//alert(type);
	if(instance == "banner"){		
		$.ajax({
    		type: "POST",
			url: href,
			data: "&request="+type,
			dataType: "html",
			beforeSend: function () {
				customFadeOut('.jspPane');
				if(type ==  "ajaxf" || type ==  "ajaxm"){
					customFadeOut('#banner-content');					
				}				
			},
			success: function (data){
				result = data.split("[page]");
				$('title').remove();
				$('head').prepend(result[1]);
				$('.jspPane').html(result[2]);
				$('#tabs-container').html(result[3]);
				if(type ==  "ajaxf" || type ==  "ajaxm"){
					$('#banner-content').html(result[4]);
					
				}
			},
			complete: function () {
				customFadeIn('.jspPane');
				$('#content-area').jScrollPane();
				if(type ==  "ajaxf" || type == "ajax"){
					$('#banner-content div img').load(function() {customFadeIn('#banner-content');});
					initSlider($('#banner-content').children()[0]);	
				}
				if(type ==  "ajaxm"){
					
					customFadeIn('#banner-content');
					initGooglemap();
				}
				ajaxLinks();
				validInput();
						
			}
			
		});
	} else {
		$.ajax({
    		type: "POST",
			url: href,
			data: "&request="+type,
			dataType: "html",
			beforeSend: function () {
				if(type ==  "ajaxf"){
					customFadeOut('.jspPane');						
				}
				customFadeOut('#banner-content');	
			},
			success: function (data){
				result = data.split("[page]");
				if(type == "ajaxf"){
					$('title').remove();
					$('head').prepend(result[1]);
					$('.jspPane').html(result[2]);
					$('#tabs-container').html(result[3]);
					$('#banner-content').html(result[4]);
				} 
				else {
				$('#banner-content').html(result[0]);
				
				}
			},
			complete: function () {
				if(type ==  "ajaxf"){
					customFadeIn('.jspPane');
					$('#content-area').jScrollPane();
				}
				$('.gallery_container a img').load(function() {customFadeIn('#banner-content');});
				ajaxLinks();
				initFancy();
			}
			
		});
	}
	
};

function ajaxLinks () {
	
	$('a#logo, #main_menu li a:not(#login), #gallery_nav_right a, #gallery_nav_left a, #tabs-container a, a.button, a.y_project').bind('click', function(i) {
		var a = this.href;
		
		
		
			
			
		$.address.value(a.replace('http://www.slam.com.au', ''))
		return false;
	});
};

function customFadeOut(id) {
	if (!$.browser.msie) {$(id).fadeTo(300, 0)};
		$(id).html('').css('background', 'url(../images/preloader2.gif) no-repeat 50% 50%');}
			
function customFadeOut_noloader(id) {
	if (!$.browser.msie) {jQuery(id).fadeTo(100, 0)};};

function customFadeIn(id) {
	if (!$.browser.msie) {$(id).fadeTo(300, 1)};
	$(id).css('background', 'none');
}
		
function customFadeIn_noloader(id) {
	if ($.browser.msie) {return} 
		else {$(id).fadeTo(100, 1)};
		};
		
// Here we go

$(document).ready(function() {

	$.address.change(function(event) {
		
		var hash = location.hash.replace('#','/') || '';
    	var query_string = location.search || '';
	    _gaq.push(['_trackPageview',location.pathname + query_string  + hash]);

		

		if(event.value == '/' || event.value =='/index/wespecialisein')	{ 
			if(document.getElementById("main_slider")){
				AjaxLoad(event.value, "ajax", "banner");
			} else {
				AjaxLoad(event.value, "ajaxf", "banner");
			}
		}
		
		if(event.value.indexOf('/aboutus') != -1 || event.value.indexOf('/contacts') != -1)	{
			if(event.value.indexOf('/googlemap') != -1)	{ 
					AjaxLoad(event.value, "ajaxm", "banner");			
				} else {
					if(document.getElementById("about_slider") && event.value.split('/')[1] == current.split('/')[1]){
						AjaxLoad(event.value, "ajax", "banner");
					} else {
						AjaxLoad(event.value, "ajaxf", "banner");
					}
				}
		}
		if(event.value.indexOf('/yourproject') != -1)	{ 
				if(event.value.indexOf('/page') != -1 && event.value.split('/')[2] == current.split('/')[2])	{ 
					AjaxLoad(event.value, "ajaxp", "gallery");			
				} else {
					AjaxLoad(event.value, "ajaxf", "gallery");
				}
		}

		//Active elements
		
		
			current = $.address.path();

		$('#main_menu li a').removeClass('current');
	
		
		if(event.value == '/' || event.value =='/index/wespecialisein') {			
			$('a#home').addClass('current');					
		} else {
			$('#main_menu li a').each(function() {
				if($(this).attr("href").split('/')[1] == event.value.split('/')[1]) {
				$(this).addClass('current');
				}
			});
			
		}
		
	});
  

});


