var modal_box_type = "signup";
var client_logged_in = false;
var new_register = false;
var redirectUrl;

function setRedirectUrl(newRedirectUrl){
	redirectUrl = newRedirectUrl;
}

function getRedirectUrl(){
	return redirectUrl;
}

function getModalBoxType(){
	return modal_box_type;
}

function isNewRegister(){
	return new_register;
}

function setNewRegister(val){
	new_register = val;
}

function loadContent(filePath){	
	//$('#content').slideUp('slow');
	$('#content').empty();
	
	//google analytics log function for ajax loads
	$(document).ready( function () {
		pageTracker._trackPageview(filePath); 
	});
	
	document.getElementById('content').innerHTML = 
		"<div style='width: 60px; text-align: center; margin-left: auto; margin-right: auto;'>" +
			"<img src='assets/loading/loading.gif' style='margin-left: auto; margin-right: auto'/>"
		"</div>";
	
	
	var result =$.post(filePath, 
		function(data){
			$('#content').html(data);
			//$('#content').slideDown('slow');
		}
	);
	
	
	
}

function loadContentAndBookmark(filePath, newBookmark){
	
	if(newBookmark){
		dhtmlHistory.add(newBookmark);
	}
	else{
		dhtmlHistory.add(filePath.split('/')[0],'link');
	}
	document.title = "Crave - Know what's up";
	
	loadContent(filePath);
}

function isLoggedIn(){
	return client_logged_in;
}

function setLoggedIn(value, signedInName){
	client_logged_in = value;
	if(value == true){
		if(signedInName){
			document.getElementById('signin').innerHTML = "Sign Out " + signedInName;
		}
		else{
			document.getElementById('signin').innerHTML = "Sign Out";
		}
	}
	else{
		document.getElementById('signin').innerHTML = "Sign In";
	}
}

function click_logo(){
	loadContentAndBookmark('home/content.jsp');
}

function click_sign_in(){
	if(client_logged_in == false){
		openModalWindow('login');
	}
	else{
		$.post('login/logout.jsp', {}, 
			function(data){
				setLoggedIn(false);
				loadContentAndBookmark('home/content.jsp');
			}
		);
	}
}

function click_feedback(){
	openModalWindow('feedback');
}

function openModalWindow(window_type){
	modal_box_type = window_type;
	if(window_type == 'login' || window_type == 'signup'){		
		jQuery.get('login/login_content.jsp', function(data) {
			grayOut(true, {'zindex':'50', 'bgcolor':'#000000', 'opacity':'90'});
		    $.facebox(data);
		});
	}
	else if(window_type == 'changenumber'){
		jQuery.get('settings/change_number.jsp', function(data) {
			grayOut(true, {'zindex':'50', 'bgcolor':'#000000', 'opacity':'90'});
		    jQuery.facebox(data);
		});
	}
	else if(window_type == 'feedback'){
		jQuery.get('feedback/feedback_html.jsp', function(data) {
			grayOut(true, {'zindex':'50', 'bgcolor':'#000000', 'opacity':'90'});
		    jQuery.facebox(data);
		});
	}
	else{
		jQuery.get('howto/content.jsp', function(data) {
			grayOut(true, {'zindex':'50', 'bgcolor':'#000000', 'opacity':'90'});
		    jQuery.facebox(data);
		});
	}
	$('#flash_container').css("visibility","hidden");
	if(isIE6){
		$('#get_started_button').css("display","none");
	}
}

function closeModalWindow(){
	jQuery.facebox.close();
}
	
function grayOut(vis, options) {
	  // Pass true to gray out screen, false to ungray
	  // options are optional.  This is a JSON object with the following (optional) properties
	  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
	  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
	  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
	  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
	  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
	  // in any order.  Pass only the properties you need to set.
	  var options = options || {}; 
	  var zindex = options.zindex || 50;
	  var opacity = options.opacity || 70;
	  var opaque = (opacity / 100);
	  var bgcolor = options.bgcolor || '#000000';
	  var dark=document.getElementById('darkenScreenObject');
	  if (!dark) {
	    // The dark layer doesn't exist, it's never been created.  So we'll
	    // create it here and apply some basic styles.
	    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
	    var tbody = document.getElementsByTagName("body")[0];
	    var tnode = document.createElement('div');           // Create the layer.
	        tnode.style.position='absolute';                 // Position absolutely
	        tnode.style.top='0px';                           // In the top
	        tnode.style.left='0px';                          // Left corner of the page
	        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
	        tnode.style.display='none';                      // Start out Hidden
	        tnode.id='darkenScreenObject';                   // Name it so we can find it later
	    tbody.appendChild(tnode);                            // Add it to the web page
	    dark=document.getElementById('darkenScreenObject');  // Get the object.
	  }
	  if (vis) {
	    // Calculate the page width and height 

	    
	    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
	        var pageWidth = (document.body.scrollWidth)+'px';
	        var pageHeight = (document.body.scrollHeight)+'px';
	    } else if( document.body.offsetWidth ) {
	      var pageWidth = (document.body.offsetWidth)+'px';
	      var pageHeight = (document.body.offsetHeight)+'px';
	    } else {
	       var pageWidth='100%';
	       var pageHeight='100%';
	    } 
		  //var pageWidth = $("html").css("width");
		 //var pageHeight = $("html").css("height");
	    $("body").css("background","white");
	    $("html").css("background","#191919");
	    //$("html").css("background","#191919");
	    //set the shader to cover the entire page and make it visible.
	    dark.style.opacity=opaque;                      
	    dark.style.MozOpacity=opaque;                   
	    dark.style.filter='alpha(opacity='+opacity+')'; 
	    dark.style.zIndex=zindex;        
	    dark.style.backgroundColor=bgcolor;  
	    dark.style.width= pageWidth;
	    dark.style.height= pageHeight;
	    dark.style.display='block';                          
	  } else {
	     dark.style.display='none';
	  }
}

$(document).ready(
	function(){
		$('#side_bar').hover(
			function() {
				$('#side_bar').css("background-position", "30px 0px");
				$('#side_bar').css("width", "30px");
			},
			function () {
		    	$('#side_bar').css("background-position", "0px 0px");
		    	$('#side_bar').css("width", "27px");
		    }
		);
	}
);