var allenOrgan = {};
allenOrgan.duration = 250;
allenOrgan.theatreWidth = 1000;
allenOrgan.classicalWidth = 700; 
allenOrgan.isZoomed = [false, false, false];
allenOrgan.isClicked = [false, false, false];

allenOrgan.zoomIn = function() {
  var index = $("#organ-list li").index(this);
  $(this).clearQueue();
  
  if(!allenOrgan.isZoomed[index]) {
    allenOrgan.isZoomed[index] = true;
    $(this).find("img").effect("size", {origin: ['top', 'left'], to: {width: 319, height:348}}, allenOrgan.duration, function(){    
      
    });    
  }
};

allenOrgan.zoomOut = function() {
  
  var index = $("#organ-list li").index(this);
  
  if(allenOrgan.isZoomed[index] && !allenOrgan.isClicked[index]) {    
    allenOrgan.isZoomed[index] = false;

    // mouse out      
    $(this).find("img").effect("size", {origin: ['top', 'left'], to: {width: 310, height:338}}, allenOrgan.duration);    
    
    $("#organ-popup-" + (index + 1)).css({display: 'none'});    
  }
  
};

allenOrgan.click = function(event) {
  
  //console.log("#organ-list li click");
  event.preventDefault();
  event.stopPropagation();
  $(this).clearQueue();
  
  allenOrgan.close(); // Close any currently open instances  
  
  var index = $("#organ-list li").index(this);
  allenOrgan.clickedOrgan = index;
  
  $("#organ-list").find("li img").each(function(i, element){
    if(i != index) {
      $(element).effect("size", {origin: ['top', 'left'], to: {width: 310, height:338}}, allenOrgan.duration);
      allenOrgan.isZoomed[i] = false;
    }
  });
  
  allenOrgan.isClicked[index] = true;
    
  $("#organ-popup-" + (index + 1)).css({display: 'block'});
  
  $("a.close-button").bind("click", allenOrgan.close);
  

};

allenOrgan.close = function() {

  var index = allenOrgan.clickedOrgan;
  allenOrgan.isClicked[index] = false;
  $("#organ-popup-" + (index + 1)).css({display: 'none'});
  
  $("a.close-button").unbind("click");
  allenOrgan.clickedOrgan = -1; // Reset the state
};


