/**
 * Ajax links
 * need to show loading bar - need to center that
 * Load content into a custom div, not the scroll div
 * Handle the following links:
 * category
 * Item
 * link from Explore deeper
 * Image gallery categories
 * 
 * When clicking image gallery categories hide the product category links and show the image category links
 */
var gallerySideNav;
var galleryLink;
var imageGalleryLink;
var galleryHomeLayoutId = 0;
var imageGalleryHomeLayoutId = 0;
var imageGalleryHomeNonAjaxLayoutId = 0;
var categoryLayoutId = 0;
var imageGalleryCategoryLayoutId = 0;
var productLayoutId = 0;

$(function(){
	gallerySideNav = $('#gallerySideNav');
    galleryLink = $('#productGalleryLink');
    imageGalleryLink = $('#imageGalleryLink');
    var galleryHref = galleryLink.attr('href');
    
	setupProduct()
	setupImageGalleryLightbox();
    setupGalleryLinks();
    
    
    imageGalleryLink.click(function() {
    	makeImageGalleryCurrent();
    	handleGalleryLinkClick(this, galleryHref + '?lid=' + imageGalleryHomeLayoutId, function() {setupImageGalleryLightbox(); setupGalleryLinks();});
    	return false;
    });
    galleryLink.click(function() {
    	makeGalleryCurrent();
    	handleGalleryLinkClick(this, galleryHref + '?lid=' + galleryHomeLayoutId, setupGalleryLinks);
    	return false;
    });
    // Make the Image Gallery active if it's linked to directly
    var s = window.location.search;
    if (s.length > 0) {
    	var p = s.split('=');
    	if (p[1] != undefined && p[1] == imageGalleryHomeNonAjaxLayoutId) {
    		makeImageGalleryCurrent();
    	}
    }
});

function makeGalleryCurrent() {
	imageGalleryLink.removeClass('current');
	galleryLink.addClass('current');
	$('a.imageGalleryCategoryAjaxLink', gallerySideNav).each(function() {
		$(this).removeClass('imageGalleryCategoryAjaxLink').addClass('categoryAjaxLink');
	});
	setupCategoryAjaxLinks();
}
function makeImageGalleryCurrent() {
	galleryLink.removeClass('current');
	imageGalleryLink.addClass('current');
	$('a.categoryAjaxLink', gallerySideNav).each(function() {
		$(this).removeClass('categoryAjaxLink').addClass('imageGalleryCategoryAjaxLink');
	});
	setupImageGalleryLightbox();
	setupImageGalleryCategoryAjaxLinks();
}

function setupImageGalleryLightbox() {
	$("a.fancyImage").each(function(){
	      var frameWidth = 760;
	      var frameHeight = 630;
	      $(this).fancybox({
	        overlayShow: true,
	        overlayOpacity: 0,
	        frameWidth: frameWidth,
	        frameHeight: frameHeight,
	        padding: 0,
	        hideOnContentClick: false,
	        centerOnScroll: false,
	        callbackOnShow: function(){
	          if (setupImageGalleryLightboxProductAjaxLinks) {
	        	  setupImageGalleryLightboxProductAjaxLinks('#fancy_div');
	          }
	        }
	      });
	  });
}
function setupProduct() {
	if($.browser.msie && ($.browser.version.substr(0,1) == "6" || $.browser.version.substr(0,1) == "7")){
		  $("#product-accordion").accordion({ 
		    autoHeight: false,
		    change: function(){
		      $('.product-accordion-content', '#product-accordion').each(function(){
		        $(this).css('visibility','visible');
		      });
		      initScrollers();
		      resetScrollers();
		    },
		    changestart: function(){
		      $('.product-accordion-content', '#product-accordion').each(function(){
		        $(this).css('visibility','hidden');
		      });
		    }
		  });
		  $('h3.product-accordion-header', '#product-accordion').click(function(){
		     $('a', this).click();
		     return false;
		  });
	  }else{
		  $("#product-accordion").accordion({ 
		    autoHeight: false,
		    change: function(){
		      initScrollers();
		      resetScrollers();
		    }
		  });
	  }
	  $('h3.product-accordion-header').each(function(){
	     $(this).click(function(){
	       this.blur();
	     });     
	  });
	  $('a:not(.videoLink)', '#product-thumbs').each(function(i){ 
	    $(this).click(function(){
	      $('img', '#product-window').hide();
	      $('#videoPreview', '#product-window').hide();
	      $('img', '#product-window').eq(i).show();
	      return false;
	    });
	  });  
	  $('a.videoLink', '#product-thumbs').each(function(i){ 
	    $(this).click(function(){
	      $('img', '#product-window').hide();
	      $('#videoPreview', '#product-window').show();
	      return false;
	    });
	  });
	  makeGalleryCurrent();
}

