//Front end Java Script File
var row_val = 0;
var col_val = 0;
var c_value = 0;
var r_value = 0;
var pre_id = "";
var opa = "";
var default_load = "true";
var page_name = new Array();

page_name[1]="over_ons";
page_name[3]="portfolio";
page_name[4]="home";
page_name[5]="contact";
page_name[7]= "kantoormeubelen";
page_name[10]= "testimonials";
page_name[0] = "test";

jQuery(document).ready(function() 
{
	//languages changing events
	jQuery(".en").bind("click",function(){
		
		jQuery.ajax({
			  url: 'index.php?lang=1&mode=lang',
			  success: function(data) {
				jQuery(".nl").removeClass("selected");
				jQuery(".en").addClass("selected");
			    if(data==true)
			    {
			    	ajax_functions();
			    }
			  }
			});
	});
	//select the Dutch language
	jQuery(".nl").bind("click",function(){
		
		$.ajax({
			  url: 'index.php?lang=2&mode=lang',
			  success: function(data) {
				jQuery(".en").removeClass("selected");
				jQuery(".nl").addClass("selected");
			    if(data==true)
			    {
			    	ajax_functions();
			    }
			  }
			});
	});
	
	
	//Binding the class for the site
	jQuery(".rij").bind("click",function(){
		
		var idv = jQuery(this).attr("id");
		var div_width = jQuery(this).css("width");
		var div_height = jQuery(this).css("height");
		
		var div_top = jQuery(this).css("top");
		var div_left = jQuery(this).css("left");
	
		moving_center(div_width,div_height,div_top,div_left,idv);
		

	});
	
	//Binding for the hover event for the site
	jQuery(".rij").hover(
			  function () {
				opa=jQuery(this).css("opacity");
				idv = jQuery(this).attr("id");
				jQuery(".rij").animate({"opacity":"0.1"});
			    jQuery(this).animate({"opacity":"1"});
				
				if(pre_id!=idv) {
					var t = jQuery(this).css("top");
					var h = jQuery(this).height();
					var w = jQuery(this).width();
					
					
					var idvalue = idv.split("_");
					var row = Math.floor(parseInt(idvalue[1])%3);
					var col = Math.floor(parseInt(idvalue[1])/3);
	
		    		temp =0;
		    		for(var i=1;i<=col;i++)
		    		{
		    			temp += convert_int(jQuery(".cc"+i).css("width"))	;
		    		}
		    		l = temp+77;
		    		t = convert_int(t)+50;
		    		w = w+10;
		    		h = h+13;
					
					//console.log(t+" "+l+" "+h+" "+w);
		    		jQuery(this).append('<div id="cursor_container" class="cursor_container"></div>');
		    		//jQuery("#cursor_container").removeAttr('class');
		    		//jQuery("#cursor_container").attr('class', idv);
		    		//"top":t+"px","left":l+"px",

		    		
		    		t=0;
		    		l=0;
		    		
					jQuery("#cursor_container").css({"width":w+"px","position":"absolute","height":h+"px","z-index":"100","cursor":"pointer","top":t+"px","left":l+"px"});
				}
				else
				{
					//jQuery("#cursor_container").removeAttr('class');
					//jQuery("#cursor_container").css({"top":"1px","left":"1px","width":"1px","height":"1px","display":"none"});
				}
				//pre_id = idv;
				//jQuery(this).html("");
				
			    jQuery(".rij").dequeue();
			    jQuery(this).dequeue();
			  },
			  function () {
				  jQuery("#cursor_container").remove();
				jQuery(".rij").animate({"opacity":"0.1"});  
				jQuery("#"+pre_id).animate({"opacity":"1"});
				jQuery("#"+pre_id).dequeue();
			  //   jQuery(this).animate({"opacity":"0.2"});
			    //jQuery(this).dequeue();
			  }
			);
	
	//default loading page
	var val = jQuery("#outer_main").find(".rij:eq(2)");
	init_value(val);
	
	//Read more event for the News page
	jQuery(".read_more").bind("click",function(){
		var val = jQuery(this).attr("href");
		readmore_news(val);
	});
	
	//At loading the page loading the default portfolio
	portfolio_cat();
	youtube();
	topmenu_sel();
	mouse_move();
	jQuery('#rij_inside').jScrollPane({showArrows:true,scrollbarWidth: 16});
	jQuery('#rij_contact').jScrollPane({showArrows:true,scrollbarWidth: 16});
	jQuery('#rij_furniture').jScrollPane({showArrows:true,scrollbarWidth: 16});
	jQuery('#rij_testimonial').jScrollPane({showArrows:true,scrollbarWidth: 16});
	jQuery('#rij_home').jScrollPane({showArrows:true,scrollbarWidth: 16});
	flash_enable();
});
function cat_view(va)
{
	var val = jQuery("#outer_main").find("#rij_3");
	init_value(val);
    portfolio(va);
}
function flash_enable()
{
	
	
	var requiredMajorVersion = 8;
	// Minor version of Flash required
	var requiredMinorVersion = 0;
	// Minor version of Flash required
	var requiredRevision = 0;
	// Version check based upon the values entered above in "Globals"
	var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

	// Check to see if the version meets the requirements for playback
	if (hasReqestedVersion) {
			
	} else {  // flash is too old or we can't detect the plugin
		myfunction();
	}

	
}

