
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride = {};
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.ClientDim = function( dim ){
  var value = document.documentElement['client'+dim];
  if( !value ){
    value = document.body['client'+dim];
  }                 
  return Number(value);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.CustEvent = function( element, eventType, handler, capture ){
  //////////////////////////////////// 
  try  {
    if( element.addEventListener ){
      element.addEventListener(eventType, handler, capture);
    }  else if( element.attachEvent ){
      element.attachEvent('on' + eventType, handler);
    } else {
      alert("Unsupported method [customEvent]!" + "\n" + handler)
      return;
    }
    return handler;
  }catch(e){  alert('customEvent Error [' + element + ']' + "\n" + e + "\n" + handler );  }
  //////////////////////////////////// 
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack = function( xTg){
  
	this.Obj = 'RIDE_Imgs';
	this.Tg = xTg;	
  this.Div = getDivStyle(xTg);
  this.Img = document.getElementById(xTg+'Image');
  this.ImgS = getDivStyle(xTg+'Image');
  this.Items = new Array();
  this.MinH = 650;
	
	var _Self = this;
  
  Ride.CustEvent( window,   'resize',   	function(){   _Self.Resize();  },  				false);  
	Ride.CustEvent( window,   'load',   		function(){   _Self.Resize();  },  				false);
  Ride.CustEvent( this.Img, 'load',      function(){   _Self.Load_Complete();  },  	false);   
  
  this.Div.display = '';
  this.ImgSel = -1;
  this.Resize();
  
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.Add = function( xPath, W, H, A ){
  
  var _Self = this;
	var Ind = Number(this.Items.length);
  var Item = {Path:xPath, W:W, H:H, A:A, Ind:Ind, Img:new Image() };
	this.Items.push(Item);
	Item.Img.onload = function(){
    _Self.Preload_Complete( Ind );
  }
	if( Item.Ind==0 ){
  	Item.Img.src = xPath;
	}
  
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.Init = function( xPath ){

  var _Self = this;
                                                                     
  if( this.Items.length>1 ){
		
		this.Paging = document.getElementById('SlidePaging');   
		if( this.Paging ){
			var HTML = "";
			for( var i=0; i<this.Items.length; i++ ){        
				HTML += '<a href="javascript:'+this.Obj+'.SkipTo('+i+');"><div class="ImgPaging" id="ImgPagingDiv_'+i+'"></div></a>';
			}    
			this.Paging.innerHTML = HTML + '<div class="FloatClear"></div>';
		}   
		
		document.getElementById('ImgArrowPrev').href = "javascript:"+this.Obj+".Skip(-1)";
		document.getElementById('ImgArrowNext').href = "javascript:"+this.Obj+".Skip(1)";
		var TD = document.getElementById(this.Tg+'TD');
		if( TD ){
			Ride.CustEvent( TD, 'mouseup', function(){ 	_Self.Next() 	}, false);
		}
  }
	if( this.ImgSel>=0 ){
		this.SelectImage(this.ImgSel)
	}

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.Preload_Complete = function( xInd ){
	
  if( xInd==0 ){
    this.SelectImage(0);
  }	
	if( xInd<this.Items.length-1 ){
		this.Items[xInd+1].Img.src = this.Items[xInd+1].Path;
	}
  
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.Load_Complete = function(){

  this.Resize();
	this.ImgS.display = '';
  
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.SelectImage = function( xInd ){
	////////////////////////////////////
  this.SelectPaging(this.ImgSel, 'ImgPaging');

  var xItem = this.Items[xInd];
	this.ImgS.display = 'none';
  this.ImgSel = xInd;
  this.Img.src =  xItem.Path;

  if( this.Items.length>1 ){
    this.SelectArrows('Prev', (this.ImgSel>0)?'':'none');
    this.SelectArrows('Next', (this.ImgSel<this.Items.length-1)?'':'none');
  }

  this.SelectPaging(this.ImgSel, 'ImgPagingSel');
  ////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.SelectPaging = function( xInd, xClass ){
  ////////////////////////////////////
  var PagingItem = document.getElementById('ImgPagingDiv_'+this.ImgSel);
  if( PagingItem ){
    PagingItem.className = xClass;
  }
  ////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.SelectArrows = function( xTg, xDisplay ){
  ////////////////////////////////////
  var ArrowItem = getDivStyle('ImgArrow'+xTg);
  if( ArrowItem ){
    ArrowItem.display = xDisplay;
  }
  ////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.Resize = function( xItem ){
  //////////////////////////////////// 
  var PageH = Ride.ClientDim('Height')-3;
  var PageW = Ride.ClientDim('Width');  
  /*
  if(PageW<1025 || PageH<768){
    document.body.style.overflow = "auto";
  } else {
    document.body.style.overflow = "hidden";
  }
	*/
  this.Div.height = PageH+'px';
  this.Div.width = PageW+'px';
  //////////////////////////////////// 
  if( this.ImgSel==-1 ){
    return;
  }
  //////////////////////////////////// 
  var xItem = this.Items[this.ImgSel];
  var R = xItem.W/xItem.H;
  ////////////////////////////////////  
  if( PageW/PageH>R ){
    var H = Number(PageH);
  } else {
    var H = PageW/R;
  }
  H = Math.max(H, this.MinH);
  ////////////////////////////////////
  this.Img.height = Math.round(H);
  this.Img.width = Math.round(H*R);
  //////////////////////////////////// 
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.SkipTo = function( Ind ){
  //////////////////////////////////// 
  var Ind = Math.min(Math.max(Ind, 0), this.Items.length-1);
  this.SelectImage( Ind );
  //////////////////////////////////// 
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.Skip = function( n ){
  //////////////////////////////////// 
  this.SkipTo( this.ImgSel+n );
  //////////////////////////////////// 
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.PROTO_SlideBack.prototype.Next = function(){
  //////////////////////////////////// 
  var Ind = this.ImgSel + 1;
  if( Ind>=this.Items.length ){
    Ind = 0;
  }
  this.SkipTo( Ind );
  //////////////////////////////////// 
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.TextExpandable = new Array();
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.TextExpandableOpenFirst = function(){
	if( Ride.TextExpandable.length>0 ){
		Ride.TextExpand( Ride.TextExpandable[0] );
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
Ride.TextExpand = function( xTg ){
	//////////////////////////////////// 
	var curDiv = getDivStyle(xTg);
	var curBt = document.getElementById("bt"+xTg)
	if(curDiv.display == "none"){
		curDiv.display = "";
		curBt.className = curBt.className.replace('Open','')+'Open';
	} else {
		curDiv.display = "none";
		curBt.className = curBt.className.replace('Open','');
	}
	//////////////////////////////////// 
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////// 


