/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
$.ajaxSetup ({
    // Disable caching of AJAX responses */
 //   cache: false
});



var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
//		$("#backgroundPopup").css({
//			"opacity": "0.7"
//		});
//		$("#backgroundPopup").fadeIn("fast");
		$("#popupMessage").fadeIn("fast");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
//		$("#backgroundPopup").fadeOut("fast");
		$("#popupMessage").fadeOut("fast");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(event){
	var ie = document.all ? true : false;
	//request data for centering
	var windowWidth;
	var windowHeight;
	//var viewportwidth;
   //var viewportheight;
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

 if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       windowWidth = document.documentElement.clientWidth,
       windowHeight = document.documentElement.clientHeight
 } 
 
 else if (typeof window.innerWidth != 'undefined')
 {
      windowWidth = window.innerWidth,
      windowHeight = window.innerHeight
 }
 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 
 // older versions of IE
 
 else
 {
       windowWidth = document.getElementsByTagName('body')[0].clientWidth,
       windowHeight = document.getElementsByTagName('body')[0].clientHeight
 }

	var popupHeight = $("#popupMessage").height();
	var popupWidth = $("#popupMessage").width();
	var left=3;
	var top=3;
	var h;
	//windowWidth=document.getElementsByTagName('body')[0].clientWidth;
	//windowHeight=document.getElementsByTagName('body')[0].windowHeight;
	//alert(windowHeight);
	//alert(event.pageY);
	var u = ie ? event.clientY + document.documentElement.scrollTop : event.pageY;
	var l = ie ? event.clientX + document.documentElement.scrollLeft : event.pageX;
	//alert(document.getElementsByTagName('body')[0].clientWidth);
	
		//left = ie ? (windowWidth/2)+(popupWidth) : (windowWidth/2)-(popupWidth/2) ;	
	left = (windowWidth/2)-(popupWidth/2) ;
	
	h= $("#popupMessage").height() + top;
	//centering
	$("#popupMessage").css({
		"position": "absolute",
		"top":(event.pageY - top) + 'px',
		//"left": (l + left) + 'px'
		"left": left + 'px'
	});
	//only need force for IE6
	
//	$("#backgroundPopup").css({
	   //"width":100 + '%',