function mouse_move()
{
	jQuery("#cursor_container").bind("click",function(){
		var cla = jQuery("#cursor_container").attr("class");
		cla = cla.split("_");
		cla = cla[1];

		var val = jQuery("#outer_main").find("#rij_"+cla);
		init_value(val);
		
	});
}
function topmenu_sel()
{
	jQuery(".topmenu_item").unbind();
	jQuery(".topmenu_item").bind("click",function(){
		var v=jQuery(this).attr("id");
		v = v.split("_");
		var val = jQuery("#outer_main").find(".rij:eq("+v[1]+")");
		init_value(val);
	});
	jQuery(".rem_topmenu_item").unbind();
}

//flash called the function to load the site
function myfunction()
{
	jQuery("#outer_wrapper").css({"visibility":"visible"});
	jQuery("#flash").remove();

//	setTimeout(function(){ jQuery("#flash").remove(); }, 4000);
//	setTimeout(function(){ jQuery("#outer_wrapper").css({"visibility":"visible"}); }, 3500);
//	
	setTimeout(function(){
		//It is loading the page based on the URL
		var loc_val = location.href;
		loc_val = loc_val.split("#");
		
		if(loc_val.length>1)
		{
			loc_val = loc_val[1];
			jQuery.each( page_name, function(i, n){
				if(n==loc_val){
					var val = jQuery("#outer_main").find("#rij_"+i);
					init_value(val);
				}	
			});
			//decoding to the youtube
			youtube();
		}}	,500);
}

//readmore news effect
function readmore_news(val)
{
	var loc_val = val;
	loc_val = loc_val.split("#");
	
	if(loc_val.length>1)
	{
		loc_val = loc_val[1];
		jQuery.each( page_name, function(i, n){
			if(n==loc_val){
				var val = jQuery("#outer_main").find("#rij_"+i);
				init_value(val);
			}	
		});
	}
}
//decoding to the youtube URL
function youtube()
{
	var i = 0;
	jQuery("a").each(function(i,n){
		v = jQuery(this).attr("href");
		va = v.split(":");
		if(va.length>1)
		{
			if(va[0]=="{{youtube")
			{
				i++;
				val = va[1].split("}");
				jQuery(this).attr("rel","prettyPhoto[]");
				jQuery(this).attr("href","http://www.youtube.com/watch?v="+val[0]);
			}
		}
		
	});
	
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_rounded'});
}
function menu_sel()
{
	jQuery(".menu_item").bind("click",function(){
		var v=jQuery(this).attr("id");
		v = v.split("_");
		var val = jQuery("#outer_main").find(".rij:eq("+v[1]+")");
		init_value(val);
	});
}
function init_value(val)
{
	var idv = jQuery(val).attr("id");
	
	var div_width = jQuery(val).css("width");
	var div_height = jQuery(val).css("height");
	
	var div_top = jQuery(val).css("top");
	var div_left = jQuery(val).css("left");
	moving_center(div_width,div_height,div_top,div_left,idv);
}
jQuery.fn.center = function () {
		    this.css("position","absolute");
		    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
		    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
		    return this;
		}
		 
		//Use the above function as:
		
