﻿
var blairmditc = { src: '/assets/flash/BlairMdITC.swf', ratios: [7, 1.32, 11, 1.31, 13, 1.24, 14, 1.25, 19, 1.23, 27, 1.2, 34, 1.19, 42, 1.18, 47, 1.17, 48, 1.18, 69, 1.17, 74, 1.16, 75, 1.17, 1.16] };

sIFR.ua.supported = sIFR.ua.supported && (!sIFR.ua.ie || sIFR.ua.ieVersion > 6);
sIFR.activate(blairmditc);

sIFR.replace(blairmditc, { selector: '#image_info h2', wmode: 'transparent', css: '.sIFR-root { color: #ffffff; leading: 10; }'
    ,filters: {
      DropShadow: {
        knockout: false
        ,distance: 2
        ,blurX: 0
        ,blurY: 0
        ,color: '#000000'
        ,strength: .50
        ,angle: 45
      }
    }
});

sIFR.replace(blairmditc, { selector: '#image_info p', wmode: 'transparent', css: '.sIFR-root { color: #ffffff; leading: 10; }'
    , filters: {
        DropShadow: {
            knockout: false
                , distance: 2
                , blurX: 0
                , blurY: 0
                , color: '#000000'
                , strength: .50
                , angle: 45
        }
    }
});


sIFR.replace(blairmditc, {
    selector: '.banner_A h2'
    , wmode: 'transparent'
    , css: ['.sIFR-root { text-align: center; }', 'a { text-decoration: none; }', 'a:link { color: #1A344E; }', 'a:hover { color: #D8191F; }']
});

sIFR.replace(blairmditc, {
    selector: '.banner_A h1'
    , wmode: 'transparent'
    , css: ['.sIFR-root { text-align: center; }', 'a { text-decoration: none; }', 'a:link { color: #1A344E; }', 'a:hover { color: #D8191F; }']
});

sIFR.replace(blairmditc, { selector: 'h1', wmode: 'transparent', css: ['.sIFR-root {color: #1A344E;}'] });

