/*
 * Javascript Image Gallery
 * Version:  1.0.0
 * Author : snowtigersoft
 */ 
function adRotator() {};
adRotator.init=function(o,w,h,s,s1)
{
	this._p = new Array();
	this._c = 0;
	this._o = o;

	this._timeout = null;
	this._html = "";

	// configration.
	// @ image width
	// @ image height
	// @ button bars width
	// @ rotator speed
	// @ fade speed
	this._w = w;
	this._h = h;
	this._pw = 30;
	this._s = 5;
	this.fadespeed = .4;

	return this;
};
adRotator.add=function(p) { with (this)
{
	var tmp = new Array();
	tmp["src"] = p+"?_="+new Date();
	_p.push(tmp);
	if(_p.length*_pw>_w)
		_pw = _w/_p.length;
}};
adRotator.clear=function() { with (this)
{
	_pw = 30;
	_c = 0;
	_old = 0;
	_p = new Array();
}};

adRotator.play=function() { with (adRotator)
{	
	if($("#gallery").size() == 0){	
		_html = "";
		_html += '<div id="gallery" style="border:0;overflow:hidden;width:'+_w+'px;height:'+(_h+30)+'px">'
		_html += '<div id="rotatorPlayer" valign="middle" align="left"'
				+ ' style="border:0;overflow:hidden;width:'+_w+'px;height:'+_h+'px">'
		_html += '<img id="rotatorPic" src="" style="border:0;display:none;"/>';
		_html += '<div id="rotatorNews" style="border:0;overflow:hidden;width:'+_w+'px;height:'+_h+'px">';
		_html += '<iframe id="peopleFrame" height="'+_h+'" frameborder="0" width="'+(_w-140)+'" scrolling="no" name="peopleFrame" src="'+_p[_c]["src"]+'" margin-right="10"></iframe>'
		_html += '<img src="http://www.dtah.com/news/books.jpg" width="130" align="top" height="371"/>';
		_html += "</div>";
		_html += "</div>";
		_html += '<div id="bars" align="left">';
					
		for(var i=0; i<_p.length; i++)
			_html += '<div style="overflow:hidden;float:left;border:0px;margin:10px 0px 1px 0px;height:5px;width:'+_pw+'px;"></div>';		
		_html += "</div></div>";
		$("#"+_o).html(_html);	
		$("#bars div").each(function(i){
			if(_c == i)
				$(this).css("background-color","orange").css("cursor","default").click(function(){adRotator.select(i);});
			else
				$(this).css("background-color","#ccc").css("cursor","pointer")
					.click(function(){adRotator.select(i);})
					.hover(function(){
							$(this).css("background-color","orange");
						},function(){
							$(this).css("background-color","#ccc");
						});
		});
	}else{
		if( _c == 0 ){
			//show text
			$("#rotatorPic").fadeOut(fadespeed*1000,function(){															 
				$("#rotatorPic").hide();
				if($("#rotatorNews iframe").attr("src") != _p[_c]["src"]){
					$("#rotatorNews iframe").attr("src",_p[_c]["src"]);
					_html = "";
					for(var i=0; i<_p.length; i++)
						_html += '<div style="overflow:hidden;float:left;border:0px;margin:10px 0px 1px 0px;height:5px;width:'+_pw+'px;"></div>';
					$("#bars").html(_html);	
					$("#bars div").each(function(i){
						if(_c == i)
							$(this).css("background-color","orange").css("cursor","default")
								.unbind().click(function(){adRotator.select(i);});
						else
							$(this).css("background-color","#ccc").css("cursor","pointer")		
								.click(function(){adRotator.select(i);})
								.hover(function(){
											$(this).css("background-color","orange");
										},function(){
											$(this).css("background-color","#ccc");
								});
					});
				}else{
					$("#bars div").each(function(i){
						if(_c == i)
							$(this).css("background-color","orange").css("cursor","default")
								.unbind().click(function(){adRotator.select(i);});
						else
							$(this).css("background-color","#ccc").css("cursor","pointer")							
								.hover(function(){
											$(this).css("background-color","orange");
										},function(){
											$(this).css("background-color","#ccc");
								});
					});
				}
				$("#rotatorNews").fadeIn(fadespeed*1000);
			});			
		}else{
			//show image
			if( _p[_c]["r"] == 1 )
			{
				if(_old==0){
					$("#rotatorNews").fadeOut(fadespeed*1000,function(){
						$("#rotatorNews").hide();
						$("#rotatorPic").attr("src",_p[_c]["src"])
							.css("width",_p[_c]["w"])
							.css("height",_p[_c]["h"])
							.css("margin-top",_p[_c]["top"]);	
						$("#rotatorPic").fadeIn(fadespeed*1000);
						$("#bars div").each(function(i){
							if(_c == i)
								$(this).css("background-color","orange").css("cursor","default")
									.unbind().click(function(){adRotator.select(i);});
							else
								$(this).css("background-color","#ccc").css("cursor","pointer")							
									.hover(function(){
											$(this).css("background-color","orange");
										},function(){
											$(this).css("background-color","#ccc");
										});
						});				
					});
				}else{
					$("#rotatorPic").fadeOut(fadespeed*1000,function(){
						$("#rotatorNews").hide();
						$("#rotatorPic").attr("src",_p[_c]["src"])
							.css("width",_p[_c]["w"])
							.css("height",_p[_c]["h"])
							.css("margin-top",_p[_c]["top"]);	
						$("#rotatorPic").fadeIn(fadespeed*1000);
						$("#bars div").each(function(i){
							if(_c == i)
								$(this).css("background-color","orange").css("cursor","default")
									.unbind().click(function(){adRotator.select(i);});
							else
								$(this).css("background-color","#ccc").css("cursor","pointer")							
									.hover(function(){
											$(this).css("background-color","orange");
										},function(){
											$(this).css("background-color","#ccc");
										});
						});			
					});		
				}
			}else{
				_timeout=window.setTimeout(adRotator.play,100);	
			}
		}
	}
}};
adRotator.next=function() { with (this)
{
	(_p.length-1==_c) ? _c=0 : _c++;
	_timeout=window.setTimeout(adRotator.play,_s*1000);
}};
adRotator.select=function(i) { with (this)
{
	window.clearTimeout(_timeout);
	_old=_c;
	_c=i;
	adRotator.play();
}};
adRotator.perload=function(){with (this){
	for (var i=1; i<_p.length; i++)
    {
            var src = _p[i]["src"];			
            img = document.createElement("img");
			img.title = i;
            img.style.display = "none";
			img.src = src;
			$("body").append(img);
            $(img).load(function()
            {	
				var c = parseInt(this.title);
				var   H=$(this).height();   
				var   W=$(this).width();   
				var   scale_1=_h/_w;
				var   scale_2=H/W; 
				if(scale_1<scale_2){ 
					_p[c]["w"] = _h/scale_2;
					_p[c]["h"] = _h;
					_p[c]["top"] = 0+"px";
				}else{
					_p[c]["w"] = _w;
					_p[c]["h"] = _w*scale_2;
					_p[c]["top"] = (_h - _w*scale_2)/2+"px";
				}
				_p[c]["r"] = 1;
                document.body.removeChild(this);
                this.onload = null;
                this.style.display = "";				
            });
   }
}};
