(function($) {
    if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
        var oldCurCSS = jQuery.curCSS;
        jQuery.curCSS = function(elem, name, force){
            if(name === 'background-position'){
                name = 'backgroundPosition';
            }
            if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
                return oldCurCSS.apply(this, arguments);
            }
            var style = elem.style;
            if ( !force && style && style[ name ] ){
                return style[ name ];
            }
            return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
        };
    }

    var oldAnim = $.fn.animate;
    $.fn.animate = function(prop){
        if('background-position' in prop){
            prop.backgroundPosition = prop['background-position'];
            delete prop['background-position'];
        }
        if('backgroundPosition' in prop){
            prop.backgroundPosition = '('+ prop.backgroundPosition;
        }
        return oldAnim.apply(this, arguments);
    };

    function toArray(strg){
        strg = strg.replace(/left|top/g,'0px');
        strg = strg.replace(/right|bottom/g,'100%');
        strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
        var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
        return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
    }

    $.fx.step. backgroundPosition = function(fx) {
        if (!fx.bgPosReady) {
            var start = $.curCSS(fx.elem,'backgroundPosition');

            if(!start){//FF2 no inline-style fallback
                start = '0px 0px';
            }

            start = toArray(start);

            fx.start = [start[0],start[2]];

            var end = toArray(fx.options.curAnim.backgroundPosition);
            fx.end = [end[0],end[2]];

            fx.unit = [end[1],end[3]];
            fx.bgPosReady = true;
        }
        //return;
        var nowPosX = [];
        nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
        nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
        fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

    };
})(jQuery);


$(document).ready(function(){

    /* Animate the blocks */
    $("a.slide-block").hover(function(){
        $(this).children("span").stop();
        $(this).children("span").animate({
            top: "0"
        },200);
    },function(){
        $(this).children("span").stop();
        $(this).children("span").animate({
            top: "155px"
        },600);
    });


    /* Control Panel Link Page */
    $("a#ccp-link img").hover(function() {
        $(this).attr("src","/assets/images/ccp-preview.jpg");
    }, function() {
        $(this).attr("src","/assets/images/ccp-preview-bw.jpg");
    });


    /* Jump Links (anchors) */
    $('a.scrollto,a.footnote,a.footnotelink').click(function() {
        var $target = $(this.hash);
        $target = $target.length && $target
        || $('[name=' + this.hash.slice(1) +']');
        if ($target.length) {
            var targetOffset = $target.offset().top;
            var $bodyelem;
            if($.browser.webkit){
                $bodyelem = $("body")
            } else {
                $bodyelem = $("html")
            }
            $bodyelem.animate({
                scrollTop: targetOffset
            }, 800);
            $(this).blur();
            return false;
        } else {
            return true;
        }
    });


    /* Animate Jump Links (anchors) */
    $("a.scrollto").hover(function(){
        $(this).stop();
        $(this).animate({
            paddingLeft: '34px'
        },100,
        function() {
            $(this).animate({
                backgroundPosition: '0px 16px'
            },200)
        });
    },function(){
        $(this).stop();
        $(this).animate({
            backgroundPosition: '0px 55px'
        },200,function() {
            $(this).animate({
                paddingLeft: '0px'
            },200)
        });
    });


    /* Animate scroll to top of page */
    $('a.toplink').click(function() {
        var $bodyelem;
        if($.browser.webkit){
            $bodyelem = $("body")
        } else {
            $bodyelem = $("html")
        }
        $bodyelem.animate({
            scrollTop: 0
        }, 800);
        $(this).blur();
        return false;
    });


    /* Animate RHS Shortcuts */
    $("a.shortcut").hover(function(){
        $("a.shortcut").stop();
        $('div.shortcuts').css('overflow','hidden');
        $("a.shortcut").animate({
            backgroundPosition: '-30px 5px'
        },0);
        $(this).animate({
            backgroundPosition: '15px 5px'
        },200,function(){
            $('div.shortcuts').css('overflow','visible');
            $(this).animate({
                backgroundPosition: '0px 5px'
            },200);
        });
    },function(){
        $("a.shortcut").stop();
        $(this).animate({
            backgroundPosition: '15px 5px'
        },200,function(){
            $('div.shortcuts').css('overflow','hidden');
            $(this).animate({
                backgroundPosition: '-30px 5px'
            },200);
        });

    });


    /* Animate internal cross-page navigation */
    $('a.sitelink').click(function() {
        var $offset = $(document).scrollTop();
        if ($offset > 0) {
            var $bodyelem;
            if($.browser.webkit){
                $bodyelem = $("body")
            } else {
                $bodyelem = $("html")
            }
            var $target = $(this).attr('href');
            $bodyelem.animate({
                scrollTop: 0
            }, $offset, function() {
                window.location = $target;
            });
            $(this).blur();
            return false;
        } else {
            return true;
        }
    });

    /* Comparison table highlights */
    $('th.canHighlight,td.canHighlight').hover(function() {
        var $column = $(this).index() + 1;
        $(this).parents('table').find('td:nth-child('+$column+'),th:nth-child('+$column+')').addClass('highlight');
    }, function() {
        var $column = $(this).index() + 1;
        $(this).parents('table').find('td:nth-child('+$column+'),th:nth-child('+$column+')').removeClass('highlight');
    });

});


function submitForm(formId) {
    var formObj = $("#"+formId);
    formObj.children(".button").replaceWith("<span class=\"button\">Request Call</span>")
    formObj.submit();
}
