// Google Analytics 
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25219866-1']);
_gaq.push(['_trackPageview']);

(function() 
{
	var ga = document.createElement('script'); 
	ga.type = 'text/javascript'; 
	ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();



var g_fSupportConsole = ($.browser.safari && $.browser.webkit);
var g_strSubmittedEmail = null;

function FadeOutMessageBox(duration)
{
	var MsgBox = $("div#messageBox");
	MsgBox.fadeOut(duration);
	return true;
}

function FadeOutFeedbackMessageBox(duration)
{
	var MsgBox = $("div#feedbackMessageBox");
	MsgBox.fadeOut(duration);
	return true;
}

function PositionFeedbackTab()
{
	var docHeight = $(document).height();
    var docWidth = $(document).width();
    var feedback = $("div#feedback");
    var feedbackTab = $("div#feedbackTab");
    var feedbackCont = $("div#feedbackCont");
	
	var leftPos = docWidth - feedbackCont.width() - 20;
	var topPos = docHeight - feedbackTab.height() - $("div#footer").outerHeight();
	feedback.offset({top: topPos, left: leftPos});
	
	return true;
}

var g_fExpanded = false;
var g_strFeedbackDefaultValue = "Let us know if you have any questions or comments!";

function FeedbackTabOnClick(e)
{
	var feedback = $("div#feedback");
	var feedbackCont = $("div#feedbackCont");
	var offset = feedbackCont.height();
	
	if(!g_fExpanded)
	{
		feedback.animate({top: ("-=" + offset + "px")}, "slow");
		g_fExpanded = true;
	}
	else
	{
		feedback.animate({top: ("+=" + offset + "px")}, "slow");
		g_fExpanded = false;
	}	
	
	return true;
}

var bgResizeFunction;

$(document).ready(
    function(e)
    {
        /*
        var docHeight = $(document).height();
        var docWidth = $(document).width();
        */
       
       	var docHeight = $(document).height();
       	var docWidth = $(document).width(); 
        
        var bgImg = $("img#bgImage");
        var bgImgDiv = $("div#bgImage");
        var mainContent = $("div#mainContent");
        var mainContentBg = $("div#mainContentBg");
        var incidentLogo = $("div#incidentLogo");
		var footer = $("div#footer");
		var overlay = $("div#overlay");
        /*
        if(!$.browser.webkit)
        {
			bgImgDiv.width(200);
			bgImgDiv.height(200);
			
			bgImg.attr('height', '100%');
			mainContent.css('color', 'green');
		}
		*/							
        
        footer.css('margin-top', parseInt(-1 * (footer.outerHeight())));
        
        bgResizeFunction = function(e)
        {
            // get rid of this
    		bgImg.removeAttr('width');
    		
    		var newHeight = $(document).height();
        	var newWidth = $(document).width();
    		                  
            var bgImgAspectRatio = bgImg.width() / bgImg.height();
            var docAspectRatio = newWidth / newHeight;
			
			if(g_fSupportConsole)
            	console.log("img aspect ratio: " + bgImgAspectRatio + " doc aspect: " + docAspectRatio);
            	
            var bgImgWidth = bgImg.width();
			var bgImgHeight = bgImg.height();					
			
			if(bgImgAspectRatio < docAspectRatio)
			{
                var newImgWidth = newWidth;
                
                var mFactor = bgImgWidth / newImgWidth;
                var projImgHeight = bgImgHeight / mFactor;                                                                          
                
                if(projImgHeight < newHeight)
                {
                	console.log("aie");
                	var multiplyFactor = projImgHeight / newHeight;
                	newImgWidth = newImgWidth * multiplyFactor;
                }
                
                bgImg.width(newImgWidth * 1.0);
                bgImg.height(projImgHeight);
                
                var newImgHeight = bgImg.height();                
                
                if(newImgHeight > newHeight)
                {
                	var adjHeight = (newImgHeight - newHeight) / 2 + $("div#footer").outerHeight();
                	bgImgDiv.css('top', parseInt(-1 * adjHeight));
                }                                                            	
            }
            else
            {
                var newImgHeight = newHeight;
                
                var mFactor = bgImgHeight / newImgHeight;
                var projImgWidth = bgImgWidth / mFactor;                                                                          
                
                if(projImgWidth < newWidth)
                {
                	console.log("aiew");
                	var multiplyFactor = projImgWidth / newWidth;
                	newImgHeight = newImgHeight * multiplyFactor;
                }
                
                bgImg.height(newImgHeight * 1.0);
                bgImg.width(projImgWidth);
                var newImgWidth = bgImg.width();  
                              
                
                if(newImgWidth > newWidth)
                {
                	var adjWidth = (newImgWidth - newWidth) / 2;
                	bgImgDiv.css('left', parseInt(-1 * adjWidth));
                }                   
            }	                                  	
        };
        
        bgImg.load(bgResizeFunction);
        
        // feedback tab
        overlay.ready(
        	function(e)
        	{
        		$("div#feedbackTab").click(
        			function(e)
        			{
        				return FeedbackTabOnClick(e);
        			}
        		)
        		
        		var feedbackMsgBox = $("div#feedbackMessageBox");
        		feedbackMsgBox.hide();
        		
				
				var feedbackInput = $("textarea#feedbackInput");
				feedbackInput.focus(
					function(e)
				    {
				        if(feedbackInput.val() == g_strFeedbackDefaultValue)
				            feedbackInput.val('');
						feedbackInput.css('color', 'black');
				
				    }
			   	);
				
			   	feedbackInput.blur(
		        	function(e)
			        {
			            if(feedbackInput.val().length == 0)
			            {
			                feedbackInput.val(g_strFeedbackDefaultValue);
			                feedbackInput.css('color', 'gray');
			            }
			        }
			   	);     
                  		
        		PositionFeedbackTab();
        		
        		var tFeedbackOut = null;
               	var tFeedbackOutTime = 4000;
        		
        		/*
        		if(!$.browser.webkit)
        		{
        			// keydown the document if in IE7 or something without webkit
        			$(document).keydown(
        				function(e)
	               		{
	               			if(e.keyCode == '13')
	               			{
	               				e.preventDefault();
	               				inputEmailSubmit.click();
	               			}
	               		}	
        			);	
        		}
        		*/	
               
               var feedbackSubmit = $("div#feedbackSubmit");
               
               feedbackInput.keydown(
               		function(e)
               		{
               			if(e.keyCode == '13')
               			{
               				e.preventDefault();
               				feedbackSubmit.click();
               			}
               		}
               );
        		
        		// feedback submit 
        		
        		feedbackSubmit.click(
        			function(e)
        			{
        				console.log("feedback submit");
        				
        				var strFeedback = feedbackInput.val();
                       // revert to old school js
                       	if(!$.browser.webkit)                       	        					
        					strText = document.getElementById('feedbackInput').value;        				                       
                       
                       var postValues = {strFeedback: strFeedback};
                       if(g_strSubmittedEmail != null && g_strSubmittedEmail.length > 0)
                       		postValues.strEmail = g_strSubmittedEmail;
                       
                       console.log(postValues);
                       
                       	
               			
               			if(tFeedbackOut != null)
                       		clearTimeout(tFeedbackOut);
                       	
                       msgBox.fadeOut('fast');
                       
                       $.post("SubmitFeedback.php", postValues,
                            function(data)
                            {
                                console.log(data);
                                if(data.ACK == undefined)
                                {                                
                                    
                                    feedbackMsgBox.text("Failed to submit feedback, try again!");
                                    feedbackMsgBox.css('background-color', 'red').css('color', 'white');
                                    feedbackMsgBox.fadeIn('slow');
                                    
                                    tFeedbackOut = setTimeout("FadeOutFeedbackMessageBox('slow')", tFeedbackOutTime);
                                    
                                    return false;
                                }
                                else if(data.ACK.toLowerCase() != 'success')
                                {                                    
                                    feedbackMsgBox.text(data.msg);
                                    feedbackMsgBox.css('background-color', 'red').css('color', 'white');
                                    feedbackMsgBox.fadeIn('slow');
                                    
                                    tFeedbackOut = setTimeout("FadeOutFeedbackMessageBox('slow')", tFeedbackOutTime);
                                    
                                    return false;
                                }
                                else
                                {
                                    //console.log("email added as id: " + data.id);     
                                    if(data.email != undefined && data.email.length > 0)                               
                                    	feedbackMsgBox.text("Thanks, " + data.email + "!" );
                                    else
                                    	feedbackMsgBox.text("Thanks!");
                                    	
                                    feedbackMsgBox.css('background-color', 'green').css('color', 'white');
                                    feedbackMsgBox.fadeIn('slow');
                                    
                                    tFeedbackOut = setTimeout("FadeOutFeedbackMessageBox('slow')", tFeedbackOutTime);
                                    
                                    return true;
                                }
                            }, "json"
                       );
        			}
        		);
        	}
        );
        
        // Handle the resize function
        $(window).resize(
        	function(e)
        	{
        		PositionFeedbackTab();
        		
        		var newHeight = $(document).height();
        		var newWidth = $(document).width();
        		
        		// resize image
               	bgResizeFunction(e);
        
        		var newLeft = (newWidth / 2) - (mainContent.width() / 2);
                var newTop = (newHeight / 2) - (mainContent.height() / 2);
                mainContent.offset({ top: newTop, left: newLeft});                
                mainContentBg.offset({ top: newTop, left: newLeft});
                
                
        	}
        );
        
        mainContent.ready(
            function(e)
            {                                            
                /*
                if(!$.browser.webkit)
        			mainContent.css('background', 'black');
        			*/
                
                mainContent.css("position", "absolute");
                var newLeft = (docWidth / 2) - (mainContent.width() / 2);
                var newTop = (docHeight / 2) - (mainContent.height() / 2);
                mainContent.offset({ top: newTop, left: newLeft});                
                mainContentBg.offset({ top: newTop, left: newLeft});
                
                if($.browser.mozilla)
                {
                	$("div#incidentLogo").css('margin-top', '5px');
                	$("div#mainContent").css('font-weight', '100px');                	
                	incidentLogo.css('top', '-5px').css('padding-top', '5px');
                	var tempHeight = $("div#emailInputSubmit").height();
                	tempHeight -= 3;
                	$("div#emailInputSubmit").css('padding-top', '9px').height(tempHeight);
                }
                
                if($.browser.msie)
                {
                	//mainContentBg.css('background', 'green');
                	mainContentBg.css('background', 'black');
                	var jobs = $("div#jobs");
                	jobs.css('padding-top', '9px').css('padding-bottom', '11px');  
                	$("input#emailInputFormIn").css('padding-top', '5px');              
                }
            }
        );
        
        // Set up and hide the message box
        var msgBox = $("div#messageBox");
        msgBox.ready(
        	function(e)
        	{
        		msgBox.hide();
        	}
        );

        $("div#emailInput").ready(
            function(e)
            {
               var inputEmail = $("input#emailInputFormIn");
               
               if(g_fSupportConsole)
               		console.log("div#emailInput.ready");
               		
               inputEmail.css('color', 'gray');
               var strDefaultValue = "your@email.com";
               inputEmail.val(strDefaultValue);
               inputEmail.focus(
                    function(e)
                    {
                        if(inputEmail.val() == strDefaultValue)
                            inputEmail.val('');
                        inputEmail.css('color', 'black');

                    }
               );

               inputEmail.blur(
                    function(e)
                    {
                        if(inputEmail.val().length == 0)
                        {
                            inputEmail.val(strDefaultValue);
                            inputEmail.css('color', 'gray');
                        }
                    }
               );
               
               var inputEmailSubmit = $("div#emailInputSubmit");
               
               if(!$.browser.webkit && !$.browser.mozilla)
               {
               		inputEmailSubmit.height(inputEmailSubmit.height() + 13);	// Adjust for browser diffs
               	}
               
               	if(!$.browser.webkit)
        		{
        			// keydown the document if in IE7 or something without webkit
        			$(document).keydown(
        				function(e)
	               		{
	               			if(e.keyCode == '13')
	               			{
	               				e.preventDefault();
	               				inputEmailSubmit.click();
	               			}
	               		}	
        			);	
        		}	
               
               inputEmail.keydown(
               		function(e)
               		{
               			if(e.keyCode == '13')
               			{
               				e.preventDefault();
               				inputEmailSubmit.click();
               			}
               		}
               );

               
               var tOut = null;
               var tOutTime = 4000;
               
               inputEmailSubmit.click(
                    function(e)
                    {
                       if(tOut != null)
                       	clearTimeout(tOut);
                       	
                       msgBox.fadeOut('fast');
                       var strText = inputEmail.val();
                       
                       	if(!$.browser.webkit)
                       	{
        					// revert to old school js
        					strText = document.getElementById('emailInputFormIn').value;
        				}
                       
                       
                       $.getJSON("AddUserSignup.php", {email:strText},
                            function(data)
                            {
                                //console.log(data);
                                if(data.ACK == undefined)
                                {                                
                                    msgBox.text("Failed to add email");
                                    msgBox.css('background-color', 'red').css('color', 'white');
                                    msgBox.fadeIn('slow');
                                    
                                    tOut = setTimeout("FadeOutMessageBox('slow')", tOutTime);
                                    return false;
                                }
                                else if(data.ACK.toLowerCase() != 'success')
                                {
                                    msgBox.text(data.msg);
                                    msgBox.css('background-color', 'red').css('color', 'white');
                                    msgBox.fadeIn('slow');                                                                        
                                    
                                    tOut = setTimeout("FadeOutMessageBox('slow')", tOutTime);
                                    return false;
                                }
                                else
                                {
                                    //console.log("email added as id: " + data.id);
                                    msgBox.text("Thanks!");
                                    msgBox.css('background-color', 'green').css('color', 'white');
                                    msgBox.fadeIn('slow');
                                    
                                    // Save the email globally
                                    g_strSubmittedEmail = strText;
                                    var oldStrFeedback = g_strFeedbackDefaultValue;
                                    g_strFeedbackDefaultValue = "Let us know what you think, " + g_strSubmittedEmail;
                                     
                                    var feedbackInput = $("textarea#feedbackInput");
                                    if(feedbackInput.val() == oldStrFeedback)
                                    	feedbackInput.val(g_strFeedbackDefaultValue);				
                                                                                                            
                                    tOut = setTimeout("FadeOutMessageBox('slow')", tOutTime);
                                    return true;
                                }
                            }
                       );
                            
                    }
               );
            }
        );
    }
);

