// Initialize the Scripts for this site.

$(document).ready(function() {
    
    // Store Minicart:
    $('#social').before('<div id="miniCart">&nbsp;</div>');
    var miniCart = $('#miniCart');
    miniCart
        .addClass('loading')
        .attr('title','Loading Minicart...');
    pixelsilk2.renderSkin({skin: '['+'[StoreMiniCart]'+']', path:''}, function(html) {
        miniCart
            .html(html)
            .removeClass('loading')
            .attr('title','');
    });
    
    // Opens up Smart Solutions in new window.
    $('.lnkBlank').click(function() {
        window.open($(this).attr('href'), 'newWindow', '');
        return false;
    });
    
    // Selects text in Search Box when focused:
    $('.clearText').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>a').size() > 1) {
        $('#slides').cycle({
            timeout:    8000
        });
    }
    
    // If there is more than one slide, initialize Cycle:
    if ($('#slides').size() > 0 && $('#slides a').size() > 1) {
        $('#slides').cycle(
            {
                timeout:    8000,
                speed:    1000,
                pager:    '.slidePager',
                activePagerClass:    'current'
            }
        );
    }
    
    // Hide Pager if only one slide exists:
    if ($('#slides a').size() < 2) {
        $('.slidePager').hide();
    }
    
    
    if ($('.cta2').size() > 0 && $('.cta2 ul li').size() > 1) {
        $('#slider').easySlider({
            prevId: 'prevBtn',
            prevText: '&nbsp;',
            nextId: 'nextBtn',  
            nextText: '&nbsp;',
            controlsShow: true,
            pause: 5000,
            speed: 800,
            auto: true,
            continuous: true
        });
        $('span#prevBtn,span#nextBtn').addClass('spriteTheme');
    }
    
    if ($('#videoGallery').size() > 0) {
        $('#videoGallery #rightColumn div.items').pixelsilkVideoGallery({
            autoLoadFirst: true,
            autoPlayOnClick: true,
            fadeInSpeed: 300,
            fadeOutSpeed: 150,
            videoWidth: '640',
            videoHeight: '480'
        });
    }
    
    // hide text in spans (text replacement). Adds sprite class for 'image'.
    $('.imgText').each(function() {
        $(this).find('>span').hide();
        $(this).addClass('spriteTheme');
    });
    
    // Smartz Logo
    $('#smartzLink')
        .find('>span')
        .html('&nbsp;')
        .addClass('spriteTheme');
    
    // Content Area additions / theme extras:
    var contentArea = $('#contentArea');
    
    contentArea.append('<div class="shadowLeft spriteTheme">&nbsp;</div><div class="shadowRight spriteTheme">&nbsp;</div>');
    
    // IE6 Detector plugin:
    $('body').ieDetector();
    
    // Setup the navigation / menu system:
    pixelsilk2.renderSkin({skin: '[' + '[Menu1]' + ']', path: ''}, function(html) {
        $("#subNavigation").html(html);
        var sections = $("#subNavigation div");
        var topElements = $("#Menu li:not(.separator)");
        for (z = 0; z < sections.length; z++) {
            var li = topElements[z];
            var lihtml = li.innerHTML;
            li.innerHTML = lihtml + sections[z].innerHTML;
        }
        $("#Menu").navMenu({
            menuWidth: 200,
            menuElement: "ul",
            rightArrow: ' »',
            containerElement: '#theme'
        });
        $("#Menu li").hover(function(){
            $(this).addClass("over");
        }, function(){
            $(this).removeClass("over");
        });
        
        // Remove the bottom border on all last items in ULs:
        $('#Menu>li:last').css({'border-right':'0px'});
        $('#Menu').find('ul li:last-child').each(function() {
            $(this).css('border-bottom','0px');
        });
        
    });
    
    // add store categories to the menu under "Products":
    pixelsilk2.renderSkin({ skin: '[' + '[ProductsMenuItems]' + ']', path:''}, function(storeNavHTML) {
        $('#Menu>li').find("a:contains('Products')").after(storeNavHTML);
        $('#Menu>li').find("a:contains('Products')").parent().find('ul:first').attr('fadestate','none');
    });

    
    /* Print Stylesheet Content */
    
    // find the date:
    var d = new Date();
    
    var ampm = '';
    var hour = d.getHours();
    var minute = d.getMinutes();
    
    // set hours:
    if (hour <12) {
        ampm = 'AM';
        if (hour == 0) {
            hour = 12;
        }
    } else if (hour >= 12) {
        ampm = 'PM';
        if (hour >12 ) {
            hour = Number(hour-12);
        }
    }
    
    // set minutes:
    if (minute <10) {
        minute = '0'+minute;
    }
    
    var dateStr = Number(d.getMonth()+1) +'/'+ d.getDate() +'/'+ d.getUTCFullYear();
    var timeStr = hour +':'+ minute +' '+ampm;
    var header = $('#header');
    var printHtml = '';
    printHtml += '<div class="siteName">'+header.find('a.logo img').attr('alt')+'</div>';
    printHtml += '<div class="dateTime"><strong>Date:</strong> '+dateStr+' '+timeStr+'</div>';
    
    header.append(printHtml);
    
});
