
$(function() {
    // thumbs
    $('.thumbimg').click(function() {
        var sZoomImg = $(this).attr("zoom");

        $('#loader').addClass('loading')
                  .html("");

        if (sZoomImg == "") {
            sZoomImg = $('#lastimgzoom').val();
        }

        renderLargeImage(sZoomImg);


    });
    
    // hide broken images
    $('.productImgThumb').each(function(index) {
        if ($(this).find("img").attr("src") == "http://images.miretail.com/" || $(this).find("img").attr("src") == "http://images.test.miretail.com/") {
            $(this).hide();        
        }
    });    

    // review form
    $("#hrefWriteReview").colorbox({ width: "800px", height: "700px", inline: true, href: "#divModalReviewForm" });

    $('.styleimg').click(function() {
        $('.imgMain').attr("src", $(this).attr("zoom"));
    });


    // used/new tabs
    var usedNewTabContainers = $('#divUsedNewContent > div');
    $('.divUsedContent').hide();

    $('.hrefUsed').click(function() {
        $('.hrefUsed').addClass('current');
        $('.hrefNew').removeClass('current');
        $('.divNewContent').hide();
        $('.divUsedContent').show();
    });

    $('.hrefNew').click(function() {
        $('.hrefNew').addClass('current');
        $('.hrefUsed').removeClass('current');
        $('.divNewContent').show();
        $('.divUsedContent').hide();
    });

    // details overview tabs
    $('.divDetailsContent').hide();
    $('.hrefOverview').click(function() {
        $('.hrefOverview').addClass('current');
        $('.hrefDetails').removeClass('current');
        $('.divOverviewContent').show();
        $('.divDetailsContent').hide();
    });

    $('.hrefDetails').click(function() {
        $('.hrefDetails').addClass('current');
        $('.hrefOverview').removeClass('current');
        $('.divDetailsContent').show();
        $('.divOverviewContent').hide();
    });

    // get sample thumbs in
    $('.productImgThumb').each(function(index) {
        if (index < 4 && $(this).find("img").attr("src") != "http://images.miretail.com/") {
            var img = "<div class=productImgThumb2><img src=" + $(this).find("img").attr("src") + " onclick=showModalThumbs('" + $(this).find("img").attr("zoom") + "') height=50 width=50 zoom=" + $(this).find("img").attr("zoom") + "></div>";
            $('#divMainThumbs').append(img);
        }
    });

    // hide Product Details tab if no content
    var prodDetails = $('#divDetailsContent').html();
    prodDetails = $.trim(prodDetails);
    if (prodDetails == "") {
        $('.liDetails').hide();
    }

});


function showModalThumbsMain() {
    if ($('#hdnDefaultZoom').val() != "") {
        $.fn.colorbox({ width: "900px", height: "825px", inline: true, href: "#divModalThumbs" });
        setTimeout("showModalThumbs($('#hdnDefaultZoom').val())",200);
        
    }
}


function showModalThumbs(sZoomImg) {
    if ($('#hdnDefaultZoom').val() != "") {
        $.fn.colorbox({ width: "900px", height: "825px", inline: true, href: "#divModalThumbs" });
        setTimeout("renderLargeImage('" + sZoomImg + "')",500);
        
    }
}

function renderLargeImage(sZoomImg) {
    $('#loader').html("");
    var img = new Image();
    $(img)
        .load(function() {
            $(this).hide();
            $('#loader')
            .removeClass('loading')
            .append(this);

            if (this.height > 600) {
                var dFactor = 600 / this.height;
                var width = dFactor * this.width;
                $(this).attr('height', "600");
                $(this).attr('width', width);
            }
            $(this).vAlign();
            $(this).fadeIn();
        })
        .error(function() {
            $(this).vAlign();
        })
        .attr('src', sZoomImg);

}    
    
$.fn.vAlign = function() {
	return this.each(function(i){
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	});
};    
    
    
function swapUsedOptions() {
    $(".item").hide('fast');                            
    if ($("#usedCondition option:selected").val() != "0") {
        var showId = "used" + $("#usedCondition option:selected").val();
        $(".item").hide('fast');
        if ($("." + showId).length > 0) {
            $("." + showId).show('slow');
        }
        else {
            $(".usedUnavailable").show('slow');
        }
    }
    else {
        if ($(".item:not(.usedUnavailable)").length > 0) {
            $(".item:not(.usedUnavailable)").show('slow');
        }
        else {
            $(".usedUnavailable").show('slow');
        }
    }
}