// jquery stuff

$(function(){
    
    // Remove the last horizontal rule in the home page right col
    $('.sub_col .widget_rule:last-child').remove();
    
    $('a[href^="http"]')
        .not('a[href^="http://' + window.location.host + '"]')
        .not('a[href^="https://' + window.location.host + '"]')
        .each(function(){
            $(this).popUp({
                width: '960', 
                height: '700',
                scrollbars: 'yes',
                resizeable: 'yes',
                statusbar: 'yes',
                menubar: 'yes',
                toolbar: 'yes'
            })        
        });
    
    $('a.popup, a[rel="_blank"], a[href$=".pdf"]').popUp({
        width: '960', 
        height: '700',
        scrollbars: 'yes',
        resizeable: 'yes',
        statusbar: 'yes',
        menubar: 'yes',
        toolbar: 'yes'
    });
    
    // vertically align solution blocks
    $('div.solutions li .title').each(function() {
        newMargin = ($(this).parent().height() - $(this).outerHeight()) / 2;
        $(this).css("margin-top",newMargin)
    });
    
    // Resize the interior page main column if the sub column (sidebar)
    // is longer than the main column
    var body_interior = $('body#interior'),
        main_col = body_interior.find('.main_col'),
        sub_col = body_interior.find('.sub_col');
    
    if(body_interior.length > 0 && sub_col.height() > main_col.height()){
        main_col.height(sub_col.height() + 30);
    }
    
    // define the array of elements / classes;
    var addClassArray = new Array (
        [ $('#breadcrumbs ul li:first-child'), 'first' ],
        [ $('#breadcrumbs ul li:last-child'), 'last' ],
        [ $('#footer ul.pages li:first-child'), 'first' ]
    );
    
    // add class to each element in the array
    for (var i = 0; i < addClassArray.length; i++) {
        var definedElement = addClassArray[i][0];
        var newClass = addClassArray[i][1];
        definedElement.addClass(newClass);
    }
    
    // add pipes between the links
    $('#footer ul.pages li').not(':first-child').before('/ ');
    
    // Grab the default value of search inputs, store in the data object, blur/focus and replace value if necessary.
    $('form .keywords').focus(function(){
        $(this).data('field_value', $(this).val());
        $(this).val('');
    }).blur(function(){
        if($(this).val() == ''){
            $(this).val($(this).data('field_value'));
        }
    });
    
    // This overlays the labels onto the input itself, and hides the lable when focused
    $('#contact_form label').perfectForm({ignore: '.ignore'});
    
    $('#comment_form label').autoWidth();
    
    // The member select list on the blog pages
    $('.member_select').change(function(){
        window.location = $(this).val();
    });
    
    if($.browser.msie && $.browser.version < 7) {
        $('body.blog .post img').each(function(){
            if( $(this).width() > 445 ){
                $(this).css('width', '445px');
            }
        })
    } else {
        // size the buttons
        $('a.button, button').each(function() {
           var textWidth = $(this).children('span.button_text').outerWidth();
           var halfWidth = Math.round(textWidth / 2);
           var paddingRight = $(this).children('span.button_text').css('padding-right');
           
           // This is to fix incorrect widths in IE 7, but it works for all browsers just the same
           if(paddingRight && $(this)[0].nodeName.toLowerCase() == 'button'){
               paddingRight = parseInt(paddingRight.replace('px', ''));
               $(this).width(textWidth + paddingRight + 5);
           }
           
           $(this).children('span').not('.button_text').width(halfWidth);
           
           if($.browser.webkit){
               $(this).css({'padding-left': '0', 'padding-top': '0'});
           }
        });
        
        // We have a decent browser, so do this stuff
        $('ul.fields :radio, ul.fields :checkbox, select').customFormElements({
            checkboxHeight: "18",
            radioHeight: "18",
            radioWrapClass: 'radio_wrap',
            checkboxWrapClass: 'checkbox_wrap',
            selectWrapClass: 'select_wrap'
        });
    }
    
    /* Define all the gradient text items */
    $('#content.interior .widget h3').gradientText({
        font_size: 13,
        swf_path: js_base_url + 'assets/media/flash/gradient_text.swf',
        color_top: 'ffffff',
        color_bottom: 'bababa',
        text_transform: 'uppercase'
    });
    
    $('#content.home #info_panel h3').gradientText({
        font_size: 20,
        swf_path: js_base_url + 'assets/media/flash/gradient_text.swf',
        color_top: 'ffffff',
        color_bottom: 'bababa',
        text_transform: 'uppercase'
    });
    
    $('#content.home .widget.green h3').gradientText({
        font_size: 18,
        swf_path: js_base_url + 'assets/media/flash/gradient_text.swf',
        color_top: 'c8c63d',
        color_bottom: '969400',
        text_transform: 'uppercase'
    });
    
    $('#content.home #welcome h1').gradientText({
        font_size: 32,
        swf_path: js_base_url + 'assets/media/flash/gradient_text.swf',
        color_top: '5FC2FF',
        color_bottom: '064a98'
    });
    
    $('#content.interior .main_col h1').gradientText({
        font_size: 24,
        swf_path: js_base_url + 'assets/media/flash/gradient_text.swf',
        color_top: '5FC2FF',
        color_bottom: '064a98'
    });
    
});

