$(function() {
	if( BrowserDetect.browser != 'Safari' ) { 
		$('ul#cart').Sortable({
				accept : 'sortableitem',
				handle: 'span',
				activeclass : 'sortableactive',
				hoverclass : 'sortablehover',
				helperclass : 'sorthelper',
				opacity: 0.5,
				fit : false,
				onChange : function(ser)
				{
					temp = $("input[@class=sortInput]").serialize();
					//alert(temp);
					ajax('ajax.php?op=order&'+temp,'cart');
					
				}
		});
	}
	
    $('.remove-item').bind('click', function() {
		var rid = $(this).attr("id");
		var trid = "#l_"+ $(this).attr("id");
		$(trid).remove();
		remove(rid);
		$('ul#cart').append('<li class="empty-li"></li>');
	});

	$('.show-link').click( function() { 
		$('div#help-wrapper').show();								
		
		return false;
	});

	$('#tell-vacation').click( function() { 
		$('div#tell-vacation-wrapper').show();								
		
		return false;
	});

	$('#tell-difference').click( function() { 
		$('div#tell-difference-wrapper').show();								
		
		return false;
	});




	$('.close-help').click( function() { 
		$('div.help-popup').hide();								
		
		return false;
	});


	$('.request-list-item').click( function() { 
		var addid = true;
		var tid = $(this).attr("id");
		var lsRegExp = /\+/g;
		var tname = unescape(String($(this).attr("rel")).replace(lsRegExp, " "));
		lsRegExp = /\\/g;
		tname = unescape(String(tname).replace(lsRegExp, ""));
		//$('ul#cart li#empty-list').remove();
		$('ul#cart li.empty-li').remove();

		$('body').find("ul#cart li").each(function() { 
			if ( $(this).attr("id") == ('l_'+tid )) {
				addid = false;
				fillList();
			}
		});
		
		if (addid) {
			var li = '<li id="l_' + tid + '" class="sortableitem"  style="-moz-user-select: none;"><input type="hidden" class="sortInput" name="id[]" value="'+tid+'"/><input type="hidden" class="sortInput" name="name[]" value="'+tname+'"/><span>'+ tname + '<span></li>';
			var img ='<img src="images/icons/delete.gif" alt="Remove" id="'+ tid + '" />';
	
			// Binds click event to img so new item can be removed
			img = $(img).addClass("remove-item").bind("click", function(){
				var rid = $(this).attr("id");
				var trid = "#l_"+ $(this).attr("id");
				$(trid).remove();
				remove(rid);
				fillList();
			});
			
			li = $(li).append(img);
			$('ul#cart').append(li);
			$('ul#cart').SortableAddItem(document.getElementById('l_'+tid));
				

			fillList();
			
			addto(tid,tname);
		}
	});
});

function fillList() {
	for( var x = $("ul#cart li.sortableitem").length; x < 10; x++ ){
		$('ul#cart').append('<li class="sortableitem empty-li"></li>');
	}	
}


function ajax(url,returnid){
	var doc = null;
	if (typeof window.ActiveXObject != 'undefined' ){
		doc = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		doc = new XMLHttpRequest();
	}
	if (doc){
		doc.open("GET", url, false);
		doc.send(null);
		
		if (doc.responseText == '<li id="empty-list" style="list-style:none;background-position: 15px 0px;padding-left:20px;">Your list is empty.</li>'){
		document.getElementById(returnid).innerHTML = '<li id="empty-list" style="list-style:none;background-position: 15px 0px;padding-left:20px;">Your list is empty.</li>';
		}
	}
}
	
function addto(uid, str){
	if(uid!='') {
		ajax('ajax.php?op=add&id='+uid+'&name='+str,'cart');
		message();
	}
}

function remove(uid){
	if(uid!=''){
		ajax('ajax.php?op=off&id='+uid,'cart');
		message();
	}
}

function message(){
	ajax('ajax.php?op=msg','messages');
}

function show(what,theother) {
	document.getElementById(what).style.display = 'block';
	document.getElementById(theother).style.display = 'none';
}