//VARIABLES
var carouselSettings = {
    btnNext: "#carouselDownButton",
    btnPrev: "#carouselUpButton",
    vertical: true,
    visible: 3,
    start: 0,
    speed: 800,
    auto: 3000
}; //The settings for the home page "carousel" feature. jCarouselLite: http://www.gmarwaha.com/jquery/jcarousellite/

/*** Menu ***
 *
 * TODO: Make this handle an arbitary level of menus.
 *
 */
Menu =function(){
	this.init=function()
	{
		//$('ul.nav a').each(this.setupEvents);
		$('ul.nav a').each(function(i){
			this.hoverElement = '#menu_'+ i +'_hover';//document.getElement('#menu_'+ item +'_hover');
			if(!$(this.hoverElement).length>0) {
				return;
			}
			$(this).bind('mouseover', function(e){

				var position = $('#nav').position();

				$(this.hoverElement).css({display:'block',left:position.left});
				$(this).addClass('hover');
				var hoverBg = document.getElementById("hoverBackground");
				if(hoverBg) {
					hoverBg.style.display="block";
					hoverBg.style.left = position.left;
					hoverBg.style.top = position.top+38;
				}
			});
			$(this).bind('mouseout', function(e){
				$(this.hoverElement).css({display:'none'});
					$(this).removeClass('hover');
					var hoverBg = document.getElementById("hoverBackground");
					if(hoverBg) {
						hoverBg.style.display="none";
					}
			});

			$(this.hoverElement).bind('mouseover', function(e){

				$(this).css({display:'block'});
				$(this).addClass('hover');
				var hoverBg = document.getElementById("hoverBackground");
				if(hoverBg) {
					hoverBg.style.display="block";
				}
			});
			$(this.hoverElement).bind('mouseout', function(e){

				$(this).css({display:'none'});
				$(this).removeClass('hover');
				var hoverBg = document.getElementById("hoverBackground");
				if(hoverBg) {
						hoverBg.style.display="none";
				}
			});

		});
	}
}


//INITIALIZATION
$(document).ready(docReady);


