var Utf8 = {

    // public method for url encoding
    encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // public method for url decoding
    decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

var Univers = {
	imagePath: null,
	lineArea: null,
	diaporama: null,
	diaporamaTimeout:null,
	timeout: 1000,
	obj: null,
	opacity: 100,
	ProductsImages: null,
	showHideProducts: false,
	animEnded: false,
	
	init: function(cat_id, timeout) {
		this.lineArea = document.getElementById('line');
		if (timeout != null) this.timeout = timeout;
	},
	
	startAnim: function() {
		if (this.imagePath != null && this.imagePath != '') {
			document.getElementById('CategoryPhotos').style.display = 'none';
			document.getElementById('CategoryContent').style.display = 'none';
			
			var imgUnivers = document.createElement('img');
			imgUnivers.setAttribute('alt', '');
			imgUnivers.src = this.imagePath;
			imgUnivers.id = 'imgCategory';
			imgUnivers.className = 'imgCategoryIntro';
			imgUnivers.style.display = 'block';
			imgUnivers.style.alpha = 100;
			imgUnivers.style.filter = 'alpha(opacity:100)';
			
			this.lineArea.appendChild(imgUnivers);
			
			this.diaporamaTimeout = setTimeout("Univers.endAnim()", this.timeout);
		}
	},
	
	endAnim: function() {
		this.obj = 'imgCategory';
		this.opacity = 100; 
		
		this.diaporama = setInterval("Univers.changeOpacity()", 1);
		
		this.obj = null;
		this.animEnded = true;
	},
	
	displayText: function() {
		document.getElementById('imgCategory').style.display = 'none';
		document.getElementById('CategoryPhotos').style.display = 'block';
		document.getElementById('CategoryContent').style.display = 'block';
	},
	
	changeOpacity: function() {
		var opacity = this.opacity / 100;
		
		if (Browser.ie) document.getElementById('imgCategory').style.filter = 'alpha(opacity:'+this.opacity+')';
		else document.getElementById('imgCategory').style.opacity = opacity;
		
		this.opacity-=4;
		
		
		if (this.opacity < 0) {
			this.displayText();
			clearInterval(this.diaporama);
			this.diaporama = null;
		}
	},
	
	preloadProductsImages: function() {
		var tmpImages = new Array;
		for (i=0; i<Univers.ProductsImages.length; i++) {
			tmpImages[i] = new Image;
			tmpImages[i].src = Univers.ProductsImages[i];
		}
	},
	
	preloadCategoryImages: function() {
		var tmpImages = new Array;
		for (i=0; i<Univers.CategoryImages.length; i++) {
			tmpImages[i] = new Image;
			tmpImages[i].src = Univers.CategoryImages[i];
		}
	}
};

var Product = function() {
	this.init.apply(this, arguments);
}
Product.prototype = {
	objName: null,
	params: null,
	option_id: null,
	Attribs: null,
	productImage: null,
	productDescription: null,
	optionImage: null,
	loaded: false,
	
	init: function() {
		this.objName = arguments[0];
		this.params = (arguments[1] == null) ? {id:null, has_option:false, productType:null} : arguments[1];
		this.option_id = (arguments[2] == null) ? null : arguments[2];
		this.Attribs = new Array;
	},
	
	get: function() {
		var product_id = (arguments[0] == null) ? null : arguments[0];
		var option_id = (arguments[1] == null) ? null : arguments[1];
		this.highLight(product_id, option_id);
		
		if (Univers.showHideProducts) {
			if (this.loaded)
				this.display();
			else
				var prd = makeRequest(pagesURL+'utils/getProduit.cfm?ajax=true&produit_id='+this.params.id, this.objName+'.buildList');
			
			return false;
		}
		else return true;
	},
	
	highLight: function(product_id, option_id) {
		var containerId = (option_id == null) ? 'product_'+product_id : 'product_'+product_id+'_'+option_id;
		var root = document.getElementById('categoryProducts');
		var products = root.getElementsByTagName('p');
		
		for (i=0; i<products.length; i++) {
			if (products[i].className.toString().indexOf('highlighted') > -1) products[i].className = 'product';
		}
		
		if (document.getElementById(containerId)) {
			document.getElementById(containerId).className = 'product highlighted';
		}
	},
	
	buildList: function(httpRequest) {
		try {
			if (httpRequest.readyState == 4 && httpRequest.status == 200) {
				var xmldoc = httpRequest.responseXML;
				var RootNode = xmldoc.getElementsByTagName('product')[0];
				var imgRoot = RootNode.getElementsByTagName('image')[0];
				var attRoot = RootNode.getElementsByTagName('attribs')[0];
				
				this.productImage = imgRoot.getAttribute('big');
				this.productDescription = Utf8.decode(unescape(RootNode.getAttribute('description')));
				
				if (attRoot.getAttribute('nb') > 0 && this.option_id != null) {
					var attributes = attRoot.getElementsByTagName('attr');
					for (i=0; i<attributes.length; i++) {
						if (attributes[i].getAttribute('option_id') == this.option_id) {
							this.optionImage = attributes[i].getAttribute('img');
						}
					}
				}
				
				this.loaded = true;
				this.display();
			}
		}
		catch (e) { alert(e); }
	},
	
	display: function() {
		// If transition has not ended yet
		if (!Univers.animEnded) {
			clearTimeout(Univers.diaporamaTimeout);
			clearInterval(Univers.diaporama);
			Univers.animEnded = true;
			Univers.diaporama = null;
			var imgCategory = document.getElementById('imgCategory');
			if(imgCategory){
				if (Browser.ie) document.getElementById('imgCategory').style.filter = 'alpha(opacity:0)';
				else document.getElementById('imgCategory').style.opacity = 0;
				document.getElementById('imgCategory').style.display = 'none';
			}
		}
		
		document.getElementById('CategoryPhotos').style.display = 'none';
		document.getElementById('CategoryContent').style.display = 'none';
		
		var previousDiv = document.getElementById('line').getElementsByTagName('div');
		for (divInd=0; divInd<previousDiv.length; divInd++) {
			if (previousDiv[divInd].className == 'productContent') Page.destroy(previousDiv[divInd].id, 'line');
		}
		var cur_id = this.params.id;
		var img_src = (this.optionImage == null) ? this.productImage : this.optionImage;
		
		var contentHTML = '';
		contentHTML += '<div class="photos"><img src="'+img_src+'" alt=""/></div>';
		contentHTML += '<div class="description"><h1>'+this.params.productType+'</h1>';
		contentHTML += '<p>'+this.productDescription+'</p>';
		contentHTML += '</div>';
		
		var productContent = document.createElement('div');
		productContent.id = 'productContent_'+this.params.id;
		productContent.className = 'productContent';
		productContent.innerHTML = contentHTML;
		document.getElementById('line').appendChild(productContent);
		
		document.getElementById('productContent_'+this.params.id).style.display = 'none';
		Page.fadeIt('productContent_'+this.params.id);
		
		var closeLink = document.getElementById('closeProduct');
		closeLink.onclick = function() {
			document.getElementById('productContent_'+cur_id).style.display = 'none';
			document.getElementById('closeProduct').style.display = 'none';
			
			document.getElementById('CategoryPhotos').style.display = 'block';
			document.getElementById('CategoryContent').style.display = 'block';
		};
		closeLink.style.display = 'block';
	}
};