function moving_center(div_width,div_height,div_top,div_left,idv)
{
	opa =1;
	var temp_idv = row_val;
	if(pre_id==idv)
		return false;
	else
		pre_id = idv;
	
	var te = idv.split("_");
	
	if(default_load!="true")
	location.href="#"+page_name[te[1]];
	default_load = "false";
	
	if(idv=="rij_4")
	{
		menu_sel();
	}
	else
		jQuery(".menu_item").unbind("click");
	
	if(idv=="rij_3")
	{
		var selcat=jQuery("#menu_cat ul li:first a").attr("id").split("_");
		var selid="cat_"+selcat[1];
		//cat_view(selid);
		//portfolio(selid);
	}

		
	jQuery(".rij").unbind("click");
	jQuery("#"+idv).removeClass("rij").addClass("rij_select");

	jQuery("#screen").css({"display":"block"});
	
	jQuery(".rij").animate({'opacity':'0.1'},10);	
	
			var idvalue = idv.split("_");
			var row = Math.floor(parseInt(idvalue[1])%3);
			var col = Math.floor(parseInt(idvalue[1])/3);
			jQuery("#cursor_container").remove();
			div_width = convert_int(div_width);
			div_height = convert_int(div_height);
			div_top = convert_int(div_top);
			div_left = convert_int(div_left);
			
			var body_width = convert_int(jQuery("body").css("width"));
			var body_height = convert_int(jQuery("body").css("height"));
		
			var cen_width = (parseInt(body_width)-parseInt(div_width))/2;
			var cen_height = (parseInt(body_height)-parseInt(div_height))/2;
			
			//jQuery("#screen").css({"width":div_width+"px","height":div_height+"px"});
			//jQuery("#screen").css({"left":cen_width+"px","top":cen_height+"px"});
			jQuery("#screen").center();
			var left_main = convert_int(jQuery("#outer_main").css("left"));
			var top_main = convert_int(jQuery("#outer_main").css("top"));
			
			    
			    var scr_left = convert_int(jQuery("#screen").css("left"));
				var scr_top = convert_int(jQuery("#screen").css("top"));
				//alert(scr_left+" "+scr_top);
			    	if(div_left==0)
			    	{
			    		temp =0;
			    		for(var i=1;i<=col;i++)
			    		{
			    			temp += convert_int(jQuery(".cc"+i).css("width"))	;
			    		}
			    		div_left = temp;
			    		
			    	}
			    	if(div_top ==0){
			    		temp =0; 
			    		for(var i=1;i<=row;i++)
			    		{
			    			temp += 706	;
			    		}
			    		div_top = temp;
			    	}
		
			    		temp = scr_left - div_left;
				    	scr_left =  temp-95;
				    	//scr_left-=95;
				    	
			    	
				    	temp = scr_top - div_top;
				    	scr_top = temp-48;
				    	//scr_top -=48;
				    	
			  
				 var arr = "";   	
				 if(temp_idv != row && row=='1')
					arr="top";
				 else
					 arr="left";
			 
				t_scr_top = 0;
				t_scr_left = 0;
				//console.log(scr_left+" "+scr_top+" "+c_value+" "+r_value+" "+arr);
				if(arr=="left")
				{
					
					t_scr_top = r_value;
					t_scr_left = scr_left;
				}
				else if((r_value == 0) && (c_value == 0))
				{
					t_scr_top = scr_top;
					t_scr_left = scr_left;
					i=2;
				}
				else
				{
					t_scr_left = c_value;
					t_scr_top = scr_top;
				}
					
				jQuery("#outer_main").animate({'top':t_scr_top+"px","left":t_scr_left+"px"}, 400,function(){ 
				
				jQuery("#outer_main").animate({'top':scr_top+"px","left":scr_left+"px"}, 400,function()
				{
					r_value = scr_top;
					c_value = scr_left;
					
				row_val = row;
				col_val = col;
				jQuery("#"+idv).animate({"opacity":"1"},'slow');
				if(pre_id!="")
				{
					jQuery("#"+pre_id).removeClass("rij_select");
					jQuery("#"+idv).addClass("rij");
				}
				jQuery("#screen").css({"display":"none"});
				jQuery(".rij").bind("click",function(){
					
					var idv = jQuery(this).attr("id");
					
					var div_width = jQuery(this).css("width");
					var div_height = jQuery(this).css("height");
					
					var div_top = jQuery(this).css("top");
					var div_left = jQuery(this).css("left");
					moving_center(div_width,div_height,div_top,div_left,idv);
		
					});
				}); 
			});


}

function convert_int(val)
{
	var temp = val.replace("px","");
	if(IsNumeric(temp))
	temp = parseInt(temp);
	else
		temp =0;
	return temp;
}

