	    /********************************************************************************/
	    /*										    																											*/
	    /*				VETRINA IMMOBILI IMMOBILIARE.IT ver 3.0		    												*/	
	    /*					@uthor:Mario Esposito			    																			*/	
	    /*					  date: 2011-03-26			    																				*/						
	    /*										    																											*/
	    /********************************************************************************/
	    
	    


MEVetrina = function(vetrina_cont){
	
	
	if (typeof vetrina_cont == "undefined"){
		throw("You must specify a container 'id' for MEVetrina");
		return null;
	}
	this.vetrina_container = vetrina_cont;
	
	this.MUL = 1; //this var increases the ajax call intervals 
	this.IMAGES_NUM = 1 ; //the number of showed images
	this.PRECAR_DX = 2 ; // the number of precharged images in the hidden right-box - minimum 2 (cause problem with explorer)
	this.PRECAR_SX = 2; // the number of precharged images in the hidden left-box - minimum 2 (cause problem with explorer)
	this.DEFAULT_DIR='left'; // The default autoplay this.direction - values 'left' or 'right'
	this.IMGBOX_WIDTH = 120; //Image width, write only the number not include px (i.e. if 132px --> IMG_WIDTH=132)
	this.IMGBOX_HEIGHT = 35; //Image Height, write only the number not include px (i.e. if 132px --> IMG_HEIGHT=132)
	//this.VETRINA_HEIGHT = 75; //Vetrina height, write only the number not include px (i.e. if 132px --> this.VETRINA_HEIGHT=65)
	this.IMG_MARGIN = 12 ; //the margin between images
	this.TRANS_TIME=2; //the transition time bettwen shifts, in seconds
	this.STOP_TIME=1; //the stop time between two consecutive transitions, in seconds
	this.REQUEST_INTERVAL = 15; //tha intervall beetween ajax images request, in seconds
	this.FIRST_MOVE_DELAY = 0; //seconds to wait before the vetrina become visible
	this.NAME_VETRINA='images'; //the name of this window
	this.INC_VAL = 20; //the this.shift increment value top speed transitions
	this.LEFT_BUTTON="/vetrina/img/left.png"; //left button image
	this.RIGHT_BUTTON="/vetrina/img/right.png"; //right button image
	this.LEFT_BUTTON_OVER="/vetrina/img/left_over.png"; //left mouse over button image
	this.RIGHT_BUTTON_OVER="/vetrina/img/right_over.png"; //right mouse over button image
	this.BTN_HEIGHT = "23";
	this.BTN_WIDTH = "23";
	
	return this;
}

