$(function(){
    initSlideshow();
});

var curSelectedIndex = 0;
var slideShowCount = slideShowLeft.length;
var slideHandle = null;

function initSlideshow()
{
    $('#slideshownavbar').hide();
    loadImg();
    selectSlide(0);
    for(i=0;i<slideShowCount;i++)
        $('#slideshownavbar').append('<div></div>');
    $('#slideshownavbar div').html('<img src="images/trans.gif" />');
    $('#slideshownavbar div').each(function(n){
        $(this).bind('mouseover',function(){
            $(this).addClass('selected');
        }).bind('mouseout',function(){
            if( n!= curSelectedIndex )
                $(this).removeClass('selected');
        }).bind('click',function(){
            slideShowStop();
            selectSlide(n);
        });        
    });
    
    setImagePostion();
    $(window).resize(function(){
    		setImagePostion();
    	});
    
    $('#btn_span').css('cursor','pointer');
    
    $('#slideLeft2').attr('src',slideShowLeft[0]);
    $('#slideLeft1').attr('src',slideShowLeft[0]);
    $('#slideRight2').attr('src',slideShowRight[0]);
    $('#slideRight1').attr('src',slideShowRight[0]);
    
    slideShowStart();
    selectSlide(0);    
}
function loadImg()
{
    for(i=0;i<slideShowCount;i++)
    {
        $('#slideshowhide').append('<img src="'+ slideShowLeft[i] + '" />' );
        $('#slideshowhide').append('<img src="'+ slideShowRight[i] + '" />' );
    }
}
function selectSlide(iIndex)
{
   $('#slideshownavbar div').each(function(n){
        $(this).removeClass('selected');
        if( n==iIndex )    
        {
           curSelectedIndex=n;
           $(this).addClass('selected');            
           
            $('#slideLeft2').attr('src',slideShowLeft[iIndex]);
            $('#slideLeft1').fadeOut('slow',function(){
            	$(this).attr('src',$('#slideLeft2').attr('src')).fadeIn('slow');}
            );
            
            $('#slideRight2').attr('src',slideShowRight[iIndex]);
            $('#slideRight1').fadeOut('slow',function(){
            	$(this).attr('src',$('#slideRight2').attr('src')).fadeIn('slow');}
            );  
            
            if( $.trim(slideLinkLeft[n]) == '' )
            {
                $('#slideLeft1').unbind('click');
                $('#slideLeft1').css('cursor','default');
            }
            else
            {
                $('#slideLeft1').bind('click',function(){window.open( slideLinkLeft[n] );});
                $('#slideLeft1').css('cursor','pointer');
            }
            
            if( $.trim(slideLinkRight[n]) == '' )
            {
                $('#slideRight1').unbind('click');
                $('#slideRight1').css('cursor','default');
            }
            else
            {
                $('#slideRight1').bind('click',function(){window.open(slideLinkRight[n]);});
                $('#slideRight1').css('cursor','pointer');
            }
            
        }
   });   
}


function slide()
{
    if( curSelectedIndex > slideShowCount -2 )   
        curSelectedIndex = 0;
    else
        curSelectedIndex++;
    selectSlide(curSelectedIndex);
}

function setImagePostion()
{
	var position = $('#slideshowleft').offset();		
	$('.swappers1').css(position);
	
	position = $('#slideshowright').offset();
	$('.swappers2').css(position);
}



function slideShowStart()
{
	slideHandle = setInterval( 'slide()', slideShowTimer );   
	$('#btn_span').removeClass('play');
	$('#btn_span').addClass('stop'); 	
	$('#btn_span').unbind('click');
	$('#btn_span').bind('click',function(){
			slideShowStop();
	});
}
function slideShowStop()
{
	clearInterval(slideHandle);
	$('#btn_span').removeClass('stop');
	$('#btn_span').addClass('play'); 	
	$('#btn_span').unbind('click');
	$('#btn_span').bind('click',function(){
			slideShowStart();
	});
}