$(document).ready(function(){


  
  $(".header-right a").click(function(event){
    event.preventDefault();
    $.fancybox.close();
  });

  // INNER PAGE CONTENT DISPLAY
  $("ul.tab-list li a").click(function(event){
    event.preventDefault();
    
    if($(this).parent().hasClass("active")) {
      // Hide
      $(this).parent().removeClass("active");
      
      $(this).siblings().hide();
    }
    else {
      // Show
      $(this).parent().addClass("active");
      
      $(this).siblings().show();
    }
    
  });
  
  
  // ORGAN SPOTLIGHT FUNCTIONALITY
  $("#organ-list li").hover(
    allenOrgan.zoomIn,
    allenOrgan.zoomOut
  );  
  
  $("#organ-list li").click(allenOrgan.click);
  
  
  // Add rounded corners on organ page
  $("div.inner-popup").corner();       
  
  allenOrgan.clickedOrgan = -1;
  
  $("a.organ-link").click(function(){
    $("#organ-list li img").clearQueue();  
  });
  
  var speedIn = 500;
  if($.browser.msie) {
    speedIn = 100;
  }
  
  var fancyboxStart = function() {	

   if(allenOrgan.clickedOrgan == 0) {
      // put the center organ on the left
      $("#left-organ").attr("src", "images/classical_default.png");
    }
    else if (allenOrgan.clickedOrgan == 2) {
      $("#right-organ").attr("src", "images/classical_default.png");
    }
    
    // Reset the organs back to their small size on close
    $("#organ-list li img").clearQueue();
    $("#organ-list li img").effect("size", {origin: ['top', 'left'], to: {width: 310, height:338}}, allenOrgan.duration);
                  
    
    $("#center-organ").css({'visibility': 'hidden'});
	
  };
  
  var fancyboxClose = function() {
    $("#left-organ").attr("src", "images/theatre_default.png");
    $("#right-organ").attr("src", "images/combination_default.png");
    $("#center-organ").css({'visibility': 'visible'});
    
    // Reset the organs back to their small size on close
    $("#organ-list li img").clearQueue();
    $("#organ-list li img").effect("size", {origin: ['top', 'left'], to: {width: 310, height:338}}, allenOrgan.duration);
    
    var modelsContainer = $("#models-container");
    modelsContainer.css({'display': 'none'});
    
    $("#models-holder").css({display:'none'});
  };
  
  $("a.close-button").click(function(event){
    event.preventDefault();
    $.fancybox.close();
  });
  
  var fancyboxOptions = {
    'hideOnContentClick': false,
		'hideOnOverlayClick': true,
		//'overlayColor': '#000',
		//'overlayOpacity': 0.88,
		'overlayOpacity': 0,
		'showCloseButton': false,
		'padding':0,
		'margin':1,
		'titleShow':false,
		'showNavArrows':false,
		'scrolling': 'no',
		'centerOnScroll':false,
		'speedIn': speedIn,
		'hideOnOverlayClick':false,
		'left': 0,
		'onStart': fancyboxStart,
    'onClosed': fancyboxClose
  };
  
  

	$("div.inner-popup .models").click(function(event) {
	 event.preventDefault();
	 
	 var modelsContainer = $("#models-container");
	 var modelsContent = $("#models-content");
	
	$("#models-holder").css({display:'block'});
	$("#models-container").css({display:'block'});
	
	$("#models-close").click(function(event){
	 event.preventDefault();
	 $("#models-container").css({'display': 'none'});
	 $("#models-holder").css({display:'none'});
  });
	
	 var index = -1;
   var left = 0;
   var headerImg = "";
   var width = 500;
   
   // The order of Theatre and Classical are reversed in this layout compared with the top menu
   // Since we are copying html from the top menu, get the correct menu item
   
   if(allenOrgan.clickedOrgan == 0) {
    index = 1;
    left = 15;
    headerImg = "images/theatre-organs.gif";
    width = allenOrgan.theatreWidth;
   } 
   else if(allenOrgan.clickedOrgan == 1) {
    index = 0;
    left = 245;    
    width = allenOrgan.classicalWidth;
    headerImg = "images/classical-organs.gif";
   } else {   
    headerImg = "images/combination-organs.gif";
    index = 2;
   }
   
   if($.browser.msie) {
    $(modelsContainer).css({'width': width}); // Fixed width is only necessary in IE
   }
   
   $(modelsContainer).find("#models-head-img").attr("src", headerImg);
   
   var content = $($(".drop-content")[index]).html();
   $(modelsContent).text("");
   $(modelsContent).append(content);
   
   $(modelsContent).children().css({'display': 'block'});
   
   $("#models-holder").css({'left': left});
   
   $(".slide-out").hide();
   
   $(modelsContent).find(".drop-list").css({'padding-left': 8});
   
   $(modelsContent).find('.drop-list > li').click(slideOutClick);   
   
   $(modelsContent).find(".organ-menu li a").click(organMenuClick);
	
   
  });

  var slideOutClick = function(event) {
    event.stopPropagation();
    var imgIndex = $(this).index();
    
    $("#inner-content .drop-content").eq(allenOrgan.index).clearQueue();  
    $(this).parents(".drop-list").clearQueue();
   
     // clear ALL slide-out queues
     $(".slide-out").clearQueue();
     $(".slide-out").delay(200);
    
    if($(this).parents().find('.slide-out').eq(imgIndex).is(":visible")) {
      
      // Hide
      $(this).parents().clearQueue();
      $(this).parents().find('.slide-out').eq(imgIndex).hide("blind", {direction: 'horizontal'}, allenOrgan.duration);
      
    
    } else {
      
      // Display
      if($.browser.msie){ //&& $("#models-container").css("display") == "block" ) {
        
        $(this).parent().clearQueue();
        
        var newWidth = 500;
        newWidth = 1000;
        
        var container = this;
        
        
        $(this).parent().animate({
            width: newWidth
            }, allenOrgan.duration * 2, function() {
              animateSlideOuts(container, imgIndex);
            });
                
      }
      else {
        animateSlideOuts(this, imgIndex);
      }
    }
  };
  
  var animateSlideOuts = function(scope, imgIndex) {
  //debugger
    
    // Close any open slide outs
    if($(scope).parent().find('.slide-out:visible').length > 0) {
      $(scope).parent().find('.slide-out:visible').hide("blind", {direction: 'horizontal'}, allenOrgan.duration, function() {
        
        // Display this one    
        $(scope).parent().find('.slide-out').eq(imgIndex).show("blind", {direction: 'horizontal'}, allenOrgan.duration, function() {
        
        var currentImage = $(scope).parents("ul.drop-list").parent().find('li a img').eq(imgIndex) 
        
        $(scope).parents("ul.drop-list").parent().find('li a img').not(currentImage).animate({
          opacity:0.3
        }, allenOrgan.duration / 4);    
        
        $(scope).parents("ul.drop-list").parent().find('li a img').eq(imgIndex).css({'opacity': 1});
        
        
        });
        
      });
    }
    else {
      // Display this one    
      $(scope).parent().find('.slide-out').eq(imgIndex).show("blind", {direction: 'horizontal'}, allenOrgan.duration, function() {
      
      var currentImage = $(scope).parents("ul.drop-list").parent().find('li a img').eq(imgIndex) 
      
      $(scope).parents("ul.drop-list").parent().find('li a img').not(currentImage).animate({
        opacity:0.3
      }, allenOrgan.duration / 4);    
      
      $(scope).parents("ul.drop-list").parent().find('li a img').eq(imgIndex).css({'opacity': 1});
      
      
      });
    }
    
  };
  
  var organMenuClick = function(event) {
    event.stopPropagation();
    event.preventDefault();
    
    if($(this).attr("href") != "#") {
        document.location.href = $(this).attr("href");
    } else if($(this).parents("ul.organ-menu-1").length > 0) {
      // Second level menu
      $("ul.organ-menu-1 li ul").hide();
    } else {
      // First level menu
      $("ul.organ-menu li ul").hide();
      $("ul.organ-menu li img.grey").attr("src", "images/bullet-grey-left.gif");
    }
    
    $("ul.organ-menu-1 li img.brown").attr("src", "images/bullet-brown-space.gif");
    $("ul.organ-menu-1 li a").removeClass("bold");
    
      if($(this).siblings("ul").is(":visible")) {
        $(this).siblings("ul").hide();
      } else {
        $(this).siblings("ul").show();
      }
      
      if($(this).siblings("img").hasClass("grey")) {
        if($(this).siblings("img").attr("src").indexOf("left") > -1) {
          $(this).siblings("img").attr("src", "images/bullet-grey-down.gif");
        }
        else {
          $(this).siblings("img").attr("src", "images/bullet-grey-left.gif");
        }
      }
      else if($(this).siblings("img").hasClass("brown")) {
        if($(this).siblings("img").attr("src").indexOf("left") > -1) {
          $(this).siblings("img").attr("src", "images/bullet-brown-space.gif");
        }
        else {
          $(this).siblings("img").attr("src", "images/bullet-brown-left.gif");
        }
      }
      
      if($(this).parents("ul.organ-menu-1").length > 0) {
        if($(this).hasClass("bold")) {
          $(this).removeClass("bold");
        }
        else {
          $(this).addClass("bold");
        }
        
      }
      
    }; 


	//all our click and hover handlers will go here
	$('.drop-menu-organ').hover(
		//mouse over function
		function(){
			//fires the animate function with a delay of 200 miliseconds.			
      showFirstSubMenu($('.drop-nav'), $('.drop'));
		},
		//mouse out function
		function(event){
		    hideFirstSubMenu($('.drop-nav'), $('.drop'));
			hideFirstSubMenumidi($('.drop-navmidi'), $('.dropmidi'));
			hideFirstSubMenuabout($('.drop-navabout'), $('.dropabout'));
			hideFirstSubMenunews($('.drop-navnews'), $('.dropnews'));
		}
		
	);
// added Deb Crowell 
	$('.drop-menu-midi').hover(
		//mouse over function
		function(){
			//fires the animate function with a delay of 200 miliseconds.			
      showFirstSubMenumidi($('.drop-navmidi'), $('.dropmidi'));
		},
		//mouse out function
		function(event){
		    hideFirstSubMenu($('.drop-nav'), $('.drop'));
			hideFirstSubMenumidi($('.drop-navmidi'), $('.dropmidi'));
			hideFirstSubMenuabout($('.drop-navabout'), $('.dropabout'));
			hideFirstSubMenunews($('.drop-navnews'), $('.dropnews'));
		}
		
	);
	$('.drop-menu-news').hover(
		//mouse over function
		function(){
			//fires the animate function with a delay of 200 miliseconds.			
      showFirstSubMenunews($('.drop-navnews'), $('.dropnews'));
		},
		//mouse out function
		function(event){
		    hideFirstSubMenu($('.drop-nav'), $('.drop'));
			hideFirstSubMenumidi($('.drop-navmidi'), $('.dropmidi'));
			hideFirstSubMenuabout($('.drop-navabout'), $('.dropabout'));
			hideFirstSubMenunews($('.drop-navnews'), $('.dropnews'));
		}
		
	);	
	$('.drop-menu-about').hover(
		//mouse over function
		function(){
			//fires the animate function with a delay of 200 miliseconds.			
      showFirstSubMenuabout($('.drop-navabout'), $('.dropabout'));
		},
		//mouse out function
		function(event){
		    hideFirstSubMenu($('.drop-nav'), $('.drop'));
			hideFirstSubMenumidi($('.drop-navmidi'), $('.dropmidi'));
			hideFirstSubMenuabout($('.drop-navabout'), $('.dropabout'));
			hideFirstSubMenunews($('.drop-navnews'), $('.dropnews'));
		}
		
	);		
	
	$('.drop-navmidi > li').hover(
	 function() {
	   // mouse over
	   $("#inner-contentmidi").clearQueue();	   
     $("#inner-contentmidi").show();
     
     allenOrgan.index = $(this).index();
     
     $("#inner-contentmidi .drop-contentmidi").hide();
     
     $("#inner-contentmidi .drop-contentmidi .slide-out").clearQueue();
     $("#inner-contentmidi .drop-contentmidi .slide-out").hide();
     $("#inner-contentmidi .drop-contentmidi").find('li a img').css({'opacity': 1});
     
     $("#inner-contentmidi .drop-contentmidi .drop-listmidi").hide();
	   
     $("#inner-contentmidi .drop-contentmidi").eq(allenOrgan.index).fadeIn(allenOrgan.duration);
     $("#inner-contentmidi .drop-contentmidi").eq(allenOrgan.index).show(allenOrgan.duration);
     $("#inner-contentmidi .drop-contentmidi .drop-listmidi").eq(allenOrgan.index).show();
	   $("#inner-contentmidi .drop-contentmidi").eq(allenOrgan.index).find('img').css({height:'auto', width:'auto'});	   
	   
	    // Inner image click
	    $("#inner-contentmidi .drop-contentmidi").eq(allenOrgan.index).find('.drop-listmidi li').click(function(event){
	     // Prevent events from bubbling up when clicking on sub-navigations
	     event.stopPropagation();
	    });
	    
      $("#inner-contentmidi .drop-contentmidi").eq(allenOrgan.index).find('.drop-listmidi > li').click(slideOutClick);
	   
     // Inner image hover

     // End inner image hover
	   
   },
	 function() {
	   // mouse out
	   var index = $(this).index();
     
   }
  );	
  
  
	$('.drop-navnews > li').hover(
	 function() {
	   // mouse over
	   $("#inner-contentnews").clearQueue();	   
     $("#inner-contentnews").show();
     
     allenOrgan.index = $(this).index();
     
     $("#inner-contentnews .drop-contentnews").hide();
     
     $("#inner-contentnews .drop-contentnews .slide-out").clearQueue();
     $("#inner-contentnews .drop-contentnews .slide-out").hide();
     $("#inner-contentnews .drop-contentnews").find('li a img').css({'opacity': 1});
     
     $("#inner-contentnews .drop-contentnews .drop-listnews").hide();
	   
     $("#inner-contentnews .drop-contentnews").eq(allenOrgan.index).fadeIn(allenOrgan.duration);
     $("#inner-contentnews .drop-contentnews").eq(allenOrgan.index).show(allenOrgan.duration);
     $("#inner-contentnews .drop-contentnews .drop-listnews").eq(allenOrgan.index).show();
	   $("#inner-contentnews .drop-contentnews").eq(allenOrgan.index).find('img').css({height:'auto', width:'auto'});	   
	   
	    // Inner image click
	    $("#inner-contentnews .drop-contentnews").eq(allenOrgan.index).find('.drop-listnews li').click(function(event){
	     // Prevent events from bubbling up when clicking on sub-navigations
	     event.stopPropagation();
	    });
	    
      $("#inner-contentnews .drop-contentnews").eq(allenOrgan.index).find('.drop-listnews > li').click(slideOutClick);
	   
     // Inner image hover

     // End inner image hover
	   
   },
	 function() {
	   // mouse out
	   var index = $(this).index();
     
   }
  );	  
  
	$('.drop-navabout > li').hover(
	 function() {
	   // mouse over
	   $("#inner-contentabout").clearQueue();	   
     $("#inner-contentabout").show();
     
     allenOrgan.index = $(this).index();
     
     $("#inner-contentabout .drop-contentabout").hide();
     
     $("#inner-contentabout .drop-contentabout .slide-out").clearQueue();
     $("#inner-contentabout .drop-contentabout .slide-out").hide();
     $("#inner-contentabout .drop-contentabout").find('li a img').css({'opacity': 1});
     
     $("#inner-contentabout .drop-contentabout .drop-listabout").hide();
	   
     $("#inner-contentabout .drop-contentabout").eq(allenOrgan.index).fadeIn(allenOrgan.duration);
     $("#inner-contentabout .drop-contentabout").eq(allenOrgan.index).show(allenOrgan.duration);
     $("#inner-contentabout .drop-contentabout .drop-listabout").eq(allenOrgan.index).show();
	   $("#inner-contentabout .drop-contentabout").eq(allenOrgan.index).find('img').css({height:'auto', width:'auto'});	   
	   
	    // Inner image click
	    $("#inner-contentabout .drop-contentabout").eq(allenOrgan.index).find('.drop-listabout li').click(function(event){
	     // Prevent events from bubbling up when clicking on sub-navigations
	     event.stopPropagation();
	    });
	    
      $("#inner-contentabout .drop-contentabout").eq(allenOrgan.index).find('.drop-listabout > li').click(slideOutClick);
	   
     // Inner image hover

     // End inner image hover
	   
   },
	 function() {
	   // mouse out
	   var index = $(this).index();
     
   }
  );	  
// end added Deb Crowell 	
	
	
	$('.drop-nav > li').hover(
	 function() {
	   // mouse over
	   $("#inner-content").clearQueue();	   
     $("#inner-content").show();
     
     allenOrgan.index = $(this).index();
     
     $("#inner-content .drop-content").hide();
     
     $("#inner-content .drop-content .slide-out").clearQueue();
     $("#inner-content .drop-content .slide-out").hide();
     $("#inner-content .drop-content").find('li a img').css({'opacity': 1});
     
     $("#inner-content .drop-content .drop-list").hide();
	   
     $("#inner-content .drop-content").eq(allenOrgan.index).fadeIn(allenOrgan.duration);
     $("#inner-content .drop-content").eq(allenOrgan.index).show(allenOrgan.duration);
     $("#inner-content .drop-content .drop-list").eq(allenOrgan.index).show();
	   $("#inner-content .drop-content").eq(allenOrgan.index).find('img').css({height:'auto', width:'auto'});	   
	   
	    // Inner image click
	    $("#inner-content .drop-content").eq(allenOrgan.index).find('.drop-list li').click(function(event){
	     // Prevent events from bubbling up when clicking on sub-navigations
	     event.stopPropagation();
	    });
	    
      $("#inner-content .drop-content").eq(allenOrgan.index).find('.drop-list > li').click(slideOutClick);
	   
     // Inner image hover

     // End inner image hover
	   
   },
	 function() {
	   // mouse out
	   var index = $(this).index();
     
   }
  );
  
  // Organ menu functions
  $(".organ-menu li a").click(organMenuClick);
	
});

