/************************************************************************************************************
Copyright 2008 - Netalogue Technologies plc

Based on code from www.dhtmlgoodies.com
************************************************************************************************************/
var flyingSpeed = 25;
var flyTo_div = false;
var flyTo_x = false;
var flyTo_y = false;
var flyingDiv = false;
var currentProductDiv = false;
var slide_xFactor = false;
var slide_yFactor = false;
var diffX = false;
var diffY = false;
var currentXPos = false;
var currentYPos = false;
var accBasket;

var controlOffset = 'ctl00_ctl31_'
var MPSize = 'ctl00_ctl32_lblMPSize'

var ajax = new sack();

function getTop(obj) {
  var returnValue = obj.offsetTop;
  while((obj = obj.offsetParent) != null)if(obj.tagName!='HTML')returnValue += obj.offsetTop;
  return returnValue;
}

function getLeft(obj){
  var returnValue = obj.offsetLeft;
  while((obj = obj.offsetParent) != null)if(obj.tagName!='HTML') returnValue += obj.offsetLeft;
  return returnValue;
}

function addToBasket(productId,catRef,tbQuantity,photo,inBasket,pType,kw,sSKU,aux,minQty,tbcustomise)
{
    if (minQty == undefined || minQty == 0) minQty = 1;
    minimumQty = minQty;
    qty = (tbQuantity=='') ? minQty.toString() : document.getElementById(tbQuantity).value;
    
//    if (qty<minQty && (aux=='' || aux==undefined)) {
////        alert('The minimum order quantity for this product is ' + minQty);
////        return true;
//    }else if(qty<minQty && aux != ''){
//        qty = minQty;
//    }
    
    if (qty<=0) {
        alert("Quantity must be greater than 0");
        return true;
    }
    
    var customiseText = '';
    if(document.getElementById(tbcustomise) != null){
        customiseText = document.getElementById(tbcustomise).value;
        //customiseText must not be blank if tbCustomise is not null
        if (customiseText==''){
            alert('Please specify a CUSTOMISED TEXT');
            return true;
        }else{
            customiseText = customiseText.replace("'", "''");
            customiseText = customiseText.replace('"','').replace('"','').replace('"','').replace('"','').replace('"','').replace('"','').replace('"','');
        }
    }

    flyTo_div = $get('flyTo');	
	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}	
	
	flyTo_x = getLeft(flyTo_div);
	flyTo_y = getTop(flyTo_div);	
	currentXPos = getLeft(document.getElementById(productId.replace('flyX','base')));
	currentYPos = getTop(document.getElementById(productId.replace('flyX','base')));
	currentProductDiv = document.getElementById(productId);	
	diffX = flyTo_x - currentXPos;
	diffY = flyTo_y - currentYPos;
	
	var ContentCopy = currentProductDiv.cloneNode(true);
	ContentCopy.id='';
	ContentCopy.style.display='';
	
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(ContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	 
	flyToBasket(productId,catRef,tbQuantity,photo,inBasket,pType,kw,sSKU,aux,customiseText);	
	
	return false;
}


function flyToBasket(productId,catRef,tbQuantity,photo,inBasket,pType,kw,sSKU,aux,customiseText)
{
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;
	var moveY = (diffY / maxDiff) * flyingSpeed;	
	
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	
	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';		
	
	if(moveX>0 && currentXPos > flyTo_x) flyingDiv.style.display='none';
	if(moveX<0 && currentXPos < flyTo_x) flyingDiv.style.display='none';		
	if(flyingDiv.style.display=='block') setTimeout('flyToBasket("' + productId + '","' + catRef + '","' + tbQuantity + '","' + photo + '","' + inBasket + '","' + pType + '","' + kw + '","' + sSKU + '","' + aux + '","' + customiseText + '")',10); else ajaxAddProduct(productId,catRef,tbQuantity,photo,inBasket,pType,kw,sSKU,aux,customiseText);	
}

/////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

var basketMinHeight = 30;
var yOffset = basketMinHeight;
var basketMaxHeight = 450;
var keepOpen = false;
var dropSpeed = 15;
var mouseOverKeepOpen = false;

