



var dgdShop = function()
{
	this.upc		= '';
	this.isrc		= '';
	this.mediatype	= '';

	var me = this;
	
	this.initialize = function()
	{
		this.setPrices();
		this.loadMinicart(this.getStatusUrl());
		this.addEventCountrySelect();
		this.addEventYesNoButtons();
		this.addEventCookieButton();
		this.startTimer();
	}
	
	
	this.setSelectedProduct = function(mediatype, upc, isrc)
	{
		this.mediatype = mediatype;
		this.upc = upc;
		this.isrc = isrc;
	}
	
	
	this.createProductSKU = function()
	{
		if (this.isrc) { // build for a track
			sku = this.upc.substring(1, 13);
		} else {	// build for a product
			sku = this.upc.substring(1);
		}
		// if track is a digital album, append postfix
		if (!this.isrc && prodSkuPostfix[this.mediatype]) {
			sku += prodSkuPostfix[this.mediatype];
		} else if (this.isrc) {
			sku += this.isrc.toLowerCase();
		}
			
		return sku;
	}
	
	
	this.createAttributeSKU = function()
	{
		sku = this.createProductSKU();
		
		if (attrSkuPostfix[this.mediatype]) {
			sku += attrSkuPostfix[this.mediatype];
		}
		
		return sku;
	}
	
	
	this.confirmedBuy = function(id, mediatype, upc, isrc) 
	{
		this.stopTimer();
		
		this.setSelectedProduct(mediatype, upc, isrc);
		
		$.blockUI({
			message: $(id)
		});
	}
	
	
	this.buy = function()
	{
		prodSKU = this.createProductSKU();
		attrSKU = this.createAttributeSKU();
		
		url = storeDomain + '/bin/venda?bsref=' + storeName 
						    	 + '&log=22&mode=add&ex=co_disp-shopc&buy=' + attrSKU
						    	 + '&invt=' + prodSKU + '&qty=1&layout=vendaminicart&css='
						    	 + cssLocation;
		
		this.block('#dgdShop_i18n_UpdatingCart');
		this.loadMinicartWithUnblock(url);
		return true;
	}

	
	this.block = function(id)
	{
	    $.blockUI({ 
	        message: $(id)
	    });
	    return true;
	}


	this.unblock = function()
	{
		$.unblockUI();
	}


	this.unblockAndReload = function()
	{
		window.shop.unblock();
		window.shop.startTimer();
	}
	
	
	this.startTimer = function()
	{
		$(document).everyTime(timerReload, timerLabel, function(i) {
			window.shop.loadMinicart(window.shop.getStatusUrl());
		});
	}
	
	
	this.stopTimer = function()
	{
		$(document).stopTime(timerLabel);
	}


	this.getStatusUrl = function()
	{
		url = storeDomain + '/page/home&layout=vendaminicart&setlocn=' + setLocn
		  + '&css=' + cssLocation;

		return url;
	}

	
	this.loadMinicart = function(src)
	{
		$('#dgdShop_Minicart_Container iframe').attr({'src':src}).unbind('load');
	}

	
	this.loadMinicartWithUnblock = function(src)
	{
		$('#dgdShop_Minicart_Container iframe').attr({'src':src}).bind('load', this.unblockAndReload);
	}
	
	
	this.createMap = function() 
	{
		var tracks	 = {};
		var products = {};
		
		jQuery('.dgdShop_Price').each(function() {
			var md = $(this).metadata(); 
			
			if (md.isrc) {	// a track
				if (! tracks[md.upc]) tracks[md.upc] = {};
				tracks[md.upc][md.isrc] = md.duration;
			} else {	// a product
				products[md.upc] = null;
			}
		});
		
		return {'tracks': tracks, 'products': products};
	}
	

	this.setPrices = function()
	{
		
		jQuery.ajax({
			type:		'POST',
			dataType:	'JSON',
			data:		this.createMap(),
			success:	function(data, textStatus) {
							var data = jQuery.parseJSON(data);
							var tracks = data['tracks'];
							var products = data['products'];
							
							jQuery('.dgdShop_Price').each(function() {
								e = $(this);
								var md = e.metadata();
								
								if (md) { 
									if (md.isrc) {		// a track
										if (tracks && tracks[md.upc] && tracks[md.upc][md.isrc] && tracks[md.upc][md.isrc][md.mtype]) {
											e.find('a:first').html(tracks[md.upc][md.isrc][md.mtype]);
										}
									} else {		// a product
										if (products && products[md.upc] && products[md.upc][md.mtype]) {
											e.find('a:first').html(products[md.upc][md.mtype]);
										}
									}
								}
							});
						},
			url:		'/dgdshop/api/fetch/prices'
		});
		
	}


	this.addEventCountrySelect = function() 
	{
		jQuery('#dgdShop_Country_Select').change(function() {
			window.shop.stopTimer();
			$.blockUI({
				message: $('#dgdShop_i18n_ChangeCountry')
			});
			$('#dgdShop_i18n_ChangeCountry .dgdShop_yes').click(function() {
				$('#dgdShop_Country_Form').submit();
			});
			$('#dgdShop_i18n_ChangeCountry .dgdShop_no').click(function() {
				$('#dgdShop_Country_Select' + ' option[value=' + country + ']').attr('selected','selected');
				$.unblockUI();
				return false;
			});
		});
	}
	
	
	this.addEventYesNoButtons = function()
	{
		$('.dgdShop_yes').click(function() { 
	        window.shop.buy();
	        return true; 
	    }); 
		
		$('.dgdShop_no').click(function() { 
	        $.unblockUI();
	        window.shop.startTimer();
	        return false; 
	    });
	}
	
	this.addEventCookieButton = function()
	{
		$('#dgdShop_Link_Cookie').click(function() {
			$.blockUI({
				message: $('#dgdShop_CookieInformation')
			});
		});
		
		$('.dgdShop_close').click(function() {
			$.unblockUI();
			return false;
		});
	}
	
}


// ON LOAD
jQuery(document).ready(function() {
	window.shop = new dgdShop();
	window.shop.initialize();
});

