var cart = {
	id_root: "cart",
	id_content: "cart_content",
	id_msg: "cart_msg",
	
	change: function( url, remove, row ) {
		url = url + '&ajax';
		url += (remove) ? ( "&remove=" + remove ) : '';  
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: this.processResponse
		});
		
		
		
		if (row) {
		
		
			var nodes = row.cells;
			
			for ( var n = 0 ; n < nodes.length; n++ ) {
			
				if ( nodes.item(n).innerHTML != "Albums" ) {
					nodes.item(n).parentNode.removeChild(nodes.item(n));
					n--;
				}
				
			}
		
			// var album_table = document.getElementById( "album_table");
// 			
// 			if ( album_table ) {
// 				
// 				var first_row = album_table.rows.item(0);
// 				
// 				console.log(first_row.innerHTML);
// 				
// 				if ( first_row && first_row.cells.length == 1 ) {
// 					
// 					alert( first_row.innerHTML);
// 					
// 					album_table.parentNode.insertBefore(document.createTextNode("Your Shopping Cart Is Empty"), album_table );
// 					
// 					album_table.parentNode.removeChild( album_table );
// 					
// 				}
// 				
// 			}
			
		}
		
		return false;
	},

	changeMerch: function(form) {
		var product = form.product.value;
		var style = form.style.value;
		var size = form.size.value;
		var quantity = form.quantity.value;
		var id = product + "." + style + "." + size + "." + quantity
		var url = "/cart/?ADDMERCHCART=" + id
		return this.change(url, '', '');
	},

	processResponse: function(transport) {
		var response = transport.responseText.split("<!-- cart_splitter -->");

		var cart_content = response[0];
		document.getElementById(cart.id_root).innerHTML = cart_content;
		
		if (document.getElementById("kart_total") ) {
			var total_amount = document.getElementById("kart_total");
			
			if ( cart_content.match(/Total:.+?<strong>(.+?)<\/strong>/) ) {
				total_amount.innerHTML = RegExp.$1;
			}
			
			
		}
		
		var cart_message = response[1];
		cart.notify(cart_message);
	},	
	
	notify: function(msg) {
		new Notify(msg, {
			height: 38,
			duration: 1,
			closedelay: 1,
			easeup: jEasy.Fx.Easing.Quint.easeOut,
			easedown: jEasy.Fx.Easing.Quint.easeIn
		});	
	}
}


//
// Functions below this point are candidates for removal because I don't know if they are been used
//

var clipplayer;

function loadbg( url, dummy1, dummy2 ) {
	url = url + '&ajax';
	
	// get params
	var myAjax = new Ajax.Updater(
		'cart',
		url,
		{method: 'get' }
	);	
	
	new Effect.Highlight( 'cart' );
	new Effect.Appear('cartConfirm');
	
	setTimeout( "cartConfirmClose()", 4000 );
	
	return false;
}

function cartConfirmClose() {
	new Effect.Fade( 'cartConfirm', { position: 'end' } );
}

function openplayerwindow(url)
{	
	clipplayer = window.open(url,"clipplayer","toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=440,height=420,screenX=50,screenY=50,top=50,left=50");
		
	if (clipplayer.opener == null) clipplayer.opener = self;
		
	if(!clipplayer) return true;
	
	//self.focus();

	return false;
}

// parameter id of form
// parameter name of checkboxes (leave blank for ALL checkboxes on a form, regardless of name)
function toggleAll( theForm, theBoxes ) {
    formblock = document.getElementById( theForm );
    formInputs = formblock.getElementsByTagName( 'input' );
    totalImputs = 0;
    totalChecked = 0;
    // loop through all to see how many are checked
    for ( i = 0; i < formInputs.length; i++ ) {
        var regex = new RegExp( theBoxes, 'i' );
        // is this one of the specified checkboxes?
        if ( regex.test( formInputs[i].getAttribute('name') ) || (theBoxes == null) ) {
            if ( formInputs[i].checked )
            	totalChecked++;
            totalImputs++;
        }
    }
    if ( totalChecked != totalImputs )
    	checked = true;
    else
    	checked = false;
    // loop throug all to set
    for ( i = 0; i < formInputs.length; i++ ) {
        var regex = new RegExp( theBoxes, 'i' );
        // is this one of the specified checkboxes?
        if ( regex.test( formInputs[i].getAttribute('name') ) || (theBoxes == null) ) {
            formInputs[i].checked = checked;
        }
    }
}

function hidebyid(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showbyid(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function isvisible(id) {
	//safe function to get an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		if ( document.getElementById(id).style.display == 'none' )
			return false;
		else
			return true;
	}
	else {
		if (document.layers) { // Netscape 4
			if ( document.id.display == 'none' )
				return false;
			else
				return true;
		}
		else { // IE 4
			if ( document.all.id.style.display == 'none' )
				return false;
			else
				return true;
		}
	}
}

function togglevisible(id, morelabelid, lesslabelid) {
	if ( isvisible(id) ) {
		hidebyid(id);
		showbyid(morelabelid);
		hidebyid(lesslabelid);	
	} else {
		showbyid(id);
		hidebyid(morelabelid);
		showbyid(lesslabelid);
	}
}



function createItem() {

	var item_dropdown = document.getElementById("item_dropdown_0").cloneNode( true );
	var before = document.getElementById("insert_before");
	var select = item_dropdown.getElementsByTagName( "select" ).item( 0 );
	var img = document.createElement( "img" );
	var a = document.createElement( "a" );
	var label = document.createElement( "label" );
	var last_dd = item_dropdown.getElementsByTagName( "span" ).item( 0 );
	
	select.selectedIndex = 0;
	select.id = "";

	item_dropdown.id = "item_dropdown_" + counter;

	img.src = "/images/icons/cross.gif"; 
	img.width = img.height = "16";
	img.border = "0";
	img.alt = img.title = "remove item";
	img.className = "align";
	
	a.href = "#";
	a.onclick = removeItem;
	a.appendChild( img );
	a.id = "removeitem_" + counter;

	item_dropdown.getElementsByTagName( "label" ).item( 0 ).innerHTML = "&nbsp;";
	item_dropdown.getElementsByTagName( "span" ).item( 0 ).appendChild( a );
	before.parentNode.insertBefore( item_dropdown , before );
		
	counter++;
}

function removeItem() {
	
	var num = this.id.split( "_" )[1];
	var item_dropdown = document.getElementById( "item_dropdown_" + num );
	
	item_dropdown.parentNode.removeChild( item_dropdown );
}
