/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/
/*	File: 				main.js
/*	Description:		Contains all master javascript code.
/*	Target browsers:	IE6+, Mozilla/Firefox, Safari, Opera
/*	Author(s):			J. Jackson | jjackson@jplcreative.com
/*	Created: 			01.06.2010
/*	Last modified: 		01.06.2010
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	*/

/* +++++ custom scripts +++++ */
/* +++++ require jquery 1.3.2 +++++++++++++++++++++++++++++++++++++++++ */
$(document).ready(function(){
	
	// all pages
	// show mega drop downs on <li> element hover
	var default_offset = $("li.dd-menu:first").offset();
	$("li.dd-menu").each(function(i) {
		var div_len = $(this).children('div').children('div').length
		// count inner divs and set container width
		if ( div_len == 1 ) { $(this).children('div').addClass('cols-1'); }
		else if ( div_len == 2 ) { $(this).children('div').addClass('cols-2'); }
		else if ( div_len == 3 ) { $(this).children('div').addClass('cols-3'); }
		else if ( div_len == 4 ) { $(this).children('div').addClass('cols-4'); }
		// inner divs match parent div height
		var div_height = $(this).children('div').height();
		$(this).children('div').children('div').height( div_height );
	});
	function addMenu(){ $(this).addClass("hover") }
	function removeMenu(){ $(this).removeClass("hover") }
	var menuConfig = {
		interval: 125,
		sensitivity: 4,
		over: addMenu,
		timeout: 125,
		out: removeMenu
	};
	$("li.dd-menu").hoverIntent(menuConfig)
	
	// assign dynamic class
	$('li.products div').children('div:last').addClass('last');
	
	// sIRF 2.0.7 text replacements
	// require sifr.js
	if(typeof sIFR == "function"){
		sIFR();
		// navigation
		sIFR.replaceElement("#nav h3 span", named({ sFlashSrc:"/lib/swf/nav-headings.swf", sWmode:"transparent", sColor:"#ffffff", sHoverColor:"#ffffff", sLinkColor:"#ffffff", sFlashVars:"textalign=center" }) );
		
		setTimeout('$("ul#nav").addClass("loaded")',1500);
		
		// h1 page titles
		sIFR.replaceElement(".pg-content h1", named({ sFlashSrc:"/lib/swf/h1-headings.swf",  sWmode:"transparent", sColor:"#cc3205", sHoverColor:"#cc3205", sLinkColor:"#cc3205" }) );
	};
	
	// homepage
	// rotate right sidebar images
	if ( $('body').hasClass('home') ) { assignImg() }
	function assignImg(){
		var img_num = Math.floor(Math.random() * $('.img-rotation img').length)
		// assign appropriate classes to li elements
		$('.img-rotation ul li:eq(' + img_num + ')').addClass('show');
	}
	
	// homepage
	// explore callout rollover
	$('.explore a[href="#explore"]').hover(
		function() {
			$('.explore h2').fadeOut('fast', function() { $('.explore ul').fadeIn('fast') });
		},
		function() { }
	);
	$('.explore ul').hover(
		function() { },
		function() {
			$('.explore ul').fadeOut('fast', function() { $('.explore h2').fadeIn('fast') });
		}
	);
	
	// products
	// vertically align column links based on height
	if ($('#outer').hasClass('products')) {
		var max_a_ht = 0;
		
		$("div#details dl").each(function(i) {
		   var a = $(this).children('dt').children('a:last').height();
		   if ( a > max_a_ht ) { max_a_ht = a }
		});
		
		function matchHts() {
			$("div#details dl").each(function(i) {
		  		var a = $(this).children('dt').children('a:last').height();
		   		if ( a == max_a_ht && max_a_ht > 22 ) {
					$(this).children('dt').children('a:last').addClass('multi-line');
				}
			});
		}
		matchHts();
	}
	$('.products div.pg-content div#details dl:last').addClass('last');
	
	// news & events
	// assign paging elements to match comp requirements
	$('.news .pager').each(function(i) {
			$(this).children('td').children("table").children("tbody").children("tr").children("td:first").prepend('<label>Page: </label>')
	});
	assignPaging();
	function assignPaging() {
		var asp_pager = $('.pager:first td').html();
		$('.news span.paging').html(asp_pager);
		$('.news .pager:first').html(' ');
	}
	// contact us
	// create overlay for interactive rep locator
	$('a[href="#locate-rep"]').click( function() { createOverlay() });
});

function createOverlay() {
	// create elements, assign class, append to body
	var overlay = document.createElement("div");
	var scroll_position = $(window).scrollTop();
	var locator_left = ($(window).width()/2)-480;
	$(overlay).addClass('overlay').width( $(window).width() + 'px').height($(document).height() + 'px').appendTo('body');
	// hide sifr text & select elements
	$('.sIFR-replaced *, select').fadeOut('fast', function() {
		// fade in overlay
		$(".overlay").fadeIn('fast', function() {
			// fade in rep locator
			$('.rep-locator').css('left',locator_left + 'px').show();
		});
	});
	// assign flash content
	assignFlash();
}
function removeOverlay() {
	// remove overlay elements from DOM
	$('#flash-rep').remove();
	$('.overlay').remove();
	$('.rep-locator').hide();
	// show sifr text & select elements
	$('.sIFR-replaced *, select').fadeIn('fast')
	// recreate empty flash div
	var flash_div = document.createElement('div');
	$(flash_div).attr('id','flash-rep').appendTo('.rep-locator');
}

