$(document).ready(function(){

    // Never pre-populate the search input.
    $('#queries_keywords_query').val('');

    if ($('#mainFeature').attr("id") == "mainFeature") {        
        var flashVersion = "0,0,0";
        var flashAvailable = false;
        try
        {
            var Plugin = navigator.plugins['Shockwave Flash'] || ActiveXObject;
            flashVersion = Plugin.description || (function () {
                try {
                    return (new Plugin('ShockwaveFlash.ShockwaveFlash')).GetVariable('$version');
                }
                catch (eIE) {}
            }());
            
            if (flashVersion == undefined)
            {
                "0,0,0";
            }
            
            flashVersion = flashVersion.match(/^[A-Za-z\s]*?(\d+)[\.|,](\d+)(?:\s+[d|r]|,)(\d+)/);  
            flashAvailable = flashVersion != undefined && flashVersion[1] > 0 ? true : false;
        }
        
        catch(e)
        {
            flashAvailable = false;
        }        
        
        if (flashAvailable == true) {
            $("._feature").each(function(i) {
                if ($(this).find("embed").size() > 0) {
                    $(this).find("a").remove();
                }
            })
        }
        else {
            $("._feature").each(function(i) {
                if ($(this).find("img").length == 0) {
                    $(this).remove();
                } else {                        
                    $(this).find("embed").remove();                        
                }
            });
        }  
    }
    
    var siteMap = jQuery("div#footer > div.sitemap");
    var footerImage = jQuery("div#footerExpander");

    siteMap.hide();

    jQuery("div#footerExpander").click(function () {
        if (siteMap.is(":hidden")) {
            footerImage.removeClass("arrowDown");
            footerImage.addClass("arrowUp");
            siteMap.slideDown('fast');
        } else {
            jQuery.scrollTo("#page", "fast", function () {
                footerImage.removeClass("arrowUp");
                footerImage.addClass("arrowDown");
                siteMap.slideUp('fast');
            });
        }
    });
    
    
    // toggle
    //Hide (Collapse) the toggle containers on load
    $(".sitemap .toggle_container").hide();

    //Switch the "Open" and "Close" state per click
    $(".trigger").click(function() {
        if($(this).hasClass('active')){
            $(this).removeClass("active");
            return false;
        }else{
            $(this).addClass("active");
            return false;
        }
    });

    //Slide up and down on click
    $(".trigger").click(function() {
        $(this).next(".toggle_container").slideToggle("slow,");
    }); 

    Shadowbox.init();

    var timing = parseInt($("#timing").attr("value"));

    if ($('#mainFeature').attr("id") == "mainFeature") {
        $('#mainFeature').cycle({
            fx: 'fade',
            pager: '#paginationWrapper',
            timeout: 7000
        });
        $('.date-pick').datePicker();
    }
    
    /*-- Family situation validation --*/
    //Make sure that at least 1 radio button is selected before submit
    $('#residential-state-selector #select-btn').click(function(){
        
        if ($('select#main_0_ddlState option:selected').val() == "Select") {
             $('.popupwrap .form-errors').text('Please select a state');
        }
        else {
            $('.popupwrap .form-errors').text('');
            var redirectURL = $('select#main_0_ddlState option:selected').attr('value');

            if($('input#main_0_cboxRMS:checked').length > 0) {
                //create cookie
                $.cookie('state', redirectURL, {
                    path: '/'
                });
            }
            window.parent.location.href = redirectURL;
       }
    
    });
    
    /*-- Family situation validation --*/
    //Make sure that at least 1 radio button is selected before submit
    $('#business-state-selector #select-btn').click(function(){
        
        if ($('select#main_0_ddlState option:selected').val() == "Select") {
             $('.popupwrap .form-errors').text('Please select a state');
        }
        else 
        if (($('select#main_0_ddlState option:selected').val() != "Select") && ($('select#main_0_ddlBusiness option:selected').val() == "Select")) {
            $('.popupwrap .form-errors').text('');
            $('.popupwrap .form-errors').text('Please select a business type');
        }
        else {
            $('.popupwrap .form-errors').text('');
            var redirectURL = $('select#main_0_ddlState option:selected').attr('value');
            var businessURL = $('select#main_0_ddlBusiness option:selected').attr('value');
            var URL = redirectURL + businessURL;
            
            if($('input#main_0_cboxRMS:checked').length > 0) {
                //create cookie
                $.cookie('state', redirectURL, {
                    path: '/'
                });
            }
            window.parent.location.href = URL;
       }
    });
    
    //remove text when radio button clicked
    $('select#main_0_ddlState').change(function(){
        if($('select#main_0_ddlState option:selected').val() != "Select") {
            $('.popupwrap .form-errors').text('');
        }
    });
    
    //remove text when radio button clicked
    $('select#main_0_ddlBusiness').change(function(){
        if($('select#main_0_ddlState option:selected').val() != "Select") {
            $('.popupwrap .form-errors').text('');
        }
    });
    
    //make sure mega-menu items are aligned corrected
    $('.dropdown .box ul li:last-child').addClass('last');

    //var dropdown = $('.dropdown .box').eq(0);
    //var height = $(dropdown).find('.wrap > ul').height()
    //$(dropdown).find('.wrap').css('height',height+20+"px")
    //$(dropdown).css('height',height+30+"px")
});
