
function updateForm(clicked) {
    var checkedID = $('.'+clicked+'s input:checked').attr('id');
    
    //show childgroup and check an option, if applicable the one previously selected, otherwise the first
    if($('.'+checkedID).size() > 0){
        var old_value = $('.'+checkedID).siblings('div:visible').find('input:checked').val();
        $('.'+checkedID).siblings('div').hide();
        if($('.'+checkedID+' input[value='+old_value+']').size() > 0){
            $('.'+checkedID).show().find('input[value='+old_value+']').attr('checked', 'checked').click();
        } else {
            $('.'+checkedID).show().find('input:first').attr('checked', 'checked').click(); 
        }
    }
    
    //set option and price   
    var option_id = $('.'+clicked+'s input:checked').siblings('.option_id').text();
    if(option_id){    
        $('.options input[value='+option_id+']').attr('checked', 'checked'); 
        
        var price = $('.options input[value='+option_id+']').parent().find('.price').text();
        $('#optionprice .inner:visible').fadeOut(50,function(){
            $('#optionprice .price').text(price).show();
            $('#optionprice .inner').fadeIn(200);                    
        });
    } 
    
    updatePreviewLink($('.sizes input:checked').val());  
            
}  

function updatePreviewLink(sizeID) {
    if(sizeID == 0){
       return false;
    } else {
        var url = $('#preview_link').attr('href');
        if(url!=undefined){
            var urlSplit = url.split('/');
            var newUrl = '/'+urlSplit[1]+'/'+urlSplit[2]+'/'+urlSplit[3]+'/'+urlSplit[4]+'/'+sizeID+'/';
            $('#preview_link').attr('href',newUrl);
        }
    }
} 
    

$(document).ready(function() {
    $('#newsletter input.text').focus(function(){
        $(this).val('');
    });
    
    //Product Form
    $(".form-options label input").click(function(){
        var option_type = $(this).attr('name'); 
        updateForm(option_type);
    });
    updateForm('size'); 
    
    //homepage image rotator
    var bannerObj = $('#hero');
    var waitTime = 8000;
    var fadeTime = 2000;
    var previousImageId = 0;
    
    var bannerImages = $('img.banner-inner', bannerObj);
    
    if( bannerImages.length > 1 ) {
        animateImages(1);
    }
    
    function animateImages(id) {
        
        var nextImage;
        
        if( id == (bannerImages.length-1) ) {
            var nextImageId = 0;
        } else {
            var nextImageId = id+1;
        }
        
        var previousImage = $('#bannerid-'+previousImageId, bannerObj);
        var image = $('#bannerid-'+id, bannerObj);
        
        if(image) {
            $(image).delay(waitTime).fadeTo(1,0,function(){
                $(previousImage).removeClass('banner-top');
                $(image).addClass('banner-top').fadeTo(fadeTime,1,function(){
                    $(previousImage).fadeOut(1);
                    previousImageId = id;
                    animateImages(nextImageId);
                });
            });
        }
    }
    
    $(".gallery-icon").css('opacity', 0.3);    
    $(".gallery-icon").hover(function(){         
        $(this).fadeTo(400, 1);          
    }, function(){
        $(this).fadeTo(400, 0.3);
    });
    
    //ie6 dropdown nav   
    $("#nav li").hover(function(){         
        $(this).addClass('hover');          
    }, function(){
        $(this).removeClass('hover');
    });
    
    $('a.detail-photo, #lightbox-container-image, #hero').live("contextmenu",function(e){
        return false;
    });
    
    //Preview
    $('.preview_buttons a').click(function() {
        $('.preview_buttons a').removeClass('active');
        $(this).addClass('active'); 
        var theSize = $(this).attr('id')
        var theSizeParts = theSize.split('_');    
        var theSizeValue = theSizeParts[1];    
        $('.preview .image_wrapper span:visible').fadeOut(400);
        $('.preview .image_wrapper span.'+theSize).fadeIn(400);
        var action_url = $('.theme_changer form').attr('action');
        temp = action_url.split('/');  
        $('.theme_changer form').attr('action', '/'+temp[1]+'/'+temp[2]+'/'+temp[3]+'/'+temp[4]+'/'+theSizeValue+'/');      
        return false;
    });
    
    $('.theme_changer form select').change(function(){
        $('.theme_changer form').submit();
    });
    
});

$(window).load(function(){
    
    //transparent overlay
    $('a.detail-photo').append('<div class=\'overlay\'><img src=\'/_inc/img/overlay.gif\' width=\''+$('a.detail-photo').width()+'\' height=\''+$('a.detail-photo').height()+'\'/></div>');
    
    $("a.detail-photo, .gallery-icon a").lightBox();
    
});
