$(function(){
    $('#show .img_s a').bind('mouseenter', function() {
        $('#show .img_s a').removeClass('selected');
        $(this).addClass('selected');
        $('#show .img_l img').attr('src', $(this).find('img').attr('src'));
    });

    $("#header .slideshow img:gt(0)").hide();
    $("#header .slideshow .paging a")
        .each(function(i) {
            $(this).bind("click", function() {
                mousehover = true;
                show(i);
            }).bind("mouseleave", function() {
                mousehover = false;
            });
        });
    show(0);
    setInterval("auto_slide()",5000);
});

var mousehover = false;
var max_count = 3;
var current_index = 0;

function show(i) {
    $("#header .slideshow .paging a").removeClass("selected");
    $("#header .slideshow .paging a:eq(" + i + ")").addClass("selected");
    $("#header .slideshow img:eq(" + i + ")").fadeIn(800);
    $("#header .slideshow img:not(:eq(" + i + "))").fadeOut(800);
}
function auto_slide() {
    if (!mousehover) {
        if (current_index + 1 >= max_count) {
            current_index = 0;
        }
        else {
            current_index++;
        }
        show(current_index);
    }
}

function show_detail(id) {
    var item = eval('(' + $('#item_' + id).html() + ')');
    var jq_show = $('#show');
	jq_show.find('.img_s a').removeClass('selected');
    if (item.img_1) {
        jq_show.find('.img_l').show().find('img').attr('src', item.img_1);
        jq_show.find('.img_1').addClass('selected').show().find('img').attr('src', item.img_1);
    }
    else {
        jq_show.find('.img_l').hide();
        jq_show.find('.img_1').hide();
    }
    if (item.img_2) {
        jq_show.find('.img_2').show().find('img').attr('src', item.img_2);
    }
    else {
        jq_show.find('.img_2').hide();
    }
    if (item.img_3) {
        jq_show.find('.img_3').show().find('img').attr('src', item.img_3);
    }
    else {
        jq_show.find('.img_3').hide();
    }
    if (item.img_4) {
        jq_show.find('.img_4').show().find('img').attr('src', item.img_4);
    }
    else {
        jq_show.find('.img_4').hide();
    }
    jq_show.find('.sub_type').text(item.sub_type_name);
	jq_show.find('.sub_type_en').text(item.sub_type_name_en);
    jq_show.find('.model').text(item.model);
    jq_show.find('.size').text(item.size);
    jq_show.find('.description').text(item.description);
    jq_show.find('.description').html(jq_show.find('.description').text().replace(/\n/g, '<br />'));
    jq_show.find('.description_en').text(item.description_en);
    jq_show.find('.description_en').html(jq_show.find('.description_en').text().replace(/\n/g, '<br />'));

    var show_width = 850;
    var body_width = $('body').width();
    if (body_width > show_width) {
        $('#show').css('left',(body_width - show_width)/2);
    }
    else {
        $('#show').css('left',0);
    }

    jq_show.fadeIn('slow');
}

function close_detail() {
    $('#show').fadeOut('fast');
}
