$(document).ready(
	function()
	{   
		$('#hopupWindowClose').bind(
			'click',
			function()
			{
			    if($('#hopupMask').css('display') == 'block') removeMask();
			    $('#hopupIfame').attr("src",(getDomainById(1)+"/Images/Spacer.gif"));  
			    $('#hopupWindow').addClass("closeBolder"); // hide background
                $("#hopupWinContentBg").removeClass("hopupWinBolder"); //hide border 
				$('#hopupWindow').css("display","none");
			}
		);
		
		//mouseover action for all image buttons
		$(".hoverImg").hover(
		    function()
		    {
		        $(this).attr("src",$(this).attr("src" ).replace("off","on"));
		    },
		    function()
		    {
		        $(this).attr("src",$(this).attr("src" ).replace("on","off"));
		    }
		);	
		
		$("#lipStickOption").hover(
            function()
            {
            },
            function()
            {
                $(this).css("display","none");
            }
        );	
	}
);
function closeHopupWindow()
{
    if($('#hopupMask').css('display') == 'block') removeMask();
    parent.$('#hopupIfame').attr("src",(getDomainById(1)+"/Images/Spacer.gif"));  
    //$('#hopupWindow').hide();	safari do not work
    $('#hopupWindow').css("display","none");
}
function showMask()
{
  if($('#hopupMask')) {
       setHopupMaskSize(); 
       $('#hopupMask').css("display","block");
   } 
}
function removeMask()
{
   if($('#hopupMask')) {
       $('#hopupMask').width(0);
       $('#hopupMask').height(0);
       $('#hopupMask').css("display","none");
   }
}
var clientNameId;
var timeID=null;
function initHopup()
{
		$('#hopupWindow').css("display","block");
		$("#hopupWindowTop").css({"display":"block"});
		$('#hopupWindowTopContent').empty();   
		$('#hopupWindowClose').css({"right": "17px", "top":"17px"});
		$('#hopupWindowContent').find("#loadingDiv").css("display","none");
        $('#hopupWindowContent').find(".alertHopupWinBtn").remove();
        $('#hopupWindowContent').find("#hopupWarningBox").remove();		
        $("#hopupWinContentBg").removeClass("hopupWinBolder"); //hide border 
        $('#hopupWindow').addClass("closeBolder"); // hide background
        clearTimeout(timeID);//if Alert, clear its timeID to turn off the auto-close function
}
function openHopup(left,top,height,width,bgImg,pageTitle,openPage,maskFlag,pageFooter,clientId,itemName)
{
   clientNameId=clientId;
   initHopup();
    var imgUrl;
    if($('#hopupWindow')){
        //if "maskFlag" is false, do not move the hopup window to the center of screen, if true, move the hopup to center
         if (maskFlag)
         {
            if (pageTitle=="quick")
            {
//                height=160;
//                width = 400; 
                $("#hopupWindowTop").css({"height":"23px"});
            }
              showMask() ;
              $('#hopupWindow').css({"width":width,"height":height+25});
              setHopupToCenter(width, height); //move the hopup window to the center of screen, 
         }
         else
         {
                $('#hopupWindow').css({"left":left+"px","top":top+"px","width":width,"height":height});
         }
          if (bgImg!="blueBolder") 
           {
                imgUrl= "url("+getDomain()+"/Images/"+bgImg+")";
                $("#hopupWindowTopContent").css("padding-top","5px");
                $('#hopupWindow').css({"width":width, "height":height+10});
                //$('#hopupWinContentBg').css({"width":width-4}); 
               $('#hopupWindowClose').css({"top":10+"px", "right":25+"px"});
               if (bgImg == "brandHopupBg.png")
               {
                	$('#hopupPNGMask').addClass("hopupRedOutline");
                	$('#hopupPNGMask').css({"width":width, "height":height+10});
                	$('#hopupIfame').css("display","block");
              }
              else
                    $('#hopupWindow').css({"background":imgUrl,"backgroundRepeat":"no-repeat"});
              $('#hopupIfame').height(height-60);
           }
           else
           {
                $('#hopupWindow').removeClass("closeBolder");
                $('#hopupWinContentBg').css({"height":height-24});
                $("#hopupWinContentBg").addClass("hopupWinBolder");
                $("#hopupWindowTop").css({"padding-top":"0px"});
                $('#hopupPNGMask').removeClass("hopupRedOutline");
                $('#hopupWindow').css({"background":"none"}); 
                $('#hopupIfame').height(height-50);
            }
            if (openPage != "" && openPage != null) //load hopupIframe and show Loading text
            {
                $('#hopupIfame').attr("src",openPage);
                $('#hopupWindowContent').prepend(newHopupLoading($('#hopupWindowContent')));
                $("#loadingDiv > span").css("line-height", (height-120)+"px"); 
                showHopupLoading("#hopupIfame"); 
            }
            if(pageTitle=="quick"){
//                $('#hopupWindowTopContent').append("<h2  id='quickViewTitle'>QUICK <strong>SHOP</strong></h2>");
                $('#hopupWindowClose').css({"right":20+"px"});
            }
            else if (pageTitle==""){
               $("#hopupWindowTop").css({"display":"none"}); 
            }
            else if(pageTitle=="alert")
            {
                $('#hopupWindowTopContent').append("<h2  class='hopupTitle'></h2>");
               if(itemName=='cancelOrder')
              {
                        $('#hopupWindowTopContent > h2').text("Are you sure you want to cancel this order?");
              } 
              else if(itemName=='removeAddress')
              {
                        $('#hopupWindowTopContent > h2').text("Are you sure you want to remove this address?");
              }
              else
              {
                   if(itemName!="" && itemName!= undefined)
                        $('#hopupWindowTopContent > h2').text("Are you sure you want to remove this child?");
                   else
                        $('#hopupWindowTopContent > h2').text("Are you sure you want to REMOVE this item?");
              } 
                $('#hopupWindowClose').css({"top":"10px","right":"15px"});
                $('#hopupWindowTopContent > h2').css({"font-size":"17px","padding-top":"15px"});
                $('#hopupWindowContent').prepend("<div class='btnsBox alertHopupWinBtn'><input id='divYes' type='button' class='redBtn' onclick='__doPostBack(\""+clientNameId+"\",\"\");' value='Yes' /><input id='divNo' value='No' type='button' class='cancelBtn' /></div>");
                $('#hopupWindowContent'). find('#hopupIfame').height(0);
               if ($(window.parent.document).find("#shopDiv").css("display")=="block")
               {
                    $('#hopupMask').width($(document.body).width());
                    $('#hopupMask').height($(document.body).height());
               }
                $('#divNo').bind(
			    'click',
			        function()
			        {
			            if($('#hopupMask').css('display') == 'block') removeMask();
			            $('#hopupWindowContent').find(".alertHopupWinBtn").remove();
				        $('#hopupWindow').css("display","none");
			        }
		        );
		        
		        myStiky = $('#hopupWindow');
                var availTop = null;
                var availLeft = null;
                if (availTop==null)
                {
                        scrollTop = (window.parent.pageYOffset== undefined)?window.parent.document.documentElement.scrollTop:window.parent.pageYOffset;
                        availHeight = window.parent.document.documentElement.clientHeight;
                        availTop=(availHeight - myStiky.height())/2 + scrollTop - 50;
                        if ((availTop + myStiky.height()) > document.documentElement.clientHeight && window.parent!=window)
                        {
                            availTop = document.documentElement.clientHeight - myStiky.height() - 45;
                        }
                        availTop=(availTop<0)?0:availTop;
                }
                if (availLeft==null)
                {
                      scrollLeft = (window.pageYOffset== undefined)?document.documentElement.scrollLeft:window.pageXOffset;
                      if ($("#wrap").width()!=null && document.documentElement.clientWidth>$("#wrap").width() )
                      {
                            availLeft=($("#wrap").width() - myStiky.width())/2 + scrollLeft;
                      }
                      else
                      {
                            availWidth = document.documentElement.clientWidth;
                            availLeft=(availWidth - myStiky.width())/2 + scrollLeft;
                      }
                     availLeft=(availLeft<0)?0:availLeft; 
                }
                myStiky.css({"top":availTop,"left":availLeft }); 
                }
                else{
                $('#hopupWindowTopContent').append("<h2  class='hopupTitle'></h2>");
                $('#hopupWindowTopContent > h2').text(pageTitle);

                }
//			if(openPage!='')
//			{
//				$('#hopupWindowContent').attr("innerHTML",
//					"<iframe id=\"hopupIfame\" scrolling=\"no\" frameborder=\"0\" width=\"96%\" style=\"height: 60px; \" src=\"\"></iframe>");
//				$('#hopupIfame').height(height-60);
//				$('#hopupIfame').attr("src",openPage);
//				$('#hopupIfame').show();
//			}
            if (pageFooter==true)
            {
               if (pageTitle=="quick")
               {
                        $("#hopupWindowFooterContent").css({"top":height-30, "width":width-50}); 
                        }
                       else if(pageTitle =="Suggest a Product")
                      {
                            $("#hopupWindowFooterContent").css({"top":height-40, "width":width-50}); 
                      }  
                      else
                            $("#hopupWindowFooterContent").css({"top":height-40, "width":width-30}); 
                      $("#hopupWindowFooterContent").css("display","block");
                      $(".ContinueShop").click(
                        function()
                        {
                            $(this).parent().parent().find('#hopupWindowClose').click();
                        }); 
             } 
            else
            {
                $("#hopupWindowFooterContent").css("display","none");
             }   
    }  
}
function closeHopup(flag)
{
    parent.$('#hopupWindowClose').click();
    if(flag) 
        parent.$('.RefreshButton').click(); 
        //parent.eval('refreshHeader();');
 }

