/* - - - - - - - - - - - - - - - - - - - - -

Título		: 	ON music recordings
Autor		: 	Alvaro Talavera
URL 		: 	

Descripción : 	javascript para ON music recordings

Creado 		:	6 de diciembre, 2007
Modificado	:	6 de diciembre, 2007

- - - - - - - - - - - - - - - - - - - - - */



function init() {
	setInitPosition();	
	showProductsQuantity();
}

function initInner() {
	colorizeRows();	
	showProductsQuantity();
}





/*** ANIMATION INTRO ***/

var hoverEffect = '';
var ready = false;

function setInitPosition() {	
	var interval = 500;	
	
	$$(".item").each(function(item){
		item.setStyle({
			opacity: '0.0'
		});
		
		item.down('img', 0).setStyle({
			top: '-20px'
		});
		
		
		item.observe('mouseover', hoverItem);
		item.observe('mouseout', unHoverItem);
		
		setTimeout(function(){showElement(item);}, interval);
		interval = interval + 500;
	});
}

function showElement(el) {	
	new Effect.Parallel([		
		new Effect.Scale(el.down('img', 0), 100, {
			scaleFromCenter: true,
			scaleFrom: 70
		}),
		new Effect.Morph(el.down('img', 0), {
		  	style:'top: 0px;',
			duration:0.5
		}),
		new Effect.Morph(el, {
		  	style:'opacity: 1.0',
			duration:0.5
		})	
	], {
		afterFinish: function(){
			ready = true;
		}
	});	
}




/*** ANIMATION HOVER ***/

function hoverItem() {
	if (ready == true) {
		this.className = 'item-active';	
		hoverEffect = new Effect.Morph(this.down('img', 0), {
			style:'top: 7px;',
			duration:0.3
		});
		/*var queue = Effect.Queues.get('items');
		queue.each(function(e) { e.cancel() });		
		this.down('img', 0).morph('top: 7px;',{duration:0.3, queue: {scope: 'items'}})
		$$(".item").each(function(item){
			item.down('img', 0).morph('top: -7px;',{duration:0.3, queue: {scope: 'items'}})
		})*/		
	}
}

function unHoverItem() {
	if (ready == true) {
		this.className = 'item';
		hoverEffect.cancel();
		this.down('img', 0).morph('top: 0px;',{duration:0.3});
		/*var queue = Effect.Queues.get('items');
		queue.each(function(e) { e.cancel() });		
		$$(".item").each(function(item) {
			item.down('img', 0).morph('top: 0px;',{duration:0.3, queue: {scope: 'items'}})
		})*/	
	}
}






/*** INTERNAS ***/


function starDiscAnimation() {
	setTimeout(function(){discAnimation();}, 1500);
}

function discAnimation() {
	var image = $('inner').down('img', 0);
	if (image.hasClassName('disc')) {
		new Insertion.After(image, "<div id='disc'><img src='/gfx/albums/disc.jpg' width='145' height='331' /></div>");
		$('disc').morph('left: 0px;',{duration:0.5});
	}
}



function colorizeRows() {
	var rows = $$("ul li");
	var key = 1;
	rows.each(function(row){
		if (key == 1) {
			row.addClassName('color1');
			key++;
		} else if (key == 2) {
			row.addClassName('color2');
			key--;
		}
	});
}







/*** cart ***/


function initializeCart() {
	var items = $$("#shopping_cart .buying_item");
	colorizeCart(items);
	setlisteners(items);
	showProductsQuantity();
}

function showProductsQuantity() {
	var cantidad = 0;
	if (!getCookie('SHOPCART_TOTAL_CANTIDAD').blank()) {
		cantidad = getCookie('SHOPCART_TOTAL_CANTIDAD');
	}
	$('cart').down('span', 0).update(cantidad);	
}


function setlisteners(items) {	
	items.each(function(item){
		item.observe('mouseover', activeCartItem);
//		item.observe('mouseout', desactiveCartItem);
	});
}

function activeCartItem() {
	this.down('a', 0).show();
}

function desactiveCartItem() {
	this.down('a', 0).hide();
}

function colorizeCart(rows) {
	var key = 1;
	rows.each(function(row){
		if (key == 1) {
			row.className = 'buying_item';
			key++;
		} else if (key == 2) {
			row.className = 'buying_item-colorized';
			key--;
		} 
	});
}


function add2cart(artID, precio) {
	var prodPrecio = parseFloat(precio);
	var prodCant = getCookie("SHOPCART_ITEM["+artID+"][cantidad]");
	
	var cantidadProductos = getCookie("SHOPCART_TOTAL_CANTIDAD");
	
	
	var newCant = parseInt(prodCant) + 1;
	
	
	if (prodCant.blank()) {
		setCookie("SHOPCART_ITEM["+artID+"][cantidad]", 1);
	} else {
		setCookie("SHOPCART_ITEM["+artID+"][cantidad]", newCant);
	}
	
	setCookie("SHOPCART_ITEM["+artID+"][precio]", precio);
	
	if (cantidadProductos.blank()) {
		setCookie('SHOPCART_TOTAL_CANTIDAD', 1);
	} else {
		var newTotalCant = parseInt(cantidadProductos) + 1;
		setCookie("SHOPCART_TOTAL_CANTIDAD", newTotalCant);
	}

	showProductsQuantity();	
	window.location = "/cart";
}


