//public (default)configuration attributes

var IMAGES_NUM = 6 ; //the number of showed images
var PRECAR_DX = 2 ; // the number of precharged images in the hidden right-box - minimum 2 (cause problem with explorer)
var PRECAR_SX = 2; // the number of precharged images in the hidden left-box - minimum 2 (cause problem with explorer)
var DEFAULT_DIR='left'; // The default autoplay direction - values 'left' or 'right'
var IMGBOX_BORDER = 0; //Image Border, write only the number not include px (i.e. if 132px --> IMG_HEIGHT=132)
var IMGBOX_WIDTH = parseInt(IMGBOX_BORDER + 100); //Image width, write only the number not include px (i.e. if 132px --> IMG_WIDTH=132)
var IMGBOX_HEIGHT = 75; //Image Height, write only the number not include px (i.e. if 132px --> IMG_HEIGHT=132)

var VETRINA_HEIGHT = 125; //Vetrina height, write only the number not include px (i.e. if 132px --> VETRINA_HEIGHT=65)
var IMG_MARGIN = 8 ; //the margin between images
var TRANS_TIME=2; //the transition time bettwen shifts, in seconds
var STOP_TIME=4; //the stop time between two consecutive transitions, in seconds
var FIRST_CHARGE_DELAY = 1; //seconds to wait before the vetrina become visible
var NAME_FEATURED='images'; //the name of this window
var INC_VAL = 20; //the shift increment value top speed transitions


var aImages = new Array(); //contains all the images to be showed
var img_num = 0; // the size of the array images
var needed_shifts = parseInt(IMGBOX_WIDTH + IMG_MARGIN)  ; //number of shifts in transition
var first_index = 0; //the aImages index of the first image in the actual window
var showed_images = new Array(); //the images showed in the actual window
var pos=1; // the initial left absolute position of the window
var shift=0; //the number of the actual shifts in the running transition
var direction=DEFAULT_DIR; //the shift direction in transitions
var movement; //the timer that regulate transition effects
var delay=TRANS_TIME*1000; ////the transition time bettwen shifts, in milliseconds
var stop_time=STOP_TIME*1000;//the stop time between two consecutive transitions, in milliseconds
var stopped_flag=false; //true when the transition effect is stopped by mouseover on images
var changing_dir=false; //true when we invert the window transition effect direction
var block_size = IMAGES_NUM+PRECAR_SX+PRECAR_DX; //the ajax image loader block size
var first_start = true; //false when the first_image_block was received
var first_charge_delay = FIRST_CHARGE_DELAY*1000; //milliseconds to wait before the vetrina become visible
var invert=false;

//regulate the transition effect
function move(side){
	if ((shift==0)) img_num = aImages.length;
	if (shift==0){
		changing_dir=false;
	}
	if ((shift==0)&&(side=='none')){
		movement =setTimeout("move('direction')",0);
	}else if(shift<needed_shifts-IMGBOX_WIDTH/2){
		var inc=0;
		if (side=='right'){
			inc=+INC_VAL;
		}else if(side=='left'){
			inc=-INC_VAL;
		}else{ 
			if (direction=='right'){
				inc=+INC_VAL;
			}else if(direction=='left'){
				inc=-INC_VAL;
			}
		}
		var left = $(NAME_FEATURED).style.left;
		var pos = left.search('px');
		var left = left.substr(0,pos);
		pos=parseInt(left);
		pos = pos+inc;
		shift+=INC_VAL;
		$(NAME_FEATURED).style.left=pos+"px";
		rit=(delay)/needed_shifts;
		movement = setTimeout("move('"+side+"')",rit);
	}else{ 
		if (side=='right'){
			first_index=first_index-1;
		}else if (side=='left'){
			first_index=first_index+1;
		}
		showImages();
		shift=0;
		rit=(delay)/needed_shifts;
		if (img_num>IMAGES_NUM) movement =setTimeout("move('"+direction+"')",stop_time);
	}
}

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

