﻿/*================================
Common header functions
==================================*/
var flagsOpen = false;

if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) == 6) {
    try {
        document.execCommand("BackgroundImageCache", false, true);
    } catch (err) { }
}


$J(document).ready(function() {
    //Auto fader on hover
    $J('.fader').hover(
      function() {
          $J(this).stop().fadeTo("normal", 0.0);
      },
      function() {
          $J(this).stop().fadeTo("normal", 1.0);
      }
    );


    //Flag selector
    moveCurrentFlag();
    $J('.flagList').css('opacity', 0);

    $J('.currentFlag').click(function() {
        if (flagsOpen == true) {
            flagsOpen = false;
            $J(this).attr('style', '');
            $J('.flagList').stop().fadeTo("normal", 0.0, function() { $J('.flagList').css('display', '') });

        } else if (flagsOpen == false) {
            flagsOpen = true;
            $J(this).css('background-position', 'bottom left');
            $J('.flagList').css('display', 'block');
            $J('.flagList').stop().fadeTo("normal", 1.0, function() { $J('.flagList').css('opacity', '') });
        }
        $J(this).blur();
        return false;
    });

    $J('.flagSelect span').hover(
        function() { $J(this).css('text-decoration', 'underline'); },
        function() { $J(this).css('text-decoration', 'none'); }
    );


    //Main search focus
    $J('.mainSearchTextInput').focus(function() {
        $J(this).parent().addClass('searchBoxFocus');
        /*if ($J(this).attr("value") == "Search Blackmores for...") $J(this).val('');*/
    });

    $J('.mainSearchTextInput').blur(function() {
        $J(this).parent().removeClass('searchBoxFocus');
        /*if ($J(this).attr("value") == "") $J(this).val('Search Blackmores for...');*/

        if (customEESet) searchFieldUpdate($J(this).attr("value"));
    });


    //Popular searches
    $J('.popularSearchesTab').click(function() {
        focusTab = $J('.popularSearchesTab');
        focusContent = $J('.popularSearchesContent');

        swapExpandTabs();
        return false;
    });


    //Explore blackmores
    $J('.exploreBlackmoresTab').click(function() {
        focusTab = $J('.exploreBlackmoresTab');
        focusContent = $J('.exploreBlackmoresContent');

        swapExpandTabs();
        return false;
    });

    //For professionals
    $J('.forProfessionalsTab').click(function() {
        focusTab = $J('.forProfessionalsTab');
        focusContent = $J('.forProfessionalsContent');

        swapExpandTabs();
        return false;
    });

    $J('.closeExpandable').click(function() {
        closeExpandTabs();
        return false;
    });


    $J('.generalNotification .closeBtn').click(function() {
        $J('.generalNotification').css('display', 'none');
        return false;
    });
});

function searchFieldUpdate(val) {
    try {
        if (customEEBase != null && customEEList != null && customEESet != null) {
            for (i = 0; i < customEESet.length; i++) {
                if (customEESet[i] == val) window.location = 'http://' + customEEBase + customEEList[i];
            }
        }
    } catch (e) {
        //
    }
}

/*====================================================
Expandable tabs
=====================================================*/
var expandHeight = 0;
var focusTab;
var focusContent;
var selectedTab;
var selectedContent;

function swapExpandTabs() {
    $J(focusTab).blur();

    if ($J(focusContent).css('display') != 'block') {
        $J('.popularSearchesTab').removeClass('selected').blur();
        $J('.exploreBlackmoresTab').removeClass('selected').blur();
        $J('.forProfessionalsTab').removeClass('selected').blur();

        $J(focusTab).addClass('selected');

        if (selectedTab != null) $J('.expandedContent').fadeTo("fast", 0.0, function() { animateExpandTabs(); });
        else animateExpandTabs();
    } else {
        closeExpandTabs();
    }
}

function animateExpandTabs() {
    $J('.popularSearchesContent').css('display', 'none');
    $J('.exploreBlackmoresContent').css('display', 'none');
    $J('.forProfessionalsContent').css('display', 'none');

    $J(focusContent).css('display', 'block');
    expandHeight = $J(focusContent).height() + 25;

    if (selectedTab != null) $J('.expandedContent').animate({ height: expandHeight + 'px', opacity: 1 }, 400);
    else $J('.expandedContent').animate({ height: expandHeight + 'px', opacity: 1 }, 600);

    $J('.expandedContent').fadeTo("normal", 1.0, function() { $J('.expandedContent').css('opacity', '') });

    selectedTab = focusTab;
    selectedContent = focusContent;
}

function closeExpandTabs() {
    $J('.popularSearchesTab').removeClass('selected').blur();
    $J('.exploreBlackmoresTab').removeClass('selected').blur();
    $J('.forProfessionalsTab').removeClass('selected').blur();

    $J('.expandedContent').animate({ height: '0px', opacity: 0 }, 600, function() { $J(focusContent).css('display', 'none'); });

    selectedTab = null;
    selectedContent = null;
}


/*====================================================
Flag selection
=====================================================*/
function moveCurrentFlag() {
    var pageURL = window.location + "";
        
    if($J('.flagList li a').length > 0 && $J('.flagList li').length > 0){ 
        var currentFlag = $J('.flagList li a')[0];
        var currentFlagLi = $J('.flagList li')[0];

        if (currentFlag != undefined){
            $J('.currentFlag').html(currentFlag.InnerHTML);
            $J('.currentFlag').attr('title', currentFlag.title);
        }
        $J(currentFlagLi).css('display', 'none');
    }

//    if (pageURL.indexOf('.co.nz') > 0 || pageURL.indexOf('.nz.testing') > 0 || pageURL.indexOf('nz.int1') || pageURL.indexOf('nz.stage1')) {
//        $J('.currentFlag').html($J('.fNZ').html());
//        $J('.currentFlag').attr('title', $J('.fNZ').attr('title'));
//        $J('.fNZ').parent().css('display', 'none');
//    } else if (pageURL.indexOf('.com.au') > 0) {
//        $J('.currentFlag').html($J('.fAU').html());
//        $J('.currentFlag').attr('title', $J('.fAU').attr('title'));
//        $J('.fAU').parent().css('display', 'none');
//    } else if (pageURL.indexOf('.com.hk') > 0) {
//        $J('.currentFlag').html($J('.fHK').html());
//        $J('.currentFlag').attr('title', $J('.fHK').attr('title'));
//        $J('.fNZ').parent().css('display', 'none');

//    } else if (pageURL.indexOf('.com.my') > 0) {
//        $J('.currentFlag').html($J('.fMY').html());
//        $J('.currentFlag').attr('title', $J('.fMY').attr('title'));
//        $J('.fMY').parent().css('display', 'none');

//    } else if (pageURL.indexOf('.com.sg') > 0) {
//        $J('.currentFlag').html($J('.fSG').html());
//        $J('.currentFlag').attr('title', $J('.fSG').attr('title'));
//        $J('.fSG').parent().css('display', 'none');

//    } else if (pageURL.indexOf('.com.tw') > 0) {
//        $J('.currentFlag').html($J('.fTW').html());
//        $J('.currentFlag').attr('title', $J('.fTW').attr('title'));
//        $J('.fTW').parent().css('display', 'none');

//    } else {
//        $J('.currentFlag').html($J('.fAU').html());
//        $J('.currentFlag').attr('title', $J('.fAU').attr('title'));
//        $J('.fAU').parent().css('display', 'none');
//    }
}


