var fx, fx2;

function show_login () {
	var div_id = 'your_account';
	if(document.getElementById(div_id).style.top=='0px'){ 
		$('#your_account').animate({ top: '-50'}, { duration: 200, "easing": "easeOutQuad"});		
	} else{ 
		check_login_form();
		$('#your_account').animate({ top: '0'}, { duration: 200, "easing": "easeOutQuad"});
	}
}



var obj = null;
function show_submenu(id) {
	/*
	var submenu_id = 'submenu' + id;
	var menu_id = 'menu' + id;
	document.getElementById(menu_id).style.backgroundColor = '#666666';
	document.getElementById(submenu_id).style.display = 'block';
	if (obj && obj != id) {
		hide_submenu(obj);
	}
	if (obj) {
		obj = null;
	} //if	obj = id;
	*/
}

function stay_open(id) {
	obj = null;
}

function maybe_hide(id) {
	obj = id;
	// etTimeout("check_hover()", 400);
}

function hide_submenu(id) {
	var submenu_id = 'submenu' + id;
	var menu_id = 'menu' + id;
	document.getElementById(menu_id).style.background = 'none';
	document.getElementById(submenu_id).style.display = 'none';
	obj = null;
}


function check_hover() {
	if (obj) {
		hide_submenu(obj);
	} 
} 

function site_search() {
	str = escape($('#query_string').val());
	str = str.replace('+', '%2B');
	str = str.replace(/%20+/g, '+');
	str = str.replace(' ', '+');
	str = str.replace('*', '%2A');
	str = str.replace(/\/+/g, '');
	str = str.replace('@', '%40');
	//console.log(str);
	window.location = 'http://' + location.hostname + '/motorcycle/search/overview/' + str + '/';
}


function show_label (the_id) {
	if (document.getElementById(the_id).value=="") {
		var the_label_id = 'label_' + the_id; 
		document.getElementById(the_label_id).style.display = 'inline';
	}
}
function hide_label (the_id) {
	var the_label_id = 'label_' + the_id;
	document.getElementById(the_label_id).style.display = 'none';
	document.getElementById(the_id).focus();
}

function check_login_form() {
	if ($("#UserUsername").size() > 0) {
		if ( $("#UserUsername").val().length > 0) {
			hide_label("UserUsername");
		} 
	}
	if ($("#UserPassword").size() > 0) {
		if ($("#UserPassword").val().length > 0) {
		hide_label("UserPassword");
		}
	}
}

function toggle_intl_notice () {
	if(document.getElementById('intl_notice').style.display == 'none') {
		document.getElementById('intl_notice').style.display = 'inline'
	} else {
		document.getElementById('intl_notice').style.display = 'none'
	}
}

/*
why wont the below code work??!!!!!  Had to use the above code.

$("#add_to_cart_button_inactive").hover(
	function () {
		$("#intl_notice").css('display', 'inline');
	}, 
	function () {
		$("#intl_notice").css('display', 'none');
	}
);
*/
function load_bg(data) {
	bg = "url(" + data.image + ")";
	$('body').css('background-image', bg);	
}
window.onload=function(){
	var the_thing = document.getElementById('your_account');
	show_label ("query_string");
	setTimeout("check_login_form()", 300);
	$.preload([ 'wallpaper-grey'], {
	    base:'/images/ui/',
	    ext:'.png',
		onFinish: load_bg
	});

	//$.preloadImages('http://www.motionpro.com/images/ui/wallpaper.jpg');
	//$('body').css('background-image', "url(http://www.motionpro.com/images/ui/wallpaper.jpg)");
	$("#text_search").submit(function() {
		site_search();
		return false;
	});
	
	$("#subscribe_email").focus(function () {
		if ($("#subscribe_email").val() == "Email Address") {
			$("#subscribe_email").css('color','#000000');
			$("#subscribe_email").val("");
		}
	});
	$("#subscribe_email").blur(function () {
		if ($("#subscribe_email").val() == "") $("#subscribe_email").val("Email Address");

		if ($("#subscribe_email").val() == "Email Address") {
			$("#subscribe_email").css('color','#999999');
		}
	});

	$("#ups_tracking_no").focus(function () {
		if ($("#ups_tracking_no").val() == "Enter Tracking #") {
			$("#ups_tracking_no").css('color','#000000');
			$("#ups_tracking_no").val("");
		}
	});
	$("#ups_tracking_no").blur(function () {
		if ($("#ups_tracking_no").val() == "") $("#ups_tracking_no").val("Enter Tracking #");

		if ($("#ups_tracking_no").val() == "Enter Tracking #") {
			$("#ups_tracking_no").css('color','#999999');
		}
	});

	$("#query_string").focus(function () {
		if ($("#query_string").val() == "Search for Part # or Keyword") {
			$("#query_string").css('color','#000000');
			$("#query_string").val("");
		}
	});
	$("#query_string").blur(function () {
		if ($("#query_string").val() == "") $("#query_string").val("Search for Part # or Keyword");

		if ($("#query_string").val() == "Search for Part # or Keyword") {
			$("#query_string").css('color','#666666');
		}
	});

	$("#newsletter_archive_link").hover(
		function () {
				$("#newsletter_archive_box").css('display','block');
			},
		function () {
				//$("#newsletter_archive_box").css('display','none');
			}
	);
	$("#newsletter_archive_box_outer").hover(
		function () {
				//$("#newsletter_archive_box").css('display','block');
			},
		function () {
				$("#newsletter_archive_box").css('display','none');
			}
	);
	
	/* pop up menus */
	
//	$(".popnav").hover(
//		function (e) {
//			//console.log("!!!");
//			var left = $(e.currentTarget).parent().innerWidth() - 4;
//			var pos = $(e.currentTarget).position();
//			//console.log(pos.top);
//			$(e.currentTarget).addClass('pop_hover');
//			//$(e.currentTarget).children("ul").eq(0).css('z-index', 99999999);
//			$(e.currentTarget).children("ul").eq(0).css('left', left);
//			$(e.currentTarget).children("ul").eq(0).css('top', pos.top);
//			$(e.currentTarget).children("ul").eq(0).show();
//		}, function (e) {
//			$(e.currentTarget).removeClass('pop_hover');
//			$(e.currentTarget).children("ul").eq(0).hide();
//		}
//	);


	$(".popnav").hover(
		function (event) {
			var targ = event.originalEvent.currentTarget;
			if (targ == undefined) {
				// ie hack
				targ = this;
			}
			var left = $(targ).parent().innerWidth() - 4;
			var pos = $(targ).position();
			$(targ).addClass('pop_hover');
			//$(targ).css('z-index', 'auto');
			var popper = $(targ).children("ul").eq(0);
			popper.css('z-index', 2000);
			popper.css('left', left);
			popper.css('top', pos.top - 3);
			popper.show();
			var offset = popper.offset();
			if (offset) {
				var bottom_from_top = popper.outerHeight() + offset.top;
				if (bottom_from_top > $(window).height()) {
					// var newtop = pos.top - (bottom_from_top - $(window).height());
					popper.css('top', -1);
				}
			}
		}, function (event) {
			var targ = event.originalEvent.currentTarget;
			if (targ == undefined) {
				// ie hack
				targ = this;
			}
			$(targ).removeClass('pop_hover');
			$(targ).children("ul").eq(0).hide();
		}
	);
	
	
	/* signin lightbox (colorbox) */
	
	$('#signin_button').colorbox({width:"600px", inline:true, href:"#signin_popup"});
	
	
}	

	
	
	