var isMSIE = /*@cc_on!@*/false;
var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
var	hiddenimage=null;
var shownimage=null;
	var cidgallery= new Object();
	function startcidgallery(){
		for (var galid in cidgallery){
			el=$('.gal'+galid);
			//cidgallery[galid]=new Object();
			var thisgal=cidgallery[galid];
			//cidgallery[galid].imagearray= new Object();
			thisgal.imagecounter=1;
			//cidgallery[galid].imagecount=0;
			//cidgallery[galid].nextimage=new Array();
			thisgal.bigimage=$(el);
			
			thisgal.swapflag=true;
			
			thisgal.swapbuffer=0;
			thisgal.swap0=$(el).find('.swap0');
			thisgal.swap1=$(el).find('.swap1');
			
			thisgal.time=5000;
			thisgal.scroller=$(el).parent().find('.scroller');
			thisgal.scrollertop=$(el).parent().find('.scroller-top');
			thisgal.scrollerbot=$(el).parent().find('.scroller-bot');
			thisgal.scrollertop.mouseover(function(){cidmovetop(galid);});
			thisgal.scrollertop.mouseout(function(){cidmovestop(galid);});
			thisgal.scrollerbot.mouseover(function(){cidmovebot(galid);});
			thisgal.scrollerbot.mouseout(function(){cidmovestop(galid);});
			
			thisgal.move=false;
			
			setInterval("cidgalmove("+galid+");",0);
			setInterval("bigswap("+galid+");",cidgallery[galid].time);
		}
	}
	function cidgalmove(galid){
		var thisgal=cidgallery[galid];
		if (thisgal.move==true){
			var top=parseInt(thisgal.scroller.css('top'));
			var scrollheight=thisgal.scroller.height();
			scrollheight=scrollheight-((80+9+9)*4)-5;
			top=top+thisgal.movenum;
			if (top>5)top=5;
			if (top<-scrollheight)top=-scrollheight;
			thisgal.scroller.css('top',top+'px');
		}
	}
	function cidmovestop(galid){
		cidgallery[galid].move=false;
	}
	function cidmovetop(galid){
		cidgallery[galid].move=true;
		cidgallery[galid].movenum=+5;	
	}
	function cidmovebot(galid){
		cidgallery[galid].move=true;
		cidgallery[galid].movenum=-5;
	}
	function addimage(galid,imgurl){
		if (cidgallery[galid]==undefined) 
		{
			cidgallery[galid]=new Object();
			cidgallery[galid].imagecount=0;
			cidgallery[galid].imagearray=new Object();
		}
		
		cidgallery[galid].imagearray[cidgallery[galid].imagecount]=imgurl;
		cidgallery[galid].imagecount++;
	}
	function bigswap(galid){
		
		var thisgal=cidgallery[galid];
		
		if (thisgal.swapbuffer==0){
			if (thisgal.nextimage!=undefined){
				thisgal.imagecounter=thisgal.nextimage;
				thisgal.nextimage=undefined;
			}
			var tempurl=thisgal.imagearray[thisgal.imagecounter];
			thisgal.imagecounter++;
			if (thisgal.imagecounter==(thisgal.imagecount)) thisgal.imagecounter=0;
			thisgal.swapbuffer++;
			var newimage=new Image();
			newimage.scr=tempurl;
			bigswapeffect(newimage,galid);
		}
	}
	function bigswapeffect(newimage,galid){
		var thisgal=cidgallery[galid];
		if (thisgal.imagecount<2) thisgal.swapflag =false;
		if (thisgal.swapflag){
			shownimage=thisgal.swap0;
			hiddenimage=thisgal.swap1;
		}else{
			shownimage=thisgal.swap1;
			hiddenimage=thisgal.swap0;
		}
		hiddenimage.parent().css('visibility','hidden');
		hiddenimage.attr('src',newimage.scr);
		if (!is_chrome) setTimeout(function(){vcentersingle(hiddenimage);vcentersingleload(hiddenimage);},0);
		else setTimeout(function(){vcentersinglenatural(hiddenimage);vcentersingleloadnatural(hiddenimage);},0);
		
	
		hiddenimage.ready(function(){
			hiddenimage.parent().css('z-index','0');
			shownimage.parent().css('z-index','1');
			hiddenimage.parent().css('visibility','visible');
			
			if (!is_chrome) setTimeout(function(){vcentersingle(hiddenimage);vcentersingleload(hiddenimage);},0);
			else setTimeout(function(){vcentersinglenatural(hiddenimage);vcentersingleloadnatural(hiddenimage);},0);
			

			var oldwidth=shownimage.parent().width();
			shownimage.parent().animate(
				{opacity:0,width:0,left:oldwidth},
				{
					step:function(now,fx){
						
						if (fx.prop=='left') shownimage.css('left',-now+'px');
					},
					complete:function(){
						shownimage.parent().css('visibility','hidden');
						shownimage.parent().animate({opacity:1,width:oldwidth,left:0},0,function(){
							shownimage.css('left','0');
							shownimage.parent().css('visibility','hidden');
							hiddenimage.unbind('load');
							endSwap(galid);
							});					
					}
				},'slow');	
			
			
		});
	}
	function endSwap(galid){
		var thisgal=cidgallery[galid];
		thisgal.swapbuffer--;
		thisgal.swapflag=!thisgal.swapflag;
		
	}
	function swapnow(a,b){
		cidgallery[a].nextimage=b;
		bigswap(a);
	}
	function vcentersinglenatural(el){
		
		
			$(el).css('margin-left','0px');
			$(el).css('margin-top','0px');
			
		
			var imgheight=$(el).get(0).naturalHeight;
			var imgwidth=$(el).get(0).naturalWidth;
			var boxheight=$(el).parents('.vimg').height();
			var boxwidth=$(el).parents('.vimg').width();
			var newheight=((imgheight-boxheight)/2)*-1;
			var newwidth=((imgwidth-boxwidth)/2)*-1;
			$(el).css('margin-left',newwidth+'px');
			$(el).css('margin-top',newheight+'px');
			
		
	}
	function vcentersingle(el){
		
		
			$(el).css('margin-left','0px');
			$(el).css('margin-top','0px');
			
			var imgheight=$(el).height();
			var imgwidth=$(el).width();
			
			var boxheight=$(el).parents('.vimg').height();
			var boxwidth=$(el).parents('.vimg').width();
			var newheight=((imgheight-boxheight)/2)*-1;
			var newwidth=((imgwidth-boxwidth)/2)*-1;
			$(el).css('margin-left',newwidth+'px');
			$(el).css('margin-top',newheight+'px');
			
		
	}
	
	function vcentersingleload(el){
		$(el).load(function(){setTimeout(function(){vcentersingle(el);},0);});
	}
	function vcentersingleloadnatural(el){
		$(el).load(function(){setTimeout(function(){vcentersingle(el);},0);});
	}
	function vcenter(){
		$('.vimg').find('img').each(function(){
			
			
				if (this.complete) vcentersingle(this);
				else vcentersingleload(this);
			
		});
	}
	

