// JavaScript Document
$(document).ready(function(){//alert(pageTracker);
	var msisdn;
	var msisdn_status;
	var mmenu_links = new Array($("#mm0"),$("#mm1"),$("#mm2"),$("#mm3"),$("#mm4"),$("#mm5"));
	//<img src="img/div-content-top-stripe-history.jpg" width="920" height="60" />
	var content_top_stripes_names = new Array("home","prizes","history","winners","tc","faq");
	var content_top_stripes_img = new Array();
	
	// Used to make stripe reposition on Window resize
	var current_mm_id = 0;
	
	$.preloadImages = function()
	{
		for(i=0; i<content_top_stripes_names.length; i++)
		{
			content_top_stripes_img[i] = new Image();
			content_top_stripes_img[i].src = "img/div-content-top-stripe-" + content_top_stripes_names[i] + ".jpg";
			content_top_stripes_img[i].width = "920";
			content_top_stripes_img[i].height = "60";
		}
	}
	$.preloadImages();
	
	// Initiate default settings
	placeStripeSelected($("#mm0"), false);
	$("#div-top-stripe").html(content_top_stripes_img[0]);
	setContentHeaders();
	$("#div-content").load("inc/home.php", "", function(){/*writeSWF("#home-swf");*/});
	
	$("#mm0").click(function()
	{
		clearMessageDiv();
		$("#div-content").load("inc/home.php", "", function(){placeStripeSelected($("#mm0")); /*writeSWF("home-swf");*/});
		//alert(msisdn);
	});
	
	$("#mm1").click(function()
	{
		clearMessageDiv();
		$("#div-content").load("inc/prizes.php", "", function()
		{
			placeStripeSelected($("#mm1"));

			// Google analytics [still testing]
			pageTracker._trackPageview();
		});
	});
	
	$("#mm2").click(function()
	{
		clearMessageDiv();
		if(msisdn_status == "ok")
		{
			$("#div-content").load("inc/history.php","",function (){setSubmitButton("submit_pin"); placeStripeSelected($("#mm2"));});
			pageTracker._trackPageview('inc/history.php');
		}else{
			$("#div-content").load("inc/history.php","",function (){setSubmitButton("submit_msisdn"); placeStripeSelected($("#mm2"));});
			pageTracker._trackPageview('inc/history.php');
		}
	});
	
	$("#mm3").click(function()
	{
		clearMessageDiv();
		$("#div-content").load("inc/winners.php", "", function(){placeStripeSelected($("#mm3")); animateRows();});
		pageTracker._trackPageview('inc/winners.php');
	});
	
	$("#mm4").click(function()
	{
		clearMessageDiv();
		$("#div-content").load("inc/terms-conditions.php", "", function(){placeStripeSelected($("#mm4"));});
		pageTracker._trackPageview('inc/terms-conditions.php');
	});
	
	$("#mm5").click(function()
	{
		clearMessageDiv();
		$("#div-content").load("inc/faq.php", "", function(){placeStripeSelected($("#mm5"));});
		pageTracker._trackPageview('inc/faq.php');

	});
	
	// Currently disabled
	$("#logout").click(function()
	{
		$.ajax({
				  cache:"false",
				  type:"POST", 
				  url:"inc/session_reset.php",
				  success:function(msg){$("#div-content").load("inc/history.php","",function (){
																							  setSubmitButton("submit_msisdn");
																							  clearMessageDiv();
																							  $("#div-top-stripe").html('<img src="img/div-content-top-stripe-' + content_top_stripes_names[2] + '.jpg" width="920" height="60" />');
																							  placeStripeSelected($("#mm2"));
																							  })}
		});
	});
	
	function setSubmitButton(var_name)
	{
		disableFormAction();
		var data_set;
		
		if($("#history-data"))
		{
			animateHistoryData();
		}
		
		$("#submit-button").click(function()
		{	//alert("clicked");
		
			$("#preloader").html('<img src="img/preloader.gif" width="16" height="16" />');
			$("#history-feedback").html("");
			
			if(var_name == "submit_msisdn")
			{
				msisdn = $("#msisdn").val();
				data_set = "msisdn=" + $("#msisdn").val();
			}else if(var_name == "submit_pin")
			{
				data_set = "msisdn=" + msisdn + "&pin=" + $("#pin").val();
				//alert(data_set);
			}else{
				alert("Error: Data missing");
			}
			$.ajax({
				cache:"false",
				type:"POST", 
				url:"inc/proxy.php",
				data:data_set,
				success: function(msg)
				{
					$.ajax({
						   cache:"false", 
						   type:"POST", 
						   url:"inc/updateSession.php", 
						   data: var_name + "=" + msg, 
						   success: function(msg_updt){
							    //alert(msg_updt);
								function displayMessage()
								{
									//$("#history-feedback").html("msg_updt:" + msg_updt);
									//$("#history-feedback").html("Status:" + msg_updt);
									$("#preloader").html('');
									
									if(msg_updt == "msisdn_ok")
									{
										$("#div-content").load("inc/history.php","",function (){setSubmitButton("submit_pin");});
										msisdn_status = "ok";
									}
									else if(msg_updt.substr(0,6) == "pin_ok")
									{
										msg_updt_arr =  msg_updt.split("|");
										$.ajax({
											type: "POST",
											url: "inc/history.php",
											data: "ticket_num_daily=" + msg_updt_arr[1] + "&ticket_num_weekly=" + msg_updt_arr[2] + "&ticket_num_monthly=" + msg_updt_arr[3] + "&points=" + msg_updt_arr[4],
											success: function(msg_values){
												$("#div-content").html(msg_values);
												animateHistoryData();
											}
										});
									}
									else if(msg_updt.substr(0,10) == "pin_not_ok")
									{
										$("#history-feedback").html('<span style="color:#000; font-weight:bold">Status: </span>' + msg_updt.substr(10,msg_updt.length));
									}else{// When the MSISDN fails
										$("#history-feedback").html('<span style="color:#000; font-weight:bold">Status: </span>' + msg_updt);
										//setSubmitButton("submit_msisdn");
									}
								}
							    //$("#div-content").load("yhistory.php", "", displayMessage);
								displayMessage();
						   }
					});
				},
				error:function(xmlhttp)
				{
					alert('An HTTP error '+ xmlhttp.status +' occured.\n');
				}
				
			});							
		});
	}
	
	// 
	function animateHistoryData()
	{
		var li_array = $("#history-data > li");
		//var ul_length = $("#history-data > li").length;
		var veloc = 200;
		counter = 1;
		
		$.each(li_array, function(){
			$(this).css({"opacity":"0"});
			$(this).show();
			$(this).fadeTo(veloc * counter, 1);
			counter++;
		});
	}
		
	// Avoid default Enter key event
	function disableFormAction()
	{
		$('form').submit(function()
		{
			return false;
		});
	}
	
	function placeStripeSelected(el,is_anim)
	{
		var pos_left = el.offset().left;
		if(is_anim == false)
		{
			$("#stripe-inner").css({"left":pos_left + "px"});
		}else if(is_anim == null){
			$("#stripe-inner").animate({ 
			left: pos_left + "px"
			}, 500);
		}
		
		current_stripe_x_pos = el.offset().left;
		current_mm_id = (el.attr("id")).split("mm");
		current_mm_id = current_mm_id[1];
		//window.status = current_mm_id[1];
		//alert(current_stripe_x_pos);
	}
	
	// Reposition stripe when browser window is resized
	$(window).bind("resize", repositionStripe);
	function repositionStripe(e) {
		$("#stripe-inner").css({"left":($("#mm" + current_mm_id).offset().left) + "px"});
	}
	
	// 
	function setContentHeaders()
	{
		for(i=0; i<mmenu_links.length; i++)
		{
			mmenu_links[i].data("idx",i);
			mmenu_links[i].click(function()
			{
				$("#div-top-stripe").html(content_top_stripes_img[$(this).data("idx")]);
			});
			
		}
	}
	
	// Clear returned Errors from Submit actions
	function clearMessageDiv()
	{
		$("#history-feedback").html("");
	}
	
	// Animate Winners Table Rows
	function animateRows()
	{
		var veloc = 10;
		var counter = 1;
		var div_array = $(".table-winners div");
		$.each(div_array, function(){
			//alert($(this));
			$(this).css({"opacity":"0"});// Filter none for IE
			$(this).show();
			$(this).fadeTo(veloc * counter, 1, fixIE);
			
			function fixIE()
			{
				$(this).css({"filter":"none"});// IE < 8
				
				// This is not a fix for IE
				$(this).parent(".table-winners").css({"border-bottom":"none"});
			}
			
			counter++;
		});
	}
	
	function writeSWF(swf_id)
	{
        if($.browser.msie && ($.browser.version < 8))
		{
			$.getScript('js/write_swf.js');
			//alert($.browser.version);
		}else{
			//alert($.browser.version);
		}
	}
	
});// End JQuery ready function