function setQuantity(id, obj) {
	
	var value = obj.value;
	
	if (value.empty() || value == 0) {
		value = 1;
		obj.value = 1;
	}
		
	var prodCant = getCookie("SHOPCART_ITEM["+id+"][cantidad]");
	var cantidadProductos = getCookie("SHOPCART_TOTAL_CANTIDAD");	
	var newProdCant = (parseInt(cantidadProductos) - parseInt(prodCant)) + parseInt(value);

	setCookie("SHOPCART_TOTAL_CANTIDAD", newProdCant);
	setCookie("SHOPCART_ITEM["+id+"][cantidad]", value);
	showProductsQuantity();
	
	var sub_total = parseFloat(getCookie("SHOPCART_ITEM["+id+"][precio]")) * parseInt(obj.value);
	obj.next('div', 0).down('span', 0).update(Math.round(sub_total*100)/100);
	
	
	setSubTotals(id, obj);
}


function setSubTotals(id, obj) {	
	var sub_total = parseFloat(getCookie("SHOPCART_ITEM["+id+"][precio]")) * parseInt(obj.value);
	obj.next('div', 0).down('span', 0).update(Math.round(sub_total*100)/100);
	setTotal();	
}


function setTotal() {
	var total = 0;
	$$('div.sub-total').each(function(sub_total){
		total = parseFloat(total) + parseFloat(sub_total.down('span', 0).innerHTML);
	});
	$('total').down('span', 0).update(Math.round(total*100)/100);
}



function removeFromCart(el, id) {
	var element = el.parentNode;
	var prodCant = getCookie("SHOPCART_ITEM["+id+"][cantidad]");
	var cantidadProductos = getCookie("SHOPCART_TOTAL_CANTIDAD");	
	var newProdCant = (parseInt(cantidadProductos) - parseInt(prodCant));
	setCookie("SHOPCART_TOTAL_CANTIDAD", newProdCant);
	
	clearCookie("SHOPCART_ITEM["+id+"][cantidad]");
	clearCookie("SHOPCART_ITEM["+id+"][precio]");
	
	new Effect.Fade(element, {
		afterFinish: function() {
			element.remove();			
			colorizeCart($("shopping_cart").childElements());
			showProductsQuantity();	
			setTotal();		
		}
	});
}



function acceptNum(evt) {
	var key;
	if(window.event) {
		key = evt.keyCode;
	} else if(evt.which) {
		key = evt.which;
	}

	return (key <= 13 || (key >= 48 && key <= 57) || key == 46);
}




/*** reproductor ***/



function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}


function FlashControl(audio, obj) {
	getFlashMovie("berta_player").playAudio(audio);
	$('player').down('span', 0).update("You are listening to: "+obj.innerHTML+"<br />");
}











/**************/

/*
function setCookie(name, value)
         {
         //If name is the empty string, it places a ; at the beginning
         //of document.cookie, causing clearCookies() to malfunction.
         if(name != '')
			document.cookie = name + "=" + value + "; path=/";
         }

*/
function setCookie(name, value) { // use: setCookie("name", value);
    var today = new Date();
	if (value != null && value != "")
//	document.cookie=name + "=" + escape(value);
	document.cookie = name + "=" + escape(value) + "; path=/";
	bikky= document.cookie; // update bikky
}


function getCookie(name)
         {
         //Without this, it will return the first value 
         //in document.cookie when name is the empty string.
         if(name == '')
            return('');
         
         name_index = document.cookie.indexOf(name + '=');
         
         if(name_index == -1)
            return('');
         
         cookie_value =  document.cookie.substr(name_index + name.length + 1, 
                                                document.cookie.length);
         
         //All cookie name-value pairs end with a semi-colon, except the last one.
         end_of_cookie = cookie_value.indexOf(';');
         if(end_of_cookie != -1)
            cookie_value = cookie_value.substr(0, end_of_cookie);

         //Restores all the blank spaces.
         space = cookie_value.indexOf('+');
         while(space != -1)
              { 
              cookie_value = cookie_value.substr(0, space) + ' ' + 
              cookie_value.substr(space + 1, cookie_value.length);
							 
              space = cookie_value.indexOf('+');
              }

         return(cookie_value);
         }

function clearCookie(name)
         {                  
         expires = new Date();
         expires.setYear(expires.getYear() - 1);

         document.cookie = name + '=null' + '; expires=' + expires + "; path=/";
         }
         
function clearCookies()
         {
         Cookies = document.cookie;
         Cookie = Cookies;
         expires = new Date();
         expires.setYear(expires.getYear() - 1);

         while(Cookie.length > 0)
              {
              //All cookie name-value pairs end with a semi-colon, except the last one.
              Cookie = Cookies.substr(0, Cookies.indexOf(';'));
              Cookies = Cookies.substr(Cookies.indexOf(';') + 1, Cookies.length);

              if(Cookie != '')
                 document.cookie = Cookie + '; expires=' + expires;
              else
                 document.cookie = Cookies + '; expires=' + expires;			  			  	  
              }		 		 
         }





/*****************************/

	function checkShippingSubmitPayPal() {
			document.paypal.submit();
	}
	function checkShippingSubmitUpdate() {
			document.update.submit();
	}
	function checkShippingSubmitformCheckout() {
			document.formCheckout.submit();
	}