function IsNumeric(strString)
//  check for valid numeric strings	
{
var strValidChars = "0123456789.-";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

//  test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
   {
   strChar = strString.charAt(i);
   if (strValidChars.indexOf(strChar) == -1)
      {
      blnResult = false;
      }
   }
return blnResult;
}

function ajax_functions()
{
	over_ons();
	home_page();
	news();
	menus();
	contactus();
	furniture();
	testimonial();
	portfolio_cat();
	topmenu_view();
	//portfolio();
}
function topmenu_view()
{
	jQuery.ajax({
		  url: 'index.php?mode=topmenus',
		  success: function(data) {
			jQuery("#rij_top_menu").html(data);
			topmenu_sel();
		  }
		});
}
function portfolio_cat()
{
	if(jQuery("").length>0)
	{
	var idv = jQuery(".menu_container .selected").attr("id");
	var va = idv.split("_");
	idv = va[1];
	}
	jQuery.ajax({
		  url: 'index.php?mode=portfolio_categories&idv='+idv,
		  success: function(data) {
			jQuery("#menu_cat").html(data);
			p_id = jQuery("#menu_cat .selected").attr("id");
			portfolio(p_id);
			jQuery(".category").unbind("click");
			jQuery(".category").bind("click",function(){
				jQuery(".category").removeClass("selected");
				jQuery(this).addClass("selected");
				portfolio(jQuery(this).attr("id"));
				youtube();
			});
		  }
		});
}
function portfolio(val)
{
	var va = val.split("_"); 
	jQuery.ajax({
		  url: 'index.php?mode=portfolio&id='+va[1],
		  success: function(data) {
			jQuery("#rij_portfolio").html(data);
			jQuery("#rij_portfolio").css({"height":"487px"});
			jQuery('#rij_portfolio').jScrollPane({showArrows:true,scrollbarWidth: 16});
			//jQuery("div.product_container .jScrollPaneContainer").css({"height":"487px"});
			gallery_effect();
			
			if(jQuery("a[rel^='prettyPhoto']").size()>0)
			{	
				  setTimeout(function(){
					prettyphotos();
				});
				
			}
		  }
		});
}

function prettyphotos()
{
	
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({theme:'light_rounded'});
}
function gallery_effect()
{
	jQuery(".effect").unbind("click");
	jQuery(".effect").bind("click",function(){
		var va=jQuery(this).attr("id");
		v = va.split("_");
		
		jQuery(".pretty_"+v[1]).css({"display":"none"});
		jQuery("#bigimg_"+v[1]+"_"+v[2]).fadeIn("slow");
		
	});
}
function menus()
{
	jQuery.ajax({
		  url: 'index.php?mode=menus',
		  success: function(data) {
			jQuery("#rij_menu").html(data);
		  }
		});
}
function furniture()
{
	jQuery.ajax({
		  url: 'index.php?mode=furniture',
		  success: function(data) {
			jQuery("#rij_furniture").html(data);
			youtube();
			jQuery('#rij_furniture').jScrollPane({showArrows:true,scrollbarWidth: 16});
		  }
		});
}
function testimonial()
{
	jQuery.ajax({
		  url: 'index.php?mode=testimonial',
		  success: function(data) {
			jQuery("#rij_testimonial").html(data);
			youtube();
			jQuery('#rij_testimonial').jScrollPane({showArrows:true,scrollbarWidth: 16});
		  }
		});
}
function contactus()
{
	jQuery.ajax({
		  url: 'index.php?mode=contact',
		  success: function(data) {
			jQuery("#rij_contact").html(data);
			jQuery("#contactSubmit").unbind("click");
			jQuery("#contactSubmit").bind("click", function() {	submitContactForm(); });
			jQuery('#rij_contact').jScrollPane({showArrows:true,scrollbarWidth: 16});
		  }
		});
}
function home_page()
{
	jQuery.ajax({
		  url: 'index.php?mode=home',
		  success: function(data) {
			jQuery("#rij_home").html(data);
			jQuery('#rij_home').jScrollPane({showArrows:true,scrollbarWidth: 16});
		  }
		});
}
function news()
{
	jQuery.ajax({
		  url: 'index.php?mode=news',
		  success: function(data) {
			jQuery("#rij_news").html(data);
		  }
		});
}
function over_ons()
{
	jQuery.ajax({
		  url: 'index.php?mode=over_ons',
		  success: function(data) {
			jQuery("#rij_inside").html(data);
			youtube();
			jQuery('#rij_inside').jScrollPane({showArrows:true,scrollbarWidth: 16});
		  }
		});
}