function dropTheBasket(direction, andUp)
{
    if (timeoutID == 0){
        flyTo_div = $get('flyTo');
        mpBasket=($get(MPSize)==null)?true:$get(MPSize).innerHTML=='';
        if(flyTo_div)if($get("ctl00_ctl30_lblBasket").innerHTML=='0 items'&&mpBasket) return false;
        if (keepOpen == false && mouseOverKeepOpen == false) 
            yOffset += (direction * 10)	
        else 
            yOffset = basketMaxHeight
        
        if (yOffset < basketMinHeight) yOffset=basketMinHeight;
        document.getElementById("downBasket").style.height = yOffset + 'px';	
        
	    if (direction > 0) {
	        if (yOffset < basketMaxHeight) 
	            setTimeout('dropTheBasket(1,' + andUp + ')',dropSpeed); 	        
            else if (andUp==true) setTimeout('dropTheBasket(-1)',1500);
        } else {
            if (keepOpen == false && mouseOverKeepOpen == false)
	            if (yOffset > basketMinHeight) 
	                setTimeout('dropTheBasket(-1)',dropSpeed);
        } 
    }
}

function dropTheBasketMixedPack(direction, andUp)
{
    flyTo_div = $get('flyTo');
    mpBasket=($get(MPSize)==null)?true:$get(MPSize).innerHTML=='';
    if(flyTo_div)if($get("ctl00_ctl30_lblBasket").innerHTML=='0 items'&&mpBasket) return false;
    if (keepOpen == false && mouseOverKeepOpen == false) 
        yOffset += (direction * 10)	
    else 
        yOffset = basketMaxHeight
    
    if (yOffset < basketMinHeight) yOffset=basketMinHeight;
    document.getElementById("downBasket").style.height = yOffset + 'px';	
    
    if (direction > 0) {
        if (yOffset < basketMaxHeight) 
            setTimeout('dropTheBasketMixedPack(1,' + andUp + ')',dropSpeed); 	        
        else if (andUp==true) setTimeout('dropTheBasketMixedPack(-1)',1500);
    } else {
        if (keepOpen == false && mouseOverKeepOpen == false)
            if (yOffset > basketMinHeight) 
                setTimeout('dropTheBasketMixedPack(-1)',dropSpeed);
    } 
}

function dropBasket() {    
    if (document.getElementById("downBasket").style.height == '' || parseInt(document.getElementById("downBasket").style.height) == 0) {
        yOffset = basketMinHeight;
        dropTheBasket(1, false);
    }
}

function hideBasket() {
    yOffset = basketMaxHeight;
    dropTheBasket(-1,false);
}

function ClosePanes(obj) {
    //if($find(obj+'_AccordionExtender'))$find(obj+'_AccordionExtender').set_SelectedIndex(-1)
}
    
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////////////////////////////////////////////////////

function ajaxAddProduct(productId,catRef,tbQuantity,photo,inBasket,pType,kw,sSKU,aux,customiseText)
{
    qty = (tbQuantity=='') ? minimumQty : document.getElementById(tbQuantity).value;
    
    ajax = new sack();
	ajax.requestFile = 'addToBasket.aspx';
	ajax.setVar('CatRef',catRef);
	ajax.setVar('Quantity',qty);
	ajax.setVar('ProductID',productId);
	ajax.setVar('Photo',photo);
	ajax.setVar('InBasket',inBasket);
	ajax.setVar('pType',pType);
	ajax.setVar('KW',kw);
	ajax.setVar('SSKU',sSKU);
	ajax.setVar('Aux',aux);
	ajax.setVar('customiseText',customiseText);
	ajax.onCompletion = whenCompleted;	
	ajax.runAJAX();
}

function ajaxRemoveProduct(catRef,pType)
{
    keepOpen = false;

	ajax = new sack();
	ajax.requestFile = 'addToBasket.aspx';
	ajax.setVar('CatRef',catRef);
	ajax.setVar('Quantity',-1);
	ajax.setVar('ProductID','');
	ajax.setVar('Photo','');
	ajax.setVar('InBasket','');
	ajax.setVar('pType',pType);
	ajax.setVar('KW','');
	ajax.onCompletion = whenCompleted;	
	ajax.runAJAX();
}

var timeoutID = 0;

