PG_START = 0;
PG_LIMIT = 10;
PG_COUNT = 0;

MAIN_CAT = 0;

LAST_CALL = null;

$(document).ready(function(){
	
	param_arr = getURLHashParam();		
	
if(param_arr==null || param_arr ==""){
	window.location.hash = "page:"+"1";
	param_arr = getURLHashParam();	
	page = param_arr[1];			
	loadProducts("",page);
}else{
	param_arr = getURLHashParam();	
	page = param_arr[1];
	loadProducts("",page);
}	

		initPaginations();
		loadMainCats();
		
})

function loadProducts(temp,page){

	$("#main-itemdetails").fadeOut(500,function(){
	
	document.getElementById("search_prod").value = "";
	
			var dataURL = "action=loadProducts&page=" + page;
	
			$(".pglinks").removeClass('active');
			$("#pga"+page).addClass('active');
	
				$.ajax({
					   type: "POST",  
					   url: http_path+"system/controllers/index-products-controllers.php",  
					   data: dataURL,  
					   success: function(response){  
	
							$("#main-itemdetails").html(response);
							$("#main-itemdetails").fadeIn(500);
	
					   }
				 });


	});

		window.location.hash = "page:"+page;
		
		LAST_CALL = "loadProducts";

}

function showProduct(id){

	$("#main-itemdetails").fadeOut(500, function(){

	$("#pagination-div").slideUp("fast");

				var dataURL = "action=showProduct&id=" + id;
		
					$.ajax({
						   type: "POST",  
						   url: http_path+"system/controllers/index-products-controllers.php",  
						   data: dataURL,  
						   success: function(response){  

								$("#main-itemdetails").html(response);							
								$("#main-itemdetails").fadeIn(500);
						   }
					 });
	
	});

}

function searchNow(temp,page){

	var value = document.getElementById("search_prod").value;

	if(page==null || page==""){
		page = 1;
	}

			$(".pglinks").removeClass('active');
			$("#pga"+page).addClass('active');

	$("#main-itemdetails").fadeOut(500,function(){

			var dataURL = "action=searchProducts&value=" + value + "&page=" + page;

							$.ajax({
								   type: "POST",  
								   url: http_path+"system/controllers/index-products-controllers.php",  
								   data: dataURL,  
								   success: function(response){  

										$("#main-itemdetails").html(response);							
										$("#main-itemdetails").fadeIn(500);
								   }
							 });

	});
	
	window.location.hash = "page:"+page;
	
		LAST_CALL = "searchNow";	
	
}


function initPaginations(){

	PG_COUNT = $(".pgnumcount").length;

	param_arr = getURLHashParam();	
	page = param_arr[1];		
	
	var curr_page = (page-1) * PG_LIMIT ;


	PG_START = 1;

		for(i=PG_START; i<=PG_LIMIT; i++){

			if(i<PG_START+PG_LIMIT){	
				$("#pg"+i).show();
			}

		}

}


function nextPage(){
	
	if(PG_START+PG_LIMIT<=PG_COUNT)
	{
		
		$(".previous-off").addClass('previous');
		$(".previous").removeClass('previous-off');		
	
		$(".pgnumcount").hide();
	
		PG_START = PG_START+PG_LIMIT;

			for(i=PG_START; i<PG_START+PG_LIMIT; i++){
					$("#pg"+i).show();
			}
			
			if(PG_START+PG_LIMIT>PG_COUNT){
				$(".next").addClass('next-off');
				$(".next-off").removeClass('next');				
			}
	}

}


function prevPage(){

	if(PG_START-PG_LIMIT>=1)
	{
		
		$(".next-off").addClass('next');
		$(".next").removeClass('next-off');
		
		$(".pgnumcount").hide();
	
		PG_START = PG_START-PG_LIMIT;
			
			for(i=PG_START; i<PG_START+PG_LIMIT; i++){
					$("#pg"+i).show();
			}

				if(PG_START-PG_LIMIT<1){
							$(".previous").addClass('previous-off');
							$(".previous-off").removeClass('previous');	
				}					
	}

}

function backToProducts(){

	$("#pagination-div").slideDown("fast");

	param_arr = getURLHashParam();	
	page = param_arr[1];

	if(LAST_CALL=="loadProducts"){
		loadProducts("",page);
	}else if(LAST_CALL=="searchNow"){
		searchNow("",page);
	}else if(LAST_CALL=="filterProductsFromMainCat"){
		filterProductsFromMainCat(LAST_CAT_ID,page);
	}else if(LAST_CALL=="filterProductsFromSubCat"){
		filterProductsFromSubCat(LAST_CAT_ID,page);
	}
		
}

function mainCatClick(id,page){
	
	MAIN_CAT = id;
	
	$(".prod_cats").fadeOut(250,function(){
	
				var dataURL = "action=showSubCategories&id=" + id;
					
					$.ajax({
						   type: "POST",  
						   url: http_path+"system/controllers/index-products-controllers.php",  
						   data: dataURL,  
						   success: function(response){  

								$(".prod_cats").html(response);
								$(".prod_cats").fadeIn(250);

						   }
					 });
	 });
	 
	
	 mainCatPaginations(id,page);
	 
}

function subCatClick(id,page){

	subCatPaginations(id,page);

}