function setupGalleryLinks(element) {
	element = element || 'body';
	setupCategoryAjaxLinks(element);
	setupProductAjaxLinks(element);
	setupImageGalleryCategoryAjaxLinks(element);
}
function setupCategoryAjaxLinks(element) {
	$('a.categoryAjaxLink', $(element)).each(function() {
		var path = getPath(this) + '?lid=' + categoryLayoutId;
		loadGalleryContent(this, path, makeGalleryCurrent);
	});
}
function setupImageGalleryCategoryAjaxLinks(element) {
	$('a.imageGalleryCategoryAjaxLink', $(element)).each(function() {
		var path = getPath(this) + '?lid=' + imageGalleryCategoryLayoutId;
		loadGalleryContent(this, path, makeImageGalleryCurrent);
	});
}
function setupProductAjaxLinks(element) {
	$('a.productAjaxLink', $(element)).each(function() {
		var path = getPath(this) + '?lid=' + productLayoutId;
		loadGalleryContent(this, path, setupProduct);
	});
}
function setupImageGalleryLightboxProductAjaxLinks(element) {
	$('a.productAjaxLink', $(element)).each(function() {
		var path = getPath(this) + '?lid=' + productLayoutId;
		loadGalleryContent(this, path, setupProduct, hideGalleryFancybox);
	});
}
function setupExploreDeeperProductAjaxLinks(element) {
	$('a.productAjaxLink', $(element)).each(function() {
		var path = getPath(this) + '?lid=' + productLayoutId;
		loadGalleryContent(this, path, showExploreDeeperTab, hideGalleryFancybox);
	});
}
function showExploreDeeperTab() {
	setupProduct();
	$('#explore-tab').show();
}
function hideGalleryFancybox() {
	$.fn.fancybox.close();
}
function getPath(link) {
	var path = link.pathname;
//    if(($.browser.msie && (($.browser.version.substr(0,1) == "6") || ($.browser.version.substr(0,1) == "7") || ($.browser.version.substr(0,1) == "8"))) || ($.browser.opera)){
	if($.browser.msie || $.browser.opera){
      // IE and Opera does not follow W3C standards, and they leave off the leading slash for anchor pathnames
    	path = '/' + path;
    }
    return path;
}
function loadGalleryContent(link, path, callback, clickCallback) {
	$(link).unbind('click').click(function() {
		handleGalleryLinkClick(link,path, callback, clickCallback);
		return false;
	});
}
function handleGalleryLinkClick(link, path, callback, clickCallback) {
	$('#galleryLoading').show();
	$('#contentContainer').hide().load(path, function(){
      $('#galleryLoading').hide();          
      $(this).show();
      initScrollers();
      resetScrollers();
      setupGalleryLinks(this);
      if($.browser.msie && ($.browser.version.substr(0,1) == "6")){
	      $('.gallery-small-icons td, .gallery-large-icons td').each(function(){
	    	if (!$(this).hasClass('noContent')) {
		      $(this).mouseenter(function(){
		        $(this).addClass('ie-hover');
		      }).mouseleave(function(){
		        $(this).removeClass('ie-hover');
		      });
	    	}
	      });
      }
      if (typeof callback == 'function') {
    	  callback.call(this);
      }
    });
	if (typeof clickCallback == 'function') {
		clickCallback.call(this);
	}
}

