// Intelligence Squared US Frontend JS

$(document).ready(function() {
	//vars
	var futureDebatesImage = "#futureDebatesImage";
	var futureDebatesPath = {
		on:			"/wp-content/themes/intelligence-squared/images/future_debates_on.gif",
		off:		"/wp-content/themes/intelligence-squared/images/future_debates.gif"
	};
	var futureDebatesDropdown = "#futureDebatesDropdown";
	var debateDropdownElement = ".dropdownDebateWrap a";
	var pastDebatesImage = "#pastDebatesImage";
	var pastDebatesPath = {
		on:		"/wp-content/themes/intelligence-squared/images/past_debates_on.gif",
		off:	"/wp-content/themes/intelligence-squared/images/past_debates.gif"
	};
	var debateNav = {
		mainTarget:				"#debateNav",
		overviewTarget:			"#debateNavOverview a",
		aboutTarget:			"#debateNavAbout a",
		avTarget:				"#debateNavAV a",
		photosTarget:			"#debateNavPhotos a",
		transcriptsTarget:		"#debateNavTranscripts a",
		purchaseTarget:			"#debateNavPurchase a",
		buyTicketsButtonTarget:	"#buyTicketsButton"
	}
	var buyButton = {
		activePath:	"/wp-content/themes/intelligence-squared/images/buy_tickets_now_over.png",
		offPath:	"/wp-content/themes/intelligence-squared/images/buy_tickets_now.png"
	}
	var dropdownTime = 1000;
	var futureDebatesTO;
	var dropdownTO;
	var isShowingFutureDropdown = false;
	var tooltipID = "#tooltip";
	var CURRENT_SECTION = "";
	var PREVIOUS_SECTION = "";
	var PREVIOUS_DEBATE_BUTTON = "#previousDebateButton";
	var NEXT_DEBATE_BUTTON = "#nextDebateButton";
	var getDebateDataScriptURL = "scripts/get_debate_data.php";
	var afterContentTarget = "#debateMainContent";
	//max 2 for now
	var numDebates = 5;
	var CURRENT_DEBATE_ID = 1000;
		// DEBATE DATA
		var debate = {};
		
	//SETUP EVERYTHING
	//wait for the images to preload before binding rollovers
	$(window).load(function() {		
		initTopNav();
	});
	//only do these if the debate nav is there or else we'll get a js error.
	if( $(debateNav.target).length >= 0 ) {
		//setMiddleContent();
		initDebateNav();
		initBuyButton();
		setFirstLoad();
	}
	
	//INIT FUNCTIONS
	function initTopNav() {
		$(futureDebatesDropdown).mouseover(function() {
			isShowingFutureDropdown = true;
		});
		$(debateDropdownElement).mouseover(function() {
			isOnInnerDropdownElement = true;
		});
		
		$(".dropdownDebateInner").hover(
		function() {
			$(this).attr("class","dropdownDebateInnerOver");
		},
		function() {
			$(this).attr("class","dropdownDebateInner");
		}
		);
		
		$("#topNavSupportUsLink").hover(
		function() {
			$("#topNavSupportUsImage").attr("src", "/wp-content/themes/intelligence-squared/images/support_us_on.jpg");
		},
		function() {
			$("#topNavSupportUsImage").attr("src", "/wp-content/themes/intelligence-squared/images/support_us_off.jpg");
		}
		);

		$("#topNavMediaLink").hover(
		function() {
			$("#topNavMediaImage").attr("src", "/wp-content/themes/intelligence-squared/images/media_on.jpg");
		},
		function() {
			$("#topNavMediaImage").attr("src", "/wp-content/themes/intelligence-squared/images/media_off.jpg");
		}
		);
		$("#topNavTVRadioLink").hover(
		function() {
			$("#topNavTVRadioImage").attr("src", "/wp-content/themes/intelligence-squared/images/tv_radio_on.jpg");
		},
		function() {
			$("#topNavTVRadioImage").attr("src", "/wp-content/themes/intelligence-squared/images/tv_radio_off.jpg");
		}
		);

		$("#topNavVenueInfoLink").hover(
		function() {
			$("#topNavVenueInfoImage").attr("src", "/wp-content/themes/intelligence-squared/images/venue_info_on.jpg");
		},
		function() {
			$("#topNavVenueInfoImage").attr("src", "/wp-content/themes/intelligence-squared/images/venue_info_off.jpg");
		}
		);
		
		$("#topNavAboutUsLink").hover(
		function() {
			$("#topNavAboutUsImage").attr("src", "/wp-content/themes/intelligence-squared/images/about_us_on.jpg");
		},
		function() {
			$("#topNavAboutUsImage").attr("src", "/wp-content/themes/intelligence-squared/images/about_us_off.jpg");
		}
		);
		
		$("#topNavNewsletterLink").hover(
		function() {
			$("#topNavNewsletterImage").attr("src", "/wp-content/themes/intelligence-squared/images/newsletter_on.jpg");
		},
		function() {
			$("#topNavNewsletterImage").attr("src", "/wp-content/themes/intelligence-squared/images/newsletter_off.jpg");
		}
		);
		
		$("#topNavFAQLink").hover(
		function() {
			$("#topNavFAQImage").attr("src", "/wp-content/themes/intelligence-squared/images/faq_on.jpg");
		},
		function() {
			$("#topNavFAQImage").attr("src", "/wp-content/themes/intelligence-squared/images/faq_off.jpg");
		}
		);
		$("#topNavContactLink").hover(
		function() {
			$("#topNavContactImage").attr("src", "/wp-content/themes/intelligence-squared/images/contact_on.jpg");
		},
		function() {
			$("#topNavContactImage").attr("src", "/wp-content/themes/intelligence-squared/images/contact_off.jpg");
		}
		);
		
		$(futureDebatesImage).hover(
		function() {
			isShowingFutureDropdown = true;
			$(this).attr("src", futureDebatesPath.on);
			$(futureDebatesDropdown).attr("class","on");
		},
		function() {
			isShowingFutureDropdown = false;
			$(this).attr("src", futureDebatesPath.off);
			$(futureDebatesDropdown).attr("class","off");
		}
		);
		
		$(futureDebatesDropdown).hover(
		function() {
			isShowingFutureDropdown = true;
			$(futureDebatesImage).attr("src", futureDebatesPath.on);
			$(futureDebatesDropdown).attr("class","on");
		},
		function() {
			isShowingFutureDropdown = false;
			$(futureDebatesImage).attr("src", futureDebatesPath.off);
			$(futureDebatesDropdown).attr("class","off");
		}
		);

/*		$(pastDebatesImage).mouseover(function() {
			$(this).attr("src", pastDebatesPath.on);
		});
		$(pastDebatesImage).mouseout(function() {
			$(this).attr("src", pastDebatesPath.off);
		});*/

	}
	
	
	function initDebateNav() {

		//ROLLZ
		$(debateNav.overviewTarget).live("mouseover",function() {
			$(debateNav.mainTarget).attr("class","overview");
		});
		
		$(debateNav.overviewTarget).live("mouseout",function() {
			if(CURRENT_SECTION != "overview") {
				$(debateNav.mainTarget).attr("class",CURRENT_SECTION);
			}
		});
		
		$(debateNav.aboutTarget).live("mouseover",function() {
			$(debateNav.mainTarget).attr("class","about");
		});
		$(debateNav.aboutTarget).live("mouseout",function() {
			if(CURRENT_SECTION != "about") {
				$(debateNav.mainTarget).attr("class",CURRENT_SECTION);
			}
		});
		$(debateNav.avTarget).live("mouseover",function() {
			$(debateNav.mainTarget).attr("class","av");
		});
		$(debateNav.avTarget).live("mouseout",function() {
			if(CURRENT_SECTION != "av") {
				$(debateNav.mainTarget).attr("class",CURRENT_SECTION);
			}
		});
		$(debateNav.photosTarget).live("mouseover",function() {
			$(debateNav.mainTarget).attr("class","photos");
		});
		$(debateNav.photosTarget).live("mouseout",function() {
			if(CURRENT_SECTION != "photos") {
				$(debateNav.mainTarget).attr("class",CURRENT_SECTION);
			}
		});
		$(debateNav.transcriptsTarget).live("mouseover",function() {
			$(debateNav.mainTarget).attr("class","transcripts");
		});
		$(debateNav.transcriptsTarget).live("mouseout",function() {
			if(CURRENT_SECTION != "transcripts") {
				$(debateNav.mainTarget).attr("class",CURRENT_SECTION);
			}
		});
		$(debateNav.purchaseTarget).live("mouseover",function() {
			$(debateNav.mainTarget).attr("class","purchase");
		});
		$(debateNav.purchaseTarget).live("mouseout",function() {
			if(CURRENT_SECTION != "purchase") {
				$(debateNav.mainTarget).attr("class",CURRENT_SECTION);
			}
		});
		//CLICKS
		$(debateNav.overviewTarget).live("click",function() {
			setCurrentSection("overview");
			clearPreviousSection(PREVIOUS_SECTION);
			$(debateNav.mainTarget).attr("class","overview");
			$(debateNav.overviewTarget).addClass("currentSection");
		});
		$(debateNav.aboutTarget).live("click",function() {
			setCurrentSection("about");
			clearPreviousSection(PREVIOUS_SECTION);
			$(debateNav.mainTarget).attr("class","about");
			$(debateNav.aboutTarget).addClass("currentSection");
		});
		$(debateNav.avTarget).live("click",function() {
			setCurrentSection("av");
			clearPreviousSection(PREVIOUS_SECTION);
			$(debateNav.mainTarget).attr("class","av");
			$(debateNav.avTarget).addClass("currentSection");
		});
		$(debateNav.photosTarget).live("click",function() {
			setCurrentSection("photos");
			clearPreviousSection(PREVIOUS_SECTION);
			$(debateNav.mainTarget).attr("class","photos");
			$(debateNav.photosTarget).addClass("currentSection");
		});
		$(debateNav.transcriptsTarget).live("click",function() {
			setCurrentSection("transcripts");
			clearPreviousSection(PREVIOUS_SECTION);
			$(debateNav.mainTarget).attr("class","transcripts");
			$(debateNav.transcriptsTarget).addClass("currentSection");
		});
	}
	function setFirstLoad() {
		var d = window.location;
		d = d.toString();
		if(d.indexOf("?d=") == -1) {
			CURRENT_DEBATE_ID = 1000;
			setCurrentSection("overview");
			$(debateNav.mainTarget).attr("class","overview");
			$(debateNav.overviewTarget).addClass("currentSection");
		} else {
			var id = d.substr(d.indexOf("?d=")+3);
			CURRENT_DEBATE_ID = id;
			setCurrentSection("overview");
			$(debateNav.mainTarget).attr("class","overview");
			$(debateNav.overviewTarget).addClass("currentSection");
		}
	}
	
	//insert html content

	function setCurrentSection(section) {
		CURRENT_SECTION = PREVIOUS_SECTION = section;
		//switch out the content now
		
	}
	function clearPreviousSection(section) {
		$(debateNav.mainTarget + " a").attr("class","");
	}
	function initBuyButton() {
		$(debateNav.buyTicketsButtonTarget).live("mouseover",function() {
			if( $(this).attr("class") == "off" ) {
				$(this).attr("class","on");
				$(this).attr("src",buyButton.activePath);
			}
		});
		$(debateNav.buyTicketsButtonTarget).live("mouseout",function() {
			if( $(this).attr("class") == "on" ) {
				$(this).attr("class","off");
				$(this).attr("src",buyButton.offPath);
			}
		});
	}

	//sets the middle content area
	function setMiddleContent(direction) {
		//check for empty (on first page load, for ex)
		switch(CURRENT_SECTION) {
			case "":
			case "overview":
				if(direction == "previous") {
					setCurrentSection("overview");
				} else if (direction == "next") {
					setCurrentSection("about");
					clearPreviousSection(PREVIOUS_SECTION);
					$(debateNav.mainTarget).attr("class","about");
					$(debateNav.aboutTarget).addClass("currentSection");
				} else {
				}
				break;
			case "about":
				if(direction == "previous") {
					setCurrentSection("overview");
					clearPreviousSection(PREVIOUS_SECTION);
					$(debateNav.mainTarget).attr("class","overview");
					$(debateNav.overviewTarget).addClass("currentSection");
				} else if (direction == "next") {
					setCurrentSection("about");
				} else {
				}
				break;
			default:
				break;
		}
	}

// new js	

	$('#wrapper-poll a').click(function(){
		$('#debateNavAbout a').trigger("click");
	})

	//top menu (will rewrite if time permits)
	$('#debateNavOverview a').live("click", function(){
		$('#debateMainContent').show();
		$('#debateAbout').hide();
		$('#debate-video').hide();
		$('#debate-photos').hide();
		
		return false;
	});	

	$('#debateNavAbout a').live("click", function(){
		$('#debateMainContent').hide();
		$('#debateAbout').show();
		$('#debate-video').hide();
		$('#debate-photos').hide();
		
		return false;
	});		
	
	$('#debateNavAV a').live("click", function(){
		$('#debateMainContent').hide();
		$('#debateAbout').hide();
		$('#debate-video').show();
		$('#debate-photos').hide();
		
		return false;
	});		
	
	$('#debateNavPhotos a').live("click", function(){
		$('#debateMainContent').hide();
		$('#debateAbout').hide();
		$('#debate-video').hide();
		$('#debate-photos').show();
		
		return false;
	});		
	
	// past debates
		$('#pastDebates').hover(
			function() {
				$('#pastDebatesDropdown').attr("class","on").show();
				$('#pastDebatesImage').attr("src","/wp-content/themes/intelligence-squared/images/past_debates_on.gif");
			},
			function() {
				$('#pastDebatesDropdown').attr("class","off").hide();
				$('#pastDebatesImage').attr("src","/wp-content/themes/intelligence-squared/images/past_debates.gif");
			}
		);
		
	// ajax pages
/*	$(".next-previous a,ul.middle li > a").live("click", function(){
      	var page_url = $(this).attr("href");
		$('#middleContentArea').load(page_url + " .post");
		
		return false;
    });*/
    
	// gallery overlay
	
	$(".ngg-galleryoverview").live("mouseover", function(){
		shutterReloaded.init('sh');
	});
	
	$("#debateBuyTicketsP").live("mouseover", function(){
		//$(this . ' img').removeAttr("id");
	});
	
	// alert
	$('#alert a').click(function(){
		$('#alert').slideUp();
		return false;
	})
	
	// local radio search
	function log(event, data, formatted) {
		$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
	}
	
	function formatItem(row) {
		return row[0] + " (<strong>" + row[1] + "</strong>)";
	}
	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}

	
	$("#local-radio-search").autocomplete('/wp-content/themes/intelligence-squared/data-radio.php', {
		width: 500,
		scrollHeight: 100,
		multiple: true,
		matchContains: true,
		formatItem: formatItem,
		formatResult: formatResult
	});

	$("#local-radio-search").result(function(event, data, formatted) {
		var hidden = $(this).parent().next().find(">:input");
		hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
	});
	
	// clips
	$('#l-clip-1').click(function(){
		$('#tv-container .clip-vimeo').hide();
		$('#tv-container #clip-1').show();
		
		return false;
	})
	$('#l-clip-2').click(function(){
		$('#tv-container .clip-vimeo').hide();
		$('#tv-container #clip-2').show();
		
		return false;
	})	
	$('#l-clip-3').click(function(){
		$('#tv-container .clip-vimeo').hide();
		$('#tv-container #clip-3').show();
		
		return false;
	})	
	$('#l-clip-4').click(function(){
		$('#tv-container .clip-vimeo').hide();
		$('#tv-container #clip-4').show();
		
		return false;
	})	
    
});