var slide_curslideId = 0;
var slide_slideCount = 0;
var slide_automate = true;
var slide_timeoutId = false;
var viewportwidth = 0;
var viewportheight = 0;

var timeout_resize =false;

document.observe("dom:loaded", function(){
    prepareSlideshow();	
	
	 //rand = parseInt(Math.random() * slide_slideCount)+1;
	 
	 //scrollslide(rand);
	 if(slide_automate)
		automateslide();
	//	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;
	slide_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 = (slide_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(slide_curslideId < 1){
		$('slideshowPrev').addClassName('gray');
		$('slideshowPrev').onclick = function(){stopAutomateSlide(); return false}
	} else {
		$('slideshowPrev').removeClassName('gray');
		$('slideshowPrev').onclick = function(){stopAutomateSlide(); scrollslide(-1)};
		
	}

		
	if(slide_curslideId >= (slide_slideCount-1)){
		$('slideshowNext').addClassName('gray');
		$('slideshowNext').onclick = function(){stopAutomateSlide(); return false}
	} else {
		$('slideshowNext').removeClassName('gray');
		$('slideshowNext').onclick = function(){stopAutomateSlide(); 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(slide_curslideId<0) slide_curslideId=0;  
         if(slide_curslideId>=slide_slideCount) slide_curslideId=slide_slideCount-1;         

       //  $('logo').innerHTML = slide_curslideId+" "+slideid+" "+slide_slideCount;         

          if((slide_curslideId>=0) && (slide_curslideId < slide_slideCount)){
            if((!slideid) && slide_curslideId < slide_slideCount-1){
              slideNext();
            }else if(-1 == slideid){
              if(slide_curslideId>0)
                slidePrev();
            } else{

                  while ((slideid-1)>slide_curslideId)
                    slideNext(); 
                  while ((slideid-1)<slide_curslideId)
                    slidePrev();
             }            

          }
          
          checkButtons();

}

function slideNext(){
            var clientTween = new fx.ElementScroll($$('.tx-cllicefaslideshow-pi1-listrow')[slide_curslideId], {duration: 600, transition: fx.sinoidal});
            clientTween.custom(0, -$$('.tx-cllicefaslideshow-pi1-listrow')[slide_curslideId].getWidth());
            if(slide_curslideId<slide_slideCount) slide_curslideId++;
}


function slidePrev(){
            slide_curslideId--;
            var clientTween = new fx.ElementScroll($$('.tx-cllicefaslideshow-pi1-listrow')[slide_curslideId], {duration: 600, transition: fx.sinoidal});
            clientTween.custom(-$$('.tx-cllicefaslideshow-pi1-listrow')[slide_curslideId].getWidth(), 0);
}



function automateslide(){
	if((slide_curslideId < (slide_slideCount-1)) && (slide_automate == true))
	slide_timeoutId = setTimeout(function(){slideNext();automateslide()}, 6000);
	if((slide_curslideId >= (slide_slideCount-1)) && (slide_automate == true))	{
		slide_timeoutId = setTimeout(function(){scrollslide(1); automateslide()}, 6000);
	}

}

function stopAutomateSlide(){
	slide_automate = false;
	if(slide_timeoutId) clearTimeout(slide_timeoutId);
}