function showFirstSubMenu(target, container){
	//if it was animating already, stop it.
	container.clearQueue();
	
  if(!$.browser.msie) {
  $("#inner-content").hide();
  
  // Fade in the container, and then when that is finished, show the target expand LEFT to RIGHT
  container.animate({
    opacity: 0
    },
    50,
    'easeInExpo'
  ).show(allenOrgan.duration, function(){
    
    container.css({opacity: 1});    
    target.show("blind", {direction: 'horizontal'}, allenOrgan.duration / 4);
        
  });
  
  }
  else {
    $(target).show();
    $(container).show();
  }
	
}

function hideFirstSubMenu(target, container){
  target.clearQueue();	
  
  // Reset the menu state
  $('.drop').find('.slide-out:visible').hide("blind", {direction: 'horizontal'}, allenOrgan.duration);
  $('.drop').find("ul.drop-list").find('li a img').css({'opacity': 1});
  $('.drop-content').css({display:'none'});  
  
  container.hide();	
	target.hide();
}


// added Deb Crowell
function showFirstSubMenunews(target, container){
	//if it was animating already, stop it.
	container.clearQueue();
	
  if(!$.browser.msie) {
  $("#inner-contentnews").hide();
  
  // Fade in the container, and then when that is finished, show the target expand LEFT to RIGHT
  container.animate({
    opacity: 0
    },
    50,
    'easeInExpo'
  ).show(allenOrgan.duration, function(){
    
    container.css({opacity: 1});    
    target.show("blind", {direction: 'horizontal'}, allenOrgan.duration / 4);
        
  });
  
  }
  else {
    $(target).show();
    $(container).show();
  }
	
}