//FUNCTIONS
function docReady() {

	var menu = new Menu();
	menu.init();

    //Activate the home page "carousel" feature for all three tabs.
    //It should be noted that each carousel's content must be visible when the jCarouselLite feature is initialized for that particular carousel.
    //Therefore, this script makes all carousels visible, initializes them, and then runs the tabClick() function to simulate the clicking of
    //the first tab, resetting visibility states for all carousels and returning everything to normal.
    $("#tabFeatures .tabContent").addClass("active");
    $("#recentStoriesContent").jCarouselLite(carouselSettings);
    $("#videosContent").jCarouselLite(carouselSettings);
    $("#slideshowContent").jCarouselLite(carouselSettings);
    tabClick("#recentStoriesTab", "#tabFeatures .tabContent", "#recentStoriesContent");

	//Toggles navigation item active/inactive graphic states when the mouse hovers over their respective dropdown menus.
    $("#nav ul.menu").hover(navRollOver, navRollOut);

    //Adds top and bottom padding to dropdown menus via jQuery; impossible to achieve via CSS without kludgey HTML hacks.
    $("#nav ul.menu").each(function() {
    	$(this).find("> li:first").css("padding-top", "5px");
    });
    $("#nav ul.menu").each(function() {
    	$(this).find("> li:last").css("padding-bottom", "5px");
    });

    //Pre-fades all dropdown menus to a slightly transparent state.
    $("#nav ul.menu li").fadeTo(1, 0.91);

    //Prevent clicks on tab anchor links from scrolling to the top of the page.
    $("ul.genericTabs a").bind("click", function() { return false; });

    //Set up click handlers to switch tabbed feature content.
    //This set is for the tab features at the top of the home page.
    $("#recentStoriesTab").click(function() { tabClick("#recentStoriesTab", "#tabFeatures .tabContent", "#recentStoriesContent");});
    $("#videosTab").click(function() { tabClick("#videosTab", "#tabFeatures .tabContent", "#videosContent");});
    $("#slideshowTab").click(function() { tabClick("#slideshowTab", "#tabFeatures .tabContent", "#slideshowContent");});

    //This set is for the Top Tens list on the home page.
    $("#mostPopularTab").click(function() { tabClick("#mostPopularTab", "#topTensContent ol", "#mostPopularItems");});
    $("#mostCommentedTab").click(function() { tabClick("#mostCommentedTab", "#topTensContent ol", "#mostCommentedItems");});
    $("#mostEmailedTab").click(function() { tabClick("#mostEmailedTab", "#topTensContent ol", "#mostEmailedItems");});
    $("#topSearchesTab").click(function() { tabClick("#topSearchesTab", "#topTensContent ol", "#topSearchesItems");});

    //This set is for the Top Stories list on the inside page.
    $("#mostReadStoriesTab").click(function() { tabClick("#mostReadStoriesTab", "#topStoriesContent ol", "#mostReadItems");});
    $("#mostEmailedStoriesTab").click(function() { tabClick("#mostEmailedStoriesTab", "#topStoriesContent ol", "#mostEmailedItems");});
    $("#topDiscussedStoriesTab").click(function() { tabClick("#topDiscussedStoriesTab", "#topStoriesContent ol", "#topDiscussedItems");});


    $("#articleForm").submit(function() {
			var formStr = $("#articleForm").serialize();

			$.ajax({
				type:"POST",
				url:"/login/ajaxauth",
				data:formStr,
				success: function(msg){
					$("#articleForm").html(msg);
				},
				beforeSend:function(msg){
					$("#articleForm").html("Sending data Please Wait...");
				}

			});

    		return false;
    	});

    $("#commentForm").submit(function() {
			var formStr = $("#articleForm").serialize();

			$.ajax({
				type:"POST",
				url:"/content/index/addcomment",
				data:formStr,
				success: function(msg){
					$("#commentForm").html(msg);
				},
				beforeSend:function(msg){
					$("#commentForm").html("Sending data Please Wait...");
				}

			});

    		return false;
    	});


    $("#articleEmailForm").submit(function() {
			var formStr = $("#articleEmailForm").serialize();

			$.ajax({
				type:"POST",
				url:"/content/index/emailForm",
				data:formStr,
				success: function(msg){
					$("#EmailFormBlock").html(msg);
				},
				beforeSend:function(msg){
					$("#EmailFormBlock").html("Sending data Please Wait...");
				}

			});
    		return false;
    	});

    $("#BlogarticleEmailForm").submit(function() {
			var formStr = $("#BlogarticleEmailForm").serialize();

			$.ajax({
				type:"POST",
				url:"/blogs/index/emailForm",
				data:formStr,
				success: function(msg){
					$("#EmailFormBlock").html(msg);
				},
				beforeSend:function(msg){
					$("#EmailFormBlock").html("Sending data Please Wait...");
				}

			});
    		return false;
    	});




    initJQ();

}

function navRollOver() {
	$(this).parent().find("> a").addClass("active");
}

function navRollOut() {
	$(this).parent().find("> a").removeClass("active");
}

function tabClick(tab, contentContainers, selectedContent) {
	//Remove the "active" state from all tabs.
	$(tab).parent().parent().find("a").removeClass("active");

	//Re-add the "active" state to the selected tab.
	$(tab).addClass("active");

	//Hide all non-selected content.
	$(contentContainers).removeClass("active");

	//Show selected content.
	$(selectedContent).addClass("active");
}

// Print Script
function printPage() {
  if (window.print)
    window.print();
  else
    alert("Sorry, your browser doesn't support this feature. \nDésolé, votre navigateur ne peut pas prendre cette fonction en charge.");
}

var popupStatus = 0;

//loading popup with jQuery magic!
function jQloadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#jQbackgroundPopup").css({
			"opacity": "0.7"
		});
		$("#jQbackgroundPopup").fadeIn("slow");
		$("#jQpopupForm").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function jQdisablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#jQbackgroundPopup").fadeOut("slow");
		$("#jQpopupForm").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function jQcenterPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#jQpopupForm").height();
	var popupWidth = $("#jQpopupForm").width();
	//centering
	$("#jQpopupForm").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6

	$("#jQbackgroundPopup").css({
		"height": windowHeight
	});

}


function initJQ()
{
	//LOADING POPUP
	//Click the button event!
	$("#article_email").click(function(){
		//centering with css
		jQcenterPopup();
		//load popup
		jQloadPopup();
	});

	//CLOSING POPUP
	//Click the x event!
	$("#jQpopupFormClose").click(function(){
		jQdisablePopup();
	});
	//Click out event!
	$("#jQbackgroundPopup").click(function(){
		jQdisablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			jQdisablePopup();
		}
	});
}