$(function(){
    
    /*
     * Clears the subscribe field when the user clicks in it
     * If they leave it empty, puts the message back when they
     * click out of it else leaves what they typed in it
     */
    $('#subscribe-email').focus(function(){
        if (!this.value || this.value === "Type Email Address") {
            this.value = "";
        }
    });
    $('#subscribe-email').blur(function(){
        if (!this.value) {
            this.value = "Type Email Address";
        }
    });
    
    /*
     * Image swapper for product page
     * When the thumbnails are clicked, makes them the main image
     * Also initially sets the main image on the page
     */
    if ($('#product').length) {
        
        // determine the collection
        var collection = 'mileno';
        if ($('#backlink a').attr('href') === 'plush-collection.php') {
            collection = 'plush';
        }
        
        // determine the season
        var bits = window.location.href.split('/');
        var season = bits[bits.length-2];
        
        // set main image
        var mainImgBase = "../../images/" + collection + "/" + season + "/large/";
        var images = $('#styles img');
        var image = $(images[0]).attr('src');
        var parts = image.split("/");        
        var src = mainImgBase + parts[parts.length-1]; 
        $('#main-image').html("<img src='" + src + "' />");
        
        // change the main image from the thumbnails
        $('#styles img').click(function(){
            $('#main-image').html("");
            var image = $(this).attr('src');
            var parts = image.split("/");
            var src = mainImgBase + parts[parts.length-1]; 
            $('#main-image').html("<img src='" + src + "' />");
            return false;
        });
    }
    
});
