﻿// © Coan Marco 2010
var database = new Array();
// ajax - funzione di scansione directory con file php
this.scanProducts = function(directory) {

	var _obj = this;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		this.http_request = new XMLHttpRequest();
		if (this.http_request.overrideMimeType) {
			this.http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			this.http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	// LANCIA RICHIESTA...
	this.http_request.open('POST', "code/VM_scanProducts.php", true);
	http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	this.http_request.send(null);
	// ...GESTISCE RISPOSTA
	this.http_request.onreadystatechange = function() {
		if (this.readyState == 4) {
			if (this.status == 200) {	
				// scansione risultato php
				if(this.responseText==""){
					window.setTimeout(function(){_obj.scanProducts();},100);
					return;
				}
				var filesArray = this.responseText.split("\n");
				// prepara un array associativo in database
				for(var scan=0; scan<filesArray.length; scan++){
					// product_name;product_id;product_s_desc;product_thumb_image;category_name;category_id;product_price;tax_rate
					if(filesArray[scan]!=""){
						var dbRow = new Object();
						dbRow.product_name = filesArray[scan].split(";")[0];
						dbRow.product_id = filesArray[scan].split(";")[1];
						dbRow.product_s_desc = filesArray[scan].split(";")[2];
						dbRow.product_full_image = filesArray[scan].split(";")[3];
						dbRow.category_id = filesArray[scan].split(";")[5];
						database.push(dbRow);
					}
				}
				_obj.inizializeScroll();
			}
		}
	}
}

function inizializeScroll() {
	
	var rnd1 = new VM_scroll_rnd("VM_RND_1");
	var rnd2 = new VM_scroll_rnd("VM_RND_2");
	var rnd3 = new VM_scroll_rnd("VM_RND_3");
	
	var rnd4 = new VM_scroll_rnd("VM_RND_4");
	var rnd5 = new VM_scroll_rnd("VM_RND_5");
	var rnd6 = new VM_scroll_rnd("VM_RND_6");
	
	var rnd4 = new VM_scroll_rnd("VM_RND_7");
	var rnd5 = new VM_scroll_rnd("VM_RND_8");
	var rnd6 = new VM_scroll_rnd("VM_RND_9");
	
}


function VM_scroll_rnd(targetDiv){

	this.targetDiv = targetDiv;
	var _obj = this;
				
	this.changeImg = function(targetDiv) {
		this.targetDiv = targetDiv;
		var _obj = this;
		var template = "";
		
		var targetURL = "index.php?page=shop.product_details&product_id="+this.targetDatabase.product_id+"&category_id="+this.targetDatabase.category_id+"&option=com_virtuemart&Itemid=72";
		template += "<div style='background-color:#fff; height:240px; width:240px;'>";
		template += "<a style='text-decoration:none;' href='"+targetURL+"'>";
		template += "<img id='"+this.targetDiv+"_IMG' style='max-width:240px; text-align:center' src='components/com_virtuemart/shop_image/product/"+this.targetDatabase.product_full_image+"' onerror='this.src=\"components/com_virtuemart/themes/default/images/errorimage.jpg \";'/>";
		template += "<div id='browseMenu_"+this.targetDiv+"' style='border-top:4px solid #EC6C0B; position:absolute; left:0; bottom:-244px; width:100%; height:240px; background: URL(code/menuTransWhite.png); text-align:left ' >";
		template += "<div style='margin:10px'>";
		template += "<h2>"+this.targetDatabase.product_name+"</h2>";
		template += "<p style='color:#000; font-size:13px;'>"+this.targetDatabase.product_s_desc+"</p>";
		template += "</div>";
		template += "</div>";
		template += "</a>";
		template += "</div>";
		document.getElementById(this.targetDiv).innerHTML = template;
		document.getElementById(this.targetDiv+"_IMG").onload = function(){_obj.fadeIn(_obj.targetDiv);}
	}

	this.scrollEfx = function(targetDiv){
		this.targetDiv = targetDiv;
		var _obj = this;
		_obj.targetDatabase = database[0];
		database.push(database.shift());
		//cambia immagine
		this.changeImg(this.targetDiv);
		//this.fadeIn(this.targetDiv);
	}
	
	this.fadeOut = function(targetDiv){
		var _obj = this;
		this.targetDiv = targetDiv;

		if(this.fade==undefined){
			this.fade = 0.9;
		}
		document.getElementById(this.targetDiv).style.opacity = this.fade;
		document.getElementById(this.targetDiv).style.filter = "alpha(opacity="+parseInt(this.fade*100)+")";
		
		this.fade -= 0.1;
		if(this.fade >0){
			window.setTimeout(function(){_obj.fadeOut(_obj.targetDiv);},30);
		} else {
			document.getElementById(this.targetDiv).style.opacity = 0;
			document.getElementById(this.targetDiv).style.filter = "alpha(opacity=0)";
			this.fade = undefined;
			this.scrollEfx(this.targetDiv);
		}
	}
	
	this.fadeIn = function(targetDiv){
		
		var _obj = this;
		this.targetDiv = targetDiv;
		if(this.fade==undefined){
			this.fade = 0.1;
		}
		document.getElementById(this.targetDiv).style.opacity = this.fade;
		document.getElementById(this.targetDiv).style.filter = "alpha(opacity="+parseInt(this.fade*100)+")";
		
		this.fade += 0.1;
		if(this.fade <1){
			window.setTimeout(function(){_obj.fadeIn(_obj.targetDiv);},30);
		} else {
			document.getElementById(this.targetDiv).style.opacity = 1;
			document.getElementById(this.targetDiv).style.filter = "alpha(opacity=100)";
			this.fade = undefined;
			var trigger = parseInt(Math.floor(Math.random()*1000));
			window.setTimeout(function(){_obj.fadeOut(_obj.targetDiv);},15000+trigger);
			new menuUp(this.targetDiv,"browseMenu_"+this.targetDiv,120);
		}
	}
	

	
	// lancio principale
	this.fadeOut(this.targetDiv);

}


this.scanProducts();
