// TODO - BS - Add generic error checking on page load. 
	// Look for error object written by server side into javascript
	// If a custom error handling function is defined for this page, use that, otherwise osme standard notification

// JavaScript Document
function MM_preloadImages() { //v3.0
    var d=document;
    if(d.images) {
        if(!d.MM_p)
            d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
        for (i=0; i<a.length; i++) {
            if (a[i].indexOf("#")!==0) {
                d.MM_p[j]=new Image;
                d.MM_p[j].src=a[i];
                j++;
            }
        }
    }
}

function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr;
    for (i=0; a && i < a.length && (x=a[i]) && x.oSrc; i++) {
        x.src=x.oSrc;
    }
}

function MM_findObj(n, d) { //v4.01
    var p,i,x;
    if (!d) {
        d=document;
    }
    if ((p=n.indexOf("?")) > 0 && parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document;
        n=n.substring(0,p);
    }
    if (!(x=d[n])&&d.all)
        x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) {
        x=d.forms[i][n];
    }
    for (i=0;!x&&d.layers&&i<d.layers.length;i++) {
        x=MM_findObj(n,d.layers[i].document);
    }
    if (!x && d.getElementById) {
        x=d.getElementById(n);
    }
    return x;
}

function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments;
    document.MM_sr=new Array;
    for (i=0;i<(a.length-2);i+=3) {
        if ((x=MM_findObj(a[i]))!==null) {
            document.MM_sr[j]=x;
            j++;
            if (!x.oSrc) {
                x.oSrc=x.src;
            }
            x.src=a[i+2];
        }
    }
}

function addslashes(str) {
    str=str.replace(/\'/g,'\\\'');
    str=str.replace(/\"/g,'\\"');
    str=str.replace(/\\/g,'\\\\');
    str=str.replace(/\\0/g,'\\0');
    return str;
}

function stripslashes(str) {
    str=str.replace(/\\'/g,'\'');
    str=str.replace(/\\"/g,'"');
    str=str.replace(/\\\\/g,'\\');
    str=str.replace(/\\0/g,'\0');
    return str;
}

function htmlentities(s) {
    var str = new String(s);
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    return str;
}


/**
 * for panel +/- (vaL 050809)
 */
function panel_toggle(obj){
	var parent = $(obj).parents('.panel');
	var div;
	if(parent.length){
		div = $(parent[0]).children('.panel_body');
		if(div.length){
			$(div[0]).toggleClass('panel_body_hide');	
		}
	}
	
	$(obj).children('.panel_toggle').toggleClass('panel_toggle_plus');
}


$(function (){
	// convenient javascript function for testing "empty"
	$.empty = function( mixed_var ) {

	    var key;

	    if (mixed_var === "" ||
	        mixed_var === 0 ||
	        mixed_var === "0" ||
	        mixed_var === null ||
	        mixed_var === false ||
	        mixed_var === undefined )
		{
	        return true;
	    }

	    if (typeof mixed_var == 'object') {
	        for (key in mixed_var) {
	            return false;
	        }
	        return true;
	    }

	    return false;
	};

	// ajax default error handling

	$.ajaxSetup({
		dataType: "json",
		error: function () {
			$.FLAjaxConnectionError();
		}
	});
	
	$.FLAjaxConnectionError = function() {
		$("<p>There was a network or server error. Please try again later.</p>").dialog({title: "Error", modal: true, resizable: false, buttons: { 'Ok': function() { $(this).dialog("close"); } }});
	};

	// this can be used inside the 
	$.FLCheckAjaxError = function(data) {
		if (!$.empty(data.error_code)) {
			$("<p>" + data.error_message + "</p>").dialog({title: "Error", modal: true, resizable: false, buttons: { 'Ok': function() { $(this).dialog("close"); } }});
			return true;
		} else {
			return false;
		}
	};

	if (jQuery.validator){
		jQuery.validator.addMethod("thisManyNumbers", function(value, element, param) { 
			var pattern = new RegExp('[0-9]{' + param + '}');
		 return this.optional(element) || pattern.test(value);
		}, jQuery.format("Please enter at least  {0} number(s) in your password."));
		jQuery.validator.addMethod("thisManyLetters", function(value, element, param) { 
			var pattern = new RegExp('[a-zA-Z]{' + param + '}');
		 return this.optional(element) || pattern.test(value);
		}, jQuery.format("Please enter at least  {0} letters(s) in your password."));
	}
	
	if(Server.error_code) {
		$("<p>" + Server.error_message + "</p>").dialog({
			title: "Error", modal: true, resizable: false, 
			buttons: { 'Ok': function() { $(this).dialog("close"); } }
		});
	}

	if(Server.info_code) {
		$("<p>" + Server.info_message + "</p>").dialog({
			title: "Information", modal: true, resizable: false, 
			buttons: { 'Ok': function() { $(this).dialog("close"); } }
		});
	}
	
	$("#header_logout").click(function() {
	    try {
	        FB.Connect.ifUserConnected(
	        		function() { FB.Connect.logoutAndRedirect(Server.site_url + '/logout'); },
	        		Server.site_url + '/logout');
	    }
	    catch (e) {
	    	location.href = Server.site_url + '/logout';	    	
	    }
        
	});
	
	$("#verify_email_sent").dialog({
	        title: "Confirmation Email Sent", 
	        dialogClass: "signup", 
	        modal: true, autoOpen: false, width: 500, resizable: false, 
	        buttons: { 
                'Close': function() {
                    $(this).dialog("close");
		        }
	        }
	});

    $('#resend_email_link').click(function(){
        $("#resend_email").ajaxSubmit({
            success : function(data){
                if (data.error_code > 0) {
                    $.FLCheckAjaxError(data);
                }
                else {
                    $("#verify_email_sent").dialog('open');
                }
            },
            error : function () {
                $.FLAjaxConnectionError();
            }
        });
    });

});


/*
 * for tooltips, all elements with a class '.with_tooltip' will get a tooltip
 * the tooltip's text/innerHTML can be the element's "alt" value or its <em> child
 * 
 * alt for 'text only' tooltips
 * <em> child is available because sometimes, the 'alt' contains an fb:name (&the like)
 */
$(document).ready(function(){
	$(".with_tooltip").each(function(i){
		var tooltip_text = $(this).attr('alt');
		if(tooltip_text){
			$(this).append('<em><span>'+tooltip_text+'</span></em>');
		}
		$(this).css({'position':'relative'});
	});
	
	$(".with_tooltip").hover(function(){
		if($(this).children("em").text()){
			$(this).children("em").show();
		}
	},function(){
		$(this).children("em").hide();
	});
	
	//check if the header is `tall_header`
	var isTall = $('.tall_header').length;
	if (isTall) {
		//when it's not the tall header, the page_content_container is padding:75px 0 0
		//because the header is position:absolute
		//but since the tall header is not position:absolute, we don't need that padding
		
		if ($('#page_content_container').hasClass('homepage')) {
			$('#page_content_container').css({'background-position':'100% 0','padding-top':'0'});
		}
		else {
			$('#page_content_container').css({'background-position':'100% 0','padding-top':'50px'});
		}
	}
});
