// Debug
function reset()
{
	$.get("/actions/mc/reset.php");
}
function debug_msg(){
	var minibook = 0;
	var casting = 0;
	$.get(
		"/actions/mc/get.php",
		function(data){
			var spl = $.trim(data).split("|");
			minibook = spl[0];
			casting = spl[1];
			alert(minibook+'|'+casting);
		}
	);
}
// end of debug








function add_item(model_id,model_type,item_type,use_parent){
	if(use_parent == undefined)
		use_parent = false;

	$.get(
		"/actions/mc/add.php",
		{ model_id: model_id, model_type: model_type, item_type: item_type.attr("id"), ajax: "true" },
		function(data)
		{
			if($.trim(data) == "OK")
				update_item(item_type,'add',model_id,model_type,use_parent);
		}
	);
}
function remove_item(model_id,model_type,item_type,use_parent){
	if(use_parent == undefined)
		use_parent = false;

	$.get(
		"/actions/mc/remove.php",
		{ model_id: model_id, model_type: model_type, item_type: item_type.attr("id"), ajax: "true" },
		function(data)
		{
			if($.trim(data) == "OK")
				update_item(item_type,'remove',model_id,model_type,use_parent);
		}
	);
}



function update_item(item_type,action_type,model_id,model_type,use_parent){
	if(use_parent == undefined)
		use_parent = false;

	/** Create the effect functions */
	if(!use_parent){
		jQuery.fn.extend({
			mc_effect_in: function(speed, callback){
				if($.browser.msie || $.browser.konqueror)
					return this.slideDown(speed,callback);
				else
					return this.fadeIn(speed,callback);
			},
			mc_effect_out: function(speed, callback)
			{
				if($.browser.msie || $.browser.konqueror)
					return this.slideUp(speed,callback);
				else
					return this.fadeOut(speed,callback);
			}
		});
	}

	$.get(
		"/actions/mc/get.php",
		function(data){
			var spl = $.trim(data).split("|");

			var minibook = parseInt(spl[0]);
			var casting = parseInt(spl[1]);

			if(item_type.attr("id") == 'minibook')
			{
				var new_html = (minibook != 0) ? '<a href="/minibook/preview.php?KeepThis=true&amp;TB_iframe=true&amp;height=400&amp;width=400" class="thickbox" title="Minibook">'+ minibook+' '+(minibook > 1 ? 'models':'model')+' in minibook<br/><span>click to open</span></a>' : '&nbsp;';
				var new_count = minibook;
			}
			else if(item_type.attr("id") == 'casting')
			{
				var new_html = (casting != 0) ? '<a href="/casting/preview.php?KeepThis=true&amp;TB_iframe=true&amp;height=400&amp;width=400" class="thickbox" title="Casting list">'+ casting+' '+(casting > 1 ? 'models':'model')+' in casting list<br/><span>click to open</span></a>' : '&nbsp;';
				var new_count = casting;
			}


			// No more models in item_type. Fade out the item
			if(new_count == 0)
			{
				if(!use_parent)
				{
					item_type.mc_effect_out('slow',function(){
						item_type.html(new_html);
						update_menu_item(item_type,action_type,model_id,model_type,use_parent);
						update_thickbox_item(item_type,use_parent);
					});
				}
				else
				{
					item_type.hide();
					item_type.html(new_html);
					update_menu_item(item_type,action_type,model_id,model_type,use_parent);
					update_thickbox_item(item_type,use_parent);
				}
			}

			// This is the first added model. Fade in the item
			else if(new_count == 1 && action_type == 'add')
			{
				if(!use_parent)
				{
					item_type.html(new_html).mc_effect_in('slow',function(){
						update_menu_item(item_type,action_type,model_id,model_type,use_parent);
						update_thickbox_item(item_type,use_parent);
					});
				}
				else
				{
					update_menu_item(item_type,action_type,model_id,model_type,use_parent);
					update_thickbox_item(item_type,use_parent);

				}
			}

			// Fade out - fade in
			else
			{
				if(!use_parent)
				{
					item_type.mc_effect_out('slow',function(){
						item_type.html(new_html).mc_effect_in('slow',function(){
						update_menu_item(item_type,action_type,model_id,model_type,use_parent);
						update_thickbox_item(item_type,use_parent);
						});

					});
				}
				else
				{
					item_type.html(new_html);
					update_menu_item(item_type,action_type,model_id,model_type,use_parent);
					update_thickbox_item(item_type,use_parent);
				}
			}



		}
	);
}


/** Attach a thickbox behaivour. */
function update_thickbox_item(item_type,use_parent){
	if(use_parent == undefined)
		use_parent = false;

	/* OK! IE throws an error if you use 'item' variable???*/
	if(use_parent)
		itemm = parent.$("#"+item_type.attr("id")+" a.thickbox");
	else
		itemm = $("#"+item_type.attr("id")+" a.thickbox");

	itemm.unbind("click");
	itemm.bind("click", function() {
		var t = this.title || this.name || null;
		var g = this.rel || false;
		TB_show(t,this.href,g);
		this.blur();
		return false;
	});
}


/** Updates the menu. Swtiches between "Add to item_type" and "Remove from item_type" */
function update_menu_item(item_type,action_type,model_id,model_type,use_parent){
	if(use_parent == undefined)
		use_parent = false;

	if(use_parent)
	{
		menu_minibook = parent.$('.menu_minibook');
		menu_casting = parent.$('.menu_casting');
	}
	else
	{
		menu_casting = $('.menu_casting');
		menu_minibook = $('.menu_minibook');
	}

	if(action_type == 'add')
	{
		if(item_type.attr("id") == 'minibook')
			menu_minibook.html("<a href=\"/actions/mc/remove.php?model_id="+model_id+"&amp;model_type="+model_type+"&amp;item_type=minibook\" onclick=\"remove_item("+model_id+",'"+model_type+"',$('#minibook'));return false;\">Remove from minibook</a>");
		else
			menu_casting.html("<a href=\"/actions/mc/remove.php?model_id="+model_id+"&amp;model_type="+model_type+"&amp;item_type=casting\" onclick=\"remove_item("+model_id+",'"+model_type+"',$('#casting'));return false;\">Remove from casting list</a>");
	}
	else if(action_type == 'remove')
	{
		if(item_type.attr("id") == 'minibook')
			menu_minibook.html("<a href=\"/actions/mc/add.php?model_id="+model_id+"&amp;model_type="+model_type+"&amp;item_type=minibook\" onclick=\"add_item("+model_id+",'"+model_type+"',$('#minibook'));return false;\">Add to minibook</a>");
		else
			menu_casting.html("<a href=\"/actions/mc/add.php?model_id="+model_id+"&amp;model_type="+model_type+"&amp;item_type=casting\" onclick=\"add_item("+model_id+",'"+model_type+"',$('#casting'));return false;\">Add to casting list</a>");
	}


}



/** Remove model from preview.php */
//parent.item_type