/**
 * Function to open a top level menu
 */
var plusimg = '<img src="'+appUri+'/resources/images/menu/plus.gif" />';
var plusimgsub = '<img src="'+appUri+'/resources/images/menu/plus_sub1.gif" />';
var minusimg = '<img src="'+appUri+'/resources/images/menu/minus.gif" />';

function openTopMenu( menu ) {
	var mtop = new Array();
    var i = 0;
    var id = "";
	// Discover the top level categories
    var top_level = $$('.top_level');
    for (t in top_level ) {
    	
    	if ( top_level[t].id != undefined ) {
    		id = top_level[t].id;
    		mtop[i] = id.replace(/_menu/, "");
    		i++;
    	}
    }
	
	// Loop through top level tags and hide if visible 	
	mtop.each( function (m) {
		// safety check
		if ( Element.hasClassName( m+"_sub", "sub1") ) {
			if ( m != menu ) {
				Element.removeClassName( $(m+"_top"), "on" );	
				Element.removeClassName( $(m+"_menu"), "on" );	

				if ( Element.visible(m+"_sub") ) {
					Element.update(m+'_expand_top', plusimg);
					Effect.SlideUp( m+"_sub", {duration:.3} )
				}
				
			}
		} 
		
	
	});
	
	
	Element.addClassName( $(menu + "_menu"), "on" );
	Element.addClassName( $(menu + "_top"), "on" );
	Element.update( menu+'_expand_top', minusimg );
	
	
	if ( !Element.visible(menu + "_sub") ) Effect.SlideDown( menu + "_sub", {duration:.3} );
	else {
		Effect.SlideUp( menu + "_sub", {duration:.3} );
		Element.removeClassName( $(menu + "_menu"), "on" );
		Element.removeClassName( $(menu + "_top_span"), "on" );

		Element.update(menu+'_expand_top', plusimg );
	}
}


function openSubMenu( menu ) {
	ele = $(menu + '_sub');
	Element.toggle(ele);
	if ( Element.visible(ele) ) {
		Element.addClassName(menu+'_sub1_span', "on");
		Element.update(menu+'_expand', minusimg);
	}
	else {
		Element.removeClassName(menu+'_sub1_span', "on");
		Element.update(menu+'_expand', plusimgsub);
	}
}

/**
 * Initialize hidden status for prototype
 */
function menuInit() {
	var hackedElements;
    hackedElements = $$('.hidehack');
    hackedElements.invoke('hide');
    hackedElements.invoke('removeClassName', 'hidehack');
    
}


function openMenu( tags ) {
	var i = 0;
	var top;
	
	tags.each( function(t) {
		if ( i == 0 ) {
			top = t;
			openTopMenu( t );
		}
		else if ( i == 1 ) {
			openSubMenu(top+'_'+t );
		}
		i++;
	});

}



/*** Language Functions **/
function langMouseOver(id) {
	if ( $(id).src.indexOf('_Selected') == -1 ) {
		var src = $(id).src; 
		$(id).src = src.replace('_Normal', '_Over');
	}

}
function langMouseOut(id) {
	if ( $(id).src.indexOf('_Over') != -1 ) {
		var src = $(id).src; 
		$(id).src = src.replace('_Over', '_Normal');
	}
}

function langSelect(country) {
	if(country == 'CA'){
		window.location = 'http://gpscentral.ca';
	}
	return;
	//this is never hit because we just forward straight to the canadian site for canadian pricing.
	if(thisUrl.indexOf('country=') >= 0){
		oldCountry = thisUrl.substring(thisUrl.indexOf('country=') + 8,thisUrl.indexOf('country=') + 10)
		newUrl = thisUrl.replace('country=' + oldCountry,'country=' + country);
	} else {
		if(thisUrl.indexOf('?') >= 0){
			argSeparator = '&';
		} else {
			argSeparator = '?';
		}
		newUrl = thisUrl + argSeparator + 'country=' + country;
	}
	window.location = newUrl;
}

/**
 * GP button functions
 */
function gpMouseOver(id) {
	if ( $(id).src.indexOf('_norm') != -1 ) {

		var src = $(id).src;
		$(id).src = src.replace('_norm', '_over');
	}
}

function gpMouseOut(id) {
	if ( $(id).src.indexOf('_over') != -1 ) {
		var src = $(id).src; 
		$(id).src = src.replace('_over', '_norm');
	}
}

function gpClick(id) {
	alert(id);
}

function faqLaunch() {
	window.open('/faq/','faq', 'width=490,height=470,scrollbars=no,location=no,resizable=yes');
}


/** GP Help Video Stuff **/ 

function launchFlashVideo() {
	
	// Bring the div on screen
	$('gpvideos').style.marginLeft = '-50px';
	//	Windows won't load the flash if it's off screen so we may have to wait a bit
	if (!$('gpvideos').launchFlashVideo ) {
		window.setTimeout(function() {
			$('gpvideos').launchFlashVideo(); }, 1000);
	}
	else {
		$('gpvideos').launchFlashVideo(); 
	}
}

function hideGPdiv() {
	// Move off screen
	$('gpvideos').style.marginLeft = '-999px';
	
}

/**
 * All page onload events should go here if possible
 */
