$(document).ready(function(){
	
  $(window).resize(function() {
    revampScreen();
		reinitialiseScrollPane();    
  });
  
	var clearQueue = true;
	var gotoEnd = false;
													 
	$("li.hasSubSub").hover(
		function(){
			$("ul", this).stop(clearQueue, true).show("slow");
			$("div#content").stop(clearQueue, gotoEnd).animate( { marginLeft: "183px" }, "slow" );
			$("div#rechts").css("display", "none");
			$("div#topMenu").stop(clearQueue, true).hide("slow");
		},
		function(){
			$("ul", this).stop(clearQueue, true).hide("slow");
			$("div#content").stop(clearQueue, gotoEnd).animate( { marginLeft: "5px" }, "slow" );
			$("div#content").queue(function(){
				$("div#rechts").css("display", "block");
				$(this).dequeue();
			});
			$("div#topMenu").stop(clearQueue, true).show("slow");
		}
	);
	
	//Custom scrollbar
	$("div#cont1, div#cont2, div#cont3").jScrollPane({
		scrollbarMargin: 0,
		scrollbarWidth: 15,
		//Eigen variabele containerPaddingRight
		containerPaddingRight: 20,
		//Eigen variabele containerPaddingBottom, alleen in combo met containerFixedHeight
		//containerPaddingBottom: 20,
		//containerFixedHeight: 400,
		showArrows: true,
		reinitialiseOnImageLoad: true
	});
	
	//Toggle tabs - Template 4
	$("div.aTabTitle").click(function(){
				
			if (!($(this).hasClass("aTabTitleOpen"))) {
				$("div.aTabTitleOpen").removeClass("aTabTitleOpen");
				$("div.aTabContentOpen").hide("normal").removeClass("aTabContentOpen");
				$(this).parent().find("div.aTabContent").show("normal").addClass("aTabContentOpen");
				$(this).addClass("aTabTitleOpen");
			}

	});
	
	reinitialiseScrollPane = function(){
		$('div#cont3, div#cont1').jScrollPane({
			scrollbarMargin: 0,
			scrollbarWidth: 15,
			//Eigen variabele containerPaddingRight, containerPaddingTop, containerPaddingBottom
			containerPaddingRight: 20,
			showArrows: true,
			reinitialiseOnImageLoad: true
		})
	};

	
	//Artikelen - Template 6
	$("a.articleContainerLink").click(function(){
		var hrefStr = $(this).attr("href");
		var htmlStr = $("div"+hrefStr).html();
		$("div#cont3").css("height", "400px").html(htmlStr);
		reinitialiseScrollPane();
	});

  // make #hash linkable in Template 6
  curUrl = document.location.href;
  if (curUrl.indexOf('#')!=-1)
  {
    hashTag = curUrl.substr(curUrl.indexOf('#'),curUrl.length-curUrl.indexOf('#')+1)
    if (hashTag.length>0)
    {
      //alert("Hash # found: "+hashTag);
  		var hrefStr = hashTag;
  		if (hrefStr.length>1)
  		{
	  		var htmlStr = "";
	  		htmlStr = $("div"+hrefStr).html();
	  		if (htmlStr!=null)
	  		{
	    		$("div#cont3").css("height", "400px").html(htmlStr);
	    		reinitialiseScrollPane();
	    	}
	    }
    }
  }

  // newsitems, spacing around images	
  $("#newslist img[align='right'],#cont3 img[align='right'],#content img[align='right']").css({'margin-bottom':'3px','margin-left':'10px','clear':'none'});
  $("#newslist img[align='left'],#cont3 img[align='left'],#content img[align='left']").css({'margin-bottom':'3px','margin-right':'10px','clear':'none'});
  
  // specialFunctions DIV text always add image
  $("#right #specialFunctions, #rechts3 #specialFunctions").after("<img src='/upload/images/dotted_line_pms365_1px.png' alt='' />");
  $("#cont3 #specialFunctions, #content #specialFunctions").after("<img src='/upload/images/dotted_line_pms605_1px.png' alt='' />");
  $("#cont3 #specialFunctions2, #content #specialFunctions2").after("<img src='/upload/images/dotted_line_white_1px.png' alt='' />");

	// TODO highlighter
	$("a[href*='/contact/informatieformulier/'], a[href*='#fixen']").click(function(event){
		//prompt("Ook deze link moet ik nog fixen.", document.location.href);
		
		d = "";
		d = document.location.href;
		d = d.replace("http://circle2.dev.i-minded.net", "");
		d = d.replace("http://www.circlesoftware.nl", "");
		
		t = "";
		t = document.title;
		t = t.replace(/Circle Software \| /g, "");
		t = t.replace("&", "en");
		
		//alert("U wordt nu naar ons informatieformulier omgeleid, een moment...");
		
		document.location.href="/contact/informatieformulier/?from="+trim(d)+"&title="+trim(t);
		
    event.preventDefault();	
	});
	
	// HIGHLIGHT downloads
	$("a[href*='/contact/informatieformulier/'], a[href='#fixen']").each(function(){
	  $(this).text('Informatie opvragen...');
	  $(this).before('<img src="/upload/images/Go.gif" alt="Klik de link hiernaast voor meer informatie" align="left" style="margin-top:3px;margin-right:5px;" />');
	});
	
	// FIX layout issue with tab design having 5 categories instead of the standard 4 categories
	if ($("#topMenuHorizontal ul li").length==5)
	{
	  // also calculate the padding and the margin!
	  // allowed space usage 583px, 5 items X 95px+10px(left padding)+10px(right padding)+1px(right margin) = 580px
	  $("#topMenuHorizontal ul li").css("width", "95px");
	}
	
	// FIX layout issue with tab design having 5 categories instead of the standard 4 categories
	$("#topMenuHorizontal ul li a:contains('Verandermanagement')").text("Verander management");
	
	query = parseUri(document.location.href).query
	path = parseUri(document.location.href).path
	
	if (
		(path=="/news/" || path=="/news/archief/") &&
		(query.indexOf("newsid=")==-1 && query.indexOf("newsID=")==-1)
	)
	{
		// change the newslist layout
  	$("#newslist img[align=right]").hide();	  	
  	$("#newslist img[align=]").attr("align", "left");
  	$("#newslist strong, #newslist p strong  ").css({'font-weight': 'normal', 'color':'white' } );
  	$("#newslist h3").css({'margin-top':'10px','font-size':'1.2em'} );
  	document.title = trim(document.title) + ": " + $("#newslist h3").text();
  	
  	// now rebuild the scrollbar or else wierd things might happen, like an empty background and such
    reinitialiseScrollPane();
    
	} else 
	if (path=="/news/" || path=="/news/archief/") 	
	{
		// only change the document title
		document.title = trim(document.title) + ": " + $("#newslist h3").text();		
	}
    
  revampScreen();
	
});

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function showSubSub(subSubMenu){
	
	if($("li#subsub"+subSubMenu).length == "1"){
	
			$("li#subsub"+subSubMenu).parent("ul").css("display", "block");
			$("div#content").css("margin-left", "183px");
			$("div#rechts").css("display", "none");
			$("li#subsub"+subSubMenu).parent("ul").parent("li").find("a:first").addClass("active");		
	}	
}