//push images into the this.aImages array
MEVetrina.prototype.pushImages = function(response){
	
	var xml_doc = response;
	var items_num = xml_doc.getElementsByTagName('item').length;
	/*var urls = xml_doc.getElementsByTagName('url');
	var titles=xml_doc.getElementsByTagName('title');
	var descs=xml_doc.getElementsByTagName('desc');
	var lnks=xml_doc.getElementsByTagName('link');
	*/
	var items = xml_doc.getElementsByTagName('item');
	
	for (i=0;i<items_num;i++){
		
		/*var url=urls[i].childNodes[0].nodeValue ? urls[i].childNodes[0].nodeValue : "";
		var title=titles[i].childNodes[0].nodeValue ? titles[i].childNodes[0].nodeValue : "";
		var desc= descs[i] && descs[i].childNodes[0].nodeValue ? descs[i].childNodes[0].nodeValue : "";
		var lnk=lnks[i].childNodes[0].nodeValue ? lnks[i].childNodes[0].nodeValue : "";
		var target = "";//lnks[i].attributes.getNamedItem("target").value ? lnks[i].attributes.getNamedItem("target").value : "";
		this.aImages[this.img_num+i]=new Array(url,title,lnk,target,desc);
		*/
		var item=items[i].childNodes[0].nodeValue ? items[i].childNodes[0].nodeValue : "";
		this.aImages[this.img_num+i]=item;
		
	}

	var old_number = this.img_num;
	this.img_num = this.aImages.length;
	
	if (this.started==false){
		if (this.img_num>0){
			this.started=true;
			this.showImages();
			
			if (this.autostart && this.img_num >= (this.IMAGES_NUM+this.PRECAR_DX+this.PRECAR_SX))
				this.movement = setTimeout(function(){ this.move(this.direction)}.bind(this),this.first_charge_delay);
		}
	}else{
		if (old_number < (this.IMAGES_NUM+this.PRECAR_DX+this.PRECAR_SX) && this.img_num >= (this.IMAGES_NUM+this.PRECAR_DX+this.PRECAR_SX)){
			this.showImages();
			this.move(this.DEFAULT_DIR);
		}	
	}
	this.MUL++;
	
	if (this.request_interval > 0)
		setTimeout(function(){this.getImages();}.bind(this),this.MUL*this.request_interval);
}

 //get the Image list from server in Xml Format

 MEVetrina.prototype.getImages = function(){
		var url = this.URL;
		
		if (this.URL.search(/\?/) >= 0)
			url+="&";
		else
			url+="?";
		url += "start="+this.img_num;
		var response;
		var name_vetrina = this.NAME_VETRINA
		var ajObj = getMEAjaxObj();
		
		ajObj.Request('GET',url,function(transport){this.pushImages(transport.responseXML);}.bind(this));
	}
		
 //regulate the transition effect
 MEVetrina.prototype.move = function(side){
    	_doLog("move");
    if ((this.shift==0))
    this.img_num = this.aImages.length;
    if (this.shift==0){
	this.changing_dir=false;
    }
    if ((this.shift==0)&&(side=='none')){
	
	if (typeof this.movement != "undefined")
		clearTimeout(this.movement);
	this.movement =setTimeout(function(){this.move(this.direction)}.bind(this),0);
    }else
	if(this.shift<this.steps_shifts - this.IMG_MARGIN){
	   var inc=0;
	   if (side=='right'){
	   	   inc=+this.INC_VAL;
          }else
	    if(side=='left'){
		inc=-this.INC_VAL;
	    }else{
		    if (this.direction=='right'){
		        inc=+this.INC_VAL;
		    }else if(this.direction=='left'){
			    inc=-this.INC_VAL;
			}
	    }
    	   var left = document.getElementById(this.NAME_VETRINA).style.left;
	    var pos = left.search('px');
	    var left = left.substr(0,pos);
	    pos=parseInt(left);
	    pos = pos+inc;
	    this.shift+=this.INC_VAL;
	    document.getElementById(this.NAME_VETRINA).style.left= pos+"px";
	   rit=(this.delay)/this.needed_shifts;
	   
	   window.mv = this.move.bind(this);
	   if (typeof this.movement != "undefined")
		clearTimeout(this.movement);
	   this.movement = setTimeout(function(){this.move(side)}.bind(this),rit);
	}else{
    
	    if (side=='right'){
	        this.first_index=this.first_index-this.step;
	    }else if (side=='left'){
	        this.first_index=this.first_index+this.step;
	    }
		
	   this.showImages();
	   this.shift=0;
	   rit=(this.delay)/this.needed_shifts;
	   window.mv = this.move.bind(this);
	   if (this.img_num>this.IMAGES_NUM) {
		if (typeof this.movement != "undefined")
			clearTimeout(this.movement);
		if (this.autostart)
			this.movement =setTimeout(function(){this.move(this.direction)}.bind(this),this.stop_time);
	   }
	}
}

 //restart the transition after a stop due to mouseover
 MEVetrina.prototype.restart = function(){
	
	if (typeof this.movement != "undefined")
		clearTimeout(this.movement);
if (this.img_num >= (this.IMAGES_NUM+this.PRECAR_DX)){
 this.stopped_flag=true;
 window.mv = this.move.bind(this);
 if (typeof this.movement != "undefined")
		clearTimeout(this.movement);
 if (this.changing_dir==true)
 
  this.movement = setTimeout(function(){this.move('none')}.bind(this),500);
 else
   this.movement = setTimeout(function(){this.move(this.direction)}.bind(this),500);
    }
 }
 
 MEVetrina.prototype.slide = function(side){
 clearTimeout(this.movement);
 
 if (this.direction!=side){
 this.changing_dir=true;
 this.direction=side;
 if (this.shift!=0) {
 this.shift=this.needed_shifts-this.shift;
 side='none'
 }

 }
 window.mv = this.move.bind(this);
 if (typeof this.movement != "undefined")
		clearTimeout(this.movement);
 this.movement = setTimeout(function(){this.move(side)}.bind(this),0)

 }
 MEVetrina.prototype.stopVetrina = function(){
 clearTimeout(this.movement);
 }


 //show the current images in the window
 MEVetrina.prototype.showImages = function(){

 if (this.img_num>=0){
		
		if (this.first_index<0)
				this.first_index=this.img_num+this.first_index;
		else
				this.first_index=this.first_index%this.img_num;
				
		for (i=0; i<this.IMAGES_NUM+this.PRECAR_DX+this.PRECAR_SX;i++) {
				if (i<this.img_num)
					this.showed_images[i]="<div class='"+this.NAME_VETRINA+"_item_cont'>"+this.aImages[((this.first_index+i)%this.img_num)]+"</div>";
		}
		html="";
				for (i=0; i<this.IMAGES_NUM+this.PRECAR_DX+this.PRECAR_SX; i++)
						if (i<this.img_num)html += this.showed_images[i];
				if (i>=this.img_num)
						document.getElementById(this.NAME_VETRINA).style.left='-'+(this.needed_shifts*this.PRECAR_SX)+'px';
				else
						document.getElementById(this.NAME_VETRINA).style.left='0';
		
				if (this.img_num>this.IMAGES_NUM) {
						_left = -1*this.needed_shifts*this.PRECAR_SX-this.IMG_MARGIN/4;
						document.getElementById(this.NAME_VETRINA+"_prev_butt").style.visibility='visible';
						document.getElementById(this.NAME_VETRINA+"_next_butt").style.visibility='visible';
						
				}
				else {
						_left=-this.IMG_MARGIN/this.IMAGES_NUM;
						document.getElementById(this.NAME_VETRINA+"_prev_butt").style.visibility='hidden';
						document.getElementById(this.NAME_VETRINA+"_next_butt").style.visibility='hidden';
				}
		_html="<div id='"+this.NAME_VETRINA+"' style='left:"+_left+"px;'>"+html+"</div><\/div>";
		document.getElementById('images_cont_'+this.NAME_VETRINA).innerHTML = _html;
		if (this.autostart){
			document.getElementById(this.NAME_VETRINA).onmouseover = this.stopVetrina.bind(this);
			document.getElementById(this.NAME_VETRINA).onmouseout = this.restart.bind(this);
		}
		
		if (this.img_num>=this.IMAGES_NUM){
				document.getElementById('MEVetrina_'+this.NAME_VETRINA).style.visibility='visible';
                this.exist_vetrina = true;
				if (typeof this.onloadEvent!="undefined"){
					this.onloadEvent();
				}
		}else
            this.exist_vetrina = false;
 }
	
 }


 //make a right-this.shift

 MEVetrina.prototype.nextImages = function(){

 this.slide('left');

 }

 //make a left this.shift
 MEVetrina.prototype.prevImages = function(){

 this.slide('right');

 }

 // Create vetrina HTML div's in the document
 MEVetrina.prototype.writeHTML = function(){
	
    if (this.img_num>this.IMAGES_NUM) _left = -1*this.needed_shifts*this.PRECAR_SX-this.BTN_WIDTH/2 ;
     else _left=-this.IMG_MARGIN/this.IMAGES_NUM;
	var _html = ("<div class='MEVetrina' id='MEVetrina_"+this.NAME_VETRINA+"' style='visibility:hidden;'>\
					<div class='MEVetrina_prev_butt' id='"+this.NAME_VETRINA+"_prev_butt'><img src='"+this.leftButton+"' onmousedown='this.src=\""+this.leftOverButton+"\"' onmouseup='this.src=\""+this.leftButton+"\"'></div>\
					<div class='MEVetrina_images_cont' id='images_cont_"+this.NAME_VETRINA+"'>\
						<div id='"+this.NAME_VETRINA+"' style='left:"+_left+"px'></div>\
					</div>\
				<div class='MEVetrina_next_butt' id='"+this.NAME_VETRINA+"_next_butt'><img src='"+this.rightButton+"' onmousedown='this.src=\""+this.rightOverButton+"\"' onmouseup='this.src=\""+this.rightButton+"\"'></div>\
				</div>");
	document.getElementById(this.vetrina_container).innerHTML += _html;
	document.getElementById(this.NAME_VETRINA+"_next_butt").onclick = this.nextImages.bind(this);
	document.getElementById(this.NAME_VETRINA+"_prev_butt").onclick = this.prevImages.bind(this);
	
	
 }
 
 MEVetrina.prototype.start = function(){
 

	if (!this.initVetrina()){
		return;
	}
	
	this.setStyle();
	
	this.writeHTML();
	
	this.getImages();
 
 }
 
 
 MEVetrina.prototype.initVetrina = function(){
	
	
	if (typeof this.URL == "undefined"){
		alert("MEVetrina.URL not defined");
		return false;
	}
	if (typeof this.NAME_VETRINA == "undefined"){
		this.NAME_VETRINA = this.vetrina_container + "_img_vetrina";
		return false;
	}
	if (document.getElementById(this.NAME_VETRINA)){
		//alert("vetrina with same name already defined. Define a unique name MEVetrina.NAME_VETRINA");
		//return false;
	}
	if (!document.getElementById(this.vetrina_container)){
		alert("container '"+this.vetrina_container+"' does not exist!!!");
		return false;
	}
	if (typeof window.MEVetrineArray == "undefined")
		MEVetrineArray = new Array();
	MEVetrineArray[this.NAME_VETRINA] = this;
	this.aImages = new Array();
	this.first_index = -this.PRECAR_SX;
	this.img_num = 0;
	if (typeof this.movement!="undefined")
		clearTimeout(this.movement);
	document.getElementById(this.vetrina_container).innerHTML = "";
	 //private attributes
    if (typeof this.VETRINA_HEIGHT == "undefined")
        this.VETRINA_HEIGHT = this.IMGBOX_HEIGHT + 2;
	this.step = this.STEP ? parseInt(this.STEP) : 1;
	
	
	this.needed_shifts = this.IMGBOX_WIDTH + this.IMG_MARGIN ; //number of shifts in transition
	
	this.steps_shifts = this.step * this.needed_shifts ; //number of shifts in transition
	
	this.showed_images = new Array(); //the images showed in the actual window
	this.shift=0; //the number of the actual shifts in the running transition
	this.direction=this.DEFAULT_DIR; //the this.shift this.direction in transitions
	this.delay=this.TRANS_TIME*1000; ////the transition time bettwen shifts, in milliseconds
	this.stop_time=this.STOP_TIME*1000;//the stop time between two consecutive transitions, in milliseconds
	this.stopped_flag=false; //true when the transition effect is stopped by mouseover on images
	this.changing_dir=false; //true when we this.invert the window transition effect this.direction
	this.block_size = this.IMAGES_NUM+this.PRECAR_SX+this.PRECAR_DX; //the ajax image loader block size
	this.started = false; //true when sliding is starting
	this.request_interval = this.REQUEST_INTERVAL*1000; //tha intervall beetween ajax images request, in milliseconds
	this.first_charge_delay = this.FIRST_MOVE_DELAY*1000; //milliseconds to wait before the vetrina become visible
	this.invert=false;
	this.position = 'agenzie';
	this.leftButton=this.LEFT_BUTTON;
	this.autostart = this.AUTOSTART != 	null ? this.AUTOSTART : true;
	this.rightButton=this.RIGHT_BUTTON;
	this.leftOverButton=this.LEFT_BUTTON_OVER;
	this.rightOverButton=this.RIGHT_BUTTON_OVER;
	this.exist_vetrina = false; //serve per notificare la presenza di immagini nella vetrina
	return true;
 }
 
 // set CSS style for vetrina
 MEVetrina.prototype.setStyle = function(){
   var _left; 
    if (this.img_num>this.IMAGES_NUM) { _left = -1*this.needed_shifts*this.PRECAR_SX ; disp='block'}
     else {_left=0; disp='none';}
     
    
    
   
  _css = ''+
 '#MEVetrina_'+this.NAME_VETRINA+'{'+
 ' height:'+(this.IMGBOX_HEIGHT+ 20) + 'px;'+
 ' width:'+(this.IMAGES_NUM*this.needed_shifts+(2*this.BTN_WIDTH)+this.IMG_MARGIN/2)+'px;'+
 '}'+

 '#MEVetrina_'+this.NAME_VETRINA+' #images_cont_'+this.NAME_VETRINA+'{'+
 ' float:left; '+
 ' width:'+(this.IMAGES_NUM*this.needed_shifts)+'px;'+
 'overflow-x:hidden !important;' +
 'display:inline;'+
 'position:absolute'+
 '}'+

 '#MEVetrina_'+this.NAME_VETRINA+' #images_cont_'+this.NAME_VETRINA+' #'+this.NAME_VETRINA+'{'+
 'width:'+((this.IMAGES_NUM+this.PRECAR_DX+this.PRECAR_SX)*this.needed_shifts)+'px;'+
 'position:relative;'+
 'left:'+_left+'px;\!important'+
 '}'+
 
 '#MEVetrina_'+this.NAME_VETRINA+' #images_cont_'+this.NAME_VETRINA+' #'+this.NAME_VETRINA+' .'+this.NAME_VETRINA+'_item_cont{'+
 'width:'+(this.IMGBOX_WIDTH)+'px;'+
 'margin-left:'+this.IMG_MARGIN+'px;'+
 'float:left;'+
 'position:relative;'+
 'text-align:center;'+
 'left:0px !important;'+
 'left:-'+this.IMG_MARGIN+'px;'+
 '}'+

 '#MEVetrina_'+this.NAME_VETRINA+' .MEVetrina_prev_butt{'+
 ' float:left;'+
 ' position:relative;'+
 ' top:'+((this.IMGBOX_HEIGHT/2)-(this.BTN_HEIGHT/2))+'px;'+
 '}'+

 '#MEVetrina_'+this.NAME_VETRINA+' .MEVetrina_next_butt{'+
 'float:right;'+
 'top:'+((this.IMGBOX_HEIGHT/2)-(this.BTN_HEIGHT/2))+'px;'+
 'position:relative;'+
 /*'left:'+this.BTN_WIDTH+'px;'+*/
 '}'+

 '#MEVetrina_'+this.NAME_VETRINA+' .MEVetrina_next_butt img{'+
 'cursor:pointer;'+
 '}'+

 '#MEVetrina_'+this.NAME_VETRINA+' .MEVetrina_next_butt img{'+
 'cursor:pointer;'+
 '}';
 
 this.appendHtmlCss(_css,this.NAME_VETRINA+"_cssStyle");
 
 }
 
 MEVetrina.prototype.appendHtmlCss = function(html_css,idCss){
	
	if (document.getElementById(idCss))
		return false;
	
    //var headID = document.getElementsByTagName("head")[0];         
    //var cssNode = document.createElement('style');
    //cssNode.style.display = "block";
    //if (typeof idCss !="undefined" && idCss!="")
	//	cssNode.id = idCss;
    
	//cssNode.innerHTML = html_css;
	
	//headID.innerHTML += html_css;
	document.write("<div id='"+idCss+"'><style>"+html_css+"</style></div>")
	return true;
 }