jQuery.fn.perfectForm = function(options)
{
    var settings = {
        overlap: true,
        ignore: ''             
    }
    
    if(!this.length) return this;

    if(options) {
        jQuery.extend(settings, options);
    };
    
    this.not(settings.ignore).each(function(){
        var input = $(this).next('input[type=text], textarea');
        var label = $(this);
        
        // first check all inputs for values, then hide it's label if it has a value
        if( settings.overlap ) {
            label.addClass('overlap');
        }
        
        if( settings.overlap && input.val() != '' ) {
            label.hide();
        }
        
        label.click(function(){
            label.hide();
            input.focus();
        }, function(){
            if( input.val() == '' ) {
                label.show();
            }
        });
        
        // then add a focus event to all inputs to hide their labels when focused on, 
        // and remain hidden on blur if a value is entered
        input.focus(function(){
            label.hide();
        }).blur(function(){
            if( $(this).val() == '' ) {
                label.show();
            }
        });
    });
}


jQuery.fn.autoWidth = function(options) 
{
    var settings = {
        minWidth   : false,
        limitWidth  : false,
        ignore  : '',
        padding : 10
    }
    
    if(!this.length) return this;

    if(options) {
        jQuery.extend(settings, options);
    };

    var maxWidth = 0;

    this.not(settings.ignore).each(function(){
        if ($(this).width() > maxWidth){
            if(settings.limitWidth && maxWidth >= settings.limitWidth) {
                maxWidth = settings.limitWidth;
            } 
            else if(settings.minWidth && maxWidth <= settings.limitWidth)
            {
                maxWidth = settings.minWidth;
            } 
            else 
            {
                maxWidth = $(this).width();
            }
        }
    });  

    this.not(settings.ignore).width(maxWidth + settings.padding);
}



jQuery.fn.gradientText = function(options)
{
    var settings = {
        font_size: 16,
        color_top: '147cb2',
        color_bottom: '054897',
        swf_path: '/',
        text_transform: false ,
        replaced_class: 'replaced'    
    }
    
    if(!this.length) return this;

    if(options) {
        jQuery.extend(settings, options);
    };
    
    function getRandomNumber(range)
    {
        return Math.floor(Math.random() * range);
    }

    function getRandomChar()
    {
        var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
        return chars.substr( getRandomNumber(62), 1 );
    }

    function randomID(size)
    {
        var str = "";
        for(var i = 0; i < size; i++)
        {
            str += getRandomChar();
        }
        return str;
    }
    
    
    this.each(function(){
        var ele = $(this);
        var text = ele.html().replace('&amp;','%26').replace('&#38;','%26').replace('&','%26');
        var height = settings.height ? settings.height : ele.height();
        var width = settings.width ? settings.width : ele.width();
        // Create a unique ID to replace with swfobject b/c it doesn't take a jQuery object
        var id = 'flash_text_' + randomID(60);

        // Assign zee ID
        ele.wrapInner('<span id="'+ id +'"></span>');
        ele.addClass('replaced');
        
        // Since CSS transform rules won't be taken into account
        switch(settings.text_transform)
        {
            case "uppercase":
                text = text.toUpperCase();
            break;
            case "lowercase":
                text = text.toLowerCase();
            break;
        }
        
        swfobject.embedSWF(
            settings.swf_path, id, width, height, "8", "", 
            {
                text: text, 
                bottomColor: '0x'+ settings.color_bottom, 
                topColor: '0x'+ settings.color_top , 
                fontSize: settings.font_size,
                maxWidth: width
            }, 
            {
                wmode:"transparent",
                menu: "false"
            }
        );
    });
}


jQuery.fn.popUp = function(options)
{
    var settings = {
        name: 'newWindow',
        width: 500,
        height: 600,
        left: false,
        top: false,
        scrollbars: 'yes',
        resizeable: 'no',
        statusbar: 'no',
        menubar: 'no',
        toolbar: 'no'
    }

    if(options) {
        jQuery.extend(settings, options);
    };

    this.each(function(){
        $(this).click(function(){
            var rel = $(this).attr('rel');
            var href = $(this).attr('href');
            if(!rel){
                window.open(href, settings.name);
            } else {
                var rel_split = rel.split('|');
                var width = rel_split[0] != "_blank" ? rel_split[0] : settings.width;
                var height = rel_split[1] ? rel_split[1] : settings.height;
                var scrollbars = rel_split[2] ? rel_split[2] : settings.scrollbars;
                var leftPos = settings.left ? settings.left : (screen.width-width)/2;
                var topPos = settings.right ? settings.right : (screen.height-height)/2;
                
                var config = 'width='+ width +',height='+ height +', \
                            left='+ leftPos +',top='+ topPos +', \
                            scrollbars='+ scrollbars +', \
                            resizable='+ settings.resizeable +', \
                            statusbar='+ settings.statusbar +', \
                            menubar='+ settings.menubar +', \
                            toolbar='+ settings.toolbar;
                            
                window.open(href,settings.name, config);
            }
            return false;
        });
    });
}