function getDocParam( name ){
  
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( document.location.href );
  
  if( results == null )
    return "";
  else
    return results[1];
}
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
  		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
	  	uri = {},
		  i   = 14;

  while (i--) uri[o.key[i]] = m[i] || "";
  
  uri[o.q.name] = {};
  uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
  	if ($1) uri[o.q.name][$1] = $2;
  });

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
  		name:   "queryKey",
  		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
  		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
  		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

function revampScreen()
{
	// first check screen resolution, if height <= 600 change few CSS values
	if (screen.height<=768 || $(window).height()<=768)
	{
		$("#mainmenucontainer").css("marginTop","0px");
		$("#blocks").css("marginTop","15px");
		$("#content2").css({'marginTop':'10px','height':'275px'});
		//$("#cont1, #cont2, #cont3").css("height","275px");
		$('.jScrollPaneContainer').css("height","350px");
	} else
  // first check screen resolution, if height > 768 change few CSS values
	if (screen.height>768 || $(window).height()>768)
	{
		//$("#mainmenucontainer").css("marginTop","0px");
		//$("#blocks").css("marginTop","50px");
		//$("#content2").css({'marginTop':'10px','height':'650px'});
		//$("#cont1, #cont2, #cont3").css("height","650px");
		//$('.jScrollPaneContainer').css("height","450px");
	} else
  {
    //alert("Screensize: "+screen.height+", window size: "+$(window).height());
    //alert("Screensize: "+screen.width+", window size: "+$(window).width());
  } 
  
	if (screen.width>1600 || $(window).width()>1600)
	{
		$("body").css({"background-position":"top left"});
	}   
  
}

