﻿// JScript File

var the_timeout;

function PagingClicked(pageRequest,firstLink,previousLink,nextLink,lastLink)
{
    //need to make sure we hide the tooltip before a page change request so it still does not show up after new products are shown.
    var tooltip = $find('ctl00_Main_RadToolTip1');
    tooltip.hide(); 
    
    var index=PurpleParrot.Storefront.WebUI.Search_ProductCatalog.ReturnCurrentPageIndex();
    if(((pageRequest=="Prev"||pageRequest=="First") && index.value[0]==0)||((pageRequest=="Next"||pageRequest=="Last") && index.value[0]==index.value[1]))
    {
    }
    else
    {
    
        var res=PurpleParrot.Storefront.WebUI.Search_ProductCatalog.InventoryPaging(pageRequest);
        document.getElementById("divInventory").innerHTML=res.value[0];
        if(res.value[1]==0)
        {
            document.getElementById(previousLink).disabled=true;
            document.getElementById(previousLink).href="";
            document.getElementById(firstLink).disabled=true;
            document.getElementById(firstLink).href="";
        }
        else
        {
         document.getElementById(previousLink).disabled=false;
         document.getElementById(firstLink).disabled=false;
        }
        
        if(res.value[1]==res.value[2])
        {   
            document.getElementById(nextLink).disabled=true;
            document.getElementById(nextLink).href="";
            document.getElementById(lastLink).disabled=true;
            document.getElementById(lastLink).href="";
        }
        else
        {
            document.getElementById(nextLink).disabled=false;
             document.getElementById(lastLink).disabled=false;
        }
        
        document.getElementById("ctl00_Main_lblPageCount").innerHTML="Page <b>"+ (res.value[1]+1) + "</b> of <b>" + (res.value[2]+1)+"</b>";
        }
}


function Sort(SortColumn)
{
   var res=PurpleParrot.Storefront.WebUI.Search_ProductCatalog.Sorting(SortColumn);
   
   if(res!=null)
   {
        document.getElementById("ctl00_Main_lblPageCount").innerHTML=res.value[1];
        document.getElementById("divInventory").innerHTML=res.value[2];
        if(res.value[0])
        {   
            //paged data
            var firstLink="ctl00_Main_btnFirst";
            var previousLink="ctl00_Main_btnPrev";
            var nextLink="ctl00_Main_btnNext";
            var lastLink="ctl00_Main_btnLast";
            
            document.getElementById(previousLink).disabled=true;
            document.getElementById(previousLink).href="";
            document.getElementById(firstLink).disabled=true;
            document.getElementById(firstLink).href="";
            document.getElementById(nextLink).disabled=false;
            document.getElementById(lastLink).disabled=false;
            
            
        }
      document.getElementById("ctl00_Main_lblsortby").innerHTML=res.value[3];
      document.getElementById("ctl00_Main_lblorderdir").innerHTML=res.value[4];
      
     if(res.value[4]=="Ascending")
    {
        document.getElementById("ctl00_Main_sortimage").src="../Images/Navigation/arrowup.gif";
    }
    else
    {
        document.getElementById("ctl00_Main_sortimage").src="../Images/Navigation/arrowdown.gif";
    }
        return false;    
        
   }

}


function SeeAllItems()
{

    document.getElementById("ctl00_Main_paging").style.display="none";
    document.getElementById("ctl00_Main_lbSeeAllItems").style.display="none";
    document.getElementById("ctl00_Main_lbPagedItems").style.display="block";
    
    var res=PurpleParrot.Storefront.WebUI.Search_ProductCatalog.GetAllItems();

   if(res!=null)
   {
         document.getElementById("ctl00_Main_lblPageCount").innerHTML=res.value[1];
        document.getElementById("divInventory").innerHTML=res.value[2];     
         document.getElementById("ctl00_Main_lblsortby").innerHTML=res.value[3];
      document.getElementById("ctl00_Main_lblorderdir").innerHTML=res.value[4];
      
     if(res.value[4]=="Ascending")
    {
        document.getElementById("ctl00_Main_sortimage").src="../Images/Navigation/arrowup.gif";
    }
    else
    {
        document.getElementById("ctl00_Main_sortimage").src="../Images/Navigation/arrowdown.gif";
    }
   
   }
   
 
        return false;
        
}

