/**
 * @author FORMER03 | florian.schupp
 * copyright 2007 FORMER03 GmbH
 */


function switchImg(switcher, new_image) {

	var original = $('default_product_image');
	original.src = new_image;

	var switchers0 = document.getElementsByName('switcher');
	var switchers = $A(switchers0);

	switchers.each(function(li){
		li.className = '';
	});

	if(switcher.parentNode.className == 'active')
	{
		switcher.parentNode.className = '';
	}
	else
	{
		switcher.parentNode.className = 'active';
	}

}

function attributeChange(select) {
	setAttributePrice(select);
	setAttributeImage(select);

}

function setAttributePrice(select) {
	var totalprice = parseFloat(base_price);
	var pricechange = true;
	var dropdowns0 = document.getElementsByClassName('attrib_dropdown');
	var dropdowns = $A(dropdowns0);
	var add_price;

	dropdowns.each(function(select){
		add_price = select[select.selectedIndex].getAttribute('rel')

		if(parseFloat(add_price) > 0)
		{
		    totalprice = parseFloat(totalprice) + parseFloat(add_price);
			pricechange = true;
		}
	});

	if(pricechange)
	{
		$('pricetag').innerHTML = formatCurrency(totalprice);
	}
}

function setAttributeImage(select) {
	var dropdowns0 = document.getElementsByClassName('attrib_dropdown');
	var dropdowns = $A(dropdowns0);
	var attrib_image;
	var attrib_image_nr;

	dropdowns.each(function(select){
		attrib_image = select[select.selectedIndex].getAttribute('attrib_image');

		if(attrib_image)
		{
			attrib_image_nr = select[select.selectedIndex].getAttribute('attrib_image_nr');
			attrib_image = 'images/product_images/info_images/' + attrib_image;
			switcher = $('img_focus_'+attrib_image_nr);

			switchImg(switcher, attrib_image);
		}

	});


}



function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + ',' + cents+' '+currency);
}



PopUp = function(autoapply){
	this.types = [];
	this.defaults = {
		width:800,
		height:600,
		top:0,
		left:0,
		location:false,
		resizable:false,
		scrollbars:false,
		status:false,
		toolbar:false,
		menubar:false,
		center:true,
		title:"Dieser Link wird in einem neuen Fenster geöffnet"
	}
	this.addType({
		name:"standard",
		location:true,
		resizable:true,
		scrollbars:true,
		status:true,
		toolbar:true,
		menubar:true
	});
	if(autoapply) this.apply();
}
o = PopUp.prototype;
o.apply = function(){
	var links = document.getElementsByTagName("a");
	if(!links) return;
	for(var i=0;i<links.length;i++){
		var l = links[i];
		if(l.className.indexOf("popup") > -1){
			this.attachBehavior(l,this.getType(l));
		}
	}
}
o.addType = function(type){
	for(var prop in this.defaults){
		if(type[prop] == undefined) type[prop] = this.defaults[prop];
	}
	this.types[type.name] = type;
}
o.getType = function(l){
	for(var type in this.types){
		if(l.className.indexOf(type) > -1) return type;
	}
	return "standard";
}
o.attachBehavior = function(l,type){
	var t = this.types[type];
	l.title = t.title;
	l.popupProperties = {
		type: type,
		ref: this
	};
	l.onclick = function(){
		this.popupProperties.ref.open(this.href,this.popupProperties.type);
		return false;
	}
}
o.booleanToWord = function(bool){
	if(bool) return "yes";
	return "no";
}
o.getTopLeftCentered = function(typeObj){
	var t = typeObj;
	var r = {left:t.left, top:t.top};
	var sh = screen.availHeight-20;
	var sw = screen.availWidth-10;
	if(!sh || !sw) return r;
	r.left = (sw/2)-(t.width/2);
	r.top = (sh/2)-(t.height/2);
	return r;
}
o.getParamsOfType = function(typeObj){
	var t = typeObj;
	var c = this.booleanToWord;
	if(t.center){
		var tc = this.getTopLeftCentered(typeObj);
		t.left = tc.left;
		t.top = tc.top;
	}
	var p = "width="+t.width;
	p+=",height="+t.height;
	p+=",left="+t.left;
	p+=",top="+t.top;
	p+=",location="+c(t.location);
	p+=",resizable="+c(t.resizable);
	p+=",scrollbars="+c(t.scrollbars);
	p+=",status="+c(t.status);
	p+=",toolbar="+c(t.toolbar);
	p+=",menubar="+c(t.menubar);
	return p;
}
o.open = function(url,type){
	if(!type) type = "standard";
	var t = this.types[type];
	var p = this.getParamsOfType(t);
	var w = window.open(url,t.name,p);
	if(w) w.focus();
	return false;
}