function hideFirstSubMenunews(target, container){
  target.clearQueue();	
  
  // Reset the menu state
  $('.dropnews').find('.slide-out:visible').hide("blind", {direction: 'horizontal'}, allenOrgan.duration);
  $('.dropnews').find("ul.drop-listnews").find('li a img').css({'opacity': 1});
  $('.drop-contentnews').css({display:'none'});  
  
  container.hide();	
	target.hide();
}
/////
function showFirstSubMenuabout(target, container){
	//if it was animating already, stop it.
	container.clearQueue();
	
  if(!$.browser.msie) {
  $("#inner-contentabout").hide();
  
  // Fade in the container, and then when that is finished, show the target expand LEFT to RIGHT
  container.animate({
    opacity: 0
    },
    50,
    'easeInExpo'
  ).show(allenOrgan.duration, function(){
    
    container.css({opacity: 1});    
    target.show("blind", {direction: 'horizontal'}, allenOrgan.duration / 4);
        
  });
  
  }
  else {
    $(target).show();
    $(container).show();
  }
	
}

function hideFirstSubMenuabout(target, container){
  target.clearQueue();	
  
  // Reset the menu state
  $('.dropabout').find('.slide-out:visible').hide("blind", {direction: 'horizontal'}, allenOrgan.duration);
  $('.dropabout').find("ul.drop-listabout").find('li a img').css({'opacity': 1});
  $('.drop-contentabout').css({display:'none'});  
  
  container.hide();	
	target.hide();
}
//
function showFirstSubMenumidi(target, container){
	//if it was animating already, stop it.
	container.clearQueue();
	
  if(!$.browser.msie) {
  $("#inner-contentmidi").hide();
  
  // Fade in the container, and then when that is finished, show the target expand LEFT to RIGHT
  container.animate({
    opacity: 0
    },
    50,
    'easeInExpo'
  ).show(allenOrgan.duration, function(){
    
    container.css({opacity: 1});    
    target.show("blind", {direction: 'horizontal'}, allenOrgan.duration / 4);
        
  });
  
  }
  else {
    $(target).show();
    $(container).show();
  }
	
}

