//update display of the large image
function displayImage(domAAroundImgThumb)
{
    if (domAAroundImgThumb.attr('href'))
    {
        var newSrc = domAAroundImgThumb.attr('href').replace('thickbox','large');
        if ($('#bigpic').attr('src') != newSrc)
		{ 
            $('#bigpic').fadeOut('fast', function(){
                $(this).attr('src', newSrc).show();
                if (typeof(jqZoomEnabled) != 'undefined' && jqZoomEnabled)
	                $(this).attr('alt', domAAroundImgThumb.attr('href'));
            });
        }
        $('#views_block li a').removeClass('shown');
        $(domAAroundImgThumb).addClass('shown');
    }
}

// Serialscroll exclude option bug ?
function serialScrollFixLock(event, targeted, scrolled, items, position)
{
	serialScrollNbImages = $('#thumbs_list li:visible').length;
	serialScrollNbImagesDisplayed = 3;
	
	var leftArrow = position == 0 ? true : false;
	var rightArrow = position + serialScrollNbImagesDisplayed >= serialScrollNbImages ? true : false;
	
	$('a#view_scroll_left').css('cursor', leftArrow ? 'default' : 'pointer').css('display', leftArrow ? 'none' : 'block').fadeTo(0, leftArrow ? 0 : 1);		
	$('a#view_scroll_right').css('cursor', rightArrow ? 'default' : 'pointer').fadeTo(0, rightArrow ? 0 : 1).css('display', rightArrow ? 'none' : 'block');
	return true;
}

//To do after loading HTML
$(document).ready(function() {
	//init the serialScroll for thumbs
	$('#thumbs_list').serialScroll({
		items:'li:visible',
		prev:'a#view_scroll_left',
		next:'a#view_scroll_right',
		axis:'x',
		offset:0,
		start:0,
		stop:true,
		onBefore:serialScrollFixLock,
		duration:700,
		step: 2,
		lazy: true,
		lock: false,
		force:false,
		cycle:false
	});
	
	$('#thumbs_list').trigger('goto', 1);// SerialScroll Bug on goto 0 ?
	$('#thumbs_list').trigger('goto', 0);

	//hover 'other views' images management
	$('#views_block li a').hover(
		function(){displayImage($(this));},
		function(){}
	);
	
	//set jqZoom parameters if needed
	if (typeof(jqZoomEnabled) != 'undefined' && jqZoomEnabled) {
		$('img.jqzoom').jqueryzoom({
			xzoom: 200, //zooming div default width(default width value is 200)
			yzoom: 200, //zooming div default width(default height value is 200)
			offset: 21 //zooming div default offset(default offset value is 10)
			//position: "right" //zooming div position(default position value is "right")
		});
	}

	//add a link on the span 'view full size' and on the big image
	$('span#view_full_size, div#image-block img').click(function(){
		$('#views_block li a.shown').click();
	});

	// Hide the customization submit button and display some message
	$('p#customizedDatas input').click(function() {
		$('p#customizedDatas input').hide();
		$('p#customizedDatas').append('<img src="' + img_ps_dir + 'loader.gif" alt="" /> ' + uploading_in_progress);
	});

	//init the price in relation of the selected attributes
	if (typeof productHasAttributes != 'undefined' && productHasAttributes)
		findCombination(true);
});


$(document).ready(function() {
	$("#group_" + qqProductTypeGroup).bind("change", productType_changed);

    productOptions_bind();
});

function productType_changed(e) {
    $("#attributes").hide();
    $("#qq_loader").show();

    $.post(
        baseDir + "/product-attributes.php",
        $("#buy_block").serialize(),
        function (data) {
            productOptions_unbind();
            $("#id_initial_product_type").val($("#productTypeGroup").val());
            $("#qq_loader").hide();
            $("#attributes").html(data);
            $("#attributes").show();
            productOptions_bind();
        }
    );
}

function productOptions_unbind(e) {
    $(".attributeGroup").unbind("change", productOptions_changed);

    $(".attributeGroup_helpIcon").each(function(){
        $(this)
            .unbind("mouseenter", productOptions_showDescription)
            .unbind("mouseleave", productOptions_hideDescription);

        $("#" + this.id.replace("_helpIcon", "_helpText")).remove();
    });
}

function productOptions_bind(e) {
    $(".attributeGroup").bind("change", productOptions_changed);
    
    $(".attributeGroup_helpIcon").each(function(){
        $(this)
            .bind("mouseenter", productOptions_showDescription)
            .bind("mouseleave", productOptions_hideDescription);

        //$("#" + this.id.replace("_helpIcon", "_helpText")).appendTo("body");
    });
}

function productOptions_showDescription(e) {
    var groupID = this.id.replace("_helpIcon", "");
    var groupElement = $("#" + groupID);
    var parentElement = groupElement.parent();
    var descriptionElement = $("#" + groupID + "_helpText");

    var left = parentElement.width() - descriptionElement.width() - 5;
    var top = groupElement.outerHeight() + 5;

    descriptionElement
        .css({"left": left + "px", "top": top + "px"})
        .show();
}

function productOptions_hideDescription(e) {
    $("#" + this.id.replace("_helpIcon", "_helpText"))
        .hide();
}

function productOptions_changed(e) {
    $("#qq_optionsChanged").show();
}