/***************************  
   Author: Luis Tello
   version: 1.5
   copyright 2011
****************************/

var Global = {
    bodyClass:null,
    bodyId:null,
    defaultFontSize:null,
    defaultLineHeight:null,
    increaseFontSize:null,
    increaseLineHeight:null,
    expireDays:null,
    ipAddress: null,
    fontSize: null,
    fontHeight: null,
    
    construct: function(){
        var that = this;
        this.setBodyClass($('body').attr("class"));
        this.setBodyId($('body').attr("id"));
        $('#print').click(function(){
            that.print();
        });
        $('#etp').click(function(){
            that.etp();
        });
        this.expireDays = 7;

        this.defaultFontSize = "12px";
        this.increaseFontSize = "14px";

        this.defaultLineHeight = "24px";
        this.increaseLineHeight = "30px";

        this.fontSize = "FontSize";
        this.fontHeight = "FontHeight";

        if(this.getCookie(this.fontSize) == null){
            this.setCookie(this.fontSize, this.defaultFontSize, new Date(Date.parse("Jan 1, 2012")));    
        }
        else{
            this.setFontSize(this.getCookie(this.fontSize));  
        }

        if(this.getCookie(this.fontHeight) == null){
            this.setCookie(this.fontHeight, this.defaultLineHeight, new Date(Date.parse("Jan 1, 2012")));
        }
        else{
            this.setLineHeight(this.getCookie(this.fontHeight));
        }

        $('#fontSize').click(function(){
            that.toggleFontSize();
        });
    },
    setBodyClass: function(value){
        this.bodyClass = value;
    },
    getBodyClass: function(){
        return this.bodyClass;
    },
    setBodyId: function(value){
    	this.bodyId = value;
    },
    getBodyId: function(){
    	return this.bodyId;
    },
    print: function(){
        window.print();
        return false;
    },
    etp: function(){
        Recaptcha.create("6LcVRcYSAAAAADZfzrdvX7IX5DW88KN7_xOFuUdl", "recaptcha_div", {
             theme: "red",
             callback: Recaptcha.focus_response_field});
        $("#response").empty();
    },
    setCookie: function (sName, sValue, oExpires, sPath, sDomain, bSecure) {
        var sCookie = sName +"="+ encodeURIComponent(sValue);

        if(oExpires){
            sCookie += "; expires=" + oExpires.toGMTString();        
        }
        
        if(sPath){
            sCookie += "; path=" + sPath;
        }

        if(sDomain){
            sCookie += "; domain=" + sDomain;
        }

        if(bSecure){
            sCookie += "; secure";
        }
        document.cookie = sCookie;
    },
    getCookie: function(sName) {
        var sRE = "(?:; )?" + sName + "=([^;]*);?";
        var oRE = new RegExp(sRE);

        if(oRE.test(document.cookie)){
            return decodeURIComponent(RegExp["$1"]);
        }
        else{
            return null;
        }
    },
    setFontSize: function(size){
        $(".modifyText").css("font-size", size);
    },
    setLineHeight: function(height){
        $(".modifyText").css("line-height", height);
    },
    getFontSize: function(size,height){
        return $(".modifyText").css("font-size");
    },
    getLineHeight: function(height){
        return $(".modifyText").css("line-height");
    },
    toggleFontSize: function(){
        if(this.getFontSize() == this.defaultFontSize && this.getLineHeight() == this.defaultLineHeight){
            this.setFontSize(this.increaseFontSize);
            this.setLineHeight(this.increaseLineHeight);
            this.setCookie(this.fontSize, this.increaseFontSize, new Date(Date.parse("Jan 1, 2012")));
            this.setCookie(this.fontHeight, this.increaseLineHeight, new Date(Date.parse("Jan 1, 2012")));
        }
        else{
            this.setFontSize(this.defaultFontSize);
            this.setLineHeight(this.defaultLineHeight);
            this.setCookie(this.fontSize, this.defaultFontSize, new Date(Date.parse("Jan 1, 2012")));
            this.setCookie(this.fontHeight, this.defaultLineHeight, new Date(Date.parse("Jan 1, 2012")));
        }     
    },
    setip: function(json){
        ipAddress = json.ip;
    },
    getip: function(){
        return ipAddress;
    }
};
$(document).ready(function(){
   var iGlobal = NoahsArk.clone(Global);
   var emergencyClinicZoomLevel = 11;
   iGlobal.construct();
   switch(iGlobal.getBodyId()){
        case 'home':
            var iRCallout = NoahsArk.clone(rotatingCallout);
            iRCallout.construct();
            break;
        case 'foc':
            var iFOC = NoahsArk.clone(FOC);
            var iClinic = NoahsArk.clone(clinic);
            var iBoroughCounties = NoahsArk.clone(boroughCounties);
            iFOC.construct(iClinic,iBoroughCounties,emergencyClinicZoomLevel);
            break;
       case 'resources':
            //look at the url and if there is a parameter then open the accordion
            var hash = document.location.hash;
            $(hash).parent().children("div").animate({
                height: 'toggle'
            },  1000);
            modifyHeader(hash);       
            break;
            
       default:
           //no selector in the body tag
   }

    //Resources  
    var headers = $('#accordion h3');
    headers.click(function (){
        var clicked = $(this);
        clicked.parent().children("div").animate({
            height: 'toggle'
        },  1000);
        modifyHeader(clicked);        
    });

    //Weight Profile
    var sliderFlag = 0;  //stops concurrent pileup
    var profile = $('#profile_nav li a');
    profile.click(function (){
    if(sliderFlag == 0){
        sliderFlag = 1;
        var clicked = $(this);
        //check if the link is active
        if(clicked.children("div").hasClass('active')){
            //do nothing
            sliderFlag = 0;
        }
        else{
            //keep a reference of which link was clicked
            var currentId = clicked.parent().attr('id');
            //close off all links
            clicked.parent().parent().children("li").each(function(index){
                if($(this).children("a").children("div").hasClass("active")){
                    //remove the class
                    $(this).children("a").children("div").removeClass("active");
                    $(this).animate({
                        width: '37px'
                    }, 500, function(){
                        if(index == 4){
                            $(this).css("width","38px");    
                        }
                        clicked.children("div").addClass("active");
                    });
                }
            });        
            clicked.parent().animate({
                width: '416px'
            },  500, function(){
                sliderFlag = 0;
            });
        }
        
    }
    
    });

    $('#cButton').click(function(){
        if($(this).hasClass("nonactive")){
            $(this).removeClass("nonactive");
            $(this).addClass("active");
            $('#fButton').removeClass("active");
            $('#fButton').addClass("nonactive");
            $('.canine').each(function(){
                $(this).removeClass('hide').addClass('show');
            });
            $('.feline').each(function(){
                $(this).removeClass('show').addClass('hide');
            });
        }
        
    });

    $('#fButton').click(function(){
        if($(this).hasClass("nonactive")){
            $(this).removeClass("nonactive");
            $(this).addClass("active");
            $('#cButton').removeClass("active");
            $('#cButton').addClass("nonactive");
            $('.canine').each(function(){
                $(this).removeClass('show').addClass('hide');
            });
            $('.feline').each(function(){
                $(this).removeClass('hide').addClass('show');
            });            
        }
    });

   $("#inline").fancybox({
        'scrolling'		: 'no',
	    'titleShow'		: false,
        'overlayOpacity': 0.8,
        'overlayColor'  : '#000',
    	'onClosed'		: function() {
	        $("#validation_messages").hide();
            $("#validation_messages").empty();
	    }
   });
   
   $("#login_form").bind("submit", function() {
        var that = this;
        var ck_name = /^[A-Za-z ]{2,20}$/;
        var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; 
        var name = $('#sender_name').val();
        var email = $("#recipient_email").val();
        var errors = [];

        var complete_url = location.href;
        var url_parts = complete_url.split('?');
        var email_url = url_parts[0];

        if (!ck_name.test(name)) {
            errors[errors.length] = "Enter a valid name.";
        }
        if (!ck_email.test(email)) {
            errors[errors.length] = "You must enter a valid e-mail address.";
        }
        if (errors.length > 0) {
            $("#validation_messages").empty();
            reportErrors(errors);
            return false;
        }

        if (errors.length == 0) {
            $("#validation_messages").hide();
            $("#validation_messages").empty();
        }

        var server_response = "";

        server_response = $.ajax({
            type: "POST",
            url: "validate_recaptcha.php",
            data: "challenge="+Recaptcha.get_challenge()+"&response="+Recaptcha.get_response()+"&sender_name="+name+"&recipient_email="+email+"&url="+email_url,
            async: false,
            success: function(msg){
                                
            }
        }).responseText;
        if(server_response == "true"){
            //the response was correct
            server_response = "";
            $("#response").empty();
            $("#response").append('Your email has been sent.');
            setTimeout($.fancybox.close,2000);
            return false;
        }
        else if(server_response == "false"){
            //the response was incorrect
            Recaptcha.create("6LcVRcYSAAAAADZfzrdvX7IX5DW88KN7_xOFuUdl", "recaptcha_div", {
                theme: "red",
                callback: Recaptcha.focus_response_field});
            $.fancybox.resize();
            server_response = "";
            $("#response").empty();
            $("#response").append('<span class="required">Please enter the new words.</span>');
            return false;
        }  
   });

   function reportErrors(errors){
        var msg = "Please validate your information:\n";
        for (var i = 0; i<errors.length; i++) {
            var numError = i + 1;
            msg += "<br/>" + numError + ". " + errors[i];
        }
        $("#validation_messages").append(msg);
        $("#validation_messages").show();
        $.fancybox.resize();
   }
   function modifyHeader(iden){
        if($(iden).children('a').hasClass('close')){
            $(iden).children('a').removeClass('close');
            $(iden).children('a').addClass('open');
        }
        else{
            $(iden).children('a').removeClass('open');
            $(iden).children('a').addClass('close');
        }        
   }  
});