//		"height": windowHeight
//	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	var tb_pathToImage = "/static/default/images/ajax-loader.gif";
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	//alert('sdfsdfsdfsdfsfsdf');
	$('a').click(function(event){
		//alert('sfsd');
	    var obj = this;
	    if(obj.rel=='readmore'){
	    //$.jCache.clear();
		var data = {}
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		      $('#TB_load').show();
		$.get("product_description/?product_id="+obj.id.split('_')[1],
		
		function(responseData) {
				  $('#TB_load').remove();
                  $('#popupArea').html(responseData);
		          $('#popupTitle').text(obj.name);
                },
                "json"); 
	    	
	    	centerPopup(event);
	    	loadPopup();
	    	
	    	return false;
  	   }
  	   //var flag=true;
  	   if(obj.rel=='add_into_cart'){
  	   		//jQuery.cache = { };
  	   		//$.ajaxSetup ({
			    // Disable caching of AJAX responses */
			 //   cache: false
			//});
  	   		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
			$('#TB_load').show();
  	   		url=this.id+'&product_qty='+document.getElementById(this.name + '_qty').value;
  	   		//url=this.id;
  	   		//alert(url);
  	   		$.ajax({
			    url: url,
			    type: 'GET',
			    cache : false,
			    timeout: 1000,
			    success: function(responseData){
			    		$('#TB_load').remove();
			    		alert(responseData);
			    		//$('#cart_info').load("cart_info/");
			    		//$('#cart_info').load("cart_info/");
			    		$.ajax({
						    url: "cart_info/",
						    type: 'GET',
						    cache : false,
						    timeout: 1000,
						    success: function(responseData){
			//			    	alert(responseData);
			//			    	alert(document.getElementById('cart_info').innerHTML);
								//$('#cart_info').load("cart_info/");
			    				document.getElementById('cart_info').innerHTML=responseData;
			    			}
			    		});
						//$('#cart_info').show();
			        // do something with xml
			    }
			});
			  	   		
			//$.get(url,function(responseData) {
			//		$('#TB_load').remove();
			//		if (responseData!=""){
			//			alert(responseData);
			//		}
					//$.jCache.clear();
			//		$('#cart_info').load("cart_info/");
			//		$('#cart_info').show();
             //   },'json');
                
             return false;
  	   }
  	   
  	   
  	   if(obj.rel=='update_cart'){
  	   		//jQuery.cache = { };
  	   		//$.ajaxSetup ({
			    // Disable caching of AJAX responses */
			 //   cache: false
			//});
			//alert('sadf');
			//alert($);
  	   		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
			$('#TB_load').show();
			var i = Math.round(10000*Math.random());
  	   		//url=this.id+'&product_qty='+document.getElementById(this.name + '_qty').value;
  	   		url=this.href+'&'+this.name+'='+document.getElementById(this.name).value+'&random_number='+i;
  	   		//url=this.id;
  	   		//alert(url);
  	   		$.ajax({
			    url: url,
			    type: 'GET',
			    cache : true,
			    timeout: 1000,
			    success: function(responseData){
			    		$('#TB_load').remove();
			    		alert(responseData);
			    		
			    		//alert("cart_detail_info/?rendom_number="+i);
			    		$('#updated_cart').load("cart_detail_info/?rendom_number="+i);
			    		//$('#cart_info').load("cart_info/");
			    		//$.ajax({
						 //   url: "cart_detail_info/",
						  //  type: 'GET',
						   // cache : false,
						    //timeout: 1000,
						    //success: function(responseData){
			//			    	alert(responseData);
			//			    	alert(document.getElementById('cart_info').innerHTML);
								//$('#cart_info').load("cart_info/");
			    		//		document.getElementById('updated_cart').innerHTML=responseData;
			    		//	}
			    		//});
						//$('#cart_info').show();
			        // do something with xml
			    }
			});
			  	   		
			//$.get(url,function(responseData) {
			//		$('#TB_load').remove();
			//		if (responseData!=""){
			//			alert(responseData);
			//		}
					//$.jCache.clear();
			//		$('#cart_info').load("cart_info/");
			//		$('#cart_info').show();
             //   },'json');
                
             return false;
  	   }
  	   
  	   //if(obj.rel=='update_cart'){
  	   		//$.ajaxSetup ({
			    // Disable caching of AJAX responses */
			    //cache: 1;
			//});
			//alert("DFFFF");
  	   //		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
	//		$('#TB_load').show();
  	 //  		url=this.href+'&'+this.name+'='+document.getElementById(this.name).value;
  	   		
  	   		//$.ajax({
			//    url: url,
			//    type: 'GET',
			//    cache :true,
			 //   timeout: 1000,
			//   success: function(responseData){
			//    	$('#TB_load').remove();
  	   			
  	   			//	alert(responseData);
		//			$('#updated_cart').load("cart_detail_info/");
		//			$.ajax({
		//			    url: "cart_detail_info/",
		//			    type: 'GET',
		//			    cache : false,
		//			    timeout: 1000,
		//			    success: function(responseData){
					    	//alert(responseData);
		//			    	alert(document.getElementById('cart_info').innerHTML);
							//$('#cart_info').load("cart_info/");
		  //  				document.getElementById('updated_cart').innerHTML=responseData;
		    //			}
		    //		});
					
			//    }
			//});
  	   		
  	   //		$.get(url,function(responseData) {
  	   //			$('#TB_load').remove();
  	   			
  	   //			if (responseData!=""){
	//					alert(responseData);
						//alert(responseData.message);
	//			}
				
		//		jQuery.cache = { };
  	  			//$('#cart_info').load("cart_info/");
				
				
				
				//$('#cart_table').remove();
			//	$('#updated_cart').load("cart_detail_info/");
		//		$('#updated_cart').show();
				
  	  //		},'json');
  	  		
  	  		//$.ajax({
			//		    url: "cart_detail_info/",
			//		    type: 'GET',
			//		    cache : false,
			//		    timeout: 1000,
			//		    success: function(responseData){
					    	//alert(responseData);
		//			    	alert(document.getElementById('cart_info').innerHTML);
							//$('#cart_info').load("cart_info/");
		  //  				document.getElementById('updated_cart').innerHTML=responseData;
		   // 			}
		   // 			});
  	  		
	//	return false;
  	   		
  	//  }
  	   
  	   
        });
    
  	

				
	//CLOSING POPUP
	//Click the x event!
	$("#popupMessageClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
