jQuery(function($){
	
	/* dialog properties */
	var dialog_props = {
		show : 'fade',
		hide : 'fade',
		draggable : false,
		bgiframe : true,
		autoOpen : false,
		modal : true,
		height: 435
	}
	
	$.extend(dialog_props, {width : jQuery.support.noCloneEvent ? 160 : 158});
	
	/* init dialog */
	$('#dialog').dialog(dialog_props);
	
	/* init carousel */
	$('#products_carousel').jcarousel({
        vertical: true,
        scroll: 1,
		auto: 3,
        wrap: 'last',
		initCallback: carousel_init_callback,
		buttonNextHTML: '<div>next &raquo;</div>',
		buttonPrevHTML: '<div>&laquo; previous</div>'
    });

	/* click handler for widget */	
	$('#widget_container').live('click', function(e){
		var target = $(e.target),
			dialog = $('#dialog');
		
		switch(target.attr('class'))
		{
			case 'external':
				$.post(target.attr('href'), {'product_id' : target.attr('rel'), 'product_meta' : 'redirect_to_buy'});
				window.open(target.attr('rev'));
				e.stopPropagation();
				e.preventDefault();
				break;
				
			case 'detail':
				$('.buy').unbind('click');
				
				var product_name = target.attr('alt'),
					price = target.parent().nextAll('.price').html(),
					comments = target.next().html(),
					buy_link = target.parent().next('.more_info').children('a:first'),
					meta_url = buy_link.attr('href'),
					buy_url = buy_link.attr('rev'),
					product_id = buy_link.attr('rel'),
					product_img = '\n\t<img width="' + target.attr('width') + '" height="' + target.attr('height') + '" src="' + target.attr('src') + '" alt="" />\n',
					content = '';
				
				/* record product detail view */
				$.post(meta_url, {'product_id' : product_id, 'product_meta' : 'detail_view'});
				
				content += '<div class="product_name">' + product_name + '</div>\n';
				content += '<div class="product_image">\n\t<a href="' + meta_url + '" class="buy" rev="' + buy_url + '" rel="' + product_id + '">' + product_img + '</a>\n</div>\n';
				content += '<div class="enlarge">\n\t<a class="enlarge" href="' + target.attr('src') + '" target="_blank">+ Enlarge</a>\n</div>\n';
				content += '<div class="product_price">' + price + '<a class="buy" href="' + meta_url + '" rev="' + buy_url + '" rel="' + product_id + '">Go to Store</a>\n</div>\n';
				
				if (comments != undefined && comments != null)
					content += '<div class="product_comments">' + comments + '</div>\n';
				
				dialog.html(content).dialog('open');
				
				$('.buy:visible').bind('click', function(e){
					$.post($(this).attr('href'), {'product_id' : $(this).attr('rel'), 'product_meta' : 'redirect_to_buy'});
					window.open($(this).attr('rev'));
					dialog.dialog('close');
					e.stopPropagation();
					e.preventDefault();
				});
				
				e.stopPropagation();
				e.preventDefault();
				break;
		}
		
	});
});

function carousel_init_callback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
	
	$('span.more_info a').bind('click', function() {
		carousel.stopAuto();
	});
	
	$('a.ui-dialog-titlebar-close').bind('click', function() {
		carousel.startAuto();
	});

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(
		function()
		{
        	carousel.stopAuto();
    	},
		function()
		{
        	carousel.startAuto();
    	});
};

Array.prototype.contains = function(obj) {  var i = this.length;  while (i--) {    if (this[i] === obj) {      return true;    }  }  return false;}


$(document).ready(function(){
	var cookie = $.cookie('ratings');
	if(!cookie) { 
		cookie = '';
		$.cookie('ratings',cookie);
	}
	var data = cookie.split(',');
	
	$('li').each(function(i, o) {
		var id = $(o).find('a.external').attr('rel');
		if (!data.contains(id) && !data.contains('-' + id)) {
			$(o).append('<div class="rate"><a href="#" class="thumbDown"></a><a href="#" class="thumbUp"></a></div>');
		}
	});
	
	$('a.thumbDown').live('click', function(e){
		var value = -1 * $(e.target).parents('li').find('a.external').attr('rel');
		var cookie = $.cookie('ratings');
		if(cookie.length > 0) {
			cookie += ',';
		}
		cookie += value;
		$.cookie('ratings',cookie);
		$(e.target).parents('div.rate').hide();
	});
	
	$('a.thumbUp').live('click', function(e){
		var value = $(e.target).parents('li').find('a.external').attr('rel');	
		var cookie = $.cookie('ratings');
		if(cookie.length > 0) {
			cookie += ',';
		}
		cookie += value;
		$.cookie('ratings',cookie);
		$(e.target).parents('div.rate').hide();
	});
});
