$(document).ready(function() {

	/*
	var options = { 
		target:        '#output1',   // target element(s) to be updated with server response 
		beforeSubmit:  showRequest,  // pre-submit callback 
		success:       showResponse  // post-submit callback 
 
		// other available options: 
		//url:       url         // override for form's 'action' attribute 
		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
		//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
		//clearForm: true        // clear all form fields after successful submit 
		//resetForm: true        // reset the form after successful submit 
 
		// $.ajax options can be used here too, for example: 
		//timeout:   3000 
	}; 
 
	// bind form using 'ajaxForm' 
	$('#myForm1').ajaxForm(options); 

*/
	$('#askw').ajaxForm(function(data) { 

		if (data == '1') {
			alert('Good times');
		} else {
			alert("Error. Data: " + data);
		}
		return false;
	});



	$("#email_signup").focus(function(){
		$(this).css("background-position","-200px -200px");
		$(this).css("text-align","left");
	});

	$("#email_signup").blur(function(){
		if ( this.value == "" ){
			$(this).css("background-position","-25px -6px");}
	});

	// Home page WPS cycle

	var hwps_li_total = $("#hwps_items li").size();
	var hwps_li_width = hwps_li_total * 102;
	$("#hwps_items").css("width",hwps_li_width+"px");
	var halfway = ((hwps_li_width - 102) / 2 ) - 50;
	$("#hwps_items").css("left","-"+halfway+"px");

	var left_limit = "46px";
	var right_limit = "-" + ( parseInt(hwps_li_width) - 156 );

	var curr_left, new_left;

	// Home page WPS right arrow
	$("#hwps_right").click(function(){

		curr_left = parseInt($("#hwps_items").css("left"));
		new_left = curr_left - 101;
		$("#hwps_items").animate({ left: new_left + "px"}, 450);
		curr_left = new_left;
		if ( curr_left <= right_limit ){
			$("#hwps_right").css("visibility","hidden");}
	});

	// Home page WPS left arrow
	$("#hwps_left").click(function(){

		$("#hwps_right").css("visibility","visible"); 
		curr_left = parseInt($("#hwps_items").css("left"));
		new_left = curr_left + 101;
		$("#hwps_items").animate({ left: new_left + "px"}, 450);
		curr_left = new_left;
		if ( curr_left >= left_limit ){
			$("#hwps_left").css("visibility","hidden");}
	});

	///////////////////////////////////////////////////////


	// Guest Guru Reccommended list
	var guru_current = 1;
	var guru_li_total = $("#guru_recc_items li").size();
	var guru_li_width = guru_li_total * 140;
	$("#guru_recc_items").css("width",guru_li_width +"px");

	if ( $("#guru_recc_items").css("left") == "0px" ){
		$("#g_r_left").css("visibility","hidden");}
	
	var g_pages = Math.ceil(guru_li_total / 4);

	if ( g_pages == 1 ){

		var left_indent = 0;
		$("#g_r_right").css("visibility","hidden");
		
		if ( guru_li_total == 3 ){
			left_indent = 70;}
		else if ( guru_li_total == 2 ){
			left_indent = 140;}
		else if ( guru_li_total == 1 ){
			left_indent = 210;}

		$("#guru_recc_items").css("margin-left",left_indent+"px");
	}

	$("#g_r_right").click(function(){
		$("#guru_recc_items").animate({ left: "-"+(guru_current * 557)+"px"}, 450);
		$("#g_r_left").css("visibility","visible");
		guru_current++;
		if ( guru_current == g_pages ){
			$("#g_r_right").css("visibility","hidden");}
	});

	$("#g_r_left").click(function(){
		guru_current--;
		$("#guru_recc_items").animate({ left: "-"+((guru_current - 1) * 557)+"px"}, 450);
		$("#g_r_right").css("visibility","visible");
		if ( guru_current == 1 ){
			$("#g_r_left").css("visibility","hidden");}
	});

	///////////////////////////////////////////////////////

	// Home Spotlight cycle
	var home_ws_current = 1;
	var home_ws_total = $("#spotlightListings li").size();
	
	// Home Spotlight left arrow
	$("#home_ws_left").click(function(){
		switchPage();	// Pauses videos
		home_ws_current--;
		if ( home_ws_current == 0 ) { home_ws_current = home_ws_total; }
		$("#spotlightListings").animate({ left: "-"+(650 * (home_ws_current-1) )+"px"}, 800);
	//	$("object").css("visibility","hidden");
	//	$("#spotlightListings li:nth-child("+home_ws_current+")").css("visibility","hidden");
		return false;
	});

	// Home Spotlight right arrow
	$("#home_ws_right").click(function(){
		switchPage();	// Pauses videos
		home_ws_current++;
		if ( home_ws_current > home_ws_total ) { home_ws_current = 1; }
		$("#spotlightListings").animate({ left: "-"+(650 * (home_ws_current-1) )+"px"}, 800);
		return false;
	});

	// WPS GG cycle
	var wps_wgg_current = 1;
	var wps_wgg_total = $("#guruListings li").size();

	$("#guruListings li").hide();
	$("#guruListings li:first").show();

	// WPS GG left arrow
	$("#wgg_left").click(function(){
		$("#guruListings li:nth-child("+wps_wgg_current+")").hide();
		wps_wgg_current--;
		if ( wps_wgg_current == 0 ) { wps_wgg_current = wps_wgg_total; }
		$("#guruListings li:nth-child("+wps_wgg_current+")").show();
		return false;
	});

	// WPS GG right arrow
	$("#wgg_right").click(function(){
		$("#guruListings li:nth-child("+wps_wgg_current+")").hide();
		wps_wgg_current++;
		if ( wps_wgg_current > wps_wgg_total ) { wps_wgg_current = 1; }
		$("#guruListings li:nth-child("+wps_wgg_current+")").show();
		return false;
	});

	var wps_curr_page = 0;

	// WPS Items pagination
	$("a[id^='link_']").click(function(){
		var newValue = $(this).attr("name");
		wps_curr_page = newValue;
		$("#rec_slider").animate({ left: "-"+(412 * newValue )+"px"}, 350);
		$("#next_page").attr("alt","seven");
		return false;
	});

	// WPS Items pagination
	$("#next_page").click(function(){
		var nextValue = $(this).attr("alt");
		
		return false;
	});

	/*	if( (!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1) ) { }	*/

	var total = 0;
	var count = 1;

	//Jump page: Toggle Detail View
	$('.popup').live("click",function(e){

		//	$("#flash").css("visibility","hidden");
			$("[id^=flash]").css("visibility","hidden");
			
			target = $(this).attr("href");
			
			$("#details_load").load(target+' #details_container',null,function(){
				
				$('#details_bg').css("top","190px").fadeIn("fast");
				$('#details').css("top","190px").fadeIn("fast");

				$("#childListings li:first").show();

				total = $("#childListings li").size();
				count = 1;

				$("#dt_counter").text( count + "/" + total);

				if ( total == 1 ){
					$("#dt_previous").hide();
					$("#dt_next").hide();
					$("#dt_counter").hide();
				}
			});
			
			return false;

	});	
	
	
	// Cycle through details -- PREVIOUS
	$("#dt_previous").live("click",function(e) {
			$("#dt_loading").fadeIn("fast");
			$("#childListings li:nth-child("+count+")").hide();
			count--;
			if (count == 0 ) { count = total; }
			$("#childListings li:nth-child("+count+")").show();
			$("#dt_counter").text( count + "/" + total);
			$("#dt_loading").fadeOut("fast");
		return false;
	});

	// Cycle through details -- NEXT
	$("#dt_next").live("click",function(e) {
			$("#dt_loading").fadeIn("fast");
			$("#childListings li:nth-child("+count+")").hide();
			count++;
			if (count > total) { count = 1; }
			$("#childListings li:nth-child("+count+")").show();
			$("#dt_counter").text( count + "/" + total);
			$("#dt_loading").fadeOut("fast");
		return false;
	});



////////////////////////////////////////////////////

	// Guru: assign rel order
	$(".guru_popup").each(function(intIndex){
		$(this).attr("id",intIndex + 1).fadeIn();
	});
	
	// Guru page: Toggle Detail View

	$('.guru_popup').live("click",function(e){

			target = $(this).attr("href");

			total = $("#guru_recc_items li").size();
			count =  $(this).attr("id");
			
			$("#details_load").load(target+' #details_container',null,function(){
			
				$('#details_bg').css("top","190px").fadeIn("fast");
				$('#details').css("top","190px").fadeIn("fast");

				$("#childListings li:first").show();

				$("#gg_counter").text( count + "/" + total);

				if ( total == 1 ){
					$("#gg_previous").hide();
					$("#gg_next").hide();
					$("#gg_counter").hide();
				}
			});
		
			return false;
	});		
	
	// Cycle through details -- PREVIOUS
	$("#gg_previous").live("click",function(e) {

			$("#dt_loading").fadeIn("fast");
			count--;
			if (count == 0 ) { count = total; }
			$("#"+count).click();
			$("#dt_counter").text( count + "/" + total);
			$("#dt_loading").fadeOut("fast");

		return false;
	});

	// Cycle through details -- NEXT
	$("#gg_next").live("click",function(e) {
		
			$("#dt_loading").fadeIn("fast");
			count++;
			if (count > total) { count = 1; }
			$("#"+count).click();
			$("#dt_counter").text( count + "/" + total);
			$("#dt_loading").fadeOut("fast");

		return false;
	});

////////////////////////////////////////////////////

	// W Spotlight: assign rel order
	$(".ws_popup").each(function(intIndex){
		$(this).attr("id",intIndex + 1).fadeIn();
	});
	
	// Guru page: Toggle Detail View

	$('.ws_popup').live("click",function(e){

			$("[id^=flash]").css("visibility","hidden");

			target = $(this).attr("href");

			total = $(".home_spotlight_thumbs .ws_popup").size();
			count =  $(this).attr("id");
			
			$("#details_load").load(target+' #details_container',null,function(){
			
				$('#details_bg').css("top","190px").fadeIn("fast");
				$('#details').css("top","190px").fadeIn("fast");

				$("#childListings li:first").show();

				$("#ws_counter").text( count + "/" + total);

				if ( total == 1 ){
					$("#ws_previous").hide();
					$("#ws_next").hide();
					$("#ws_counter").hide();
				}
			});
		
			return false;
	});		
	
	// Cycle through details -- PREVIOUS
	$("#ws_previous").live("click",function(e) {

			$("#dt_loading").fadeIn("fast");
			count--;
			if (count == 0 ) { count = total; }
			$("#"+count).click();
			$("#ws_counter").text( count + "/" + total);
			$("#dt_loading").fadeOut("fast");

		return false;
	});

	// Cycle through details -- NEXT
	$("#ws_next").live("click",function(e) {
		
			$("#dt_loading").fadeIn("fast");
			count++;
			if (count > total) { count = 1; }
			$("#"+count).click();
			$("#ws_counter").text( count + "/" + total);
			$("#dt_loading").fadeOut("fast");

		return false;
	});


////////////////////////////////////////////////////

	//Sweepstakes rules popup
	$('#sweeps_rules').live("click",function(e){
			
			$('#details_bg').css("top","190px").fadeIn("fast");
			$('#details').css("top","190px").fadeIn("fast");
			
			return false;
	});	

////////////////////////////////////////////////////

	// Close detail window
	$('#details_close').live('click',function(e){
			$('#details').fadeOut("fast");
			$('#details_bg').fadeOut("fast");
			count = 0;
		//	$("#flash").css("visibility","visible");
			$("[id^=flash]").css("visibility","visible");
	});


	$(".ask_w_open").click(function(){

		$("#ask_w_collapse form").fadeIn("fast");

		$("#ask_w_collapse").animate({
							height: "162px"
							}, 200);
		
		$("#ask_w_bottom_line").fadeIn("fast");
	});

	
	// ShareThis
	$("#sharethis_0").hover(function(){
		$("#footer").css("padding-bottom","210px");
	});


	// Open and close Spotlight Index list
	$("#ws_arrow").click(function(){

		var n;

		if ( $(this).attr("alt") == "open" ) {
			$("#spotlight_index_list").animate({ height: "0px" }, 450);
			$('#spotlight_index_list').customFadeOut(0, function() {});
			$(this).attr("alt","close");
		}
		else {
			$("#ws_more").fadeOut();
			n = $("#spotlight_index_list a").length * 17;
			$('#spotlight_index_list').customFadeIn(0, function() {});
			$("#spotlight_index_list").animate({ height: n }, 450);
			$(this).attr("alt","open");
		}
		
	});



	$("#ask_w_close").click(function(){

		$("#ask_w_collapse form").fadeOut("fast");

		$("#ask_w_collapse").animate({
							height: "0px"
							}, 200).hide("fast");
		$("#ask_w_bottom_line").fadeOut("fast");


	});




	var rssNow = 0;

	// Scroll between RSS feed stories
	$('a.rssItemLink').text('Get The Whole Story >').attr('target','_blank');

	$('#on_w_menu a').click(function(){
		// Animate myList
		alert('Hello!');
		$("#on_w").animate({ left: "-"+(265 * $(this).attr("alt"))+"px"}, 350);

		// Change colors
		$("#on_w_menu a").removeClass("on_w_on");
		$(this).addClass("on_w_on");
	});


// ********* SAMPLE BEGIN *********

	// Animate myList
	$("#myList").animate({ left: "-"+(800 * $(this).attr("id"))+"px"}, 350);

	// Load and hide detail window
	$('.listings').click(function(){

		$('.listings').removeClass("list_on");
		$('.my_listings').removeClass("list_on");

		var IDN = $(this).attr("alt");
		
		$('#sp_details_load').load("details.asp?id="+IDN,"",function(){
			$('#sp_details').css("top","90px");
			$('#sp_details').fadeIn("fast");
			
			if ( itemSaved(IDN) ) {
				$("#addToBag a").attr("id","removeLink").html("REMOVE FROM MY SHOPPING BAG");
			}

			$('#sp_details_desc').jScrollPane();
		});

		$(this).addClass("list_on");
	});

	// Close detail window
	$('#sp_details .close').live('click',function(){
		$('#sp_details').fadeOut("fast");
		$('.listings').removeClass("list_on");
		$('.my_listings').removeClass("list_on");
	});
	
	// Share Email
	$("#sendtoafriend").click(function(){
		$('#sharebox_cont').fadeIn();
		 return false;
	});

	$("#share").click(function(){
		$('#sharebox_cont').fadeIn();
		 return false;
	});

	$(".cancelsend").click(function(){
		$('#sharebox_cont').fadeOut();
		 return false;
	});

	$("form#sendform").submit(function(){

		 $.post($(this).attr("action"),{
			   fromname: $("#fromname").val(),
			   fromemail: $("#fromemail").val(),
			   toname: $("#toname").val(),
			   toemail: $("#toemail").val()
			 }, function(xml) {
					messageSent(xml); });
		 return false;
	});

// ********* SAMPLE END *********


}); // End (document).ready


