var api;
$(document).ready(function()
{
	$('[tooltip]').each(function(){	
		$(this).qtip({
			content: $(this).attr('tooltip'),

		   	show: {
                when: {event: 'mouseover'},
                effect: {type: 'grow'}
            },

		   	hide: 'mouseout',

		   	position: {
		   		/*corner: {
		   			target: 'topLeft',
		   			tooltip: 'bottomRight'
		   		}*/
            	at: 'top left',
     	        my: 'bottom right'
		   	},
		   	
		   	style: {
		   		background: '#FFF',
		   		color: '#000',
		   		//tip: 'bottomRight',
		   		border: { 
		   			color: COLOUR_PRIMARY,
		   			radius: 4,
		   			width: 3
		   		},
			   	width: {min: 200}
		   	}
		})
	});

	// Add a scroll feature to the description container
	DescriptionScroll();
	
	// If a product has multiple CMI, then hide a description container until user selects an approproate cmi
	HideDescriptionContainer();
	
	carousel_init('#product_carousel',$('#product_carousel_count').text(),0,2);
	carousel_init('#ordered_product_carousel',$('#ordered_product_carousel_count').text(),0,2);
});

var editor;

// If a product has multiple CMI, then hide a description container until user selects an approproate cmi
function HideDescriptionContainer()
{
	var multipleCmi = $("#multipleCMIFlag").val();

	if(multipleCmi == 1)
	{
		$("#pageContent").addClass("hidden");
	}
}

// Add a scroll feature to the description container
function DescriptionScroll()
{
	/*$('#product_description').jScrollPane({
		showArrows: true,
		scrollbarWidth: 23
	});*/
	
	if($('#product_description').length > 0)
	{
		api = $('div#product_description').jScrollPane({showArrows: true}
		).data('jsp');
	}
}

// Select a CMI option from the drop down menu and display appropriate CMI description
function showMultipleDescriptions(CMICode, productId)
{
	// If a default option has not been selected (i.e -- Select a CMI Description --)
	if(CMICode != 0)
	{
		$("#pageContent").removeClass("hidden");
		
		$.ajax({
			type: "POST",
			url: BASE + '/ajax/showproductdescription',
			data: "CMICode=" + CMICode +
				"&productId=" + productId,
			success: function(description)
			{
				//$("#product_description").html(description);
				//DescriptionScroll();
				api.getContentPane().html(description);
            	api.reinitialise(); 
            	$('.cboxElement').colorbox({
            		scrolling: false,
            		onComplete: function()
            		{
            			$.fn.colorbox.resize();
            		}
            	});
			}
		});
	}
	else
	// Hide description container for a default option (i.e -- Select a CMI Description --)
	{
		$("#pageContent").addClass("hidden");	
	}
}

function displayRichTextBox(div)
{
	var description = $("#product_description").html();
	
	$("#productDetail").remove();
	$("#editButton").hide();
	
	$("#editProductDescription").show();
	$("#editProductDescription").html(description);
	
	editor = CKEDITOR.replace(div);
	$("#saveButton").show();
}

/* 
// Live edit of the description 
function saveDescription(productId)
{
	var content_text = editor.getData();

	content_text = escape(content_text);
	content_text = content_text.replace('+', '%2B');
	content_text = content_text.replace('/', '%2F');

	$.ajax({
		type: "POST",
		url: BASE + '/ajax/productsavedescription',
		data: "contentText=" + content_text
			+ "&productId=" + productId,
		success: function(msg)
		{
			if (msg == 'OK')
			{
				window.location = BASE + "/product/" + productId;
			}
		}
	});

}
*/
