/*
iFrame redirect functions
**************/

    String.prototype.format = function() {
        var pattern = /\{\d+\}/g;
        var args = arguments;
        return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
    }
    function getQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split('&');
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split('=');
            if (pair[0] == variable) {
                return pair[1];
            }
        } 
        alert('Query Variable ' + variable + ' not found');
    }

	    function existsQueryVariable(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split('&');
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split('=');
            if (pair[0] == variable) {
                return true;
            }
        } 
        return false;
    }
/*
IMAGE SCROLLER
**************/

doScroll = function(){
	viewLeft = viewLeft - 1;
	viewWrap.css("left", viewLeft);
	if (viewLeft == -(viewWidth)) {
		viewLeft = 0;
		viewWrap.children(".scroll:first-child").clone().appendTo(viewWrap);
		viewWrap.children(".scroll:first-child").remove();
	}
}

function autoScroll(id){
	var container = jQuery("#" + id);
	container.removeClass("scrollImages");
	viewWidth = 0;
	container.children(".scroll").children("img").each(function(){
		viewWidth = viewWidth + $(this).width() + 21;
		$(this).css("top", -((67 - $(this).height()) / 2))
	});
	viewHeight = container.height();
	container.children(".scroll").wrap($('<div class="viewWrap"></div>'));
	viewWrap = container.find(".viewWrap");
	viewWrap.css({width: viewWidth*2, height: viewHeight});
	viewWrap.children(".scroll").clone().appendTo(viewWrap);
	viewWrap.children(".scroll").css({float: "left", width: viewWidth});
	viewLeft = 0;
	setInterval("doScroll()", 75);
}




/*
PULL DOWN MENUS
**************/

var menuTimer, prevMainMenu, prevSubMenu;

function showSubMenu(e){
	//Dconsole.log('showSubMenu');

	e.className = e.className.replace(/firstInList[^_]?/g, " firstInList_selected ");
	e.className += ' selected ';

	newSubMenu = $(e).children('UL')[0];

	clearTimeout(menuTimer);
	timeoutMenu(prevMainMenu == e);

	if (newSubMenu){
		newSubMenu.style.left = '0';
		var links = $(newSubMenu).find('a').each(function(){
			$(this).mouseover(function(){
				rollOverSubMenu(this);
			});
			$(this).mouseout(function(){
				rollOutSubMenu(this);
			});
		});
	}

	// The submenu that was previously here
	prevSubMenu = newSubMenu;
	prevMainMenu = e;
}

function hideSubMenu(e){
	//Dconsole.log('hideSubMenu');
	newSubMenu = $(e.parentNode).find('UL')[0];
	if (newSubMenu){
		clearTimeout(menuTimer);
		menuTimer = setTimeout("timeoutMenu()", 1000);
	} else {
		timeoutMenu();
	}
}

function timeoutMenu(keepMainOver){
	//Dconsole.log('timeoutMenu');
	if (prevSubMenu) { prevSubMenu.style.left = '-9999px'; }
	if (prevMainMenu && !keepMainOver){ rollOut(prevMainMenu);}
}

function rollOverSubMenu(){
	//Dconsole.log('rollOverSubMenu');
	clearTimeout(menuTimer);
}

function rollOutSubMenu(){
	//Dconsole.log('rollOutSubMenu');
	clearTimeout(menuTimer);
	menuTimer = setTimeout("timeoutMenu()", 1000);
}

function rollOver(e){
	//Dconsole.log('rollOver');
	if (e.className.indexOf('selected') == -1){
		if (prevMainMenu) { rollOut(prevMainMenu); }
		prevMainMenu = e;

		el.className = el.className.replace(/firstInList[^_]?/g, " firstInList_selected ");
		this.className += ' selected ';
	}
}

function rollOut(el){
	//Dconsole.log(el.className);
	el.className = el.className.replace(/(firstInList)?_?selected/g, "$1");
}




/*
BANNER SLIDER
**************/

doFadeIn = function(){
	imageViewer.find("li:first").next().animate({opacity: 1}, 800, "swing");
	imageViewer.find("li:first").clone().appendTo(imageViewer);
	imageViewer.find("li:first").remove();
}

doFadeOut = function(){
	imageViewer.find("li:first").animate({opacity: 0}, 800, "swing", function(){
		doFadeIn();
	});
}

jQuery.fn.autoFade = function(){
  var container = $(this);
	container.removeClass("jQslider").addClass("jQstripView");
	pictWidth = container.find("img").width();
	pictHeight = container.find("img").height();
	pictEls = container.find("li").size();
	z = pictEls;
	container.find("li").each(function(){
		$(this).css({zIndex: z, opacity: 0});
		z--;
	});
	imageViewer = container.find("ul");
	imageViewer.css("width", pictWidth);
	imageViewer.css("height" , pictHeight);
	imageViewer.find("li:first-child").animate({opacity: 1}, 1500, "swing");
	if (container.find("li").length < 2) { return; }
	setInterval("doFadeOut()", 6000);
}




/*
INITIALIZE
**************/
$(window).load(function(){
	// Init home page logos' scroller.
	if (document.getElementById('homeLeft')) {
		autoScroll('homeLeft');
	}

	// Init home banners' slider
	if (document.getElementById('imagesSet')) {
		$("#imagesSet").autoFade();
	}

});

$(document).ready(function(){

	// Init top menu rollovers
	if (document.getElementById('mainMenu')){
		$("#mainMenu > ul").children().each(function(){
			$(this).mouseover(function(){
				showSubMenu(this);
			});
			$(this).mouseout(function(){
				hideSubMenu(this);
			});
		});
	}

	// Init excerpt collapse / expand.
	if (document.getElementById('excerpt')){
		$("#excerpt a, #textBody a").click(function(e){
			el = $("#textBody");
			if (el.is('.expandContent')){
				el.hide();
				el.removeClass('expandContent');
			}
			el.slideToggle();
			e.preventDefault()
		});
	}

	$(".zipField input").focus(function(){
		if ($(this).val() == "Enter ZIP code here") {
			$(this).val("");
		} else {
			$(this).select();
		}
		$(this).blur(function(){
			if ($(this).val() == "") {
				$(this).val("Enter ZIP code here");
			}
		});
	})

});
	

