var vignetGalleries = "";
var vignetteImages = "";
var currentImage = "";
var currentGalleryIndex = 0;
var galleryScript =  "";


function goNext()
{
    $.clipregion.next();
}

function goPrev()
{
    $.clipregion.prev();
}

function nextGallery ()
{
  //alert ( "before: currentGalleryIndex: " + currentGalleryIndex )
  if ( ((currentGalleryIndex) + 1 <= currentGalleryIndex < 1 ) && currentGalleryIndex < vignetGalleries.length - 1 )
  {
     currentGalleryIndex = currentGalleryIndex + 1 ;
       //alert ( "after: currentGalleryIndex: " + currentGalleryIndex )
     changeMainVignetteImage ( vignetGalleries[ currentGalleryIndex ] );
     if ( ( currentGalleryIndex - 1 ) < ( vignetGalleries.length - 5 ))
       $.clipregion.setIdx(currentGalleryIndex - 1);
  }

}

function previousGallery () 
{ 
  //alert ( "before: currentGalleryIndex: " + currentGalleryIndex )
  if ( (currentGalleryIndex) - 1 >= 0 )
  {
      currentGalleryIndex = currentGalleryIndex - 1 ;
     // alert ( "after: currentGalleryIndex: " + currentGalleryIndex )
      changeMainVignetteImage ( vignetGalleries[ currentGalleryIndex ] );
      if (( currentGalleryIndex - 1 ) < ( vignetGalleries.length - 5 ))
        $.clipregion.setIdx(currentGalleryIndex - 1);
  }
}
$(document).ready(function() {

    currentGalleryIndex = 0; 
   // changeMainVignetteImage ( vignetGalleries[ currentGalleryIndex ] );
    loadVignettes();

    $('#tab-videos-main').bind("click", function()
    {
        $('#tab-videos-main').attr("src", $('#tab-videos-main').attr("src").replace("-off", "-on"));
        $('#tab-videos-gallery').attr("src", $('#tab-videos-gallery').attr("src").replace("-on", "-off"));

        $("#video-videos").show();
        $("#videos-photoGallery").hide();
        //Updated 2011-09-16 by Tiago Dakuzaku | LELAK web+design
		window.location.reload();
		//var videos = [];
		//window.addVideo = function(SWFObject) {videos.push(SWFObject);}
		//videos[0].write("video-embed");
    });

    $('#tab-videos-gallery').bind("click", function()
    {
		
		//Updated 2011-09-16 by Tiago Dakuzaku | LELAK web+design
		//Destroys the video player
		$('#video-embed').empty();
		
        $('#tab-videos-main').attr("src", $('#tab-videos-main').attr("src").replace("-on", "-off"));
        $('#tab-videos-gallery').attr("src", $('#tab-videos-gallery').attr("src").replace("-off", "-on"));

        $("#video-videos").hide();
        $("#videos-photoGallery").show();
				

		$('#goNext').bind("click", function()
         {
            $.clipregion.next();
        });

        $('#goPrev').bind("click", function()
         {
            $.clipregion.prev();
        });

    });

    $('#clipregion-photoGallery').clipregion({
        'itemHeight':110,
        'itemWidth':109,
        'totalItems':$('ul > li', $('#clipregion')).size(),
        'itemsToShow':5,
        'itemStep':true
    });

  $("#videos-main-img").html (""); 
   try
       {
            $("#videos-main-img").html ( vignHTML ) ;
       }
       catch(err)
       {
           $("#videos-main-img").html ( "<br/><br/><h2>This service is currently unavailable. Please try again soon.</h2>" ) ;
       }

});

function loadVignettes()
{
    var imageGalleryPhotoHTML = "";
    var imagePath = "";

    imageGalleryPhotoHTML = '<ul class="clip-list">'
         
    for (var i = 0; i < vignetteImages.length; i++)
    {
        var vignImage = vignetteImages[i];
        imagePath = vignImage.imagePath.replace("/", "\/");

        imageGalleryPhotoHTML = imageGalleryPhotoHTML + '<li><a id="img'+ i  +'" href="#" onclick="changeMainVignetteImage(\''+ vignImage.galleryId + '\' ); currentGalleryIndex = ' + i +  '"><img src="' + imagePath + '" width="104" height="104" border="0"\/><\/a>'
        imageGalleryPhotoHTML = imageGalleryPhotoHTML + '<\/li>';
        $("#clipregion-photoGallery").html(imageGalleryPhotoHTML);
    }

    imageGalleryPhotoHTML = imageGalleryPhotoHTML + '<\/u>';
}

function changeMainVignetteImage ( galleryId )
{
    setGalleryScript ( galleryId )
 }

function setGalleryScript ( galleryId )
{
  
    $.getScript(kohlerServer + "/services/vignettes/getHytecvignette.jsp?id=" + galleryId + "&btnpos=3&verbose=1&vign_opento=dnlt&srctp=HYTC&maxX=600&maxY=600", function() {

       try
       {
            $("#videos-main-img").html ( vignHTML ) ;
       }
       catch(err)
       {
           $("#videos-main-img").html ( "<h2>This service is currently unavailable. Please try again soon.</h2>" ) ;       
       }
    })


}

var videos = [];
window.addVideo = function(swfObject) {
    videos.push(swfObject);
}


$(document).ready(function() {
    $("#clipregion-videos li:gt(4)").hide();

    $("#video-videos .prev a").click(function() {
        slideScroll('right');
        return false;
    });
    $("#video-videos .next a").click(function() {
        slideScroll('left');
        return false;
    });
    $("#video-videos .videos-paging-left").click(function() {
        showVideo(videoIndexShowing - 1);
        return false;
    });
    $("#video-videos .videos-paging-right").click(function() {
        showVideo(videoIndexShowing + 1);
        return false;
    });
    $("#clipregion-videos a").click(function() {
        var index = $("#clipregion-videos a").index(this);
        showVideo(index);
        return false;
    });

    var videoCount = $("#clipregion-videos li").size();
    function slideScroll(direction) {
        if (direction == 'left') {
            setScroll(scrollPos + 1);
        } else {
            setScroll(scrollPos - 1);
        }
    }

    var scrollPos = 0;
    function setScroll(startingIndex) {
        if (startingIndex < 0 || startingIndex + 5 > videoCount) {
            return;
        }
        $("#clipregion-videos li").css("display", "none");
        $("#clipregion-videos li:gt(" + (startingIndex - 1) + ")").filter("li:lt(5)").css("display", "");
        scrollPos = startingIndex;
    }

    var videoIndexShowing = 0;
    function showVideo(index) {
        if (index < 0 || index >= videoCount) {
            return;
        }
        $("#video-videos #video-embed").html("");
        videos[index].write("video-embed");
        $("#clipregion-videos li img").removeClass("active");
        $("#clipregion-videos li:eq(" + index + ") img").addClass("active");

        $("#video-videos .videos-description").removeClass("active");
        $("#video-videos .videos-description").eq(index).addClass("active");

        // Set the scroll position
        if (index < scrollPos) {
            setScroll(index);
        }
        if (index > scrollPos + 4) {
            setScroll(index - 4);
        }
        videoIndexShowing = index;
    }

    showVideo(0);
});

