var page_url = location.href;
var $j = jQuery.noConflict();
var loading = false;

setInterval(function(){
	if (location.href!=page_url && !loading) {
		setAjaxContent();
		page_url = location.href;
	}
}, 1000);


function scanForLinks() {
	$j("a[href^="+baseURL+"], a[href^=/]").each(function(){
		if($j(this).attr("rel")!="internal" && $j(this).attr("rel")!="portfolio" && $j(this).attr("href").indexOf(".pdf")<0 && $j(this).attr("href").indexOf(".jpg")<0 && $j(this).attr("href").indexOf(".png")<0 && $j(this).attr("href").indexOf(".gif")<0 && $j(this).attr("href").indexOf(".doc")<0) {
			$j(this).click(function(){
				makeSharpUrl($j(this).attr("href"));	
				return false;	
			});
			
		}
		if($j(this).attr("rel")!="portfolio") {
			$j(this).attr("rel", "internal");
		}
	});	
}

function makeSharpUrl(url) {
	url = url.replace(baseURL, "");
	var base = location.href;
	if (location.href.indexOf("#") >= 0) {
		base = location.href.split("#")[0];
	}				
	this.location = base + "#" + url;
	setAjaxContent();
}

function setAjaxContent(){
	loading = true;
	var url = "/";
	if (location.href.indexOf("#")>=0) {
		url = location.href.split("#")[1];
		if (url=="") url = "/";
		$j(".loading:hidden").fadeIn("fast");
		if($j('#content').is(":hidden") && currentPage!=url) {
			$j('#bar').css({height:'30px'});	
		}
	} else {
		$j("#content").fadeIn("fast");
		$j(".loading:visible").fadeOut("fast");
	}
					
	if(currentPage!=url) {
		//alert(templateURL+"/__c.php");
		$j.scrollTo("#header", 600);
		$j.ajax({
		      url: templateURL+"/__c.php",//"?"+Math.random(),
		      global: false,
		      type: "POST",
		      data: ({slug : url}),
		      dataType: "html",
			  context: document.body,
			  error: function(XMLHttpRequest, textStatus, errorThrown) {
				loading = false;
			  },
		      success: function(data){
				// alert(data);
						

						currentPage = url;
						$j('#bar').css({height:$j("#bar").height() + 'px'});
						//alert($j('#bar').css('height'));
						$j("#content").html(data);
						//alert(data);
						
						$j(".loading:visible").fadeOut("fast");
						$j('#bar').animate({height:$j("#content").height() + 'px'}, 500);
						setTimeout(function(){$j('#bar').css({height:'auto'});},610);
						$j("#content").fadeIn("fast");
						
						onReAjax();
						loading = false;
		      }
		   }
		);
	}	
	else {
		$j("#content").fadeIn("fast");
		$j(".loading:visible").fadeOut("fast");
	}
	
	
}

function onReAjax(){
	if($j("#twittercontainer").length > 0) {
		setTimeout(function(){doJuitter();}, 800);
	}
	scanForLinks();
	try {
		jQuery('div.wpcf7 > form').ajaxForm({
			beforeSubmit: wpcf7BeforeSubmit,
			dataType: 'json',
			success: wpcf7ProcessJson
		});
	} catch (e) {
	}

	try {
		jQuery('div.wpcf7 > form').each(function(i, n) {
			wpcf7ToggleSubmit(jQuery(n));
		});
	} catch (e) {
	}

	try {
		if (_wpcf7.cached) {
			jQuery('div.wpcf7 > form').each(function(i, n) {
				wpcf7OnloadRefill(n);
			});
		}
	} catch (e) {
	}
	
	$j(".faq-question").click(function(){
		$j(this).next("p.faq-answer").slideToggle("fast");
	});
}

function doJuitter() {
	$j(document).ready(function() {
		$j.Juitter.start({
			searchType:"searchWord", // needed, you can use "searchWord", "fromUser", "toUser"
			searchObject:"internetvalley", // needed, you can insert a username here or a word to be searched for, if you wish multiple search, separate the words by comma.

			// The values below will overwrite the ones on the Juitter default configuration. 
			// They are optional here.
			// I'm changing here as a example only
			lang:"nl", // restricts the search by the given language
			live:"live-120", // the number after "live-" indicates the time in seconds to wait before request the Twitter API for updates.
			placeHolder:"twittercontainer", // Set a place holder DIV which will receive the list of tweets example <div id="juitterContainer"></div>
			loadMSG: "image/gif", // Loading message, if you want to show an image, fill it with "image/gif" and go to the next variable to set which image you want to use on 
			imgName: templateURL+"/images/loading.gif", // Loading image, to enable it, go to the loadMSG var above and change it to "image/gif"
			total: 3, // number of tweets to be show - max 100
			readMore: "", // read more message to be show after the tweet content
			nameUser:"text", // insert "image" to show avatar of "text" to show the name of the user that sent the tweet 
			openExternalLinks: true,
			filter:"sex->*BAD word*,porn->*BAD word*,fuck->*BAD word*,shit->*BAD word*" 
		});	

	});
}
var logointerval, logotimerinterval;
jQuery(document).ready(function(){
	scanForLinks();
	setAjaxContent();
	if($j("#twittercontainer").length > 0) {
		doJuitter();
	}
	
	$j('.projectReadMore').live('click', function() {
	  if($j(".projectRestContent").is(":hidden")) {
			$j(".projectRestContent").slideDown("fast");
			$j(this).html("&laquo; Sluiten");
	   }
	   else {
			$j(".projectRestContent").slideUp("fast");
			$j(this).html("Lees verder &raquo;");
	   }
	});
	
	
	startRandomLogoHighlight();
	$j("div#partners .inner .logo").hover(function(){
		clearInterval(logointerval);
		clearTimeout(logotimerinterval);
		$j("div#partners .inner .logo").animate({opacity: 0.32}, 10);
		$j(this).animate({opacity: 1}, 300);
	},
	function(){
		$j(this).animate({opacity: 0.32}, 300);
		logotimerinterval = setTimeout(startRandomLogoHighlight, 800);
	});
});

function startRandomLogoHighlight() {
	logointerval = setInterval(randomLogoHighlight, 4200);
	randomLogoHighlight();
}

function randomLogoHighlight(){
	var logocount = $j("div#partners .inner .logo").length -1 ;
	var randlogo = Math.round(Math.random()*logocount);
	$j("div#partners .inner .logo").animate({opacity: 0.32}, 500);
	$j("div#partners .inner .logo:eq("+randlogo+")").animate({opacity: 1}, 500);

}
function setCurrentProject(id) {
	$j("div.projectBlock a").removeClass("active");
	$j("div#project_"+id + " a").addClass("active");	
}
function showFancyBox() {
									 
			jQuery("a.pfGroup:first").click();
		};