function filterProductsFromMainCat(id,page){

			$(".pglinks").removeClass('active');
			$("#pga"+page).addClass('active');

	$("#main-itemdetails").fadeOut(250,function(){
	
						var dataURL = "action=filterProductsFromMainCat&id=" + id + "&page=" + page;

							$.ajax({
								   type: "POST",  
								   url: http_path+"system/controllers/index-products-controllers.php",  
								   data: dataURL,  
								   success: function(response){  

										$("#main-itemdetails").html(response);
										$("#main-itemdetails").fadeIn(250);										
		
								   }
							 });
		
	});	
	
	window.location.hash = "page:"+page;
	
			LAST_CALL = "filterProductsFromMainCat";
			LAST_CAT_ID = id;

}

function filterProductsFromSubCat(id,page){

			$(".pglinks").removeClass('active');
			$("#pga"+page).addClass('active');

	$("#main-itemdetails").fadeOut(250,function(){
	
						var dataURL = "action=filterProductsFromSubCat&id=" + id + "&page=" + page + "&main_id=" + MAIN_CAT;

							$.ajax({
								   type: "POST",  
								   url: http_path+"system/controllers/index-products-controllers.php",  
								   data: dataURL,  
								   success: function(response){  

										$("#main-itemdetails").html(response);
										$("#main-itemdetails").fadeIn(250);										
		
								   }
							 });
		
	});	
	
	window.location.hash = "page:"+page;
	
	LAST_CALL = "filterProductsFromSubCat";
	LAST_CAT_ID = id;
	

}

function mainCatPaginations(id,page){

$("#pagination-div").fadeOut(100,function(){

				var dataURL = "action=mainCatPaginations&id=" + id + "&page=" + page;

					$.ajax({
						   type: "POST",  
						   url: http_path+"system/controllers/index-products-controllers.php",  
						   data: dataURL,  
						   success: function(response){  

								$("#pagination-div").html(response);
								initPaginations();
								$("#pagination-div").fadeIn(100)
								
						   }
					 });
		
});	 					 
	 filterProductsFromMainCat(id,page);
	 

	 
}

function subCatPaginations(id,page){

$("#pagination-div").fadeOut(100,function(){
	
				var dataURL = "action=subCatPaginations&id=" + id + "&page=" + page + "&main_id=" + MAIN_CAT;

					$.ajax({
						   type: "POST",  
						   url: http_path+"system/controllers/index-products-controllers.php",  
						   data: dataURL,  
						   success: function(response){  

								$("#pagination-div").html(response);
								initPaginations();
								$("#pagination-div").fadeIn(100)
								
						   }
					 });
});	 

	filterProductsFromSubCat(id,page);

}

function searchPaginations(temp,page){

	var value = document.getElementById("search_prod").value;

				var dataURL = "action=searchPagination&value=" + value;

					$.ajax({
						   type: "POST",  
						   url: http_path+"system/controllers/index-products-controllers.php",  
						   data: dataURL,  
						   success: function(response){  

								$("#pagination-div").html(response);
								initPaginations();
								$("#pagination-div").fadeIn(100)
								
						   }
					 });

	searchNow(temp,page)

}

function allproductsPaginations(){

				var dataURL = "action=allPagination";

					$.ajax({
						   type: "POST",  
						   url: http_path+"system/controllers/index-products-controllers.php",  
						   data: dataURL,  
						   success: function(response){  

								$("#pagination-div").html(response);
									initPaginations();
								$("#pagination-div").fadeIn(100)
								
						   }
					 });
					 
}


function loadMainCats(){

	$(".prod_cats").fadeOut(250,function(){

				var dataURL = "action=showMainCats";
					
					$.ajax({
						   type: "POST",  
						   url: http_path+"system/controllers/index-products-controllers.php",  
						   data: dataURL,  
						   success: function(response){  

								$(".prod_cats").html(response);
								$(".prod_cats").fadeIn(250);
						   }
					 });
	 
	 });
	 
}

function getURLHashParam(){
	
	var hash_val = window.location.hash;

	var hash_val = hash_val.replace("#","");
	
	var hash_val_arr = hash_val.split(":");
	
	return hash_val_arr;
}


function loadBigImage(image){

	$("#gal_main_image").attr('src',image.rel);
	$("#gal_main_image_a").attr('href',image.rel);

}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function sendMail(){
	var name = document.getElementById("name").value;
	var email = document.getElementById("email").value;
	var subject = document.getElementById("subject").value;
	var messsge = document.getElementById("messsge").value;
	
	if(name==null || name == ""){
		$("#valMsg").html("Name Required");
	}else if(email==null || email == ""){
		$("#valMsg").html("Email Required");
	}else if(!echeck(email)){	
		$("#valMsg").html("Email is not valid");
	}else if(subject==null || subject == ""){
		$("#valMsg").html("Subject Required");
	}else if(messsge==null || messsge == ""){
		$("#valMsg").html("Message Required");	
	}else{	

				var dataURL =  
				 "name=" + name+  
				 "&email=" + email+  
				 "&subject=" + subject+			 
				 "&messsge=" + messsge+				 		 
				 "&action=" + "sendContactUs";
				 

					$.ajax({
						   type: "POST",  
						   url: http_path+"system/controllers/email-controller.php",  
						   data: dataURL,  
						   success: function(response){  
								
								$("#valMsg").html("Message Sent. We will get back to you soon.");	
								
						   }
					 });
		
	
	}
	
}


function printThis(){

	$('#printArea').show();

	w=window.open();
	w.document.write($('#printArea').html());
	w.print();
	w.close();
	
	$('#printArea').hide();

}

