/** SHOPPING CART **/
function add_cart(itm){
	var tmpchck;
	var add = false;
	//Gather Item Info
	var tmpName = $('#'+itm+'_name').val();
	var tmpPrice = $('#'+itm+'_price').val();
	//Gather Shopping Cart Info
	var scart;
	var tmpCart = $('#shop-cart').val();
	
	if(tmpCart != ''){
		scart = tmpCart.split('|');
		var exist = false;
		for(var i = 0; i < scart.length; i++){
			var tmp = scart[i].split('_');
			if(itm == tmp[0]){
				exist = true;
			}
		}
		if(!exist){
			scart.push(itm+'_'+tmpName+'_'+tmpPrice+'_1');
			add = true;
		}
	}
	else{
		scart = new Array();
		scart.push(itm+'_'+tmpName+'_'+tmpPrice+'_1');
		add = true;
	}
	
	if(add == true)
	{
		$('#sCart ul.items li').remove();
		//Display Item
		scart.sort();
		var total = 0;
		for(var i = 0; i < scart.length; i++){
			var tmpitm = scart[i].split('_');
			$('<li><span class="name">'+tmpitm[1]+'</span><span class="qty">x <input type="text" size="1" value="'+tmpitm[3]+'" onchange="qty_change('+i+', this);" /> </span><span class="itmrmv"><a href="javascript:;" onclick="itm_remove('+i+', this);">[x]</a></span></li>').appendTo('#sCart ul.items');
			total += (Number(tmpitm[2]) * Number(tmpitm[3]));
		}
		$('#sCart div.total .qty').text('$'+total);
		
		//Insert into Shopping Cart Field
		var tmpval = scart.join('|');
		$('#shop-cart').val(tmpval);
		
		//Resize
		var newheight = $('#sCart div.holder').outerHeight();
		$('#sCart div.transbg').css({height: newheight+'px'});
		$('#sCart div.holder').css({marginTop: '-'+newheight+'px'});
	}
}
function form_submit(frm){
	document.getElementById(frm).submit();	
}
function qty_change(itmindex, target){
	//Gather Shopping Cart Info
	var tmpCart = $('#shop-cart').val();
	var scart = tmpCart.split('|');
	var tmpItm = scart[itmindex].split('_');
	var newqty = target.value;	
	if(/[a-zA-Z]/.test(newqty) || newqty == 0){
		target.value = 1;
	}
	else{
		tmpItm[3] = newqty;	
	}
	var tmpItmval = tmpItm.join('_');
	scart[itmindex] = tmpItmval;
	
	//Recalculate Total
	var total = 0;
	for(var i = 0; i < scart.length; i++){
		var tmpitm = scart[i].split('_');
		total += (Number(tmpitm[2]) * Number(tmpitm[3]));
	}
	$('#sCart div.total .qty').text('$'+total);
	
	//Insert into Shopping Cart Field
	var tmpval = scart.join('|');
	$('#shop-cart').val(tmpval);	
}
function itm_remove(itmindex, target){
	//Gather Shopping Cart Info
	var tmpCart = $('#shop-cart').val();
	var scart = tmpCart.split('|');
	
	$(target).parents('li').remove();
	scart.splice(itmindex, 1);
	if(scart.length < 1){
		$('<li><i>No items available.</i></li>').appendTo('#sCart ul.items');
	}
	
	//Recalculate Total
	var total = 0;
	for(var i = 0; i < scart.length; i++){
		var tmpitm = scart[i].split('_');
		total += (Number(tmpitm[2]) * Number(tmpitm[3]));
	}
	$('#sCart div.total .qty').text('$'+total);
	
	//Insert into Shopping Cart Field
	var tmpval = scart.join('|');
	$('#shop-cart').val(tmpval);
	
	//Resize
	var newheight = $('#sCart div.holder').outerHeight();
	$('#sCart div.transbg').css({height: newheight+'px'});
	$('#sCart div.holder').css({marginTop: '-'+newheight+'px'});
}

/** PRELOAD PLUGIN **/
jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}
//==========================================
var menu = [];
$(document).ready(function(){
	var cartcontent = $('#sCart .holder').outerHeight();
	$('#sCart').css({minHeight: cartcontent+'px'});
	$('#sCart div.transbg').css({height: cartcontent+'px'}).fadeTo('fast', 0.7);
	$('#sCart div.holder').css({marginTop: '-'+cartcontent+'px'});
	
	//Preload Images
	$.preloadImages('images/aboutus_gfx.jpg', 'images/aboutus_gfx_ro.jpg', 'images/approvals_gfx.jpg', 'images/approvals_gfx_ro.jpg', 'images/contactus_gfx.jpg', 'images/contactus_gfx_ro.jpg', 'images/donations_gfx.jpg', 'images/donations_gfx_ro.jpg', 'images/home_gfx.jpg', 'images/home_gfx_ro.jpg', 'images/links_gfx.jpg', 'images/links_gfx_ro.jpg', 'images/project_list_gfx.jpg', 'images/project_list_gfx_ro.jpg', 'images/store_gfx.jpg', 'images/store_gfx_ro.jpg', 'images/videos_gfx.jpg', 'images/video_gfx_ro.jpg');
	
	//Rollover
	menu['home'] = new Array('images/home_gfx.jpg', 'images/home_gfx_ro.jpg');
	menu['about'] = new Array('images/aboutus_gfx.jpg', 'images/aboutus_gfx_ro.jpg');
	menu['approval'] = new Array('images/approvals_gfx.jpg', 'images/approvals_gfx_ro.jpg');
	menu['projects'] = new Array('images/project_list_gfx.jpg', 'images/project_list_gfx_ro.jpg');
	menu['links'] = new Array('images/links_gfx.jpg', 'images/links_gfx_ro.jpg');
	menu['contact'] = new Array('images/contactus_gfx.jpg', 'images/contactus_gfx_ro.jpg');
	menu['donation'] = new Array('images/donations_gfx.jpg', 'images/donations_gfx_ro.jpg');
	menu['store'] = new Array('images/store_gfx.jpg', 'images/store_gfx_ro.jpg');
	menu['video'] = new Array('images/videos_gfx.jpg', 'images/video_gfx_ro.jpg');
	
	$('#mMenu').children('li').each(function(){
		var name = $(this).attr('class');
		if(menu[name]){
			$('.'+name).hover(function(){
				$(this).find('img').attr('src', menu[name][1]);				   
			}, function(){
				$(this).find('img').attr('src', menu[name][0]);	
			});
		}
	});
});