function SeePagedData()
{
    document.getElementById("ctl00_Main_paging").style.display="block";
    document.getElementById("ctl00_Main_lbSeeAllItems").style.display="block";
    document.getElementById("ctl00_Main_lbPagedItems").style.display="none";
    
     var res=PurpleParrot.Storefront.WebUI.Search_ProductCatalog.InventoryPaging('First');
     document.getElementById("divInventory").innerHTML=res.value[0];
       
        //paged data
            var firstLink="ctl00_Main_btnFirst";
            var previousLink="ctl00_Main_btnPrev";
            var nextLink="ctl00_Main_btnNext";
            var lastLink="ctl00_Main_btnLast";
        if(res.value[1]==0)
        {
            document.getElementById(previousLink).disabled=true;
            document.getElementById(previousLink).href="";
            document.getElementById(firstLink).disabled=true;
            document.getElementById(firstLink).href="";
        }
        else
        {
         document.getElementById(previousLink).disabled=false;
         document.getElementById(firstLink).disabled=false;
        }
        
        if(res.value[1]==res.value[2])
        {   
            document.getElementById(nextLink).disabled=true;
            document.getElementById(nextLink).href="";
            document.getElementById(lastLink).disabled=true;
            document.getElementById(lastLink).href="";
        }
        else
        {
            document.getElementById(nextLink).disabled=false;
             document.getElementById(lastLink).disabled=false;
        }
        
        document.getElementById("ctl00_Main_lblPageCount").innerHTML="Page <b>"+ (res.value[1]+1) + "</b> of <b>" + (res.value[2]+1)+"</b>";


    return false;
}

function ShowToolTip(element,InvID,btnAddToCart,AddedToCartLabel)  
{  
    var tooltip = $find('ctl00_Main_RadToolTip1');
    if (tooltip != null)
    {
//        if (!tooltip.isVisible())
//        {
            the_timeout = setTimeout(function(){  
                var divDetail  = document.getElementById("ctl00_Main_divProductDetail");
                var res=PurpleParrot.Storefront.WebUI.Search_ProductCatalog.UpdateInventoryTooltip(InvID,btnAddToCart,AddedToCartLabel);
                divDetail.innerHTML = res.value; 
                tooltip.set_targetControlID(element.id);  
                tooltip.show(); 
            }, 1000);  
//        }
    }
}  

function StopTooltip()
{
    clearTimeout(the_timeout);
}

function TooltipBeforeShow(sender, eventArgs)
{
//public enum ToolTipPosition
//    {
//        TopLeft = 11,
//        TopCenter = 12,
//        TopRight = 13,
//        MiddleLeft = 21, 
//        Center = 22,
//        MiddleRight = 23,
//        BottomLeft = 31,
//        BottomCenter = 32, 
//        BottomRight = 33
    //    }
    
    //get imagebutton that the tooltip is for
    var tarImg = sender._targetControl;
    
    //get the absolute position of the imagebutton
    var pos = findPos(tarImg);

    //get the brower's inner width and height
    var sz = GetWindowSize();
    
    //get and set variables to see if their is enough room to show tooltip at botton, 
    //otherwise, show at top
    var strGetHeight1 = sender.get_height();
    var strGetHeight2 = strGetHeight1.substring(0, strGetHeight1.length - 2);
    var numGetHeight = (+strGetHeight2);    //need to double given height as this is not returning the "real" height.

    //get and set variables to see if their is enough room to show tooltip at left, 
    //otherwise, show at right
    var strGetWidth1 = sender.get_width();
    var strGetWidth2 = strGetWidth1.substring(0, strGetWidth1.length - 2);
    var numGetWidth = (+strGetWidth2);
    
    var roomTop = pos.y + tarImg.clientHeight + numGetHeight;
    if (roomTop > sz.height)
    {
        sender.set_position(12);    //12 is top center
        if ((numGetWidth/2) > pos.x)
        {
            sender.set_position(13);    //13 is top right
        }
        if (((numGetWidth / 2) + pos.x) > sz.width)
        {
            sender.set_position(11);    //11 is top left
        }
    }
    else
    {
        sender.set_position(32);    //32 is bottom center
        if ((numGetWidth / 2) > pos.x)
        {
            sender.set_position(33);    //33 is bottom right
        }
        if (((numGetWidth / 2) + pos.x) > sz.width)
        {
            sender.set_position(31);    //31 is bottom left
        }
    }

}

function GetWindowSize()
{
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number')
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight))
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return {width: myWidth, height:myHeight};
}

// returns an object that represents the absolute position of element obj
function findPos(obj)
{
    // return a "point" for ease of use...
    return { x: findPosX(obj), y: findPosY(obj) };

}

// from Quirksmode
function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;


    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;

    return curtop;
}

function ProductDetailPage(invId) {
    if (invId > 0) {
        location.href = "ProductDetail.aspx?InvID=" + invId;
        return false;
    }

}
