var startSlideshowPos = 0;

$(function(){ initialiseScrollPane(); });

//scroll init
initialiseScrollPane = function(){
	$('.scroll-pane').jScrollPane({showArrows:true,scrollbarWidth:9});
}

$(document).ready(function(){
    
    resize();
    
    $('ul li a span, ul li a').click(function(){
    	resize();
    });
    
	//hide image description at start
	$('#bottomImageInfo').hide();
	//load first image description
	var firstTitle = $('#sideCarousel ul li:first a').attr('title');
	if(firstTitle){
		$('#bottomImageInfo').show();
		$('#bottomImageInfo').text(firstTitle);
	}

    $(".jScrollPaneContainer").css("visibility","visible");

   
    $(".productlevel1 ul").accordion({ 
	    navigation: true,
	    autoHeight: false,
	    active: false,
	    header: "a.productlevel2",                      
	    alwaysOpen: false
    });
          
    if($("#aboutparent li").hasClass("active")){
        $("#aboutmenu").show();
    }else{
        $("#aboutmenu").hide();
    }
    
    $("#pgparent").click(function() {
         $("ul.level1 li").removeClass("active");
         $(this).children("li").addClass("active");
         $("#aboutmenu").slideUp("medium");
         $("#productsmenu").slideUp("medium");
         $("#pgmenu").slideToggle("medium");
         return false;
    });
    
    if($("#pgparent li").hasClass("active")){
        $("#pgmenu").show();
    }else{
        $("#pgmenu").hide();
    }
    
    $("#productsparent").click(function() {
         $("ul.level1 li").removeClass("active");
         $(this).children("li").addClass("active");
         $("#aboutmenu").slideUp("medium");
         $("#pgmenu").slideUp("medium");
         $("#productsmenu").slideToggle("medium");
         return false;
    });
    
    $("#productsmenu").hide();
    $("#productsmenu li ul").hide();
    
	$("#productsmenu li.active").parent().show();
    
    $("ul#productsmenu").accordion({ 
        navigation: true,
        autoHeight: false,
        active: false,
        header: "a.productlevel1",                        
        alwaysOpen: false
    });
   
    accIndex = -1;
    accIndexCnt = 0;
    $("ul#productsmenu > li").each(function (i) {
        if ($(this).hasClass("active")) {
          accIndex = accIndexCnt;
        } 
        accIndexCnt++;
    });

    $("ul#productsmenu").accordion( 'activate' , accIndex );
    
    $("ul.level1 li").hover(
      function () {
        $(this).addClass("level1Gradient");
      }, 
      function () {
        $(this).removeClass("level1Gradient");
      }
    );
    
    //hide carousel if only one element
    if($('#sideCarousel ul li').length < 2){
    	$('#sideCarouselW').hide();
    }
    
    
    /*gallery*/
    $("#sideCarousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        vertical: true,
        visible: 8,
        mouseWheel: true,
        beforeStart: function(a) {

	    },
	    afterEnd: function(a) {
	        
	        	/*
		        current = a[0];
		        updateImg = $(current).children('a').attr('href');
		        $('.fullBg').attr('src', updateImg);
				
				$('#bottomImageInfo').show();
				var updateTitle = $(current).children('a').attr('title');
				if(updateTitle){
					$('#bottomImageInfo').show();
					$('#bottomImageInfo').text(updateTitle);
				}else{
					$('#bottomImageInfo').hide();
				}
				*/
				
		}
	    
  
    }); 

    
    $("a.thumbFixVert").click(function () { 

        //changeBgImgAndTitle($(this));
        $('img.fullBg').attr('src',$(this).attr("href"));
        
		updateTitle = $(this).attr("title");
		if(updateTitle){
			$('#bottomImageInfo').show();
			$('#bottomImageInfo').text(updateTitle);
		}else{
			$('#bottomImageInfo').hide();
		}
		
        $("a.thumbFixVert").removeClass("selected");
        $(this).addClass("selected");
    	
    	if($('#homeSideGal').length){
    		 
    		 qIndex = $('#homeSideGal ul li').index($(this).parents('li'));
			 startSlideshowPos = qIndex + 1
    		 //console.log(qIndex);
    	}
    	
    	return false;
    });
    
    
	$("img.changeBg").mouseover(function () { 
        $(this).addClass("highlight");
    }).mouseout(function(){
        $(this).toggleClass("highlight");
    });
    
    
    if($('#homeSideGal').length){
    	
    	var noOfImages = $('#homeSideGal ul li').length;
    	
    	//preload images
    	$('a.thumbFixVert').each(function() {
			
			$('<img src="' + $(this).attr("href") + '"/>').appendTo('#cache');

		});

    	$.periodic({period: 10000, decay: 1}, function(){
	    	
	    	var currentThumb = $('#homeSideGal a.thumbFixVert:eq(' + startSlideshowPos + ')');
	    	
	    	$('img.fullBg').attr('src',currentThumb.attr("href"));
	    	
	    	$("a.thumbFixVert").removeClass("selected");
        	currentThumb.addClass("selected");
        			
	    	startSlideshowPos = startSlideshowPos + 1;
	    	if(startSlideshowPos > (noOfImages - 1)){
	    		startSlideshowPos = 0;
	    	}
	    	
	    });    
    }
    
});


$(window).resize(function(){
    resize(); 
});


$(window).load(function(){
	resize()
});

function resize(){
	$('#contentWrapper').width($(window).width() - 260);
	$('#bgImgWrp .fullBg').width($(window).width() - 255);
}

/*
function changeBgImgAndTitle(element){

	$('img.fullBg').attr('src',this.attr("href"));
	
	updateTitle = element.attr("title");
	if(updateTitle){
		$('#bottomImageInfo').show();
		$('#bottomImageInfo').text(updateTitle);
	}else{
		$('#bottomImageInfo').hide();
	}
	
	$("a.thumbFixVert").removeClass("selected");
	element.addClass("selected");
	
}
*/