function pageInit() {
	// GP position check ( all pages except home )
	// keep GP at the bottom of the page except never on top of the footer
	if ( document.getElementById('gp') 
			&& !(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7) ) { 
		window.onscroll = function(e) {
			positionGP();
		}
		
		positionGP();
	}
	
	// Check for toplevel initialization flag 
	if ( typeof initTopLevel != "undefined" ) {
		centertoplevelflash();
		window.onresize = centertoplevelflash;
		
		// Open the appropriate menu
		openTopMenu(toptag);
		
	}
	
	if ( typeof initFAQ != "undefined" ) {
		// Do the resize on load in case the window had been resized previously and is still open
		onFAQResize();
		window.onresize = onFAQResize;	
	}
	
	if ( typeof initProduct != "undefined" ) {
		
		if ( document.getElementById('reviewratingbars') ) {
			Event.observe('reviewratingbars', 'mousemove', ratingMouseOver, true); 
			Event.observe('reviewratingbars', 'click', rateProduct, true);
		}
		
	}
	
}

/**
 * Position GP so as not to cover the footer
 */
function positionGP() {
	// Get all needed heights
	var hh = 107; // needs to be retrieved
	var ch = $('content').getHeight();
	var fh = $('footer').getHeight();
	var gh = $('gp').getHeight();
	var mh = $('menu').getHeight();
	var ph = $('page').getHeight();
	
	var scrollOffsets = document.viewport.getScrollOffsets();
	var so = scrollOffsets['top'];
	var vp = document.viewport.getHeight();
	var flag = ph - (vp + so) - fh;

	// If footer is in view
	if ( flag < 0 ) {	
		$('gp').style.bottom = (flag*-1) + 'px'; 
	}
	
	// If footer is not in view, set position to bottom 0
	else {
		$('gp').style.bottom = '0px';
	}
	
	// !!! FF2 bug !!!
	// position by top since flash is unclickable with position fixed 
	if (BrowserDetect.browser == 'Firefox' && BrowserDetect.version <= 2 ) {
		
		if ( flag > 0 ) {
			var top = vp + so - gh;	
			$('gp').style.top = top+'px;';
		}
		else {
			$('gp').style.bottom = (flag*-1) + 'px'; 
		}
	}
	//   /FF2 Bug fix
	
}


/**
 * submit the product add to cart form
 */
function submitProductCart (){
	$('add_to_cart').focus();
	$('add_to_cart').click();
}



/**
 * Handle the rating mouseover and background image sliding.
 * @return
 */
function ratingMouseOver(e){
	var debug ='';

	if (e) {
		var mX = Event.pointerX(e);
	    var mY = Event.pointerY(e);
	}
	// Viewport offset
	var voff = $('reviewratingbars').viewportOffset();
	var vY = voff['top'];
	var vX = voff['left'];
	var diff = mX-vX;
	var bgX = -84 + (mX-vX);
	var barW = 17;
	var rating  =0;
	$('reviewrating').style.backgroundImage = "url("+appUri + "/resources/images/ratingfill.jpg)";

/*
	debug += "vX: " + vX + "<br/>";
	debug += "vY: " + vY + "<br/>";
	debug += "mX: " + mX + "<br/>";
	debug += "mY: " + mY + "<br/>";
	debug += "diff: " + diff + "<br/>";
	debug += "bgX: " + bgX + "<br/>";
	
	$('debuginfo').update(debug);
*/
	
	rating = getProductRating( diff );
	
	bgX = -84 + barW * rating;
	$('reviewrating').style.backgroundPosition = bgX + 'px';
	
	
}

/**
 * Get rating based on mouse position
 */ 
function getProductRating( mPos ) {
	var barW = 17;
	
	if ( mPos <= barW ) {
		rating = 1;
	}
	else if ( mPos <= barW*2) {
		rating = 2;
	}
	else if ( mPos <= barW*3) {
		rating = 3;
	}
	else if ( mPos <= barW*4) {
		rating = 4;
	}
	else if ( mPos <= barW*5) {
		rating = 5
	}

	return rating;
}


function rateProduct( e ) {

	if (e) {
		var mX = Event.pointerX(e);
	    var mY = Event.pointerY(e);
	}
	
	// Viewport offset
	var voff = $('reviewratingbars').viewportOffset();
	var vY = voff['top'];
	var vX = voff['left'];
	var diff = mX-vX;
	var bgX = -84 + (mX-vX);
	var barW = 17;
	var rating  =0;


	var rating = getProductRating( diff );
	$('rating').value = Math.round(rating);
}

function setRating( rating ) {
	setRatingImage('reviewrating', rating);
}

function setProductRating( rating ) {
	setRatingImage('productrating', rating);

}

function setRatingImage(target, rating) {
	var barW = 17;
	var val;
	
	bgX = -84 + barW * Math.round(rating);
	$(target).style.backgroundImage = "url("+appUri + "/resources/images/ratingfill.jpg)";
	
	val = bgX + 'px'
	$(target).style.backgroundPosition = val;
}



function topNavOver(img) {
	if ( $(img).src.indexOf('_Normal') != -1 ) {
		var src = $(img).src; 
		$(img).src = src.replace('_Normal', '_Over');
	}		
}

function topNavOut(img) {
	if ( $(img).src.indexOf('_Over') != -1 ) {
		var src = $(img).src; 
		$(img).src = src.replace('_Over', '_Normal');
	}	

}