// Initialize the Scripts for this site.

$(document).ready(function() {
    
    // Opens up Smart Solutions in new window.
    $('.lnkBlank').click(function() {
        window.open($(this).attr('href'), 'newWindow', '');
        return false;
    });
    
    $('.lnkTargetNew').attr('target','_blank');
    
    // Selects text in Search Box when focused:
    $('#search input[type=text]').each(function() {
        var txtSearchText = $(this).val();
        $(this).focus(function() {
            if ($(this).val() == txtSearchText) {
                $(this).val('');
            }
        });
        $(this).blur(function() {
            if ($(this).val() == '') {
                $(this).val(txtSearchText);
            }
        });
    });
  
    $('img').each(function(){
      if($(this).attr("src") == '' || $(this).attr("src") == "/") $(this).hide();
    });
    
    // Hides list item images if no src is defined:
    $('img.listItemImage').each(function() {
        if ($(this).attr('src') == '' || $(this).attr('src') == '?w=150') {
            $(this).hide();
        }
    });
    
    $('.staffInfoItem span.text').each(function() {
        if ($(this).html() == '') {
            $(this).parent().hide();
        }
    });
    
    // If there is more than one slide, initialize Cycle:
    if ($('#slides').size() > 0 && $('#slides div.slide').size() > 1) {
        $('#slides').cycle(
            {
                timeout:    8000,
                pager:        '#pager',
                prev:        '#pagerLeft',
                next:        '#pagerRight'
            }
        );
        $('#pager').append('<div id="pLeftCorner"></div><div id="pRightCorner"></div>');
    }
    
     $('#slides .slide').find('a').click(function() {
        if ($(this).attr('href') == '') {
            return false;
        }
     });
     
    // IE6 Detector plugin:
    $('body').ieDetector();
    
    var country = $('#selectCountry');
    var countryFlag = $('#cFlag');
    
    var selectCountry = '<select>';
    selectCountry += '<option value="usa">United States</option>';
    selectCountry += '<option value="uk">United Kingdom</option>';
    selectCountry += '<option value="ca">Canada</option>';
    selectCountry += '</select>';
    
    country.html(selectCountry);
    
    country.find('select').change(function() {
        // do something when country selector is changed:
        
        navigate($(this).val());
        
    });
    
    function navigate(url) {
        if (url == 'usa') {
            location.href = '/';
        } else {
            location.href = '/'+url+'/';
        }
    }
    
    countryFlag.html('<img src="flag-'+country.find('select').val() +'.gif" />');
    
    // Top Bar bullNose:
    $('#siteMenu').bullNose({
        pathToImages: '/',
        topLeftImg: 'menu-corner-left.gif',
        topRightImg: 'menu-corner-right.gif',
        imageWidth: '10',
        imageHeight: '10',
        showBottomLeft: false,
        showBottomRight: false
    });
    
    // Banner Arrow:
    $('#banner').append('<div class="actionArrow"></div>');

    // CTA Arrow:
    var homeCTALast = $('#homeCTA>li:last');
    homeCTALast
        .css({'position':'relative','z-index':'10'})
        .append('<div class="actionArrow"></div>');
    
    // Home Page CTAs:
    $('#homeCTA>li:not(:last)').css({'margin-right':'17px'});
    
    // add store categories to the menu under "Products":
    pixelsilk2.renderSkin({ skin: '[' + '[ProductsMenuItems]' + ']', path:''}, function(storeNavHTML) {
        $('#Menu>li').find("a:contains('Store')").after(storeNavHTML);
        $('#Menu>li').find("a:contains('Store')").parent().find('ul:first').attr('fadestate','none');
    });
    
    pixelsilk2.renderSkin({ skin: '[' + '[Menu1]' + ']', path: '' }, function(html) {
        $('#subNavigation').html(html);
        var sections = $('#subNavigation div');
        var topElements = $('#Menu>li');
        for (z = 0; z < sections.length; z++) {
            var li = topElements[z];
            var lihtml = li.innerHTML;
            li.innerHTML = lihtml + sections[z].innerHTML;
        }
        StartMenu();
    });
    
    var categories = $('#categoryList');
    if (categories.size() > 0) {
        categories.find('ul:empty').remove();
        $('ul#categoryList>li>ul>li').each(function() {
            if ($(this).children('ul').length > 0) {
                $(this).find('a:eq(0)').append('<img style="float: right; position: relative; left: -6px; top: 5px;" src="/arrow-down.png" />');
            } else {
                $(this).find('a:eq(0)').append('<img style="float: right; position: relative; left: -8px; top: 3px;" src="/arrow-right.png" />');
            }
        });
    }
    
    /*
    if ($('#sectionImage').find('img').attr('src') == '') {
        $('#sectionImage').find('img').hide();
    }
    */
    
    if ($('body.home').length<1 && $('#pageHeading').size()>0) {
        // Debug:
        //$('body').append('<div id="jsDebugText" style="display:none;"></div>');
        //var jsText = $('#jsDebugText');
        
        function breadCrumbHeight() {
            var bcDefaultHeight = 20;
            var breadCrumbs = $('#breadcrumbs');
            if (breadCrumbs.height() > bcDefaultHeight) {
                var newBCHeight = Number(breadCrumbs.height() - bcDefaultHeight);
                $('#mainContent').css({'padding-top':Number(newBCHeight+60)+'px'});
                //jsText.append('1 bc height: '+Number(newBCHeight+60)+'px');
                return newBCHeight;
            } else {
                $('#mainContent').css({'padding-top':Number(bcDefaultHeight+60)+'px'});
                //jsText.append('2 bc height: '+bcDefaultHeight);
                return bcDefaultHeight;
            }
        }
        
        // Evaluate weather a section image exists and place the H1 appropriately:
        var sectionImage = $('#sectionImage');
        var sectionImageSrc = $('#sectionImage img').attr('src')
        var pageHeading = $('#pageHeading');
        var h1MinimumTop = 135;
        
        if (sectionImageSrc == '' || sectionImageSrc == 'undefined' || sectionImageSrc == '/') {
            //jsText.append('f1, ');
            sectionImage.find('img').hide();
            pageHeading.css({'top':Number(breadCrumbHeight()+35)+'px'});
            //jsText.append('NO Image.');
        } else {
            //jsText.append('f2, ');
            if (sectionImage.find('img').height()>0) {
                var sectionImageHeight = sectionImage.find('img').height();
                if (sectionImageHeight<h1MinimumTop) {
                    sectionImageHeight = h1MinimumTop;
                    //jsText.append('Image found; MINIMUM height '+sectionImageHeight+'px applied.');
                } else {
                    //jsText.append('Image found; height '+sectionImageHeight+'px applied.');
                }
                
            } else {
                var sectionImageHeight = h1MinimumTop;
                //jsText.append('Minimum Height added. '+sectionImageHeight);
            }
            
            pageHeading.css({'top':Number(sectionImageHeight+25+breadCrumbHeight())});
        }
    }
    
});
