/*
	Buying URLs according to documentation plus fixing the bug

	02.06.2010,	mJ:	added a little delay before reloading after buying
					
	04.06.2010, mJ:	basket-container.visibility 
	
	13.08.2010, mJ:	new DIGITAL_CODE
	
BUG:
	Documentation says strip the first digit of the UPC. 
	This does not work for tracks. 
	For tracks the UPC has to be stripped of the first and the last digit!
	upc.substring( 1, 13 ) 
*/

// following texts will be overridden in standard-top.inc with LOCALISATION data

var PLEASE_SELECT_A_FORMAT = "Please select a format!";
var VENDA_DOMAIN = ""

var BUY_ALBUM_TEXT = {	//_COUNTRY_ is replaced by actual country
	physical: "Buy Album as Mail Order in _COUNTRY_?",
	digital: "Buy Album as MP3 in _COUNTRY_?",
	lossless: "Buy Album as FLAC in _COUNTRY_?"
};

var BUY_TRACK_TEXT = {
	digital: "Buy Track as MP3 in _COUNTRY_?",
	lossless: "Buy Track as FLAC in _COUNTRY_?"
};


var VENDA_FORMAT = {
	digital: "MP3",
	lossless: "FLAC"
};


DIGITAL_CODE = "4";
LOSSLESS_CODE = "2";


_txtarea_ = null;


function html_entity_decode( str ) 
{
	if( !_txtarea_ )
	_txtarea_ = document.createElement( 'textarea' );
	_txtarea_.innerHTML = str;
	var v = _txtarea_.value;
// 	t.parentNode.removeChild( t );
	return v;
}


function my_alert( str )
{
	str = html_entity_decode( str );
	alert( str );
}


function my_confirm( str )
{
	str = html_entity_decode( str );
	return confirm( str );
}


function vendacart_start() 
{
	clearTimeout( window.reloadTimer );
	$('#updating-cart').show();
};
	
	
function vendacart_done() 
{
	$('#updating-cart').hide();
	$('#basket-container').css( "visibility", "visible" );
};
	
function vendacart_done_and_reload() 
{
	vendacart_done();
	setTimeout( reloadVendacart, 5000 );	
	// prevent early reloading
};
	
	
function loadVendacart( src )
{
	$('#basket-container').css( "visibility", "hidden" );
	// no history involvement
	$("#basket-container").html( '<iframe scrolling="no" height="26" frameborder="0" src="' 
									+ src + '" name="minicart" id="minicart" onload="vendacart_done()"></iframe>' );
}

function loadVendacartForAdd( src )
{
	$('#basket-container').css( "visibility", "hidden" );
	// no history involvement
	$("#basket-container").html( '<iframe scrolling="no" height="26" frameborder="0" src="' 
									+ src + '" name="minicart" id="minicart" onload="vendacart_done_and_reload()"></iframe>' );
}

	
function buyTrack( upc, isrc, format )
{
	if( !format ) {
		my_alert( PLEASE_SELECT_A_FORMAT );
		return;
	}
	if( !upc ) {
		alert( "Missing upc!" );
		return;
	}
	if( !isrc ) {
		alert( "Missing isrc!" );
		return;
	}
	//alert( "UPC: " + upc + " ISRC: " + isrc + " format: " + format );
	
	if( !my_confirm( BUY_TRACK_TEXT[format] ) ) {
		return;
	}
	
	var product_sku = upc.substring( 1, 13 ) + isrc.toLowerCase();
	var attribute_sku = product_sku;
	
	if( format == "digital" ) {
		attribute_sku += DIGITAL_CODE;
	}
	
	if( format == "lossless" ) {
		attribute_sku += LOSSLESS_CODE;
	}
	
	iframe_src = VENDA_DOMAIN + "/bin/venda?bsref=universalmusic&log=22&mode=add&ex=co_disp-shopc&buy=" + attribute_sku 
						+ "&invt=" + product_sku + "&qty=1&layout=vendaminicart&css=http://" + location.host + "/css/basket.css";
	vendacart_start();
	loadVendacartForAdd( iframe_src );
}


function buyProduct( upc, format )
{
	if( !format ) {
		my_alert( PLEASE_SELECT_A_FORMAT );
		return;
	}
	if( !upc ) {
		alert( "Missing upc!" );
		return;
	}
	
	if( !my_confirm( BUY_ALBUM_TEXT[format] ) ) {
		return;
	}
	
	//alert( "UPC: " + upc + " ISRC: " + isrc + " format: " + format );
	var product_sku = upc.substring( 1 );
	if( format != "physical" ) {
		product_sku += "d";
	}
	
	var attribute_sku = product_sku;
	
	if( format == "digital" ) {
		attribute_sku += DIGITAL_CODE;
	}
	
	if( format == "lossless" ) {
		attribute_sku += LOSSLESS_CODE;
	}
	
	var iframe_src;
	
	iframe_src = VENDA_DOMAIN + "/bin/venda?bsref=universalmusic&log=22&mode=add&ex=co_disp-shopc&buy=" + attribute_sku 
				+ "&invt=" + product_sku + "&qty=1&layout=vendaminicart&css=http://" + location.host + "/css/basket.css";
	vendacart_start();
	loadVendacartForAdd( iframe_src );
}


function selectTrackFormat( format )
{
	$.cookie('PREFERRED_TRACK_FORMAT', format, { expires: 365, path: '/' })
	
	$(".shopcart-add-icon-button").each( function() {
		var price = $(this).find( ".price" );
		price.html( price.attr( format + "_price" ) );
	} );
}


function selectFormat( format )
{
	$.cookie('PREFERRED_PRODUCT_FORMAT', format, { expires: 365, path: '/' })
}


var dropdown_timer = null;

function show_dropdown( dropdown )
{
	cancel_hide_dropdown();
	if( $('#select-format-dropdown').length == 0 ) {
		var button = $(dropdown).prev();
		button.attr( "paused", "1" );
		var ofs = button.offset();
 		ofs.left += 1;
 		ofs.top += button.outerHeight() - 2;
		
		$('body').append( '<div id="select-format-dropdown" style="position: absolute; top: ' + ofs.top + 'px; left: ' + ofs.left + 'px">' + $(dropdown).html() + '</div>' );
		
//		$('#select-format-dropdown').offset( ofs );
//		$('#select-format-dropdown').find( '.add-dropdown' ).show()
	}
}


function hide_dropdown( dropdown )
{
	var button = $(dropdown).prev();
	button.attr( "paused", "" );
	$('#select-format-dropdown').remove();
}


function deferred_hide_dropdown( dropdown )
{
	dropdown_timer  = setTimeout( function() {
		hide_dropdown( dropdown );
	}, 10 );
}


function cancel_hide_dropdown()
{
	if( dropdown_timer ) {
		clearTimeout( dropdown_timer );
		dropdown_timer = null;
	}
}



function init_add_dropdown_button_container()
{
	$('.add-dropdown-button-container,.magic-buy-now').each( function() {
		var product_nr = $(this).attr('product_nr');
 		
 		var cookie = $.cookie( 'cc2' );
 		
 		if( !cookie ) {
 			cookie = "";
 		}
 		
		if( product_nr ) {
			$(this).load( '/cat/add-to-cart-dropdown-part?PRODUCT_NR=' + product_nr + '&CC2=' + cookie );
		} else {
			var upc = $(this).attr('upc');
			if( upc ) {
				$(this).load( '/cat/add-to-cart-dropdown-part?UPC=' + upc + '&CC2=' + cookie );
			}
		}
	} );
}


$(document).ready( function() {
	init_add_dropdown_button_container();
} );

