/* 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(){
	
	/* Zebra stripe tables
	************************************************/
	$("table tbody tr:odd td").addClass("tablestripe");
	
	
	
	/* Popup class
	************************************************/
	// any tag with this class will open popup on click at the href attribute and at the popupwidth and popupheight attrubute size.
	$(".popup").attr("title","Launch in Pop-up window");
	$(".popup").click( function () {
		openPopup( $(this).attr("href"), 'Popup', Number($(this).attr("popupwidth")), Number($(this).attr("popupheight")), 'yes', 'yes', 'no', 'yes' );
		return false;
	});
	
	
	
	/*  Header/Footer submenu active item
	************************************************/
	// active submenu item
	$( ".submenu ."+$('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");  }
    );
	
	
	/*  Checkout path active item
	************************************************/
	// active submenu item
	$( ".checkout-path ."+$('body').attr("class") ).addClass("active");
	
	
	
	/*  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(",");
	//alert( openNavs );
	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 );
			}
		}
	});
	
	// Nav expand on click and close all others
	/*$(".nav ul ul").hide();
	$(".nav .heading").click( function() {						   
		$(".nav .heading").removeClass("expanded");
		$(".nav ul ul").slideUp("normal");
		$(this).addClass("expanded");
		$(this).parent().children("ul").dequeue();
        $(this).parent().children("ul").slideDown("normal");
    });*/
	
});


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();
	}
}
