$(document).ready(function(){
    initMenu();
    distributeTasks();
    fixBackground();
    changeColor();
    viewLarger();
    // showFeatures();
});

function fixBackground(){
    if ($.browser.msie) {
        if ($.browser.version == 6.0 || $.browser.version == 5.5) {
            $("li.top").each(function(){
                $(this).css("background", "none");
                $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/webresources/images/dropdown_top.png', sizing='scale')");
            });
            
            $("li.bottom").each(function(){
                $(this).css("background", "none");
                $(this).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/webresources/images/dropdown_bottom.png', sizing='scale')");
            });
        }
    }
}


function initMenu(){
    $("#menu li ul").hide();
    $("#menu li").hover(function(){
        $(this).children("ul").css("display", "none");
        $(this).children("ul").css("display", "block");
    }, function(){
        $(this).children("ul").css("display", "none");
    });
}

function distributeTasks(){
    $(".back").click(function(){
        history.back(0);
        return false;
    });
}

function changeColor(){
    $('.color_dropdown').change(function(){
        var imagepath = $('.color_dropdown option:selected').attr('imagepath');
        $('#image  img:first').attr('src', imagepath);
        var image = imagepath.split("/")[4];
        $('.viewlarger').attr('href', '/webresources/images/products/originals/' + image);
    });
}

function viewLarger(){
    $('.viewlarger').click(function(){
        var title = $('h1').text();
        GB_showImage(title, $(this).attr('href'))
        return false;
    });
}

function showFeatures(){
    $('.link-features').click(function(){
        var parent = $(this).parents('.product-cell');
        
        var features = $('.features', parent);
        if (features.css('display') == 'none') {
            parent.append('<div class="overlay"></div>');
            features.toggle();
            var overlay = $('.overlay', parent);
            $(overlay).css('z-index', 50);
            features.css('z-index', 100);
        }
        else {
            features.toggle();
            var overlay = $('.overlay', parent);
            $(overlay).css('z-index', 50);
        }
        return false;
    });
    
    $('.link-close').click(function(){
        var parent = $(this).parents('.product-cell');
        var features = $('.features', parent);
        features.toggle();
        var overlay = $('.overlay', parent);
        $(overlay).remove();
        return false;
    });
}