function hideFirstSubMenumidi(target, container){
  target.clearQueue();	
  
  // Reset the menu state
  $('.dropmidi').find('.slide-out:visible').hide("blind", {direction: 'horizontal'}, allenOrgan.duration);
  $('.dropmidi').find("ul.drop-listmidi").find('li a img').css({'opacity': 1});
  $('.drop-contentmidi').css({display:'none'});  
  
  container.hide();	
	target.hide();
}
// end Deb Crowell

// Original code
jQuery(function(){
	initPlugins();
	ieHover('.switcher li, .nav li div', 'hover');
});
// jquery ie6 hover pack
function ieHover(obj, actClass){
	var elements = obj;
	var hoverClass = actClass;
	if(jQuery.browser.msie && parseInt(jQuery.browser.version) == 6){
		jQuery(elements).hover(function(){
			jQuery(this).addClass(hoverClass);
		}, function(){
			jQuery(this).removeClass(hoverClass);
		});
	}
}

//slide drop-down function
function slideNavigation(){
	var duration = 500;
	var _holder = 'ul.nav > li';
	var _slider = 'div.drop';
	jQuery(_slider).each(function(){
		jQuery(this).hide();
	});
	jQuery(_holder).hover(function(){
		_holder = jQuery(this);
		if(!_holder.find(_slider).is(':animated')){
		
      _holder.find('.drop').css({display:'block'});      
      jQuery('.drop .holder').show("blind", {direction: 'vertical'}, duration);
		
		}
	}, function(){
		_holder = jQuery(this);
		
		
    jQuery('.drop .holder').hide("blind", {direction: 'vertical'}, duration, function() {
      _holder.find('.drop').css({display:'none'});
    });
    
   	});
	jQuery(_slider).hover(function(){
		jQuery(this).attr('flag', 'hover');
	}, function(){
		jQuery(this).attr('flag', '');
	});
}

//slide drop-down function
function slideSubNavigation(){
	var duration = 500;
	var _parent = jQuery('ul.nav div.content');
	var _holder = 'ul.drop-nav li';
	var _slider = 'div.drop-content';
	
	jQuery(_slider).each(function(){
		jQuery(this).hide();
	});
	jQuery(_holder).hover(function(){
		_holder = jQuery(this);
		if(!_parent.find(_slider).eq(_holder.index()).is(':animated')){
			
		}
	}, function(){
		_holder = jQuery(this);	
		 

		
	});
	jQuery(_slider).hover(function(){
		jQuery(this).attr('flag', 'hover');
		_holder.addClass('active');
	}, function(){
		jQuery(this).attr('flag', '');
		_holder.removeClass('active');
	});
}

// init plugins function
function initPlugins(){
	jQuery('div.block-gallery').scrollGallery({
		sliderHolder: 'div.holder',
		switchTime:1000, 
		duration:100,
		autoRotation:true
	});
	jQuery('ul.tabset').jqueryTabs({
		addToParent:true,
		holdHeight:false,
		activeClass:'active',
		tabLinks:'a.tab',
		fadeSpeed:0
	});
	jQuery('ul.subnav li').OpenClose({
		activeClass:'active',
		opener:'>a.opener',
		slider:'>ul.slide',
		slideSpeed: 400
	});
	jQuery('a.has-popup').simplebox();
}

