$(document).ready(function() {
	/* Back to Top */	   
	$('a.back-to-top').click(function(){
		 $('html, body').animate({scrollTop: '0px'}, 800);
		 return false;
	});
	
	$('a.popout-close').click(function(){
		$('#popout').hide('slow');
		return false;
	});
	
	$.localScroll();
	
	$('.censored').click(function(){
		$('.popout-censored').show('slow');
		return false;
	});
	
	$("body.page-portfolio li.off a img").lazyload({
		placeholder : "/images/layout/blue.jpg",
		effect      : "fadeIn",
		threshold 	: -200
	});

	// PRINT PAGE
	$('a.print').click(function(){
		if (window.print) { window.print(); } else { alert("Sorry, your browser doesn't support this feature."); }
		return false;
	});
	
	//Portolio Scripts
	//Simulate over with click
	// initialize scrollable
	$('.scrollable').waitForImages(function() {
		$(this).jcarousel({
	    	wrap: 'circular',
			buttonNextEvent: 'mouseover',
			buttonPrevEvent: 'mouseover'
	    });
		$('.jcarousel-next').click(function() {
		  $(this).mouseover();
		});
		$('.jcarousel-prev').click(function() {
		  $(this).mouseover();
		});
	});
	
	//Check if url hash value exists (for bookmark)
	$.history.init(pageload);	
	    
	//highlight the selected link
	$('a[href=' + document.location.hash + ']').addClass('selected');
	
	$(".left-arrow").live('click', function () {
		var nextid = $('ul.items li a.overly').closest('li').prev().children('a[rel=ajax]').attr('href');
		if(nextid == null)
		{
			nextid = $('ul.items li:last a[rel=ajax]').attr('href');
			nextid = nextid.replace(/^.*#/, '');
			var data = 'page=' + nextid;
			$('ul.items li a.overly').removeClass('overly');
			$('ul.items li:last a[rel=ajax]').addClass('overly');
		}
		else
		{
			nextid = nextid.replace(/^.*#/, '');
			var data = 'page=' + nextid;
			$('ul.items li a.overly').removeClass('overly').closest('li').prev().children('a[rel=ajax]').addClass('overly');
		}
		
		$.ajax({
			url: "/content/portfolio/loader.php",	
			type: "GET",		
			data: data,		
			cache: false,
			success: function (html) {	
				//hide the progress bar
				$('#loading').hide();	
				//add the content retrieved from ajax and put it in the #content div
				$('#contentajax').html(html);
				//display the body with fadeIn transition
				$('#contentajax').fadeIn('fast');	
			}		
		});
		return false;
	});
	
	$(".right-arrow").live('click', function () {
		var nextid = $('ul.items li a.overly').closest('li').next().children('a[rel=ajax]').attr('href');
		if(nextid == null)
		{
			nextid = $('ul.items li:first a[rel=ajax]').attr('href');
			nextid = nextid.replace(/^.*#/, '');
			var data = 'page=' + nextid;
			$('ul.items li a.overly').removeClass('overly');
			$('ul.items li:first a[rel=ajax]').addClass('overly');
		}
		else
		{
			nextid = nextid.replace(/^.*#/, '');
			var data = 'page=' + nextid;
			$('ul.items li a.overly').removeClass('overly').closest('li').next().children('a[rel=ajax]').addClass('overly');
		}
		
		$.ajax({
			url: "/content/portfolio/loader.php",	
			type: "GET",		
			data: data,		
			cache: false,
			success: function (html) {	
				//hide the progress bar
				$('#loading').hide();	
				//add the content retrieved from ajax and put it in the #content div
				$('#contentajax').html(html);
				//display the body with fadeIn transition
				$('#contentajax').fadeIn('fast');	
			}		
		});
		return false;
	});
	
	// Search for link with REL set to ajax
	$('ul.items li a[rel=ajax]').live('click', function () {
		
		//grab the full url
		var hash = this.href;
		//remove the # value
		hash = hash.replace(/^.*#/, '');
		
		//for back button
	 	$.history.load(hash);	
	 	
	 	//clear the selected class and add the class class to the selected link
	 	$('a[rel=ajax]').removeClass('overly');
	 	$(this).addClass('overly');
	 	
	 	//hide the content and show the progress bar
	 	jQuery("#contentajax").fadeOut('fast');
		
	 	//$('#loading').show();
	 	
	 	//run the ajax
		getPage();
	
		//cancel the anchor tag behaviour
		return false;
	});
	
	$('#sort a[rel=ajax]').click(function () {
		
	 	//clear the selected class and add the class class to the selected link
	 	$('a[rel=ajax]').removeClass('selected');
	 	$(this).addClass('selected');
	 	
	 	//hide the content and show the progress bar
	 	jQuery("#slider").fadeOut('fast');

	 	$('#loading').show();

		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		
		//for back button
	 	$.history.load(hash);	
		

		var data = 'cat=' + hash;
		$.ajax({
			url: "/content/portfolio/loader-slider.php",	
			type: "GET",		
			data: data,		
			cache: false,
			success: function (html) {	

				//hide the progress bar
				$('#loading').hide();	

				//add the content retrieved from ajax and put it in the #content div
				$('#slider').html(html);
				
				//display the body with fadeIn transition
				$('.scrollable').waitForImages(function() {
					$('#slider').fadeIn('fast');	
					$(this).jcarousel({
				    	wrap: 'circular',
						buttonNextEvent: 'mouseover',
						buttonPrevEvent: 'mouseover',
						initCallback: getfirst()
				    });
					$('.jcarousel-next').click(function() {
					  $(this).mouseover();
					});
					$('.jcarousel-prev').click(function() {
					  $(this).mouseover();
					});
					
				});

			}	//end success
		}); //end ajax
		
		//cancel the anchor tag behaviour
		return false;
	});
	
	
	function getfirst() {
		var nextid = $('ul.items li:first a').attr('href');
		nextid = nextid.replace(/^.*#/, '');
		var data = 'page=' + nextid;		 
		$.ajax({
			url: "/content/portfolio/loader.php",	
			type: "GET",		
			data: data,		
			cache: false,
			success: function (html) {	

				//hide the progress bar
				$('#loading').hide();	
				//add the content retrieved from ajax and put it in the #content div
				$('#contentajax').html(html);

				//display the body with fadeIn transition
				$('#contentajax').fadeIn('fast');	
				return false;
			}		
		});
		
	}
	function pageload(hash) {
		//if hash value exists, run the ajax
		clean = hash.split('_');	
		 
		if (clean[1] == parseFloat(clean[1])) {
			if (hash) getPage();  
		} else { 
			if (hash) getSlider();    
			if (hash) getfirst();
			$('#contentajax').hide();
		}
	}
	function sliderload(hash) {
		//if hash value exists, run the ajax
		//console.log(hash);
		if (hash) getSlider();    
	}
	
	function getSlider() {
		 
		var data = 'cat=' + encodeURIComponent(document.location.hash);
		$.ajax({
			url: "/content/portfolio/loader-slider.php",	
			type: "GET",		
			data: data,		
			cache: false,
			success: function (html) {	

				//hide the progress bar
				$('#loading').hide();	

				//add the content retrieved from ajax and put it in the #content div
				$('#slider').html(html);
				
				//display the body with fadeIn transition
				$('.scrollable').waitForImages(function() {
					$('#slider').fadeIn('fast');	
					$(this).jcarousel({
				    	wrap: 'circular',
						buttonNextEvent: 'mouseover',
						buttonPrevEvent: 'mouseover',
						initCallback: getfirst()
				    });
					$('.jcarousel-next').click(function() {
					  $(this).mouseover();
					});
					$('.jcarousel-prev').click(function() {
					  $(this).mouseover();
					});
					
				});

			}	//end success
		}); //end ajax
	}
	
	function getPage() {
		//generate the parameter for the php script
		var data = 'page=' + encodeURIComponent(document.location.hash);
		$.ajax({
			url: "/content/portfolio/loader.php",	
			type: "GET",		
			data: data,		
			cache: false,
			success: function (html) {	

				//hide the progress bar
				$('#loading').hide();	
				//add the content retrieved from ajax and put it in the #content div
				$('#contentajax').html(html);

				//display the body with fadeIn transition
				$('#contentajax').fadeIn('fast');	
				$('html, body').animate({scrollTop: '250px'}, 600);
				return false;
			}		
		});
	}
	

	// Footer - Click to Expand
	$('.acc-container').hide(); // Close all containers
	$('.acc-trigger').click(function(){
		if( $(this).prev().is(':hidden') ) // If immediate next container is closed
		{
			$(this).toggleClass('active').prev().slideDown();
			$(this).html('<a href="#" class="expand">- COLLAPSE LIST</a>');
		}
		else
		{
			$(this).toggleClass('active').prev().slideUp();
			$(this).html('<a href="#" class="expand">+ Expand List</a>');
		}
		return false; // Prevent the browser jump to the anchor link
	});
	
	// Our Process - Click to Expand
	// Set default open/close settings
	$('.ccc-container').hide(); // Close all containers
	$('.ccc-trigger').append('<span class="process-expand">Click to Expand</span>');
	$('.ccc-trigger').click(function(){
		if( $(this).next().is(':hidden') )  // If immediate next container is closed
		{
			$(this).toggleClass('active').next().slideDown();
			$(this).find('.process-expand').html('Click to Collapse');
		}
		else
		{
			$(this).toggleClass('active').next().slideUp();
			$(this).find('.process-expand').html('Click to Expand');
		}
		return false; // Prevent the browser jump to the anchor link
	});
	
	
	// Tooltips on homepage
	// $('.home-buttons li a[title]').tooltip({effect: 'slide', position: "top center", offset: [15, 120]});
	$('.home-button-custom1').tooltip({effect: 'slide', tip: ".custom1", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-custom2').tooltip({effect: 'slide', tip: ".custom2", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-internet1').tooltip({effect: 'slide', tip: ".internet1", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-internet2').tooltip({effect: 'slide', tip: ".internet2", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-internet3').tooltip({effect: 'slide', tip: ".internet3", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-internet4').tooltip({effect: 'slide', tip: ".internet4", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-internet5').tooltip({effect: 'slide', tip: ".internet5", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-design1').tooltip({effect: 'slide', tip: ".design1", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-design2').tooltip({effect: 'slide', tip: ".design2", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-design3').tooltip({effect: 'slide', tip: ".design3", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-design4').tooltip({effect: 'slide', tip: ".design4", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-tech1').tooltip({effect: 'slide', tip: ".tech1", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-tech2').tooltip({effect: 'slide', tip: ".tech2", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-tech3').tooltip({effect: 'slide', tip: ".tech3", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-tech4').tooltip({effect: 'slide', tip: ".tech4", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-tech5').tooltip({effect: 'slide', tip: ".tech5", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-content1').tooltip({effect: 'slide', tip: ".content1", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-content2').tooltip({effect: 'slide', tip: ".content2", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-content3').tooltip({effect: 'slide', tip: ".content3", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-branding1').tooltip({effect: 'slide', tip: ".branding1", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-branding2').tooltip({effect: 'slide', tip: ".branding2", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-branding3').tooltip({effect: 'slide', tip: ".branding3", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-branding4').tooltip({effect: 'slide', tip: ".branding4", position: "top center", relative: true, offset: [15, 120]});
	$('.home-button-branding5').tooltip({effect: 'slide', tip: ".branding5", position: "top center", relative: true, offset: [15, 120]});
	
	/* Footer map overlay */
	$(".footer-map-link").colorbox({width:"80%", height:"80%", iframe:true});
	$(".contact-map-button").colorbox({width:"80%", height:"80%", iframe:true});
	$(".demo-link").colorbox({width:620, height:460,inline:true, href:"#demo-pop"});
	$("ul.newsletters-portfolio li a").colorbox({width:440, height:700,iframe:true});
	$("li.cms-portfolio a").colorbox({width:760, height:650,iframe:true});
	$("li.pop-portfolio a").colorbox({width:610, height:420,iframe:true});
	$("li.mobile-portfolio a").colorbox({width:350, height:590,iframe:true});
});

/* Change over state on expand buttons */
function expand_click(click) {
	var tab1 = document.getElementById('home-1');
	var tab2 = document.getElementById('home-2');
	var tab3 = document.getElementById('home-3');
	var tab4 = document.getElementById('home-4');
	var tab5 = document.getElementById('home-5');
	
	var link1 = document.getElementById('link-1');
	var link2 = document.getElementById('link-2');
	var link3 = document.getElementById('link-3');
	var link4 = document.getElementById('link-4');
	var link5 = document.getElementById('link-5');
	
	if(click == "1")
	{
		if(tab1.className == "hide")
		{
			tab1.className = "show";
			link1.className = "active first";
		}
		tab2.className = "hide";
		tab3.className = "hide";
		tab4.className = "hide";
		tab5.className = "hide";
		link2.className = "";
		link3.className = "";
		link4.className = "";
		link5.className = "last";
	}
	else if(click == "2")
	{
		if(tab2.className == "hide")
		{
			tab2.className = "show";
			link2.className = "active";
		}
		tab1.className = "hide";
		tab3.className = "hide";
		tab4.className = "hide";
		tab5.className = "hide";
		link1.className = "first";
		link3.className = "";
		link4.className = "";
		link5.className = "last";
	}
	else if(click == "3")
	{
		if(tab3.className == "hide")
		{
			tab3.className = "show";
			link3.className = "active";
		}
		tab1.className = "hide";
		tab2.className = "hide";
		tab4.className = "hide";
		tab5.className = "hide";
		link1.className = "first";
		link2.className = "";
		link4.className = "";
		link5.className = "last";
	}
	else if(click == "4")
	{
		if(tab4.className == "hide")
		{
			tab4.className = "show";
			link4.className = "active";
		}
		tab1.className = "hide";
		tab2.className = "hide";
		tab3.className = "hide";
		tab5.className = "hide";
		link1.className = "first";
		link2.className = "";
		link3.className = "";
		link5.className = "last";
	}
	else if(click == "5")
	{
		if(tab5.className == "hide")
		{
			tab5.className = "show";
			link5.className = "active last";
		}
		tab1.className = "hide";
		tab2.className = "hide";
		tab3.className = "hide";
		tab4.className = "hide";
		link1.className = "first";
		link2.className = "";
		link3.className = "";
		link4.className = "";
	}
	else
	{
		tab1.className = "hide";
		tab2.className = "show";
		link2.className = "active";
		tab3.className = "hide";
		tab4.className = "hide";
		tab5.className = "hide";
	}

}


