var curslideId = 0;
var slideCount = 0;
var automate = true;
var timeoutId = false;
var viewportwidth = 0;
var viewportheight = 0;

var timeout_resize =false;

document.observe("dom:loaded", function(){
    prepareSlideshow();	
	
	 rand = parseInt(Math.random() * slideCount)+1;
	 
	 scrollslide(rand);
		
	//	window.onresize = function(){window.clearTimeout(timeout_resize);timeout_resize = window.setTimeout('prepareSlideshow()',300)};	
});




function checkCanvasSize(){
 
 	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight 
 	
 	if (typeof document.documentElement.offsetWidth!= 'undefined')
 	{
 	     viewportwidth = document.documentElement.offsetWidth
 	     return true;
 	}
 	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 	
 	else if (typeof document.documentElement != 'undefined'
 	    && typeof document.documentElement.clientWidth !=
 	    'undefined' && document.documentElement.clientWidth != 0)
 	{
 	      viewportwidth = document.documentElement.clientWidth
 	      return true;
 	}
 	
 	// older versions of IE
 	
 	else
 	{
 	      viewportwidth = document.getElementsByTagName('body')[0].clientWidth
 	      return true;
 	}
}	


function prepareSlideshow(){
	viewportwidth = $$('body')[0].getWidth();
	
	//checkCanvasSize();


	
	
	var width = 0;
	slideCount = ($$('.tx-cllicefaslideshow-pi1-listrow').length);
	$$('.tx-cllicefaslideshow-pi1-listrow').each(
		function(el){
		//el.style.width = 950+'px';		

		}
	);	


checkButtons();
//	$('slideshowNext').onclick = function(){scrollslide()};	
//	$('slideshowPrev').onclick = function(){scrollslide(-1)};
	//$('slideshowWrap').style.width = (slideCount*viewportwidth)+'px';	
	// $('statement').style.width = viewportwidth+'px';
	
	// leftPosition = $('statement').viewportOffset()[0];
	
	//$('logo').innerHTML = leftPosition;	
	
	//if(leftPosition > 0)
	//	$('statement').style.left= '-'+(leftPosition)+'px';	
		
		
	

}


function debug(str){
	$('logo').innerHTML = str;
}

function checkButtons(){
	

	if(curslideId < 1){
		$('slideshowPrev').addClassName('gray');
		$('slideshowPrev').onclick = function(){return false}
	} else {
		$('slideshowPrev').removeClassName('gray');
		$('slideshowPrev').onclick = function(){scrollslide(-1)};
		
	}
		
		
	if(curslideId >= (slideCount-1)){
		$('slideshowNext').addClassName('gray');
		$('slideshowNext').onclick = function(){return false}
	} else {
		$('slideshowNext').removeClassName('gray');
		$('slideshowNext').onclick = function(){scrollslide()};
		
	}	

}

fx.ElementScroll = Class.create();
fx.ElementScroll.prototype = Object.extend(new fx.Base(), {
      initialize: function(el, options) {
        this.el = el;
        this.iniMarginLeft = this.el.style.marginLeft;
        this.setOptions(options);
    },

    increase: function() {
        this.el.style.marginLeft = this.now + 'px';
    },
    
    toggle: function(){
        if (this.el.marginLeft > 0) this.custom(this.el.marginLeft, 0);
        else this.custom(0, this.iniMarginLeft);
    }
});



function scrollslide(slideid){ 

         if(curslideId<0) curslideId=0;  
         if(curslideId>=slideCount) curslideId=slideCount-1;         

       //  $('logo').innerHTML = curslideId+" "+slideid+" "+slideCount;         

          if((curslideId>=0) && (curslideId < slideCount)){
            if((!slideid) && curslideId < slideCount-1){
              slideNext();
            }else if(-1 == slideid){
              if(curslideId>0)
                slidePrev();
            } else{

                  while ((slideid-1)>curslideId)
                    slideNext(); 
                  while ((slideid-1)<curslideId)
                    slidePrev();
             }            

          }
          
          checkButtons();

}

function slideNext(){
            var clientTween = new fx.ElementScroll($$('.tx-cllicefaslideshow-pi1-listrow')[curslideId], {duration: 600, transition: fx.sinoidal});
            clientTween.custom(0, -$$('.tx-cllicefaslideshow-pi1-listrow')[curslideId].getWidth());
            if(curslideId<slideCount) curslideId++;
}


function slidePrev(){
            curslideId--;
            var clientTween = new fx.ElementScroll($$('.tx-cllicefaslideshow-pi1-listrow')[curslideId], {duration: 600, transition: fx.sinoidal});
            clientTween.custom(-$$('.tx-cllicefaslideshow-pi1-listrow')[curslideId].getWidth(), 0);

}



function automateslide(){
	if((curslideId < (slideCount-1)) && (automate == true))
	timeoutId = setTimeout(function(){slideNext();automateslide()}, 6000);
	if((curslideId >= (slideCount-1)) && (automate == true))	{
		timeoutId = setTimeout(function(){scrollslide(1); automateslide()}, 6000);
	}

}



function stopAutomate(){
	automate = false;
	if(timeoutId) clearTimeout(timeoutId);
}