function  SetCwinHeight(obj) 
{ 
        var iframeHeight=parseInt(getCookie("iframeHeight"))+80;
        if(iframeHeight)
       {
           delCookie("iframeHeight");
           $('#hopupWindow').css("height",iframeHeight+25+"px"); 
           $("#hopupWinContentBg").css("height", iframeHeight-24+"px");
           $("#hopupWindowFooterContent").css("top",iframeHeight-30+"px"); 
           $('#hopupIfame').height(iframeHeight-60);
           setHopupToCenter($("#hopupWindow").width(), iframeHeight); 
       } 
} 
function SetPersonalizeCwinHeight(obj)
{
   if ($(obj).attr("src").indexOf((getDomainById(1)+"/Images/Spacer.gif"))<0)
   {
       closeHopupLoading(obj); 
       SetCwinHeight(obj);
   }
}

function resetHopupHeight(height)
{
    
    $('#hopupWindow').css("height",height+25); 
    $("#hopupWinContentBg").css("height", height-24);
    $("#hopupWindowFooterContent").css("top",height-30); 
    $('#hopupIfame').height(height-50);
    $('#hopupIfame').height(height-50);
    setHopupToCenter($("#hopupWindow").width(), height); 
}
function resetHopupHeightNoHopupFooter(height)
{
    $('#hopupWindow').css("height",height+45); 
    $("#hopupWinContentBg").css("height", height);
    $('#hopupIfame').height(height);
    setHopupToCenter($("#hopupWindow").width(), height+45); 
}
function resetHopupWidth(width)
{
    $('#hopupWindow').css("width",width);
    $('#hopupWinContentBg').css({"width":width-4});
    $("#hopupWindowFooterContent").css({"width":width-50});   
}
function setHopupToCenter(width, height)
{
      if (window.pageYOffset== undefined)
     {
            scrollTop =  document.documentElement.scrollTop;//ie,ff
            scrollLeft =  document.documentElement.scrollLeft;
     }
     else
     {
            scrollTop =  window.pageYOffset;// safari
            scrollLeft =  window.pageXOffset;// safari
      }
      hopupTop=(document.documentElement.clientHeight - height)/2 + scrollTop;
      if ($("#wrap").width()!=null && document.documentElement.clientWidth>$("#wrap").width() )
      {
            hopupLeft=($("#wrap").width() - width)/2 + scrollLeft;
      }
      else
      {
            hopupLeft=(document.documentElement.clientWidth - width)/2 + scrollLeft;
      }
      if (hopupLeft<0) 
            hopupLeft = 0;
      if (hopupTop<scrollTop) 
            hopupTop = scrollTop + 20;
       $('#hopupWindow').css({"left":hopupLeft+"px","top":hopupTop+"px"});
}
function helpCenterHopup()
{
   if ($(".titleText")[0].innerHTML!="Welcome to our Help Center")
   {
        title = $(".titleText")[0].innerHTML + " - Top Questions";
   }
   else if ($(".titleText")[0].innerHTML=="Welcome to our Help Center")
   {
        title = "Top Questions";
   }
   openHopup(0,0,443,630,'blueBolder',title,'HelpCenterHopup.aspx',true, false); 
}

