﻿
$(document).ready(function() {
    // Bind the search button/box
    setSearch();

    // Stretch the scrapbook to fit the height on load and on window resize
    $(window).bind('resize', function() {
        $('div#book').css(($.browser.msie && $.browser.version < 7 ? '' : 'min-') + 'height', ($(window).height() - 368) + 'px');
    }).trigger('resize')


    // VERTICAL ALIGN FOR HOMEPAGE
    var heightMax = 0;
    $('#homepagePromos .promo').each(function() { // Cycle through each promo
        // If current promo is the biggest one, store it
        var thisHeight = $(this).height();
        if (thisHeight >= heightMax) {
            heightMax = thisHeight;
        }
    });
    // Apply padding to each div to make vertical aligned middle
    $('#homepagePromos .promo').each(function() { // Cycle through each promo
        var thisHeight = $(this).height();
        var padding = Math.ceil((heightMax - thisHeight) / 2); // calculate padding required (rounded UP)
        $(this).css('padding-top', padding); // apply padding
    });


    // FUNCTIONS FOR PRODUCT PAGE VARIENT SELECT
    $('.variantSelect input').css({ position: "absolute" }).fadeTo(1, 0); // Hide input field (1ms is quicker than 0)
    $('.variantSelect label, .variantSelect input').css({ cursor: "pointer" }); // Pointer for label and input
    // Add default image to list item
    $('.variantSelect label').css({ background: "transparent url(i/bg_varientSelect.gif) no-repeat left 50%", padding: "5px 0 5px 30px" });
    // Change background for checked="checked" (default)
    $('.variantSelect input[type="radio"]:checked').next().css({ background: "transparent url(i/bg_varientSelect_checked.gif) no-repeat left 50%" });
    // Change background image when new input is selected
    $('.variantSelect input[type="radio"]').click(function() {
        // Reset all background images
        $('.variantSelect label').css({ background: "transparent url(i/bg_varientSelect.gif) no-repeat left 50%" });
        // Change clicked imput to correct image
        $(this).next().css({ background: "transparent url(i/bg_varientSelect_checked.gif) no-repeat left 50%" });
    });

    // FUNCTIONS FOR checkBox / GALLERY SEARCH
    $('#gallerySearch input[type=checkbox]').css({ position: "absolute" }).fadeTo(1, 0); // Hide input field (1ms is quicker than 0)
    $('#gallerySearch label,#gallerySearch input').css({ cursor: "pointer" }); // Pointer for label and input
    // Add default image to list item
    $('#gallerySearch label').css({ padding: "5px 0 5px 30px" }).addClass('unselected');
    // Change background for checked="checked" (default)
    $('#gallerySearch input[type=checkbox]:checked').next().addClass('selected');
    // Change background image when new input is selected
    $('#gallerySearch input[type=checkbox]').click(function() {
        // Toggle background image
        $(this).next().toggleClass('selected');
    });    

    // PRODUCT PAGE GALLERY
    // Preload images
    $('.imageSelect li a').each(function() {
        var preload = "<img src=\"" + $(this).attr('href') + "\" />";
        $('#preloadImages').append(preload);
    });
    $('.preload img').each(function() {
    
        var largesrc = $(this).attr('src').replace('img2','img1');
        var preload = "<img src=\"" + largesrc + "\" />";
        $('#preloadImages').append(preload);
        
    });

//    // When user clicks bullet
//    $('.productGallery a').click(function() {
//        // Change class of link to show selected image
//        $('.productGallery a').removeClass('selected');
//        $(this).addClass('selected');

//        var imgSrc = $(this).attr("href"); // Get image src from link
//        // Change image
//        $('.productGallery .image img').fadeTo('slow', 0, function() { // fade out mainImg
//            $(this).attr("src", imgSrc) // Change img src
//            .fadeTo('slow', 1); // Fade in main img
//        });

//        return false; // dont follow link (to imaage)
//    });

 // DOG POPUP GALLERY
    $('.dogPopup .thumbnails img').click(function(){
        var imgSrc = $(this).attr('src'); // get img src
        var imgSrcLrg = imgSrc.replace('img2', 'img1'); // get img1 url
        $('.dogPopup .mainImg img').attr('src', imgSrcLrg); // Change image
        var dogid = $(this).attr('class');
        $('.galleryText div').html($('.desc' + dogid).html());
    });
    
    //Fix google toobar autocomplete
      var timer;
    
    for (var count = 0; count < 20; count++)
    {
        setTimeout("resetInputs()", 50 * count);
    }
    timer = setInterval("resetInputs()", 250);

});


function resetInputs(){

    $('.doodleForm').find('select').css("background-color", "#CDBB9D");
    $('.doodleForm').find('input').css("background-color", "transparent");
}

function ClearReset(myTxt, myDefault){
    if(myTxt.value == myDefault){
        myTxt.value = '';
    }
    else if(myTxt.value == ''){
        myTxt.value = myDefault;
    }
}

function clear_checkboxes(myID){
    var frm = document.forms[0];
    for (i = 0; i < frm.length; i++) {
        if (frm.elements[i].id != myID) {
                frm.elements[i].checked = false;
               $('input').next().removeClass('selected');
        }
    }
}


function popUp(URL) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=820,left = 300,top = 0');");
}

function setSearch(){
    
    $('#btnQuickSearchGo a').click(function(){
        search();        
    });
    
    $('.quickSearch').keyup(function(e) {  
        if(e.keyCode == 13) {
            search();
        }
    });


    
}

function search(){
    
    var keywords = $('.quickSearch').val();
        
    if (keywords != "" && keywords != "Search for..."){    
        window.location = "/search.aspx?keywords=" + keywords;
    }
    
}