$(function(){

	$('#product_quick_look').dialog({
		autoOpen: false,
		modal: true,
		width: 700,
		height: 330,
		show: "fade",
		hide: "fade"
	});
	
	$('#quick_look_close').live('click', function(){
		$('#product_quick_look').dialog('close');
	});

	function ajaxResponseText( data, key ){
		val = $( "response value[key=" + key + "]", data ).text();
		val = val.replace( /%26/g, "&" );
		val = val.replace( /%3C/g, "<" );
		val = val.replace( /%3E/g, ">" );
		return val;
	}

	function quickLook(product_id, href) {

		var loc = window.location;
		var regex = /^.*\/\/[^\/]*\/([^\/]*)\//gi;
		var match = regex.exec( loc );
		var virtual_loc = '';
		if( match )
		{
			virtual_loc = '/' + match[1] + '/quick_look/' + product_id
		}
		else
		{
			virtual_loc = '/home/quick_look/' + product_id
		}
		_gaq.push(['_trackPageview', virtual_loc]);

		$.ajax({
			type: "POST",
			dataType: "xml",
			url: "/ajax/products/product.php",
			data: "product_id=" + product_id,
			success: function( data, textStatus )
			{

				//var refund_status = $(data).find('value[key=status]').text();
				success = $(data).find("response").attr('success');

				if(success == 'true') {
					$('#product_quick_look').html(ajaxResponseText(data, 'html'));
					/*
					$('#size_selection').hide();
					$('#size_breakdown').show();
					*/
				}
				if(success == 'false'){
					window.location = href;
				}
				
			},
			error: function (data, textStatus){
				// Direct browser to url of requestor
				window.location = href;
			}
		});
	}
	
	$('.quick_look').live('click', function(){
		requestor = $(this);
		$('#product_quick_look').dialog('open');
		$('#product_quick_look').html('<div style="margin: 0 auto;"><br /><br /><br /><br /><img src="/media/image/common/ajax-loader.gif" alt="loading..."/><br />Loading</div>');
		$("#product_quick_look").dialog( "option", "title",  $(this).attr('data-name'));

		product_id = $(this).attr('data-product-id');

		quickLook(product_id, $(requestor).attr("href"));

		return false;
	});
	
	$('.order_quick_look').live('click', function(){
		requestor = $(this);
		$('#product_quick_look').dialog('open');
		$('#product_quick_look').html('<div style="margin: 0 auto;"><br /><br /><br /><br /><img src="/media/image/common/ajax-loader.gif" alt="loading..."/><br />Loading</div>');
		$("#product_quick_look").dialog( "option", "title",  $(this).attr('data-name'));

		product_id = $(this).attr('data-product-id');

		quickLook(product_id, $(requestor).attr("href"));

		return false;
	});

	function getBasketLight(){
		
		$.ajax({
			type: "POST",
			dataType: "xml",
			url: "/ajax/basket/getBasket.php",
			success: function(data, textStatus){

				success = $(data).find("response").attr('success');

				if(success == 'true') {
					$('#mini_basket').hide().html(ajaxResponseText(data, 'html')).show();
				}
			},
			error: function (jqXHR, textStatus, errorThrown){
				log("getBasketLight(): " + textStatus);
			}
		});

	}

});