//show the  QuickView button
function showQuickViewBtn(obj)
{
    if(obj.canShow){ 
        $(".quickViewBtn").css("display","none");
        $(obj).find(".quickViewBtn").css("display","block");
    } 
}

//builder the show function
//becase the arguments could not specified when calling the setTimeout function
//so use the transitive function
function showQuickViewBtnFunctionBuilder(obj)
{
    return function(){
       showQuickViewBtn(obj);
   } 
} 

function showQuickViewHopup(productId)
{
    var  redirectUrl="../Product/QuickView.aspx?ProductId="+productId;
    openHopup(0,0,576,850,"blueBolder","quick",redirectUrl,true,true);
}

function setHopupMaskSize()
{
       var leftValue=0;
       var widthValue=$(document).width(); 
       var parent=$('#hopupMask').parent().parent();
       if(($(document.body).width()-widthValue)>0){
//           leftValue=-($(document.body).width()-980)/2;

            //different position value cause different left value
           switch(parent.css("position"))
           {
               case "static":
                break;
               case "relative":
                    leftValue=-($(document.body).width()-widthValue)/2; 
                break;
               case "abosulte":
                break;     
           }
           widthValue=$(document.body).width();
       } 
       $('#hopupMask').css("left",leftValue);
       $('#hopupMask').width(widthValue);
       $('#hopupMask').height($(document.body).height());
}