(function(jQuery) {
	jQuery.fn.simplebox = function(options) { 
		return new Simplebox(this, options); 
	};
	
	function Simplebox(context, options) { this.init(context, options); };
	
	Simplebox.prototype = {
		options:{},
		init: function (context, options){
			this.options = jQuery.extend({
				duration: 300,
				linkClose: 'a.close, a.btn-close',
				divFader: 'fader',
				faderColor: 'black',
				opacity: 0.7,
				wrapper: '#wrapper',
				linkPopap: '.link-submit'
			}, options || {});
			this.btn = jQuery(context);
			this.select = jQuery(this.options.wrapper).find('select');
			this.initFader();
			this.btnEvent(this, this.btn);
		},
		btnEvent: function(jQuerythis, el){
			el.click(function(){
				if (jQuery(this).attr('href')) jQuerythis.toPrepare(jQuery(this).attr('href'));
				else jQuerythis.toPrepare(jQuery(this).attr('title'));
				return false;
			});
		},
		calcWinWidth: function(){
			this.winWidth = jQuery('body').width();
			if (jQuery(this.options.wrapper).width() > this.winWidth) this.winWidth = jQuery(this.options.wrapper).width();
		},
		toPrepare: function(obj){
			this.popup = jQuery(obj);
			this.btnClose = this.popup.find(this.options.linkClose);
			this.submitBtn = this.popup.find(this.options.linkPopap);
			
			if (jQuery.browser.msie) this.select.css({visibility: 'hidden'});
			this.calcWinWidth();
			this.winHeight = jQuery(window).height();
			this.winScroll = jQuery(window).scrollTop();
			
			this.popupTop = this.winScroll + (this.winHeight/2) - this.popup.outerHeight(true)/2;
			if (this.popupTop < 0) this.popupTop = 0;
			this.faderHeight = jQuery(this.options.wrapper).outerHeight();
			if (jQuery(window).height() > this.faderHeight) this.faderHeight = jQuery(window).height();
			
			this.popup.css({
				top: this.popupTop,
				left: this.winWidth/2 - this.popup.outerWidth(true)/2
			}).hide();
			this.fader.css({
				width: this.winWidth,
				height: this.faderHeight
			});
			this.initAnimate(this);
			this.initCloseEvent(this, this.btnClose, true);
			this.initCloseEvent(this, this.submitBtn, false);
			this.initCloseEvent(this, this.fader, true);
		},
		initCloseEvent: function(jQuerythis, el, flag){
			el.click(function(){
				jQuery('body > div.outtaHere').removeClass('optionsDivVisible').addClass('optionsDivInvisible')
				jQuerythis.popup.fadeOut(jQuerythis.options.duration, function(){
					jQuerythis.popup.css({left: '-9999px'}).show();
					if (jQuery.browser.msie) jQuerythis.select.css({visibility: 'visible'});
					jQuerythis.submitBtn.unbind('click');
					jQuerythis.fader.unbind('click');
					jQuerythis.btnClose.unbind('click');
					jQuery(window).unbind('resize');
					if (flag) jQuerythis.fader.fadeOut(jQuerythis.options.duration);
					else {
						if (jQuerythis.submitBtn.attr('href')) jQuerythis.toPrepare(jQuerythis.submitBtn.attr('href'));
						else jQuerythis.toPrepare(jQuerythis.submitBtn.attr('title'));
					}
				});
				return false;
			});
		},
		initAnimate:function (jQuerythis){
			jQuerythis.fader.fadeIn(jQuerythis.options.duration, function(){
				jQuerythis.popup.fadeIn(jQuerythis.options.duration);
			});
			jQuery(window).resize(function(){
				jQuerythis.calcWinWidth();
				jQuerythis.popup.animate({
					left: jQuerythis.winWidth/2 - jQuerythis.popup.outerWidth(true)/2
				}, {queue:false, duration: jQuerythis.options.duration});
				jQuerythis.fader.css({width: jQuerythis.winWidth});
			});
		},
		initFader: function(){
			if (jQuery(this.options.divFader).length > 0) this.fader = jQuery(this.options.divFader);
			else{
				this.fader = jQuery('<div class="'+this.options.divFader+'"></div>');
				jQuery('body').append(this.fader);
				this.fader.css({
					position: 'absolute',
					zIndex: 999,
					left:0,
					top:0,
					background: this.options.faderColor,
					opacity: this.options.opacity
				}).hide();
			}
		}
	}
}(jQuery));