function whenCompleted(){
    if(!ajax.response)return;
    
    var response = ajax.response.split('~');
    var pType = response[0];
    var productID = response[1];
    var quantity = response[2];
    var itemCount = response[3];
    var catRef = response[4];
    var photo = response[5];
    var inBasket = response[6];
    var errorMessage = response[8];
    
    if (pType!='p'){
        errorMessage = '';
    }
    
    if (errorMessage != ''){
        alert(errorMessage);
    }
    
    if (errorMessage==''){
        keepOpen = false;
        
        if (pType=='p') {
            var basketTotal = response[7];

            $get(controlOffset + "imgBasketPhoto").src = photo;
            $get(controlOffset + "lblBasketCatRef").innerHTML = catRef;
            $get(controlOffset + "lblBasketQuantity").innerHTML = qty;
            if(inBasket!='') $get(inBasket).innerHTML = '<a href="viewitems.aspx" class="inBasket">' + quantity + ' in basket</a>'
            $get("ctl00_lblBasketFooter").innerHTML = $get("ctl00_ctl30_lblBasket").innerHTML = itemCount=='1' ? '1 item' : itemCount + ' items'
            $get("ctl00_lblSubtotal").innerHTML = basketTotal   
            
            if(productID=='ZZZ') {       
                //$get('ctl00_accMixed').style.display='none';
                $get(MPSize).innerHTML='0/5';
                $get("mixedPackHTML").innerHTML = '<table width=186><tr><td align=center>Mixed Pack Empty<br /><br />To Start creating your Mixed Pack click on Build a Mixed Pack</td></tr></table>';
                $get('ctl00_ctl33_mixedPackAdd').style.display='none';            
                
                timeoutID = setTimeout("dropTheBasketMixedPack(1,true);timeoutID=0;", 2000);
                
                        
            }else {
        	    dropTheBasket(1,true);       
	        }
    	      
        } else {
            var isFull = response[7];
            var basketHTML = response[8];

            packSize = pType.split('|')[2];
                    
            if(packSize.indexOf(",")>-1){
                for(s=0;s<packSize.split(",").length;s++) {
                    if(parseInt(packSize.split(",")[s]) >= itemCount) {
                        packSize = packSize.split(",")[s]
                        break;
                    }
                }
            }  

            $get('ctl00_accMixed').style.display='';
            $get(MPSize).innerHTML=itemCount+'/'+packSize;
            $get("mixedPackHTML").innerHTML = basketHTML;
            $get('ctl00_ctl33_mixedPackAdd').style.display='none';            

            switch (isFull) {
                case 'Full' : 
                    $get('ctl00_ctl33_mixedPackAdd').style.display='';
                    ClosePanes('ctl00_accBasket');
                    $find('ctl00_accMixed_AccordionExtender').set_SelectedIndex(0);
                    keepOpen = true;
	                dropTheBasket(1,true);
                    break;
                    
                case 'FullAlready' :
                    $get('ctl00_ctl33_mixedPackAdd').style.display='';
                    keepOpen = true;
                    showMessage('Mixed Pack Full','Your mixed pack is already full. Please remove an item or add the mixed pack to your basket','dropTheBasket(1,true);');
                    break;    
                    
                case 'FullAlreadyStamper' :
                    $get('ctl00_ctl33_mixedPackAdd').style.display='';
                    keepOpen = true;
                    showMessage('Stamp Stack Full','You now have 5 stamps in your stack and can add it to your basket. Click OK to close this message and then click on the <b>Add To Basket</b> button.','dropTheBasket(1,true);');
                    break;                   
                    
                case 'Partial' :
                    $get('ctl00_ctl33_mixedPackAdd').style.display='';
                    keepOpen = true;
                    showMessage('Stamp Stack Options','You now have 3 stamps in your stack and can add it to your basket. Click OK to close this message and then click on the <b>Add To Basket</b> button.<br/><br/>Fancy a 5 Stack instead?<br/><br/>Click OK to close this message and then add the other 2 stamps of your choice to the stack.','dropTheBasket(1,true);');
                    break;                   

                case 'Empty' :
                    //$get('ctl00_accMixed').style.display='none';
                    $get(MPSize).innerHTML='0/5';
                    $get("mixedPackHTML").innerHTML = '<table width=186><tr><td align=center>Mixed Pack Empty<br /><br />To Start creating your Mixed Pack click on Build a Mixed Pack</td></tr></table>';
                    break;
                    
                default :
                    ClosePanes('ctl00_accBasket');
                    $find('ctl00_accMixed_AccordionExtender').set_SelectedIndex(0);
	                dropTheBasket(1,true);
            }
        }
    }
    
    
}