$(document).ready(function() {

    function createPlayer(theWrapper, thePlaceholder, thePlayer, theFile, theStart) {

        var flashvars = { file: theFile, autostart: theStart, image: "/jwPlayer/preview.jpg" }
        var params = { allowfullscreen: "true", allowscriptaccess: "always" }
        var attributes = { id: thePlayer, name: thePlayer }
        swfobject.embedSWF("/jwPlayer/player.swf", thePlaceholder, "430", "320", "9.0.115", false, flashvars, params, attributes);
    };

    // ********************** VIDEO LOADER

    $(".box_video").each(function(index) {
        var playerID = "player" + (index + 1);
        var wrapperID = "wrapper" + (index + 1);        
        var firstTitle = $(this).find('ul.media-menu').find(":nth-child(1)").attr('title');       
        createPlayer(wrapperID, playerID, playerID, firstTitle, 'false');
    });

    $(".media-menu li").click(function() {
        var wrapper = $(this).parent().parent().find('div.wrapper');
        var wrapperID = $(this).parent().parent().find('div.wrapper').attr('id');
        var wrapperLoopID = wrapperID.replace("wrapper", "");
        var playerID = null;

        // check if element exists
        if ($(this).parent().parent().find('div.wrapper').find('div:first-child').length > 0) {
            playerID = $(this).parent().parent().find('div.wrapper').find('div:first').attr('id');
        }
        else {
            // create new player element
            var playerNew = '<div id=player' + wrapperLoopID + '/>';
            $(this).parent().parent().find('div.wrapper').text = playerNew;
            playerID = 'player' + wrapperLoopID;
        }

        // remove existing selected class
        $(".media-menu li.selected").removeClass("selected");

        // add class to 
        $(this).addClass("selected");
        createPlayer(wrapperID, playerID, playerID, this.title, 'false');

    });

    jQuery('.faq').accordion({
        header: 'h3',
        autoheight: false
    });

    //$('.faq').accordion({ header: 'h3' });

    //    $('.faq h3').click(function() {
    //        $(this).next().toggle();
    //        return false;
    //    }).next().hide();


    // ********************** SEARCH HIGHLIGHT
    $.fn.highlightText = function(text, className) {
        function highlight(node) {
            if (node.nodeType == 3) { // Node.TEXT_NODE
                var val = node.nodeValue;
                var pos = val.toLowerCase().indexOf(text);
                if (pos >= 0 && !$.className.has(node.parentNode, className)) {
                    var span = document.createElement("span");
                    span.className = className;
                    span.appendChild(document.createTextNode(val.substr(pos, text.length)));
                    node.parentNode.insertBefore(span, node.parentNode.insertBefore(
                document.createTextNode(val.substr(pos + text.length)),
                  node.nextSibling));
                    node.nodeValue = val.substr(0, pos);
                }
            } else if (!$(node).is("button, select, textarea")) {
                $.each(node.childNodes, function() { highlight(this) });
            }
        }
        return this.each(function() { highlight(this) });
    }

    $(document).ready(function() {
        var elems = $(".searchable");
        if (!elems.length) return;

        function getSearchTerms(url) {
            if (url.indexOf("?") == -1) return [];
            var params = url.substr(url.indexOf("?") + 1).split("&");
            for (var p in params) {
                var param = params[p].split("=");
                if (param.length < 2) continue;
                if (param[0] == "keywords") {
                    var query = decodeURIComponent(param[1].replace(/\+/g, " "));
                    if (query[0] == "!") query = query.slice(1);
                    var terms = [];
                    $.each(query.split(/(".*?")|('.*?')|(\s+)/), function() {
                        term = this.replace(/^\s+$/, "");
                        if (term.length) {
                            terms.push(term.replace(/^['"]/, "").replace(/['"]$/, ""));
                        }
                    });
                    return terms;
                }
            }
            return [];
        }

        var terms = getSearchTerms(document.URL);
        if (!terms.length) terms = getSearchTerms(document.referrer);
        $.each(terms, function(idx) {
            elems.highlightText(this.toLowerCase(), "searchword" + (idx % 5));
        });
    });

    // ********************** MAIN MENU BACKGROUND FIX
    $('#mainNavigation').pngFix();

    // ********************** MAIN MENU
    $("ul.sf-menu").superfish({
        pathClass: 'current',
        autoArrows: false,
        speed: 'normal',
        dropShadows: false,
        disableHI: false

    });

    // ********************** TAB MENU
    $('#tab_detail').tabs();
    $('#tab_properties_wide').tabs();
    // $(".tabImages").bind("click", handleTabSelect);
    //$("#mycarousel").css("width", "600px");

    // ********************** HEADER SEARCH BAR
    //    $("#header #slideButton").toggle(function() {
    //        $('#search').animate({ width: 300 });
    //        $('#topBar').css('backgroundImage', 'url(/css/images/search-bar_out.gif)');
    //        $('#searchTab').show().animate({ opacity: 1 }, 500);
    //    }, function() {
    //        $('#search').animate({ width: 50 });
    //        $('#topBar').css('backgroundImage', 'url(/css/images/search-bar_in.gif)');
    //        $('#searchTab').animate({ opacity: 0 }).hide();
    //    });

    // ********************** FOOTER STOCK INFO
    //$('#stock').innerfade({speed: 750, timeout: 4000, containerheight: '1em'});

    $('a.tooltip').cluetip({
        sticky: true,
        closePosition: 'title',
        arrows: true,
        cluezIndex:1000,
        positionBy: 'fixed',
        leftOffset: '-495px',
        topOffset: '-100px',
        dropShadow: false,
        mouseOutClose: true,
        fx: {
            open: 'show', // can be 'show' or 'slideDown' or 'fadeIn'
            openSpeed: ''
        },
        hoverIntent: {
            sensitivity: 3,
            interval: 50,
            timeout: 0
        }
    });

    $("#tbSearch").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Enter keyword"
        }).val("");
    });

    $("#tbSearch").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).val("Enter keyword");
    });

    $("#topSearch").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Enter keyword"
        }).val("");
    });

    $("#topSearch").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).val("Enter keyword");
    });

    $("#tbReminder").focus(function() {
        $(this).filter(function() {
            return $(this).val() == "" || $(this).val() == "Enter your e-mail"
        }).val("");
    });

    $("#tbReminder").blur(function() {
        $(this).filter(function() {
            return $(this).val() == ""
        }).val("Enter your e-mail");
    });

    $("#countrySelect").change(function() {
        $(this).parents('form:first').submit()
    });

    jQuery('.imageCarousel').jcarousel({ visible: 4 });

    $(".imageCarousel img").click(function() {
            
        // get parent container of clicked item    
        var parentContainer = $(this).getParent(7);
        
        // get index of container
        var elementIndex = $(".slideShowContainer").index(parentContainer);
        
        // select the correct container
        var n = $(".slideShowContainer").eq(elementIndex);
        
        // clear existing selected items
        var sections = n.find(".imageCarousel img");
        for (var i = 0, j = sections.length; i < j; i++) {
            $(sections[i]).removeClass("selected");
        }

        // add border to selected items
        $(this).addClass("selected"); 
         
        // set preview image
        var previewImage = n.find('.mid img');      
        previewImage.attr("src", $(this).attr("src"));   
    })

    $('#headerImages_').cycle({
        fx: 'fade',
        timeout: 15000,
        speed: 2000
    });

    $('#search_tab').tabs({
        remote: false,
        initial: 3
    });

    /*
    $('#btn_print').click(function() {
        window.print();
        return false;
    });
    */

    equalHeight($("#homeBox div.grid_4 div.box_small_clean div.middle"));

    $("tr:nth-child(odd)").addClass("odd");
    $("table").find("tr:first").addClass("firstRow");
    $("tr").find("td:first").addClass("firstCell");

    $("table.list tr").find("td:last").addClass("lastCell");
    $("table.listSum tr").find("td:last").addClass("lastCell");
    $("table.listSum").find("tr:last").prev().addClass("lastRow");
    $("table.listSum").find("tr:last").addClass("lastRowCells");


    $('#link-to-keyfacts').click(function() {
        $('#tab_detail').tabs('select', 1); // switch to keyfacts
        return false;
    });

    $(".tabLink").each(function(index) {

        var numLow = 0;
        var numHigh = 1000;

        var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow)) + 1;
        var numRand = Math.floor(Math.random() * adjustedHigh) + parseFloat(numLow);
        var pathname = window.location.pathname;
        var linkAnchor = $(this).attr("href");

        $(this).attr("href", pathname + "?id=" + numRand + "&target=tab" + linkAnchor);

    });

    if ($.query) {
        var qs = $.query.get('target');
        if (qs == "tab") {
            $.scrollTo(470);
        };
    };
});

jQuery.fn.getParent = function(num) {
    var last = this[0];
    for (var i = 0; i < num; i++) {
        last = last.parentNode;
    }
    return jQuery(last);
};


function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if (thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
    group.height(tallest);
}