$(window).resize(
    function(){
   if($("#hopupMask") && $("#hopupMask").css("display")=="block")
   {
       setHopupMaskSize();
   }
   }
); 
//Hopup Loading Text functions begin
function newHopupLoading(obj) // ex. $("#test").append(loadingHTML())
{
   if (obj.find("#loadingDiv").length==0) 
        return "<div id='loadingDiv'><span><img src='/Images/loading.gif'></span><span>Please Wait ...</span></div>";
  else
        return "";
}
function showHopupLoading(obj)
{
   $(obj).css("top","-10000px");
   $(obj).parent().find("#loadingDiv").css("display","block");   
}
function closeHopupLoading(obj)
{
   $(obj).css("top","0");
   $(obj).parent().find("#loadingDiv").css("display","none"); 
}
function closeHopupIframeLoading(obj)
{
    if ($(obj).attr("src")!="" && $(obj).parent().find("#loadingDiv").css("display")=="block")
   { 
        closeHopupLoading(obj);
    }
}
//Hopup Loading Text functions end

function showLipStickList(lipStickLi)
{
    var lipStickDiv =  $("#lipStickOption");
    var newLeft = ($(lipStickLi).offset().left + $(lipStickLi).width()) - $(".wrapOut").offset().left - lipStickDiv.width() - 16;
    var newTop = ($(lipStickLi).offset().top + $(lipStickLi).height()) - $(".wrapOut").offset().top;
    if(lipStickDiv.css("display")=="none")
    {
        lipStickDiv.show();
        closeLipStickList();
    }
    else
    {
        lipStickDiv.hide();
    }
    lipStickDiv.css({"left":newLeft + "px","top":newTop + "px"});
}

function closeLipStickList()
{
    document.onclick = outsideClickLipStickList;
    function outsideClickLipStickList(event)
    {
        event = (event == null)?window.event:event;
        var srcelement= event.target?event.target:event.srcElement;
        if (srcelement.getAttribute("author") != "outClick")
        {
            $("#lipStickOption").hide();
            document.onclick = null;
        }
    }
}

function confirmRemoveShippingAddress(removeButtonId, topObj) {
    var hopupTop = (topObj != undefined) ? (topObj.offset().top + 20) : null;
    var content = "<div style='line-height:17px; padding-top:6px;' class='textCenter'><span class='font14Text bold'>Are you sure you want to remove this address?</span></div>";
    openConfirmPopup(430, 120, hopupTop, content, "", true, function () {
        __doPostBack(removeButtonId, '');
    });
}

function openConfirmPopup(width, height, top, content, title, showMask, callbackFunction) {
    var confirmPopupPara = {
        width: width,
        height: height,
        top: top,
        title: title,
        content: content,
        showMask: showMask
    };
    var confirmPopup = new ConfirmPopup(confirmPopupPara);
    confirmPopup.addCallBack("ok", callbackFunction);
    confirmPopup.show();
}
