/* 

Author: Benoit Nolens
Date: 2009-09-17

Fueled by jQuery

*/

// Note: No conflict function

// This function must be called after including the jQuery javascript file, but before including any other conflicting library, 
// and also before actually that other conflicting library gets used, in case jQuery is included last. noConflict can be called at 
// the end of the jQuery.js file to globally disable the $() jQuery alias. jQuery.noConflict returns a reference to jQuery, so it 
// can be used to override the $() alias of the jQuery object.
// Use jQuery via jQuery(...)

//jQuery.noConflict();

var BASE_URL = "http://www.shootingpartridge.com";

// Slideshow variables

var transitionSpeed = 1;
var activeTime = 3.1;

//////////////////////

var activeMenu;

$(document).ready(function(){
        
    initContent();    
  	initMenu();  
 	initSWFObject();
 	initExternalLinks();
 	initHideBtn();
 	
 });
 
 function initExternalLinks()
 {
 	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});

 }
 
 function initHideBtn()
 {
 	$("#hide_btn").click(function(){
 		if($('#content_alt').height() > 0)
 		{
 			$(".jScrollPaneContainer").slideUp(600, function(){$("#hide_btn a").text("SHOW")});
 			$("#content_alt").animate({height:"0px"}, {queue:false, duration:600});

 		}
 		else
 		{
 			$(".jScrollPaneContainer").slideDown(600, function(){$("#hide_btn a").text("HIDE")});
 			$("#content_alt").animate({height:$(window).height() - 360}, {queue:false, duration:600});
 		}
 		
 		return false;
 	});
 }

// This function handles all the menu stuff
function initMenu()
{
	fixSubmenus();
	
	// show first submenu (if its not empty) when page loads
	if(($("#submenuContainer >*").eq(0).children().length) > 0)
	{
		$("#submenuContainer #s1").show();
	}
	
	// show the active submenu when menu looses focus
	$("#background").mouseover(function() {
		clearSubmenus();
		$("#submenuContainer .show").show();
	});
	
	// for every menu item
	$("#menu li a").each(function(index){
		
		// on mouseOver the menu item
		$("#menu li a").eq(index).mouseover(function(){
			
			clearSubmenus();
			
			// show the right submenu, if it's not empty
			if(($("#submenuContainer >*").eq(index).children().length) > 0)
			{
				$("#submenuContainer >*").eq(index).show();
			}
			
			// remove all white hover arrows
			$("#menu li a").each(function(index2){
				if( ($(this).parent().hasClass("subactive") == true) && (index2 != index) )
				$(this).parent().removeClass("subactive");
			})
			
			// add white hover arrow on hovered menu item
			if( ($(this).parent().hasClass("subactive") == false) && ($(this).parent().hasClass("active") == false) )
				$(this).parent().addClass("subactive");
		});
		
		// remove action on menu item click if there is not a submenu
		$("#menu li a").eq(index).click(function(){
			if(($("#submenuContainer >*").eq(index).children().length) > 0)
			{
				return false;
			}
		});
	});
	
	
}

// split the long submenus into multiple colums
function fixSubmenus()
{
	$("#submenuContainer >*").each(function(index)
	{
		// if there're more than 6 submenu items, then split it!
		if(($("#submenuContainer >*").eq(index).children().size()) > 6)
		{	
			// is this submenu hidden or not?
			var displayStatus = $("#submenuContainer >*").eq(index).attr('class').split(" ")[1];
			
			var wrapper = "<div id='s"+(index+1)+"' class='"+displayStatus+"'>";
			var colum1 = "<ul class='submenu'>";
			var colum2 = "<ul class='submenu'>";
			var colum3 = "<ul class='submenu'>";
			
			$("#submenuContainer >*").eq(index).children().each(function(index3){
				
				// is this menu item active or not?
				var displayStatus2 = $("#submenuContainer >*").eq(index).children().eq(index3).attr('class');
				
				if(index3 <= 5)
				{
					colum1 += "<li class='"+displayStatus2+"'>"+$("#submenuContainer >*").eq(index).children().eq(index3).html() + "</li>";
				}
				else if((index3 > 5) && (index3 <= 11))
				{
					colum2 += "<li class='"+displayStatus2+"'>"+$("#submenuContainer >*").eq(index).children().eq(index3).html() + "</li>";
				}
				else if(index3 > 11)
				{
					colum3 += "<li class='"+displayStatus2+"'>"+$("#submenuContainer >*").eq(index).children().eq(index3).html() + "</li>";
				}
			});
			
			colum1 += "</ul>";
			colum2 += "</ul>";
			colum3 += "</ul>";
			wrapper += colum1 + colum2 + colum3 + "</div>";
			
			$("#submenuContainer >*").eq(index).replaceWith(wrapper);
		}
	});
}

// hide all the submenus
function clearSubmenus()
{
	$("#submenuContainer >*").hide();
}

var isResizing = false;

function initContent()
{
	$('#content').attr('id', 'content_alt');

	setContentHeight();
	$(window).bind('resize', setContentHeight);
	
	
	// cross browser hack
	setContentHeight();
	setContentHeight();
	
	setTimeout(function(){
		$(window).trigger('resize');
		$(document).trigger('resize');
		//alert('resize');
	}, 1000);

}

function setContentHeight()
	{
		if(!isResizing && $("#hide_btn a").text() == "HIDE")
		{	
			isResizing = true;
			var contentHeight = $(window).height() - 360;
			$('#content').css('height', contentHeight);
			$('#content_alt').css('height', contentHeight);
			$('.jScrollPaneContainer').css('height', contentHeight);
			$('#scroll-pane').jScrollPane({scrollbarMargin:25, scrollbarWidth:13});
			isResizing = false;
		}
	}

function initSWFObject()
{
	var imgs = [];
	
	$("#background img").each(function(index)
	{
		imgs.push($("#background img").eq(index).attr("src"));
	});
	
	var flashvars = {};
	flashvars.images = imgs;
	flashvars.transitionSpeed = transitionSpeed; // in seconds
	flashvars.activeTime = activeTime;	// in seconds
	var params = {};
	params.menu = false;
	params.wmode = 'transparent';
	var attributes = {};
	attributes.id = 'SP_SlideShow';
	attributes.name = 'SP_SlideShow';
	swfobject.embedSWF(BASE_URL + "/flash/SP_SlideShow.swf", "background", "100%", "100%", "10.0.0", "js/expressInstall.swf", flashvars, params, attributes);
}
