// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact

function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {var x = Math.round(num * Math.pow(10,dec));if (x >= 0) n1=n2='';var y = (''+Math.abs(x)).split('');var z = y.length - dec; if (z<0) z--; for(var i = z; i < 0; i++) y.unshift('0'); if (z<0) z = 1; y.splice(z, 0, pnt); if(y[0] == pnt) y.unshift('0'); while (z > 3) {z-=3; y.splice(z,0,thou);}var r = curr1+n1+y.join('')+n2+curr2;return r;}

(function(){
    EYE.main = function() {
    	if(EYE.defaultLanguage != 'cn'){
			Cufon.replace(
			    'a.mainItem span',
			    {
				fontStyle: "Myriad",
				hover: true
			    }
			);
			Cufon.replace(
			    '#header h1, #content h1',
			    {
				fontStyle: "MyriadLight"
			    }
			);
    	}
		$().piroBox({
			  my_speed: 300,
			  bg_alpha: 0.5,
			  slideShow : 'true',
			  slideSpeed : 3,
			  close_all : '.piro_close'
		});
    };
    EYE.register(EYE.main);
    /*
    EYE.backgrounds = function() {
	var current = 0,
	    timer,
	    els,
	    wait = 8000,
	    setTimer = function() {
		timer = setTimeout(goNext, wait);
	    },
	    goNext = function() {
		clearTimeout(timer);
		els.eq(current)
		    .stop()
		    .animate(
			{
			    opacity: 0
			}
			, 400
		    );
		current ++;
		if ( current >= els.size()) {
		    current = 0;
		}
		els.eq(current)
		    .stop()
		    .animate(
			{
			    opacity: 1
			}
			, 600
		    );
		setTimer();
	    }
	return {
	    init: function() {
		els = $('#headerBackground img');
		if (els.size() > 0) {
		    els.css('opacity', 0)
			.eq(0)
			    .css('opacity', 1);
		    setTimer();
		}
	    }
	};
    }();
    EYE.register(EYE.backgrounds.init);*/
    
    EYE.categories = function() {
    	var el;
    	var click = function(ev) {
    		var targetEl = $(ev.target);
    		var targetA = targetEl.closest('a.expandable');
    		if (targetA.size() == 1 && targetEl.is('img')) {
    			targetA.toggleClass('expanded').parent().find('>ul').toggleClass('active');
	    		return false;
    		}
    	};
    	return {
    		init: function() {
    			if ((el = document.getElementById('productsCategories'))) {
    				$(el).bind('click', click)
    					.find('li').filter(":has(>ul)")
    					.find('>a')
    						.addClass('expandable');
    				$(el).find('a.active')
    						.parents('li').find('>ul').addClass('active');
    			}
    		}
    	};
    }();
    EYE.register(EYE.categories.init);
    
    EYE.calculation = function() {
	var el, fields;
	var calculate = function() {
		var total = 0;
		fields.each(function(nr){
			if (this.id == 'total') {
				this.value = formatNumber(total,2,' ','.','€ ','','',' CR');
			} else {
				console.log(EYE.calculation.variables[nr+1], parseFloat(this.value, 10));
				total += EYE.calculation.variables[nr+1] * Math.round(parseFloat(this.value, 10)||0, 2);
			}
		});
	};
	return {
		init: function() {
			if ((el = document.getElementById('calculation'))) {
				fields = $(el).find('input').bind('change keyup', calculate);
				calculate();
			}
		}
	}    
    }();
    EYE.register(EYE.calculation.init);
})();
