$(document).ready(function(){

	/* SCROLLPAGE */
 	 $('a[href*=#]').click(function() {
		 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
			&& location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target
			|| $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
			var targetOffset = $target.offset().top;
			$('html,body')
			.animate({scrollTop: targetOffset}, 1000);
			 return false;
			}
		}
	});
	
	/* CYCLE HEAD SLIDER */	
	$('#slider').cycle();	
	
	/* VALIDATE FORM */
	$("#feedform").validate();
	
	/* SERVICES ACCORDION */
	$(".sum").hide();
	$(".subcol").click(function(){
		$(".sum").slideUp("fast");
		$(".subcol").removeClass("active");
		$(this).toggleClass("active").parent().next().slideDown("fast");
	});
	
	/* CLIENTS TOOLTIP */
	$(".brand_info").hide();
	$(".brand_logo").hover(function(){
		//$(this).parent().next().show();
		$(this).parent().next().fadeTo("fast", 1.0);
	}, function() {
		$(this).parent().next().fadeOut("fast", 0);
	});
	
	
	/* AJAX PROJECTS */

	//References
	var sections = $("#grid li");
	var loading = $("#loading");
	var content = $("#projects");
	
	//Manage click events
	sections.click(function(){
		
		$(sections).removeClass("on");
		$(this).toggleClass("on");
		
		//show the loading bar
		showLoading();
		//load selected section
		switch(this.id){
			case "1":
				content.fadeOut("slow", function() {
					
					/* AJAX LOAD */
					content.load("list1.htm", function (responseText, textStatus, XMLHttpRequest) {
					    if (textStatus == "success") {
					         // all good!
					         hideLoading();
					         content.fadeIn("slow",applyCover);
					    }
					});
				
				});
				
				//content.fadeIn();
				break;
			case "2":
				content.fadeOut("slow", function() {
					
					/* AJAX LOAD */
					content.load("list2.htm", function (responseText, textStatus, XMLHttpRequest) {
					    if (textStatus == "success") {
					         // all good!
					         hideLoading();
					         content.fadeIn("slow",applyCover);
					    }
					});
				
				});
				break;
			case "3":
				content.fadeOut();
				content.fadeOut("slow", function() {
					
					/* AJAX LOAD */
					content.load("list3.htm", function (responseText, textStatus, XMLHttpRequest) {
					    if (textStatus == "success") {
					         // all good!
					         hideLoading();
					         content.fadeIn("slow",applyCover);
					    }
					});
				
				});
				break;
			default:
				//hide loading bar if there is no selected section
				hideLoading();
				break;
		}
	});


	//show loading bar
	function showLoading(){
		loading
			.css({visibility:"visible"})
			.css({opacity:"1"})
			.css({display:"block"})
		;
	}
	//hide loading bar
	function hideLoading(){
		loading.fadeTo(1000, 0);
	};
	
	/* INITIAL LOAD */
	$("#grid #1").toggleClass("on");
	showLoading();
	content.fadeOut();
	content.load("list1.htm", function (responseText, textStatus, XMLHttpRequest) {
				    if (textStatus == "success") {
				         // all good!
				         hideLoading();
				         content.fadeIn("slow",applyCover);
				    }
	});
	
	function applyCover() {
		$(".foliothumb").fadeTo("slow",0.5);
		$(".foliothumb").hover(function(){
					$(this).fadeTo("fast", 1.0);
					},function(){
					$(this).fadeTo("fast", 0.5);
		});
		$(".plink").fancybox({width: 650, height: 640, 'type': 'iframe'});
	};

});

