﻿
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_initializeRequest(initializeRequest);

    function initializeRequest(sender, args) 
    {

        var item = $get('bidContent');

        if (item.style.display != "none") 
        {
            moveItemBack();
        }
    }

    function showProductCatalog(productComparisonWindow) {

        var selectedItems = new Array();

	    var itemsSerialized = "";
	    var pattern = new RegExp("CompareItem");
	    var checkBoxes = document.getElementsByTagName('input');
	    for (cbx = 0; cbx < checkBoxes.length; cbx++) {				
		    if (pattern.test(checkBoxes[cbx].name)) {
			    checkBox = checkBoxes[cbx];
			    // The Checkbox is wrapped in a SPAN that contains the itemID in it's TITLE attribute (ToolTip property on asp:Checkbox) //
			    var itemID = checkBox.parentNode.title;					
			    if (checkBox.checked) {
				    Array.add(selectedItems, itemID);
			    } else {
				    Array.remove(selectedItems, itemID);
			    }
		    }
		}

	    if (selectedItems.length > 1) {
	        comparisonWindow = $find(productComparisonWindow);                
		    comparisonWindow.set_windowState($IG.DialogWindowState.Normal); 
		    for (i=0; i<selectedItems.length; i++) {
			    itemsSerialized += selectedItems[i].toString() + ";";
		    }
		    comparisonWindow.get_contentPane().set_contentUrl("ProductComparison.aspx?IDS=" + itemsSerialized);
	    }
	    return false;
    }


    function reportSize() {
      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;
          }
        }
      }
    }

    function doPosChangeMem(elem,startPos,endPos,steps,intervals,powr) {
	    finished = false;
	    if (elem.posChangeMemInt) window.clearInterval(elem.posChangeMemInt);
		    var actStep = 0;
		    elem.posChangeMemInt = window.setInterval(   function() {
			    elem.currentPos = [
				    easeInOut(startPos[0],endPos[0],steps,actStep,powr),
				    easeInOut(startPos[1],endPos[1],steps,actStep,powr)
			    ];
			    elem.style.left = elem.currentPos[0]+"px";
			    elem.style.top = elem.currentPos[1]+"px";
    		
			    // detect if animation is complete 
			    if(elem.style.left == (endPos[0]+"px")) { 
				    finished = true;
			    }
    		
			    actStep++;
			    if (actStep > steps) window.clearInterval(elem.posChangeMemInt);
			    }
			    ,intervals   )
    		
    }

    function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
	    var delta = maxValue - minValue;
	    var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	    return Math.ceil(stepp)
    }
    		
    function moveItemIn (itemID) {
    	
	    var items =			document.getElementById('mainContent');
	    var item =			document.getElementById('bidContent');
	    var itemsPosTop =		items.style.top;
	    var itemsPosLeft =		items.style.left;
	    var itemsWidth =		items.clientWidth + 280;
	    var itemPosTop =		item.style.top;
	    var itemPosLeft =		item.style.left;
	    var itemWidth =		item.clientWidth + 300;
	    item.style.display =	"block";
	    //alert(itemID);
	    reportSize();
    	
	    doPosChangeMem(  items  ,[10,0],[-itemsWidth,0],20,20,0.5);
    	
//	    doPosChangeMem(  item  ,[myWidth,95],[260,95],20,20,0.5);
	    doPosChangeMem(  item , [myWidth,30],[10,30],20,20,0.5);
    }


    function moveItemBack () {
	    var items =			document.getElementById('mainContent');
	    var item =			document.getElementById('bidContent');
	    var itemsPosTop =		items.style.top;
	    var itemsPosLeft =		items.style.left;
	    var itemsWidth =		items.clientWidth + 280;
	    var itemPosTop =		item.style.top;
	    var itemPosLeft =		item.style.left;
	    var itemWidth =		item.clientWidth + 300;
	    reportSize();
    	
	    doPosChangeMem(  items  ,[-itemsWidth,0],[10,0],20,20,0.5);
    	
	    //doPosChangeMem(  item  ,[260,95],[myWidth,95],20,20,0.5);
	    doPosChangeMem(  item  ,[10,30],[myWidth,30],20,20,0.5);
	    // this hides the bid DIV after it slides back, that way if the user re-sizes their browser
	    // they won't see the div on top of the grid 
	    var checkIt = window.setInterval(function() {				
		    if (finished) {
			    item.style.display = "none";
			    window.clearInterval(checkIt);
		    }
	    } , 50);
    }
		
    function RetrieveAuctionItem(auctionItemId)
    {
       
       PageMethods.GetGalleryItem(auctionItemId, OnLoadAuctionMethod);
    }

    function OnLoadAuctionMethod(result)
    {

        var auctionImage = new Image();
        auctionImage.src = result.ImageSrc;
        $get('AuctionImage').src = auctionImage.src;
        $get('actualPrice').innerHTML = result.Cost;
        $get('bidMainHeaderText').innerHTML = result.Name;
        $get('actualDescription').innerHTML = result.Comments;
               
        moveItemIn(result.GalleryID);
    } 
