$(document).ready(function() {

	// offerte-tabs
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		calculateHeightSidebar();
		giveOptionsCalc();
		return false;
	});
	
	$("a.step").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class	
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).attr("rel"); //Find the href attribute value to identify the active tab + content
		$("a."+activeTab).parent('li').addClass("active"); //Add "active" class to selected tab				   
		$("#"+activeTab).fadeIn(); //Fade in the active ID content
		calculateHeightSidebar();
		giveOptionsCalc();
		return false;
	});

	// table sorter
	if ($('.tablesorter').length > 0 &&  $('#pager').length > 0) {
		$(".tablesorter") 
			.tablesorter({widthFixed: true, widgets: ['zebra']}) 
			.tablesorterPager({container: $("#pager")}); 
	} else if ($('.tablesorter').length > 0 ){
		$(".tablesorter") 
			.tablesorter({widthFixed: true, widgets: ['zebra']}) 
	}

	/* general */
	doAjaxRequest = function (scriptPath, postVars, callback){
	
		var postVars;
		var response;
		var callback;
		var scriptPath = false ? '/cms/scripts/task.php' : scriptPath;
		
		$.ajax({
			url: scriptPath,
			type: "POST",
			data: postVars,
			dataType: "json",
			async: false,
			success: function(response, textStatus){
				// start debug
				if(response.debug==true){
					//Load debugger layer
					$('body').append("<div id=\"debugger\" title=\"Debugger\"><div id=\"debuggerContent\"></div></div>");
					$("#debugger").dialog({ position: [10,10], closeOnEscape:true});
					$('#debugger').dialog('option', 'height', 350);
					$('.selector').dialog('option', 'autoOpen', false);
					$('#debuggerContent').html('<p>'+scriptPath+'</p>');
					$('#debuggerContent').append('<h1>To PHP</h1>');
					
					for (key in postVars) {
						$('#debuggerContent').append('<p><strong>'+key+': </strong>'+ postVars[key]+'</p>');
					}
					
					$('#debuggerContent').append('<h1>From PHP</h1>');
					
					if (typeof response === 'object'){
						for (key in response) if (key !== 'debug')$('#debuggerContent').append('<p><strong>'+key+': </strong>'+ response[key]+'</p>');
					} else {
						$('#debuggerContent').append('<p>'+response+'</p>');
					}	
					
				} //end debug
		
				if(response.error==true){
					doError(response.title, response.message);
					callback.call(this, response);
				} else {
					if(typeof(response.message)=="undefined" || response.message==null) {
					//nothing
					} else {
						doMessage(response.title, response.message);
					}
				
					callback.call(this, response);
				}
				
			}, // end success function
			error: function(XMLHttpRequest, textStatus, errorThrown){
				
				//Load debugger layer
				$('body').append("<div id=\"debugger\" title=\"Debugger\"><div id=\"debuggerContent\"></div></div>");
				$("#debugger").dialog({ position: [10,10], closeOnEscape:true});
				$('#debugger').dialog('option', 'height', 450);
				$('.selector').dialog('option', 'autoOpen', false);
				$('#debuggerContent').html('<p><strong>scriptPath:</strong> '+scriptPath+'</p>');
				$('#debuggerContent').append('<h1>To PHP</h1>');
				
				for (key in postVars) {
					$('#debuggerContent').append('<p><strong>'+key+': </strong>'+ postVars[key]+'</p>');
				}
				
				$('#debuggerContent').append('<h1>From PHP</h1>');
				
				if (typeof response === 'object'){
					for (key in response) if (key !== 'debug')$('#debuggerContent').append('<p><strong>'+key+': </strong>'+ response[key]+'</p>');
				} else {
					$('#debuggerContent').append('<h4>Ajax error encountered: <br /><span style="color:red;">textStatus: <strong>'+textStatus+'</strong></span> <br/> <span style="color:red;">errorThrown: <strong>'+errorThrown+'</strong></span></h4>');
				}			
			}
		
		}); //end ajax call
	};
	/* end general */

	//Keep #sidebar height size the same as #content
	calculateHeightSidebar = function () {	
		var $contentHeight = $('#content').height();
		var $sidebarHeight = $('#sidebar').height();
		
		if($contentHeight >= $sidebarHeight) {
			$('#sidebar').css({
				'min-height': $contentHeight
			});
		}
	};
	// and execute
	calculateHeightSidebar();
	
	//Zoek click reset
	$('#sZoek').focus(function() {
		var val = $(this).val();
		if(val === 'zoek'){
			$(this).val('');
		}
	}).blur(function(){ 
		var val = $(this).val();
		if(val === '') {
			$(this).val('zoek');
		}
	});
	
	//fix margin for images
	$('#imgContainer img:eq(1)').css({'margin-right':'1px'})
	
	//Click logo go to root
	$('#header h1.logo').click(function(){
		window.location = "/";
	});
	
	//Show sub menus	
	$('#sidebar #rightMenu li').hover(function(){
		$(this).children('a.mainMenu').css({'color':'#000000'});
		$(this).children('ul').fadeIn('fast');
	},function() {
		$(this).children('ul').fadeOut('fast');
		$(this).children('a.mainMenu').css({'color':'#FFFFFF'});
	});
		
	// run standard validation on form
	$("form.validate").each(function() {
		//console.log('s')
		$(this).validate();
	});
	
});


function parseJSON(json){
    try{
        if(/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(json)){
            var j = eval('(' + json + ')');
            return j;
		}
	}catch(e){
    }
    throw new SyntaxError("parseJSON");
};