﻿
window.addEvent('domready', function() {
    writeCartSummary();
    currentNavHighlight();     
})

function currentNavHighlight()
{
	var myArray = new Array();
	myArray[0] = /american-crew/;
	myArray[1] = /baxter-of-california/;
	myArray[2] = /dermalogica-shaving/;
	myArray[3] = /he-shi/;
	myArray[4] = /headblade/;
	myArray[5] = /men-u/;
	myArray[6] = /proraso/;
	myArray[7] = /redken-for-men/;
	myArray[8] = /skin-doctors/;
	myArray[9] = /spray-sun/;
	myArray[10] = /st-james-of-london/;
	myArray[11] = /tend-skin/;
	myArray[12] = /zirh/;

	var currentPage = location.href;
	
	var rangeCounter = 0;
	var rangeCounter = 0;
	while(rangeCounter < myArray.length)
	{
		if(currentPage.search(myArray[rangeCounter]) != -1)
		{
			var listItems = $('sideNav').getChildren('li');
			for(i = 0; i < listItems.length; i++)
			{
				if(i == rangeCounter)
				{
					listItems[i].getFirst('a').addClass('active');
				}
				else
				{
					listItems[i].getFirst('a').removeClass('active');
				}
			}
			rangeCounter = myArray.length;
		}
		else
		{
			rangeCounter++;	
		}
	}
}

function writeCartSummary()
{
    if (Cookie.read("shoppingCart"))
    {
        $('itemCount').set('text', '');
        $('totalPrice').set('text', '');
        
        var totalPrice = 0.00;
        var itemCount = 0;

        var shoppingCart = Cookie.read("shoppingCart");

        var itemArray = shoppingCart.split('&');

        for (var i = 0; i < itemArray.length ; i++)
        {
            var eachItem = new Array();
            eachItem = itemArray[i].split(':~:');

            var price = eachItem[2];
            var numItems = eachItem[3];
            itemCount = (itemCount + parseInt(numItems));
            totalPrice = (totalPrice + ((+price) * numItems));            
        }
        $('itemCount').set('text', 'items: ' + itemCount);
        $('totalPrice').set('text', '£' + totalPrice.toFixed(2));
        
        if($('shoppingCart'))
        {
            if($('checkoutForm'))
            {
                $('itemsTable').set("html", "<table><tr><th class='alignLeft'>product</th><th>price</th><th>quantity</th><th>line price</th></tr>"+getItemRows(itemArray)+"</table>","<p id='subTotal'>sub-total: £"  +totalPrice.toFixed(2) + "</p>");
                var listItemArray = $('cartTotal').getChildren('li');
                listItemArray[0].set('text','Items £' +totalPrice.toFixed(2));
                countryChange();
            }
            else
            {
                $('itemsTable').set("html", "<table><tr><th class='alignLeft'>product</th><th>price</th><th>quantity</th><th>line price</th></tr>" + getItemRows(itemArray) + "</table>", "<p>sub-total: £" + totalPrice.toFixed(2) + "</p>" + "<p><a href='http://www.peterolivers.com/checkout.aspx' title='checkout'>checkout</a></p>");
               // $('itemsTable').set("html", "<table><tr><th class='alignLeft'>product</th><th>price</th><th>quantity</th><th>line price</th></tr>" + getItemRows(itemArray) + "</table>", "<p>sub-total: £" + totalPrice.toFixed(2) + "</p>" + "<p><a href='https://www.peterolivers.com/checkout.aspx' title='checkout'>checkout</a></p>");
            }
        }
    }
    else
    {
        if($('shoppingCart'))
        {
            var anchorArray = $('shoppingCart').getElements('a');
            for (var i = 0; i < anchorArray.length ; i++)
            {
                anchorArray[i].toggleClass('inactiveCart');
            }           
        }
    }
}

function getItemRows(itemArray)
{
    var itemRows = "";
    for (var i = 0; i < itemArray.length ; i++)
    {
        var eachItem = new Array();
        eachItem = itemArray[i].split(':~:');

        var itemDescription = eachItem[1];
        var price = eachItem[2]  * 1;
        var numItems = eachItem[3];
        var itemLink = eachItem[4];
        var linePrice = ((+numItems) * (+price))
        itemRows += "<tr><td class='cartDescription'><a href='"+itemLink+"' title='"+itemDescription+"'>"+itemDescription+"</a></td><td>&pound;"+ price.toFixed(2) +"</td><td><input type='text' value='"+numItems+"' /></td><td>&pound;"+ linePrice.toFixed(2)+"</td><td><a href='#' onclick='removeItem("+i+"); return false' title=''>remove</a></td></tr>";
    }
    return itemRows;
}

function updateCart()
{
    if (Cookie.read("shoppingCart"))
    {
        var oldShoppingCart = Cookie.read("shoppingCart");
        var itemArray = oldShoppingCart.split('&');
        var itemAmounts = $('itemsTable').getElements('input');

        var newShoppingCart = "";

        for (var i = 0; i < itemArray.length ; i++)
        {
            var eachItem = new Array();
            eachItem = itemArray[i].split(':~:');
            eachItem[3] = itemAmounts[i].get('value');
            
            if(i == 0)
            {
                newShoppingCart =  eachItem[0]+ ":~:" + eachItem[1]+ ":~:" + eachItem[2]+ ":~:" + eachItem[3]+ ":~:" + eachItem[4];
            }
            else
            {
                newShoppingCart += "&" + eachItem[0]+ ":~:" + eachItem[1]+ ":~:" + eachItem[2]+ ":~:" + eachItem[3]+ ":~:" + eachItem[4];
            }
        }
        Cookie.write("shoppingCart",newShoppingCart,{domain: "peterolivers.com",path: "/"});
        writeCartSummary();
    }
}

