var isie 	= (document.all) ? true : false;
var W3CDOM 	= (document.createElement && document.getElementsByTagName);

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	
	if ((version >= 5.5) && (document.body.filters)) 
	{
		for(var i=0; i<document.images.length; i++)
		{
			var img = document.images[i]
			var imgName = img.src.toUpperCase()
			
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			{
				img.style.display = 'block';
				
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<div " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></div>" 
				img.outerHTML = strNewHTML
				
				i = i-1
			}
		}
	}
	
	for (var i=0; i<document.images.length; i++)
	{
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		{
			img.style.display = 'block';
		}
	}
}

function initFileUploads()
{
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var image = document.createElement('img');
	image.src='/images/button_select.gif';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}

}

function setClassName (object, name)
{
	var names 	= object.className.split(" ");
	var classes = "";
	
	if (names.length > 0 && name == "")
	{
		classes = names[0];
	}
	
	if (name != "")
	{
		classes = names[0] + " " + names[0] + "_active";
	}
	
	object.className = classes;
}

function replaceField(field)
{ 
    var password 	= document.createElement("input"); 
    
    password.type 	= "password"; 
    password.name 	= field.name; 
    password.id		= field.name; 
    
    field.parentNode.replaceChild(password, field); 
}


function changeQuantity(objectinstanceid, count, udcsizeitemid, udccoloritemid)
{
    new Ajax.Request('/_ctrl/shop/cart/add/'+objectinstanceid+'/'+count+'/', {
            onSuccess: function(transport) {
                //document.getElementById('price_'+objectinstanceid).innerHTML = transport.responseText;
                document.location.href='/_ctrl/shop/cart/list';
            }
        });
    
}


Event.observe(window, 'load', function() 
{
    // when an element with the id of cart exists and a key has occured, the calculatenewPrice will be triggered.
    
    if($('cart') != null)
    {
    	Event.observe('cart', 'keyup', calculateNewPriceQuantity);
    }
    
}
);

/**
* @description: functie who's purpose is to perform an AJAX request to update the shoppingcart
*/
function calculateNewPriceQuantity(event)
{
    var input = event.element();
    
    var updateknop = $('updatecartbtn');
    
    updateknop.value    = 'Updaten';
    updateknop.disabled = true;
      
    var url = '/_ctrl/shop/cart/update/req/ajax';
   
    var AllOptions = 
    {
        method: 'get',
        parameters: { quantity: input.value, encodedid: input.id }, 
        onSuccess: function(oXHR, oJson)
        {
        	
            updateknop.value = "Update winkelwagen";
            updateknop.disabled = false;
            
            updateCart(oXHR, oJson, input);
        },
        onFailure: function(oXHR, oJson)
        {
            alert('mislukt om een ajax request uit te voeren.');
        }
    }   
    var myAjax = new Ajax.Request(url, AllOptions);
    
}

// when ajax request is succesfull, the retrieved data from the Json object needs to be placed inside the DOM structure
// this is where the updateCart function fits in.
function updateCart(oXHR, oJson, input)
{
    var cartobject = oXHR.responseText.evalJSON();
     
	var formatedid = input.id.split('_');
	var subtotalprice = $('subtot_'+formatedid[1]);
	var totalprice    = $('totalprice');
     	     
    subtotalprice.innerHTML    = '&euro; '+cartobject.subtotalprice;
    totalprice.innerHTML       = '&euro; '+cartobject.totalprice;
    input.value                = cartobject.quantity;  
     
     if(cartobject.errorkey != null)
     {
        alert(cartobject.errormessage);
     }
     
}


function banner (location, anchor, alt)
{
	this.location 	= location;
	this.anchor 	= anchor;
	this.alt		= alt;
}


window.onload = function ()
{
	correctPNG();
	
	if ($('flash'))
	{
		var header = new SWFObject("/flash/intro1.swf", "video", "720", "300", "8");
			header.addParam("quality", "high");
			header.addParam("wmode", "transparent");
			header.addParam("salign", "t");
			header.write("flash");
	}
	
	if ($('banner1'))
	{
//		var banner1 = new SWFObject("/flash/bruidvdmaand2.swf", "video", "235", "90", "8");
//			banner1.addParam("quality", "high");
//			banner1.addParam("wmode", "transparent");
//			banner1.addParam("salign", "t");
//			banner1.write("banner1");
	}
	
	if ($('banner3'))
	{
		var random = Math.floor(Math.random()*1);
		
		var banners = new Array(1)
			banners[0] = new banner('/flash/bruidegom.swf', '/collectie/bruidegom', 'Bruidsmode voor hem.');
			//banners[0] = new banner('/flash/bruidegom.swf', '/collectie/bruidegom', 'Bruidsmode voor hem.');
			//banners[1] = new banner('/flash/bruid.swf', '/collectie/bruid', 'Bruidsmode voor haar.');
		
		var bannerobject = banners[random];
			
		var banner3 = new SWFObject(bannerobject.location, "video", "235", "90", "8");
			banner3.addParam("quality", "high");
			banner3.addParam("wmode", "transparent");
			banner3.addParam("salign", "t");
			banner3.write("banner3");
		
		$('anchor2').href 	= bannerobject.anchor;
		
		$('image2').alt 	= bannerobject.alt;
		$('image2').title 	= bannerobject.alt;
	}
	
	if ($('banner_bruidvandemaand'))
	{
		var banner_bruidvandemaand = new SWFObject("/flash/banner_bruidvandemaand.swf", "video", "464", "210", "8");
			banner_bruidvandemaand.addParam("quality", "high");
			banner_bruidvandemaand.addParam("wmode", "transparent");
			banner_bruidvandemaand.addParam("salign", "t");
			banner_bruidvandemaand.write("banner_bruidvandemaand");
	}
	
	initFileUploads();
}



function formHandler ()
{
	
}

	formHandler.prototype.Send = function (formname, method, action)
	{
		document.forms[formname].method = method;
		document.forms[formname].action = action;
		document.forms[formname].submit();
	}
	
	var formHandler = new formHandler();
	
	

function showState(state, status)
{
	document.getElementById(state).style.display = (status) ? "block" : "none";
}