
menuPointSelector = "#mainmenu > li";

function hideGallery() {
   if (currentGalleryName == "others")
     $("#otherreferences").slideUp( function() {$("#gallery").hide();}); 
   else
     $("#gallery > a > img").hide("slow", function() {$("#gallery").hide();});
}

function createInitMenuLink( selector ) {
  $(selector).unbind("click").click(function() { $(menuPointSelector).slideDown(initMenu);}); 

}
function isOldIE() {
  return $.browser.msie == true && $.browser.version < 9;
}
	
function initMenu() {
    $(".contentarea").slideUp();
    $(".menuThumbnail").slideDown();
    if (currentGalleryName != "") {
        hideGallery();
        currentGalleryName = "";
    }
    $(menuPointSelector)
    .unbind("click")
    .click(function() { menuPointId = $(this).attr("id");
                         contentId = "#" + menuPointId.replace("menupoint", "content");
                         $(contentId).slideDown();
 
                         
                         $(".menuThumbnail").slideUp();
                         
                         $(menuPointSelector).not(this).slideUp();  
                         
                         createInitMenuLink(this);   
                                           
                         //$(this).unbind("click").click(function() { $(menuPointSelector).slideDown(initMenu);}); 
    });
}
    
$(function() {

  $(menuPointSelector)
    .css("cursor","pointer")
    .hover(function() { $(this).css("font-weight", "bold");},
           function() { $(this).css("font-weight", "normal");});
  initMenu();
  
  createInitMenuLink(".menuInitlink");
  
  //$("#menu > img").click(function() { $(menuPointSelector).show("slow",initMenu);});
  

  for (y = 0; y<3; y++) {
    for (x = 0; x<4; x++) {
      var i = x + y*4 + 1;
      $("#gallery").append(" <a rel='galleryLink' id='galleryLink"+i+"' href='#'> <img class=galleryImage src='p100.png'"
                        +" id='galleryImage"+i+"' style='position:absolute;left:"+x*110+"px;top:"+y*110+"px;'></a>");
    }
  }
   
    if (!isOldIE()) $(".menuThumbnail > img").corner();

    $('#menuThumbnail1').cycle({
      timeout: 6000,
      delay: -4000,
      fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    });
    $('#menuThumbnail2').cycle({
      timeout: 6000,
      delay: -2000,
      fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    });
    $('#menuThumbnail3').cycle({
      timeout: 6000,
      delay: 0,
      fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    }); 
    
    
   $('a[rel=galleryMenu]').each( function() {
    galleryGroupPath = $(this).attr("id").replace("galleryMenu_", "");
    addGalleryIntoPreloaderContainer(galleryGroupPath);
   });
    
    
   $('a[rel=galleryMenu]').click( function() {
    galleryGroupPath = $(this).attr("id").replace("galleryMenu_", "");
    
    
    $("#gallery").show();
    addGalleryImages(galleryGroupPath, $(this).html());
    
    
	  $('a[rel=galleryLink]').fancybox({
      cyclic: true,
	    transitionIn: 'elastic',
	    transitionOut: 'elastic',
	    titlePosition: 'inside',
	    titleFormat: function(title, currentArray, currentIndex, currentOpts){
	      return '<span id="fancybox-title-inside">' +(title.length ? '<br>' + title : '') + " (" + (currentIndex + 1) + '/' + currentArray.length +')</span>';
	    }
	  });
	  
  });
  
});

var currentGalleryName = "";

function addGalleryIntoPreloaderContainer(galleryName) {

  var path = "galleryimages/";
  var extensionThumb = "_thumb.jpg"; 
  
  for (y = 0; y<3; y++) {
    for (x = 0; x<4; x++) {
      var i = x + y*4 + 1;
      var id = "/"+i;
      $("#hiddenPreloaderContainer").append("<img src='"+path+galleryName+id+extensionThumb+"'>");
    }
  }
}

function addGalleryImages(galleryName, galleryTitle) {

  galleryTitle = galleryTitle.slice(22,-4); 
  
  if (currentGalleryName == galleryName)  return;
  if (currentGalleryName != galleryName) {
  
    if (currentGalleryName == "others")
      $("#otherreferences").slideUp(); 
    else
      $("#gallery > a > img").hide("slow");
  }
  currentGalleryName = galleryName;
  
  if (galleryName == "others") {
    $("#otherreferences").slideDown(function () { 
      
      
      //$("#otherreferences").jScrollPane();
      
      /*$("#otherreferences").smoothDivScroll({ 
			autoScroll: "onstart" , 
			autoScrollDirection: "backandforth", 
			autoScrollStep: 1, 
			autoScrollInterval: 15,	
			startAtElementId: "startAtMe", 
			visibleHotSpots: "always"
		  });*/
      
      //$("#otherreferences").smoothDivScroll();
      
  }); 
		
  } else {
  
    var path = "galleryimages/";
    var extensionThumb = "_thumb.jpg"; //"_1.gif";
    //var extensionThumbHover = ".jpg"; //"_2.gif";
    var extensionBig = ".jpg";  
    
    for (y = 0; y<3; y++) {
      for (x = 0; x<4; x++) {
        var i = x + y*4 + 1;
        var id = "/"+i;
        $("#galleryLink"+i).attr ("href",path+galleryName+id+extensionBig)
                            .attr ("title",galleryTitle);
        $("#galleryImage"+i).attr("alt",galleryTitle)
                            .attr ("src",path+galleryName+id+extensionThumb);
      } 
    }
    
    $("#gallery > a > img")
    .hover(
        function() { $(this).css("border", "1px solid #000000"); } , 
        function() { $(this).css("border", "none");} )
    //.corner()
    .show("slow");
    
    if (!isOldIE()) $("#gallery > a > img").corner();

  }

}

