//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;
var popupDefaultHeight = 0;
var popupDefaultWidth = 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("slow");  
		$("#popupRemarks").fadeIn("slow");  
		popupStatus = 1;  
	}  
}

//disabling popup with jQuery magic!  
function disablePopup(){  
	//disables popup only if it is enabled  
	if(popupStatus==1){  
		$("#popupRemarksContent").height(popupDefaultHeight);
		$("#popupRemarksContent").width(popupDefaultWidth); 
		$("#popupRemarks").height(popupDefaultHeight);
		$("#popupRemarks").width(popupDefaultWidth);
		$("#backgroundPopup").fadeOut("slow");  
		$("#popupRemarks").fadeOut("slow");  
		popupStatus = 0; 
	}  
}

//centering popup  
function centerPopup(selWidth, selHeight){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = popupDefaultHeight;  
	if (selHeight) {
		popupHeight = selHeight;
	}
	var popupWidth = popupDefaultWidth; 
	if (selWidth) {
		popupWidth = selWidth;
	} 
	
	// zjistim si sirku a vysku okna
	if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    windowWidth = window.innerWidth;
	    windowHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    windowWidth = document.documentElement.clientWidth;
	    windowHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    windowWidth = document.body.clientWidth;
	    windowHeight = document.body.clientHeight;
	}
	
	var leftPos = 0;
	if (windowWidth > popupWidth)	{
		leftPos = (windowWidth-popupWidth)/2;
	}
	var topPos = 0;
	if (windowHeight > popupHeight)	{
		topPos = (windowHeight-popupHeight)/2;
	}
	
	//centering  
	$("#popupRemarks").css({"top": topPos, "left": leftPos});
	
	if (popupHeight != popupDefaultHeight) {	
		$("#popupRemarks").animate({height: popupHeight}, 'slow');
		$("#popupRemarksContent").animate({height: popupHeight}, 'slow');
	}
	if (popupWidth != popupDefaultWidth) {	
		$("#popupRemarks").animate({width: popupWidth}, 'slow');
		$("#popupRemarksContent").animate({width: popupWidth}, 'slow');
	}
		
	//only need force for IE6  
	$("#backgroundPopup").css({"height": windowHeight});  
}

//loading popup contents
function loadPopupContents(selOrderId){  
	$('#popupRemarksContent').html("<div style='text-align: center; margin: 10px;'><img class='loader' src='http://img.123shop.cz/img/css/123shop/newportal/ajax-loader.gif'/></div>");
	$.ajax({
	   type: "GET",
	   url: "/ashoworderremarks/?orderid=" + selOrderId,
	   context: document.body, 
	   dataType: "html",
	   success: function(data){
			$("#popupRemarksContent").html(data);
			$("#popupRemarksContent ul").css({"padding-left": "0px","margin-top": "5px"});
			$("#popupRemarksContent ul li").css({"margin-left": "30px"});
	   }
	});
}

//loading popup contents
function loadPopupContentsUrl(selUrl){  
	$('#popupRemarksContent').html("<div style='text-align: center; margin: 10px;'><img class='loader' src='http://img.123shop.cz/img/css/123shop/newportal/ajax-loader.gif'/></div>");
	$.ajax({
	   type: "GET",
	   url: "" + selUrl,
	   context: document.body, 
	   dataType: "html",
	   success: function(data){
			$("#popupRemarksContent").html(data);
			/*$("#popupRemarksContent ul").css({"padding-left": "0px","margin-top": "5px"});
			$("#popupRemarksContent ul li").css({"margin-left": "30px"});*/
	   }
	});
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

$(document).ready(function(){ 
	popupDefaultHeight = $("#popupRemarks").height();
	popupDefaultWidth = $("#popupRemarks").width();
 
	//LOADING POPUP  
	//Click the button event!  
	$(".remarksshow").click(function(){  
		//centering with css  
		centerPopup(); 
		
		//loadContents
		var selOrderId = $(this).attr('orderid');
		loadPopupContents(selOrderId); 
		
		//load popup  
		loadPopup();  
	});
	
	$(".pricelistshow").click(function(){  
		//centering with css  
		var selWidth = $(this).attr('popWidth');
		var selHeight = $(this).attr('popHeight');
		if (selWidth && selHeight) {
			centerPopup(selWidth, selHeight); 
		} else {
			centerPopup(); 
		}
		
		//loadContents
		var selUrl = $(this).attr('href');
		if (selUrl) {
			loadPopupContentsUrl(selUrl); 
			
			//load popup  
			loadPopup(); 
		} 
		
		return false;
	});

	//CLOSING POPUP  
	//Click the x event!  
	$("#popupRemarksClose").click(function(){  
		disablePopup();  
	});  

	//Click out event!  
	$("#backgroundPopup").click(function(){  
		disablePopup();  
	});  

	//Press Escape event!  
	$(document).keypress(function(e){  
		if(e.keyCode==27 && popupStatus==1){  
			disablePopup();  
		}  
	});  
	
	$(window).scroll(function(){ 
      $("#RaifkaBanner").css({"top": getScrollY()}); 
    });
});