// scrolling gallery plugin
jQuery.fn.scrollGallery = function(_options){
	var _options = jQuery.extend({
		sliderHolder: '>div',
		slider:'>ul',
		slides: '>li',
		pagerLinks:'div.pager a',
		generatePagination:'div.paging',
		paginationUlClass:'switcher',
		switcherClasses:'',
		switcherClassTo:'a',
		btnPrev:'a.link-prev',
		btnNext:'a.link-next',
		activeClass:'active',
		disabledClass:'disabled',
		switcherClassName:'switcher',
		curNum:'em.scur-num',
		allNum:'em.sall-num',
		circleSlide:true,
		pauseClass:'gallery-paused',
		pauseButton:'none',
		pauseOnHover:true,
		autoRotation:false,
		stopAfterClick:false,
		switchTime:5000,
		duration:650,
		easing:'swing',
		event:'click',
		splitCount:false,
		afterInit:false,
		vertical:false,
		step:false
	},_options);

	return this.each(function(){
		// gallery options
		var _this = jQuery(this);
		var _sliderHolder = jQuery(_options.sliderHolder, _this);
		var _slider = jQuery(_options.slider, _sliderHolder);
		var _slides = jQuery(_options.slides, _slider);
		var _generatePagination = jQuery(_options.generatePagination, _this);
		var _paginationUlClass = _options.paginationUlClass;
		var _switcherClasses = _options.switcherClasses;
		var _switcherClassTo = _options.switcherClassTo;
		var _btnPrev = jQuery(_options.btnPrev, _this);
		var _btnNext = jQuery(_options.btnNext, _this);
		var _pagerLinks = jQuery(_options.pagerLinks, _this);
		var _switcherClassName = _options.switcherClassName;
		var _generatePagination = jQuery(_options.generatePagination, _this);
		var _curNum = jQuery(_options.curNum, _this);
		var _allNum = jQuery(_options.allNum, _this);
		var _pauseButton = jQuery(_options.pauseButton, _this);
		var _pauseOnHover = _options.pauseOnHover;
		var _pauseClass = _options.pauseClass;
		var _autoRotation = _options.autoRotation;
		var _activeClass = _options.activeClass;
		var _disabledClass = _options.disabledClass;
		var _easing = _options.easing;
		var _duration = _options.duration;
		var _switchTime = _options.switchTime;
		var _controlEvent = _options.event;
		var _step = _options.step;
		var _vertical = _options.vertical;
		var _circleSlide = _options.circleSlide;
		var _stopAfterClick = _options.stopAfterClick;
		var _afterInit = _options.afterInit;
		var _splitCount = _options.splitCount;
		
		if(_switcherClasses){_switcherClasses = _switcherClasses.split((/\s*,\s*/));}
		
		// gallery init
		if(!_slides.length) return;

		var _switcherIndex = 0;

		if(_generatePagination.length) {
			_generatePagination.empty();
			var list = jQuery('<ul class="'+_paginationUlClass+'">');
			var classTagLi = '';
			var classTagA = '';
			for(var i=0; i<_slides.length; i++){
				if(_switcherClasses.length && _switcherClassTo == 'li'){
					classTagLi = ' class="'+_switcherClasses[_switcherIndex]+'"';
				}else if(_switcherClasses.length && _switcherClassTo == 'a'){
					classTagA = ' class="'+_switcherClasses[_switcherIndex]+'"';
				}
				jQuery('<li'+classTagLi+'><a'+classTagA+' href="#">'+(i+1)+'</a><div class="popup"><div class="popup-holder"><img src="'+_slides.eq(i).attr('rel')+'" width="149" height="80"></div></div></li>').appendTo(list);
				if(_switcherIndex < _switcherClasses.length -1){_switcherIndex++;}else{_switcherIndex = 0;}
			}
			list.appendTo(_generatePagination);
			_pagerLinks = list.children();
		}

		if(_splitCount) {
			var curStep = 0;
			var newSlide = $('<slide>').addClass('split-slide');
			_slides.each(function(){
				newSlide.append(this);
				curStep++;
				if(curStep > _splitCount-1) {
					curStep = 0;
					_slider.append(newSlide);
					newSlide = $('<slide>').addClass('split-slide');
				}
			});
			if(curStep) _slider.append(newSlide);
			_slides = _slider.children();
		}
		
		var _currentStep = 0;
		var _sumWidth = 0;
		var _sumHeight = 0;
		var _hover = false;
		var _stepWidth;
		var _stepHeight;
		var _stepCount;
		var _offset;
		var _timer;

		_slides.each(function(){
			_sumWidth+=$(this).outerWidth(true);
			_sumHeight+=$(this).outerHeight(true);
		});

		// calculate gallery offset
		function recalcOffsets() {
			if(_vertical) {
				if(_step) {
					_stepHeight = _slides.eq(_currentStep).outerHeight(true);
					_stepCount = Math.ceil((_sumHeight-_sliderHolder.height())/_stepHeight)+1;
					_offset = -_stepHeight*_currentStep;
				} else {
					_stepHeight = _sliderHolder.height();
					_stepCount = Math.ceil(_sumHeight/_stepHeight);
					_offset = -_stepHeight*_currentStep;
					if(_offset < _stepHeight-_sumHeight) _offset = _stepHeight-_sumHeight;
				}
			} else {
				if(_step) {
					_stepWidth = _slides.eq(_currentStep).outerWidth(true)*_step;
					_stepCount = Math.ceil((_sumWidth-_sliderHolder.width())/_stepWidth)+1;
					_offset = -_stepWidth*_currentStep;
					if(_offset < _sliderHolder.width()-_sumWidth) _offset = _sliderHolder.width()-_sumWidth;
				} else {
					_stepWidth = _sliderHolder.width();
					_stepCount = Math.ceil(_sumWidth/_stepWidth);
					_offset = -_stepWidth*_currentStep;
					if(_offset < _stepWidth-_sumWidth) _offset = _stepWidth-_sumWidth;
				}
			}
		}

		// gallery control
		if(_btnPrev.length) {
			_btnPrev.bind(_controlEvent,function(){
				if(_stopAfterClick) stopAutoSlide();
				prevSlide();
				return false;
			});
		}
		if(_btnNext.length) {
			_btnNext.bind(_controlEvent,function(){
				if(_stopAfterClick) stopAutoSlide();
				nextSlide();
				return false;
			});
		}
		if(_pagerLinks.length) {
			_pagerLinks.each(function(_ind){
				jQuery(this).bind(_controlEvent,function(){
					if(_currentStep != _ind) {
						if(_stopAfterClick) stopAutoSlide();
						_currentStep = _ind;
						switchSlide();
					}
					return false;
				});
			});
		}

		// gallery animation
		function prevSlide() {
			recalcOffsets();
			if(_currentStep > 0) _currentStep--;
			else if(_circleSlide) _currentStep = _stepCount-1;
			switchSlide();
		}
		function nextSlide() {
			recalcOffsets();
			if(_currentStep < _stepCount-1) _currentStep++;
			else if(_circleSlide) _currentStep = 0;
			switchSlide();
		}
		function refreshStatus() {
			if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentStep).addClass(_activeClass);
			if(!_circleSlide) {
				_btnPrev.removeClass(_disabledClass);
				_btnNext.removeClass(_disabledClass);
				if(_currentStep == 0) _btnPrev.addClass(_disabledClass);
				if(_currentStep == _stepCount-1) _btnNext.addClass(_disabledClass);
			}
			if(_curNum.length) _curNum.text(_currentStep+1);
			if(_allNum.length) _allNum.text(_stepCount);
		}
		function switchSlide() {
			recalcOffsets();
			if(_vertical) _slider.animate({marginTop:_offset},{duration:_duration,queue:false,easing:_easing});
			else _slider.animate({marginLeft:_offset},{duration:_duration,queue:false,easing:_easing});
			refreshStatus();
			autoSlide();
		}

		// autoslide function
		function stopAutoSlide() {
			if(_timer) clearTimeout(_timer);
			_autoRotation = false;
		}
		function autoSlide() {
			if(!_autoRotation || _hover) return;
			if(_timer) clearTimeout(_timer);
			_timer = setTimeout(nextSlide,_switchTime+_duration);
		}
		if(_pauseOnHover) {
			_this.hover(function(){
				_hover = true;
				if(_timer) clearTimeout(_timer);
			},function(){
				_hover = false;
				autoSlide();
			});
		}
		recalcOffsets();
		refreshStatus();
		autoSlide();

		// pause buttton
		if(_pauseButton.length) {
			_pauseButton.click(function(){
				if(_this.hasClass(_pauseClass)) {
					_this.removeClass(_pauseClass);
					_autoRotation = true;
					autoSlide();
				} else {
					_this.addClass(_pauseClass);
					stopAutoSlide();
				}
				return false;
			});
		}

		if(_afterInit && typeof _afterInit === 'function') _afterInit(_this, _slides);
	});
}