// Share Email

function messageSent1(str) {
	$("#theform").hide();
	$("#sendmailemail").val("");
	//$("#sendmailname").val("");
	$("#sendresult").show();
}
function showForm1(str) {
	$("#theform").show();
	$("#sendresult").hide();
}

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);


// Pause Flash on page change

function thisMovie(movieName) {

	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
}

function pauseMovie(choice){

	try {
		thisMovie("flash_48523781001").pauseMovie(choice);
	} catch(err) {
		//console.log("stop: " + err);
	}
}

function switchPage(){

		try {
			pauseMovie("pause");
		} catch (err) {
			1==1;
		}
}




// pre-submit callback 
function showRequest(formData, jqForm, options) { 
	// formData is an array; here we use $.param to convert it to a string to display it 
	// but the form plugin does this for you automatically when it submits the data 
	var queryString = $.param(formData); 
 
	// jqForm is a jQuery object encapsulating the form element.  To access the 
	// DOM element for the form do this: 
	// var formElement = jqForm[0]; 
 
	alert('About to submit: \n\n' + queryString); 
 
	// here we could return false to prevent the form from being submitted; 
	// returning anything other than false will allow the form submit to continue 
	return true; 
} 
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
	// for normal html responses, the first argument to the success callback 
	// is the XMLHttpRequest object's responseText property 
 
	// if the ajaxForm method was passed an Options Object with the dataType 
	// property set to 'xml' then the first argument to the success callback 
	// is the XMLHttpRequest object's responseXML property 
 
	// if the ajaxForm method was passed an Options Object with the dataType 
	// property set to 'json' then the first argument to the success callback 
	// is the json data object returned by the server 
 
	alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
		'\n\nThe output div should have already been updated with the responseText.'); 
} 

