/*
 * jQuery 1.2.6 - New Wave Javascript
 *
 * Copyright (c) 2008 John Resig (jquery.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2008-05-24 14:22:17 -0400 (Sat, 24 May 2008) $
 * $Rev: 5685 $
 */

/* Global Shite */
var iDelay = 750;
var pPrevious = false;
var home = false;
var iTimeout;

function hideAllMenus() {
	clearTimeout(iTimeout);
	$("span").removeClass("widget");
	$("ul.sn-wrap").removeClass("sub-show");
}

function showMenu(pTarget) {
	clearTimeout(iTimeout);
	$("span", pTarget).addClass("widget");
	$("ul.sn-wrap", pTarget).addClass("sub-show");
}

function hideMenu(pTarget) {
	clearTimeout(iTimeout);
	$("span", pTarget).removeClass("widget");
	$("ul.sn-wrap", pTarget).removeClass("sub-show");
}


/* JQuery Stuff */
$(function(){
	
	/* handle popup windows */
	$("a[rel='lightbox']").click(function(e) {
		e.preventDefault();
		var href = $(this).attr('href');
		var wname = $(this).attr('rel');
		window.open(href, wname, "status=0,toolbar=0,location=0,width=830,height=750");
	});
	$("a[rel='alternate']").click(function(e) {
		e.preventDefault();
		var href = $(this).attr('href');
		var wname = $(this).attr('rel');
		window.open(href, wname, "status=0,toolbar=0,location=0,width=830,height=650");	
	});
	
	
	/* search_input */
	$("div.search_input input").focus(function() {
		var frmVal = $(this).val();
		if((frmVal == "")||(frmVal == searchText)) {
			$(this).val("");
			$(this).removeClass("grey_text");
		}
	});
	$("div.search_input input").blur(function() {
		var frmVal = $(this).val();
		if(frmVal == "") {
			$(this).val(searchText);
			$(this).addClass("grey_text");
		}
	});
	
	/* menu handling */
	$("ul.mn-wrap > li > a").append("<span class='spacer'>&nbsp;</span>");
	$("ul.mn-wrap li").mouseover(
		function() {
			clearTimeout(iTimeout);
			hideMenu(pPrevious);
			pPrevious = this;
			showMenu(this);
		}
	);
	
	$("ul.mn-wrap li").mouseout(
		function() {
			clearTimeout(iTimeout);
			iTimeout = setTimeout("hideAllMenus();", iDelay);
		}
	);	
	
	/* sub-menu handling */
	$("ul.mn-wrap li ul.sn-wrap li").hover(
		function() { 
			clearTimeout(iTimeout);
			$(this).addClass("mnu-active");
		},
		function() {
			clearTimeout(iTimeout);
			$(this).removeClass("mnu-active");
		});
		
	$("ul.mn-wrap li ul.sn-wrap li a").mouseout (
		function() {
			clearTimeout(iTimeout);
			iTimeout = setTimeout("hideAllMenus()", iDelay);
	});
	
	/* Logo Fading */
	if(home) {
		var fadeLogos = true;
		$.each($.browser, function(i, val) {
			if(i=="msie" && val == true) {
				if($.browser.version.substr(0,3)<="6.0") {
					fadeLogos = false;
				}
			}
		});
		
		//fadeLogos = true;
		
		function animNext() {
			//alert("test");
			if(Current == -1) {
				Current = 0;
				CurrentLoop = 0;
				var PrevLogoOne = logoList.get(logoList.length -1);
				var PrevLogoTwo = logoList.get(logoList.length -2);
				var PrevLogoThree = logoList.get(logoList.length -3);
			} else {
				var PrevLogoOne = logoList.get(Current - 1);
				var PrevLogoTwo = logoList.get(Current - 2);
				var PrevLogoThree = logoList.get(Current - 3);			
			}
			var NextLogoOne = logoList.get(Current);
			var NextLogoTwo = logoList.get(Current + 1);
			var NextLogoThree = logoList.get(Current + 2);
			$(PrevLogoOne).hide();
			$(PrevLogoTwo).hide();
			$(PrevLogoThree).hide();
			
			if(fadeLogos){
				$(NextLogoOne).fadeIn(1000);
				$(NextLogoTwo).fadeIn(1500);
				$(NextLogoThree).fadeIn(2000);
			}else{
				$(NextLogoOne).show();
				$(NextLogoTwo).show();
				$(NextLogoThree).show();
			}
			Current += 3;
			CurrentLoop++;
			if(Current >= logoList.length) {
				Current = -1;
			}
			$("div.home ul#featured-clients").animate({opacity: 1}, 4000, "swing", animNext);
		}
		
		//if(fadeLogos) {
			$("div.home ul#featured-clients li").hide();
			var logoList = $("div.home ul#featured-clients").children();
			var Current = -1;
			var CurrentLoop = 0;
			var FullLoops = logoList.length / 3;
			if(logoList.length % 3 != 0) {
				FullLoops = Math.floor(FullLoops);
				//alert(FullLoops+' - '+logoList.length % 3);
			}
			animNext();
		//}
	}
		
});