/* Global Script for Smiggle */

function openPopup( url, name, width, height, resizable, scrollbars, menubar, status ) {
	var winLeft = (screen.width - width)/2;  	//Auto position popup in center of screen
	var winTop = (screen.height - height)/2;	//Auto position popup in center of screen
	window.open( url, name, ('width=' + width + ', height=' + height + ', resizable='+ resizable +', scrollbars='+ scrollbars +', menubar='+ menubar +', status='+ status +', top=' + winTop + ', left=' + winLeft )); 
	return false;
}

function popup( url, width, height ) {
	var winLeft = (screen.width - width)/2;  	//Auto position popup in center of screen
	var winTop = (screen.height - height)/2;	//Auto position popup in center of screen
	var newwindow = window.open( url, name, ('width='+width+', height='+height+', resizable=yes, scrollbars=yes, menubar=no, status=yes, top='+winTop+', left='+winLeft )); 
	if (window.focus) {newwindow.focus()}
}


//jQuery
$(function(){
	
	/* Table styling
	************************************************/
	/*$("table tbody tr:odd td").addClass("tablestripe");*/
	/*$("table thead th:first-child").prepend('<img class="leftend" src="/images/misc/table_head_leftend.png" alt="" />');*/
	$("table thead th:first-child, table tbody td:first-child").addClass("first-child");
	$("table thead th:last-child, table tbody td:last-child, table tr:last-child").addClass("last-child");
		
	
	/* Popup class
	************************************************/
	// any tag with this class will open popup on click at the href attribute and at the popupwidth and popupheight attrubute size.
	$("a.popup").attr("title","Launch in Pop-up window");
	$("a.popup").click( function () {
		openPopup( $(this).attr("href"), 'Popup', Number($(this).attr("popupwidth")), Number($(this).attr("popupheight")), 'yes', 'yes', 'no', 'yes' );
		return false;
	});
	
	
	/*  Search bar hint
	************************************************/
	$(".product-nav #search-bar .search-field").hint();
	
	
	/*  Active navigation highlight (ALL NAVIGATION)
	************************************************/
	if( $("body[class]").length > 0 ) {
		var bodyClasses = $("body").attr("class").split(" ");
		for( var i in bodyClasses ) {
			$("li."+bodyClasses[i]+" a:first").addClass("active");
			$(".checkout-path div."+bodyClasses[i]).addClass("active");
		}
	}
	
	
	/* forms
	************************************************/
	//field focus styles
	$("input[type='text'], input[type='password'], textarea").addClass( "box" );
	$("input[type='text'], input[type='password'], textarea").focus(function(){ $(this).addClass( "focus" ); });
	$("input[type='text'], input[type='password'], textarea").blur(function(){ $(this).removeClass( "focus" ); });

	$("form[class!='do-not-focus'] fieldset input[type!='hidden'][type!='submit']:first").focus();

	
	
	
	
	/*  Header/Footer menu active items
	************************************************/
	// active submenu item
	//$( ".menu ."+$('body').attr("id") ).addClass("active");
	
	
	/*  Menu (Tabs) active item
	************************************************/
	// active tab
	//$( ".header .menu .tab." + $('body').attr('id') ).addClass("active");

	// tab over
	/*$(".header .menu .tab").hover(
      function () {  $(this).addClass("over");  }, 
      function () {  $(this).removeClass("over");  }
    );*/
	
	
	
	
	
	/*  Navigation (Non Collapsing) active item
	************************************************/
	// active nav item
	//$( ".nav.nocollapse ."+$('body').attr("class") ).addClass("active");
	
	
	/*  Navigation
	************************************************/
	// temp to help me see which links are active
	//$(".nav a[href='#']").not(".heading").css("color","#BBB");
	
	/*$(".nav ul ul li").hover(
      function () {  $(this).addClass("hover");  }, 
      function () {  $(this).removeClass("hover");  }
    );
	
	$(".nav:not(.nocollapse) ul ul").hide();
	
	
	// expand areas saved to cookie
	var openNavs= new Array();
	if( readCookie("smiggleNav") != null ) openNavs = readCookie("smiggleNav").split(",");
	for( i=0 ; i<openNavs.length ; i++ ) {
		if( openNavs[i] != "" ) {
			$(".nav .heading:contains('"+openNavs[i]+"')").addClass("expanded").parent().children("ul").show();
		}
	}
	
	// expand on click and stay open, remember with cookies
	$(".nav:not(.nocollapse) .heading").click(function () {
		if( $(this).parent("li").children("ul").is(":hidden") ) {
			//alert("show");
			$(this).addClass("expanded");
			$(this).parent().children("ul").show();
			// save area expanded
			if( jQuery.inArray($(this).text(), openNavs) == -1 ) {
				openNavs.push( $(this).text() );
				//alert( openNavs.toString() );
				createCookie( "smiggleNav", openNavs.toString(), 1 );
			}					  
		} else {
			//alert("hide");
			$(this).removeClass("expanded");
			$(this).parent().children("ul").hide();
			// remove area expanded
			if( jQuery.inArray($(this).text(), openNavs) != -1 ) {
				openNavs.splice( jQuery.inArray($(this).text(), openNavs), 1 );
				//alert( openNavs.toString() );
				createCookie( "smiggleNav", openNavs.toString(), 1 );
			}
		}
	});*/
	
});


/*function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function setcookie() {
	if( document.getElementById('textField').value == "" ) {
		eraseCookie('character');
		window.location.reload();
	} else {
		var characterName = document.getElementById('textField').value;
		createCookie('character',characterName,1000);
		window.location.reload();
	}
}*/