function stopVetrina(){
	clearTimeout(movement);
}

 //make a right-shift

function nextImages(){
	slide('right');
}

//make a left shift
function prevImages(){
	slide('left');
}

 //show the current images in the window
 function showImages(){
	if (img_num>=0){
		if (first_index<0) first_index=img_num+first_index;
		else first_index=(first_index)%img_num;
		//alert(IMAGES_NUM+' '+PRECAR_DX+' '+PRECAR_SX);
		for (i=0; i<IMAGES_NUM+PRECAR_DX+PRECAR_SX;i++) {
			if (i<img_num)
			showed_images[i]="<div class='img'><div class=\"title\"><strong>"+aImages[((first_index+i)%img_num)][4]+"</strong></div><div class='wrap_img' ><a href='"+aImages[((first_index+i)%img_num)][2]+"' target='"+aImages[((first_index+i)%img_num)][3]+"'><img src='"+aImages[((first_index+i)%img_num)][0]+"'></a></div><div class='text'><a href='"+aImages[((first_index+i)%img_num)][2]+"'><span>"+aImages[((first_index+i)%img_num)][1]+"</span></a></div></div>";
		}
		html="";
		for (i=0; i<IMAGES_NUM+PRECAR_DX+PRECAR_SX; i++)
		if (i<img_num)html += showed_images[i];
		if (i>img_num) $(NAME_FEATURED).style.left='-'+(needed_shifts*PRECAR_SX)+'px';
		else $(NAME_FEATURED).style.left='0';
		//alert(img_num);
		if (img_num>IMAGES_NUM) {
			_left = -1*needed_shifts*PRECAR_SX;
			$("prev_butt").style.display='block';
			$("next_butt").style.display='block';
		}
		else {
			_left=0; 
			$("prev_butt").style.display='none';
			$("next_butt").style.display='none';
		}
		_html="<div id='"+NAME_FEATURED+"' style='left:"+parseInt(_left-IMG_MARGIN)+"px;' onmouseover='stopVetrina()' onmouseout='restart();'>"+html+"<\/div>";

		//$('images_cont_'+NAME_FEATURED).update(_html);
		// alert(_html);
		$('images_cont_'+NAME_FEATURED).innerHTML= _html;

		if (img_num>IMAGES_NUM) $('object_featured_'+NAME_FEATURED).style.visibility='visible'
		setTimeout("$('object_featured_'+NAME_FEATURED).style.visibility='visible'",first_charge_delay);
	}
}

 

 // Create vetrina HTML div's in the document
 function writeHTML(){
    if (img_num>IMAGES_NUM) _left = -1*needed_shifts*PRECAR_SX ;
     else _left=0;
	document.write("<div id='object_featured_"+NAME_FEATURED+"' style='visibility:hidden;'><div id='images_cont_"+NAME_FEATURED+"'><div id='"+NAME_FEATURED+"' style='left:"+_left+"px'></div></div></div>");
 }
 
 function startVetrina(){
	setStyle();
	writeHTML();
	showImages();
	restart();
 }

 // set CSS style for vetrina

 function setStyle(){
   var _left; 
    if (img_num>IMAGES_NUM) { _left = -1*needed_shifts*PRECAR_SX ; disp='block'}
     else {_left=0; disp='none';}
     
    var browser=navigator.appName;
    var b_version=navigator.appVersion;
    var version=parseFloat(b_version);
	var factor=55;
   
    if ((browser=="Microsoft Internet Explorer")&& (version<6))
    {
	 factor=55;
    } 
   
 document.write('<style>'+
 '#object_featured_'+NAME_FEATURED+'{'+
 ' height:'+VETRINA_HEIGHT+'px;'+
 ' /*width:'+(IMAGES_NUM*needed_shifts+factor+IMG_MARGIN)+'px;*/'+
 ' margin:10px 0px 10px 0px;'+
 ' overflow : hidden;'+
 '}'+

 '#object_featured_'+NAME_FEATURED+' #images_cont_'+NAME_FEATURED+'{'+
 ' position:absolute; '+
 ' width:'+(IMAGES_NUM*needed_shifts)+'px;'+
 ' overflow:hidden;'+
 ' margin-left:28px;'+
 ' margin-left:expression("-2px");'+

 
 '}'+

 '#object_featured_'+NAME_FEATURED+' #images_cont_'+NAME_FEATURED+' #'+NAME_FEATURED+'{'+
 'width:'+((IMAGES_NUM+PRECAR_DX+PRECAR_SX)*needed_shifts)+150+'px;'+
 'position:relative;'+
 'left:'+_left+'px;\!important'+
  
 '}'+

 '#object_featured_'+NAME_FEATURED+' #images_cont_'+NAME_FEATURED+' #'+NAME_FEATURED+' .img{'+
 'width:'+IMGBOX_WIDTH+'px;'+
 'margin-left:'+IMG_MARGIN+'px;'+
 'float:left;'+
 'position:relative;'+
 'text-align:center;'+
 'left:0px !important;'+
 'left:-'+IMG_MARGIN+'px;'+
 '}'+

 '#object_featured_'+NAME_FEATURED+' #images_cont_'+NAME_FEATURED+' #'+NAME_FEATURED+' .img .wrap_img{'+
 'width:'+(IMGBOX_WIDTH)+'px;'+
 'height:'+(IMGBOX_HEIGHT)+'px;'+
 'overflow:hidden;'+
 'line-height:'+(IMGBOX_HEIGHT)+'px;'+
 'border: 3px double #d6d6d6 ;'+
 '}'+

 '#object_featured_'+NAME_FEATURED+' #images_cont_'+NAME_FEATURED+' #'+NAME_FEATURED+' .img img{'+
 //'width:'+(IMGBOX_WIDTH)+'px;'+
 'overflow:hidden;'+

 '}'+

 '#object_featured_'+NAME_FEATURED+' #images_cont_'+NAME_FEATURED+' #'+NAME_FEATURED+' .img .text {'+
 'width:'+(IMGBOX_WIDTH)+'px;'+
 'overflow:hidden;'+
 'height:30px;'+

 '}'+

 '#object_featured_'+NAME_FEATURED+' #images_cont_'+NAME_FEATURED+' #'+NAME_FEATURED+' .img .text span{'+
 /*'font-size:'+(IMGBOX_WIDTH/10)+'px;'+*/
 'font-size:11px;'+
 'margin-top:3px;'+
 'text-align:center;'+
 //'font-weight:bold;'+
 'color:#3D6491'+
 '}'+
 
 '#object_featured_'+NAME_FEATURED+' #images_cont_'+NAME_FEATURED+' #'+NAME_FEATURED+' .img  div.title{'+
 /*'font-size:'+(IMGBOX_WIDTH/10)+'px;'+*/
 'font-size:11px;'+
 'font-size:11px;'+
 'margin-top:3px;'+
 'text-align:center;'+
 'font-weight:bold;'+
 'color:#3D6491'+
 '}'+

 ' .prev_butt{'+
 ' float:left;'+
 ' position:relative;'+
 ' width:25px;'+
 'display:none;'+
  'margin-top:1px;'+
 '}'+

 '.next_butt{'+
 'float:left;'+
 'width:25px; !important;'+
 'display:none;'+
 'margin-top:1px;'+
 'margin-left:10px;'+
 '}'+

 '.prev_butt img{'+
 'float:right;'+
 'position:relative;'+
 
 
 'cursor:pointer;'+
 '}'+

 '.next_butt img{'+
 'float:left;'+
 'margin-right:5px;'+
 'position:relative;'+
 
 'cursor:pointer;'+
 //'top:35px;'+
 '}'+
 '</style>');
 }