// jquery tabs plugin
jQuery.fn.jqueryTabs = function(_options){
	// default options
	var _options = jQuery.extend({
		addToParent:false,
		holdHeight:false,
		activeClass:'active',
		tabLinks:'a.tab',
		fadeSpeed:300,
		event:'click'
	},_options);

	return this.each(function(){
		var _holder = jQuery(this);
		var _fadeSpeed = _options.fadeSpeed;
		var _activeClass = _options.activeClass;
		var _addToParent = _options.addToParent;
		var _holdHeight = _options.holdHeight;
		var _tabLinks = jQuery(_options.tabLinks, _holder);
		var _tabset = (_addToParent ? _tabLinks.parent() : _tabLinks);
		var _event = _options.event;
		var _animating = false;

		// tabs init
		_tabLinks.each(function(){
			var _tmpLink = jQuery(this);
			var _tmpTab = jQuery(_tmpLink.attr('href'));
			var _classItem = (_addToParent ? _tmpLink.parent() : _tmpLink);
			if(_tmpTab.length) {
				if(_classItem.hasClass(_activeClass)) _tmpTab.show();
				else _tmpTab.hide();
			}
		});

		// tab switcher
		function switchTab(_switcher) {
			if(!_animating) {
				var _link = jQuery(_switcher);
				var _newItem = (_addToParent ? _link.parent() : _link);
				var _newTab = jQuery(_link.attr('href'));
				if(_newItem.hasClass(_activeClass)) return;

				var _oldItem = jQuery(_addToParent ? _tabset : _tabLinks).filter('.'+_activeClass);
				var _oldTab = jQuery(jQuery(_addToParent ? _oldItem.children('a') : _oldItem).attr('href'));
				if(_newTab.length) {
					_animating = true;
					if(_oldItem.length) {
						_newItem.addClass(_activeClass);
						_oldItem.removeClass(_activeClass);

						var _parent = _oldTab.parent();
						if(_holdHeight) _parent.css({height:_parent.height()});

						_oldTab.fadeOut(_fadeSpeed,function(){
							_newTab.fadeIn(_fadeSpeed,function(){
								_animating = false;
							});
							if(_holdHeight) _parent.css({height:'auto'});
						});
					} else {
						_newItem.addClass(_activeClass);
						_newTab.fadeIn(_fadeSpeed,function(){
							_animating = false;
						});
					}
				}
			}
		}

		// control
		_tabLinks.each(function(){
			jQuery(this).bind(_event,function(){
				switchTab(this);
				return false;
			});
		});
	});
}

// open-close plugin
jQuery.fn.OpenClose = function(_options){
	// default options
	var _options = jQuery.extend({
		activeClass:'active',
		opener:'.opener',
		slider:'.slide',
		slideSpeed: 400,
		animStart:false,
		animEnd:false,
		event:'click'
	},_options);

	return this.each(function(){
		// options
		var _holder = jQuery(this);
		var _slideSpeed = _options.slideSpeed;
		var _activeClass = _options.activeClass;
		var _opener = jQuery(_options.opener, _holder);
		var _slider = jQuery(_options.slider, _holder);
		var _animStart = _options.animStart;
		var _animEnd = _options.animEnd;
		var _event = _options.event;
		if(_slider.length) {
			_opener.bind(_event,function(){
				if(!_slider.is(':animated')) {
					if(typeof _animStart === 'function') _animStart();
					if(_holder.hasClass(_activeClass)) {
						_slider.slideUp(_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
						_holder.removeClass(_activeClass);
					} else {
						_holder.addClass(_activeClass);
						_slider.slideDown(_slideSpeed,function(){
							if(typeof _animEnd === 'function') _animEnd();
						});
					}
				}
				return false;
			});
			if(_holder.hasClass(_activeClass)) _slider.show();
			else _slider.hide();
		}
	});
}




	