function removeItem(itemIndex)
{
    var oldShoppingCart = Cookie.read("shoppingCart");
    var newShoppingCart = "";
    var itemArray = oldShoppingCart.split('&');
    if(itemArray.length == 1)
    {
        emptyCart()
    }
    else
    {
        var firstItem = true;    
        for (var i = 0; i < itemArray.length ; i++)
        {
            if(i != itemIndex)
            {
                if(firstItem)
                {
                    newShoppingCart = itemArray[i];
                    firstItem = false;
                }
                else
                {
                    newShoppingCart += "&" + itemArray[i];
                }
            }
        }
        Cookie.write("shoppingCart",newShoppingCart,{domain: "peterolivers.com",path: "/"});
        writeCartSummary();
    }       
}


function addItem(itemID, price, thisObject)
{
	if(itemID > 0)
	{
    var numItems = $(thisObject).getParent('div').getElement('input').get('value');
    var itemLink;
    var itemDescription;

    if(numItems < 1)
    {
        numItems = 1;
    }
        
    if($(thisObject).getParent('div').getProperty('id') == "productLongDetails")
    {
        itemLink = document.URL.toLocaleString();
        itemDescription = $(thisObject).getParent('div').getElement('h2').get('text');
    }
    else
    { 
        itemLink = $(thisObject).getParent('div').getElement('a').get('href');
        itemDescription = $(thisObject).getParent('div').getElement('a').get('text');
    }
    
    if (Cookie.read("shoppingCart"))
    {
        var shoppingCart = Cookie.read("shoppingCart");     
    
        //break existing cookie up into individual items in array
        var itemArray = shoppingCart.split('&');

        //initialise new cookie
        var newShoppingCart = "";
        
        var existingItem = -1;
        for (var i = 0; i < itemArray.length ; i++)
        {
            //break up item into separate parts
            var eachItem = new Array();
            eachItem = itemArray[i].split(':~:');
            
            //check to see if this item is already in the shopping basket
            if(eachItem[0] == itemID)
            {
                existingItem = i;
            }
        }
        
        if(existingItem >= 0)
        {
            //break up item into separate parts
            var eachItem = new Array();
            eachItem = itemArray[existingItem].split(':~:');
            
            itemArray[existingItem] = itemID + ":~:" + itemDescription + ":~:" + price + ":~:" + (parseInt(numItems) + parseInt(eachItem[3])) + ":~:" + itemLink;
            for (var i = 0; i < itemArray.length ; i++)
            {
                if(i==0)
                {
                    newShoppingCart = itemArray[i];
                }
                else
                {
                    newShoppingCart += "&" + itemArray[i];
                }
            }
            Cookie.write("shoppingCart",newShoppingCart,{domain: "peterolivers.com",path: "/"});       
        }
        else
        {
            shoppingCart += "&" + itemID + ":~:" + itemDescription + ":~:" + price + ":~:" + numItems + ":~:" + itemLink;
            Cookie.write("shoppingCart",shoppingCart,{domain: "peterolivers.com",path: "/"});       
        }
        writeCartSummary();
    }
    else
    {
        Cookie.write("shoppingCart",itemID + ":~:" + itemDescription + ":~:" + price + ":~:" + numItems+ ":~:" + itemLink,{domain: "peterolivers.com",path: "/"});       
        writeCartSummary();
    }
	$(thisObject).addClass('addedProduct');
    (function(){ $(thisObject).removeClass('addedProduct'); }).delay(3000);
  }
  else
  {
		alert("Discontinued products cannot be added to your shopping cart");
  }
}

function emptyCart()
{
    if (Cookie.read("shoppingCart"))
    {
        Cookie.dispose("shoppingCart",{domain: "peterolivers.com",path: "/"});
        $('itemCount').set('text', 'items: 0');
        $('totalPrice').set('text', '£0.00');
        if($('shoppingCart'))
        {
            var anchorArray = $('shoppingCart').getElements('a');
            for (var i = 0; i < anchorArray.length ; i++)
            {
                anchorArray[i].toggleClass('inactiveCart');
            }
            
            $('itemsTable').set('html', '<p>Your Cart is empty</p>'); 
            if($('checkoutForm'))
            {
                var listItemArray = $('cartTotal').getChildren('li');
                listItemArray[0].set('text','Items £0.00');
                listItemArray[1].set('text','Postage & Packaging £0.00');
                listItemArray[2].set('text','Order Total £0.00');
                countryChange();
            }
        }
    } 
    var mySlide = new Fx.Slide('toggled');
    mySlide.slideOut();
}

sfHover = function() {
		var sfEls = document.getElementById("topNav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
if (window.attachEvent) window.attachEvent("onload", sfHover);