/**
 * $Id: monster.js 2009-03-18 $
 *
 * @author Monika Piotrowicz
 * @copyright Copyright (c) 2009, Trapeze, All rights reserved.
 *
 * Requirements:
 *      jQuery 1.3.2        (http://www.jquery.com),
 *      sIFR 3-r436         (http://dev.novemberborn.net/sifr3/nightlies/)
 */


if (typeof trapeze == "undefined") var trapeze = new Object();

trapeze.monster = {	
    preload_images : function() {
    //Preload Function
        var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
        for(var i = a.length -1; i > 0; i--) {
            $("<img />").attr("src", a[i]);
        }
    },

rgb_to_hex : function(rgb) {
    // Convert an rgb color format to hex color equivalent
    // for example: this.rgb_to_hex('192,192,192') will return 'C0C0C0'

        if (rgb.length > 0) {
            var char = "0123456789ABCDEF";
            var a_code = rgb.split(",");
            var str_hex = '';

            for(var i=0; i<a_code.length; i++) {
                str_hex += String(char.charAt(Math.floor(a_code[i] / 16))) + String(char.charAt(a_code[i] - (Math.floor(a_code[i] / 16) * 16)));
            }
            return str_hex;
        } else {
            return '';
        }
    },

    get_sIFR_style_for : function(obj) {
    // Collect all the styles for a specific selector

        var style = '';
        var obj_selector = $(obj)[0].tagName + ($(obj).attr('class') == '' ? '' : '.' + $.trim($(obj).attr('class')).replace(new RegExp(' ', 'g'), '.'));

        $(obj).find('span').andSelf().each(function(i){
            var is_obj = ($(this)[0] == $(obj)[0]);
            if (! is_obj) {
                if ($(this).attr('class') == '') { return false };
            }

            var span_class      = '.' + $.trim($(this).attr('class')).replace(new RegExp(' ', 'g'), '.');
            var span_selector   = obj_selector + (is_obj ? '' : ' ' + span_class);
            var _color          = "#" + trapeze.monster.rgb_to_hex(($(span_selector).css('color')).slice(4,-1));
            var _font_size      = $(span_selector).css('font-size');
            var _font_weight    = $(span_selector).css('font-weight');
            if (! isNaN(_font_weight)) {
                _font_weight = Number(_font_weight)<=400 ? 'normal' : 'bold';
            }
            _text_align     = $(span_selector).css('text-align');
            
            style += (style!="" ? ", " : "") + "'" + (is_obj ? '.sIFR-root' : '.' + $.trim($(this).attr('class')).split(' ')[0]) + " { color:" + _color + "; font-size:" + _font_size + "; font-weight:" + _font_weight + "; text-align:" + _text_align + ";}'";
        });

        return style;
    },

    config_sIFR : function() {
    // Configure sIFR on the current page
      sIFR.replace(nexus, {
      selector: 'span#BadgeHeader'
      ,css: [
        '.sIFR-root { color: #54514c;}'
        ,'a { text-decoration: none; }'
        ,'a:link { color: #ffffff; }'
        ,'a:hover { color: #CCCCCC; }'
      ]
       ,wmode : 'transparent'
       ,ratio :[9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
    });    
    sIFR.replace(nexus, {
      selector: 'h2.recent,.markdown h2'
      ,css: [
        '.sIFR-root { color: #54514c;font-weight:normal; font-size:17px;}' //was 19px
        ,'a { text-decoration: none; }'
        ,'a:link { color: #ffffff; }'
        ,'a:hover { color: #CCCCCC; }'
      ]
       ,wmode : 'transparent'
       ,ratios : [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
    });   
        sIFR.replace(nexus, {
      selector: '.markdown h3'
      ,css: [
        '.sIFR-root { color: #54514c;font-weight:300; font-size:15px;}' //was 19px
        ,'a { text-decoration: none; }'
        ,'a:link { color: #ffffff; }'
        ,'a:hover { color: #CCCCCC; }'
      ]
       ,wmode : 'transparent'
       ,ratios : [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
    }); 
    sIFR.replace(nexus, {
      selector: 'h1, h2.section-subheading, .renewal-reminder h4.sifr'
      ,css: [
        '.sIFR-root { color: #54514c;font-weight:700; font-size:19px;}' //was 19px
        ,'a { text-decoration: none; }'
        ,'a:link { color: #ffffff; }'
        ,'a:hover { color: #CCCCCC; }'
      ]
       ,wmode : 'transparent'
       ,ratios : [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
    }); 
        
   sIFR.replace(nexus, {
      selector: '.related-box_ h3'
      ,css: [
        '.sIFR-root { color: #ffffff; font-size:18px;}'
      ]
       ,wmode : 'transparent'
       ,ratios : [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
    }); 
    sIFR.replace(nexus, {
      selector: '.form-top h4#Applicant_'
      ,css: [
        '.sIFR-root { color: #54514c; font-weight:700; font-size:19px;leading: -20px;}'
      ]
       ,wmode : 'transparent'
       ,offsetTop : '7'
       ,tuneHeight : '10'
    });    

sIFR.replace(nexus, {
selector: '#MainNav li.selected'
      ,css: [
        '.sIFR-root { color: #54514c; text-align:center; leading: -15px;}'
        ,'a { font-size:17px;margin-left:-2px;text-decoration: none; color:#54514c; text-align:center; font-weight:darker;}'
        ,'a:hover { color:#54514c; }'
      ]
      ,tuneHeight : '25'
      ,offsetTop : '15'
       ,wmode : 'transparent'
       ,selectable : true
       ,ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
    });
    
sIFR.replace(nexus, {
    selector: '#MainNav li'
      ,css: [
        '.sIFR-root { color: #ffffff; text-align:center; leading: -15px;}'
        ,'a { font-size:16px;margin-left:-2px;text-decoration: none; color: #ffffff; text-align:center; font-weight:bold; }'
        ,'a:hover { color: #ffffff; }'
      ]
      ,tuneHeight : '20'
      ,offsetTop : '15'
       ,wmode : 'transparent'
       ,selectable : true
       ,ratios : [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
    });   
       
sIFR.replace(myriadPro, {
selector: '#Badge p'
      ,css: [
        '.sIFR-root { color: #54514c; font-size: 18px;  leading:5px;}'
        ,'a { color: #DD7C08; font-size: 18px;  leading:5px; text-decoration:none; font-weight:normal;}'
        ,'a:hover { color: #DD7C08; }'
      ]
       ,wmode : 'transparent'
       ,ratios : [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    });
    
sIFR.replace(myriadPro, {
selector: '.pre-approval h3'
      ,css: [
        '.sIFR-root { color: #0099FF; font-size: 18px; leading: -20px;}'
        ,'a { color: #83941C; font-size: 18px; leading: -20px; text-decoration:none; }'
        ,'a:hover { color: #83941C;}'
      ]
       ,wmode : 'transparent'
       ,ratios : [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    }); 
sIFR.replace(myriadProSB, {
selector: '.home h3'
      ,css: [
        '.sIFR-root { color: #0099FF; font-size: 18px; leading: -20px;}'
        ,'a { color: #0099FF; font-size: 18px; leading: -20px; text-decoration:none; }'
        ,'a:hover { color: #0099FF;}'
      ]
       ,wmode : 'transparent'
       ,ratios : [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    }); 
sIFR.replace(myriadProSB, {
selector: '.lenders h3'
      ,css: [
        '.sIFR-root { color: #DD7800; font-size: 18px; width:200px; }'
        ,'a { color: #DD7800; font-size: 18px;text-decoration:none; }'
        ,'a:hover { color: #DD7800;}'
      ]
       ,wmode : 'transparent'
       ,ratios : [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    });   
sIFR.replace(myriadPro, {
selector: '.apply-landing .refinance h3'
      ,css: [
        '.sIFR-root { color: #83941C; font-size: 18px; width:200px; }'
        ,'a { color: #83941C; font-size: 18px;text-decoration:none; }'
        ,'a:hover { color: #83941C;}'
      ]
       ,wmode : 'transparent'
       ,ratios : [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    });   
    sIFR.replace(myriadProSB, {
selector: '.home-page .refinance h3'
      ,css: [
        '.sIFR-root { color: #83941C; font-size: 18px;}'
        ,'a { color: #83941C; font-size: 18px;text-decoration:none;}'
        ,'a:hover { color: #83941C;}'
      ]
       ,wmode : 'transparent'
       ,ratios : [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    }); 
    sIFR.replace(myriadPro, {
selector: '.switch h3'
      ,css: [
        '.sIFR-root { color: #83941C; font-size: 18px;}'
        ,'a { color: #DD7800; font-size: 18px;text-decoration:none;}'
        ,'a:hover { color: #DD7800;}'
      ]
       ,wmode : 'transparent'
       ,ratios : [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    }); 
    
     sIFR.replace(myriadPro, {
selector: '.apply-landing .purchasing h3'
      ,css: [
        '.sIFR-root { color:#0099FF; font-size: 18px;}'
        ,'a { color:#0099FF; font-size: 18px;text-decoration:none;}'
        ,'a:hover { color:#0099FF;}'
      ]
       ,wmode : 'transparent'
       ,ratios : [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    });   
   
sIFR.replace(myriadProSB, {
selector: '.rates-table-extended .rates-top span'
      ,css: [
        '.sIFR-root { color: #ffffff; font-size: 18px;}'
      ]
       ,wmode : 'transparent'
    });
sIFR.replace(myriadProSB, {
selector: '.rates-top span'
      ,css: [
        '.sIFR-root { color: #ffffff; font-size: 18px;}'
      ]
       ,wmode : 'transparent'
    });  
    
sIFR.replace(myriadProSB, {
selector: '#CalculatorTop h3'
      ,css: [
        '.sIFR-root { color: #ffffff; font-size: 18px; text-align:left;}'
      ]
       ,wmode : 'transparent'
    });  

sIFR.replace(myriadPro, {
selector: '#FormNavigation li.selected'
      ,css: [
        '.sIFR-root { color: #ffffff; font-size: 14px; font-weight:bold; text-align:center;}'
      ]
       ,wmode : 'transparent'
       ,offsetTop : '4'
       ,tuneHeight : '20'
       ,ratios: [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    });
sIFR.replace(myriadPro, {
selector: '#FormNavigation li'
      ,css: [
        '.sIFR-root { color: #dd7c08; font-size: 14px;font-weight:bold;text-align:center;}'
      ]
       ,wmode : 'transparent'
       ,offsetTop : '4'
       ,tuneHeight : '20'
       ,ratios : [6, 1.24, 7, 1.21, 8, 1.18, 10, 1.16, 11, 1.13, 12, 1.12, 13, 1.11, 15, 1.1, 17, 1.09, 19, 1.08, 22, 1.07, 26, 1.06, 32, 1.05, 41, 1.04, 58, 1.03, 97, 1.02, 1.01]
    });    
    
 },
  
    checkForm : function(){
        var validForm = true;
        $(".btn-next").click(function(event){
            errorMsg = "";
            event.preventDefault();
            $(".form .mandatory li input").add(".form .mandatory input").each(function(i, input){
                inputValue = $(input).val();
                labelID = $(input).parent().attr("id");
                if ($(input).attr("id") == "id_0-applicant_email_address" || $(input).attr("id") == "id_0-co_applicant_email_address" || $(input).attr("id")== "id_email"){ 
                        var emailFilter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                    if (!(emailFilter.test(inputValue))) {
                        validForm = false;
                        errorMsg +="<li>Error: A valid email address is required.</li>";
                    }
                }
                else if (inputValue == ""){
                    var inputID = $(input).attr("id");
                    errorMsg +="<li>Error: "+ labelID + " is required.</li>";
                    validForm = false;
                }
                else{
                    validForm = true;
                }
            });
            
            if(!validForm){
                $(".error-list").html(errorMsg);
                trapeze.scroll_to($("h1"));
                return false;
            }
            else {
                $("form").submit();
            }
        });
        $(".btn-submit").click(function(event){
            validForm = true;
            event.preventDefault();
            if (! $("input.checkbox").is(':checked')){
                errorMsg ="<li>Error: Agree to Terms is required.</li>";
                    validForm = false;
                $(".btn-previous").css("clear","left");
                }
                if(!validForm){
                $(".error-list").html(errorMsg);
                trapeze.scroll_to($("h1"));
                return false;
            }
            else {
                $("form").submit();
            }
        });

    },
    
    renewalReminder : function(){
        $("#id_preferred_contact_0").attr("checked", "checked");
    },
    
    showCoApplicant : function(){
        var isVisible = false;
        
    },
    
    hideCoApplicant : function(){
        var isVisible = false;
        $("#CoApplicant").hide();
        $("#CoApplicant fieldset").removeAttr("class");
        
        $("#id_0-co_applicant_1").click(function(){
            $("#CoApplicant").show();
            $("#CoApplicant fieldset").attr("class","mandatory");
            isVisible = true;
        });
        $("#id_0-co_applicant_0").click(function(){
            if(isVisible){
            $("#CoApplicant").hide();
            $("#CoApplicant fieldset").removeAttr("class");
            }
        });
        if ($("input#id_0-co_applicant[value='Yes'][type='hidden']").length > 0) {
        $("#CoApplicant").show();                        
        }
        if ($("#id_0-co_applicant_1:checked").length > 0) {
            $("#CoApplicant").show();
            $("#CoApplicant fieldset").attr("class","mandatory");
            isVisible = true;
        }                                                
    },
    
    printRates : function(){
        $(".print-link").css("visibility","visible");
        $(".hr.print-link").css("visibility","visible");
        $("a.print-link").click(function(){
            window.print();
        });
    },
    
    clearForm : function(){
        if($(".clear-form").length > 0){
            $(".clear-form").css("visibility", "visible");
            $(".clear-form").click(function(){
                $('input.text').add('textarea').each(function(){
                    $(this).val("");
                });
            });
        }
    },
    
    calculatorSetup : function() {
        $(".slider .slider-label").css("display","inline");
        $(".total-calculated").css("display","block");
        $(".submit-button").css("display","none");
        $(".monthly-total").css("display","none");

        $("form").keyup(function(e){
                if(e.keyCode ==13){
                e.preventDefault();
                return false;
                }
            });
        $(".report").click(function(event){
             $(".mortgage-rate input").each(function(i, input){
                rate = $(input).val();
                formattedRate = rate/100;
                $(input).val(formattedRate);
            });
        });   
    },
    // *** Place your functions here ****
    
    init : function() {
        var page = $('body').attr('class');
        page = (page.indexOf(' ') > 0) ? page.slice(0,page.indexOf(' ')) : page;
        switch(page) {
            case 'home-page':
                //Code to be executed on the home page
                break;
            case 'form-page':
                this.hideCoApplicant();
                this.checkForm();    
            break;
            case 'calculator-page':
                this.calculatorSetup();
            break;
			case 'renewal-reminder':
				this.renewalReminder();
				this.checkForm(); 
			break;
			case 'report-page':
				this.printRates(); 
			break;
			case 'contact-form-page':
				this.clearForm(); 
			break;
			case 'switching-page':
				this.calculatorSetup();
			break;
            default:
                //Code to be executed on any page, but the pages listed above.
        }

        // Code to be executed on every page
    }
};

// sIFR Configuration
//todo: media_path is not yet set here... what to do?
var myriadPro = { src: '/media/flash/myriadPro.swf'};
var nexus = { src: '/media/flash/nexus.swf'};
var myriadProSB = { src: '/media/flash/myriadProSB.swf'};
sIFR.activate(myriadPro, nexus, myriadProSB);


$(function() {
	// Code to execute when the DOM is ready
    trapeze.monster.init();
	trapeze.monster.config_sIFR();
});