var cookie_expiration = 7;
function addToClipboard(from, room, food, event_begin, event_duration, hotel_id)
{
	$('div#schowek div.schowek_content').remove();
	$('div#schowek a.schowek').removeClass('active');
	
	var itaka_clipboard = ($.cookie('itaka_clipboard')) ? JSON.parse($.cookie('itaka_clipboard')) : false;

	if(itaka_clipboard)
	{
		var counter = itaka_clipboard.length;
		add_offer = (counter >= 5) ? false : true;
		if(add_offer == true)
			for(var i in itaka_clipboard)
			{
				existing_offer = itaka_clipboard[i];
				if(existing_offer.from == from &&
				   existing_offer.room == room &&
				   existing_offer.food == food &&
				   existing_offer.event_begin == event_begin &&
				   existing_offer.event_duration == event_duration &&
				   existing_offer.hotel_id == hotel_id
				   )
				{
					add_offer = false;
					break;
				}
			}
	}
	else
	{
		itaka_clipboard = new Array();
		counter = 0;
		add_offer = true;
	}
	
	if(add_offer)
	{
		itaka_offer = {
			'from' : from,
			'room' : room,
			'food' : food,
			'event_begin' : event_begin,
			'event_duration' : event_duration,
			'hotel_id' : hotel_id
		}
		
		itaka_clipboard.push(itaka_offer);
		$.cookie('itaka_clipboard', JSON.stringify(itaka_clipboard), { expires: cookie_expiration, path: '/'});
		
		//$('div#schowek a.schowek strong').fadeOut('fast', function() {
			$('div#schowek a.schowek strong').text(itaka_clipboard.length);
		//	$('div#schowek a.schowek strong').fadeIn('fast');
		//});
	}
}
	
var clipboardOps = new clipboardOperations(ajax_url_var);
$(document).ready(function() {
	
	itaka_clipboard = ($.cookie('itaka_clipboard')) ? JSON.parse($.cookie('itaka_clipboard')) : false;
	if(itaka_clipboard)
		$('div#schowek a.schowek strong').text(itaka_clipboard.length);
	
	$.fillClipboard = function(fillData)
	{
		if(fillData)
		{
			$('div#schowek div.schowek_content ul li').remove();
			for(var i in fillData)
			{
				offer = fillData[i];
				$('div#schowek div.schowek_content ul').append(	'<li id="clipboardItem_' + i + '">' + 
															    '<a href="#" class="delete_offer">[X]</a>' + 
																'<span class="txt_cena">' +
																'<a href="' + offer.hotel_url + '"><strong>' + offer.price + '</strong>' + 
																' <em>PLN<br/>'+ ((offer.event_duration.toString().length > 0) ? (offer.event_duration + ' dni') : '') + '</em></a></span>' +
															   	'<a href="' + offer.hotel_url + '">' +
																'<h4>' + offer.name + '</h4> ' + 
																'<h2>' + offer.country + '</h2>' +
																'<p>kiedy: ' + offer.event_begin + '</p>' +
																'</a> ' +
																'</li>'
				);
			}
		}
		else
		{
			$('div#schowek div.schowek_content').remove();
			$('div#schowek a.schowek').removeClass('active');
		}
		
		$('div#schowek div.schowek_content ul li a.delete_offer').click(function() {
																					var itaka_clipboard = JSON.parse($.cookie('itaka_clipboard'));
																					var new_itaka_clipboard = new Array();
																					var item_to_delete = parseInt($(this).parent().attr('id').replace('clipboardItem_', ''));
																					for(var i in itaka_clipboard)
																					{
																						if(item_to_delete != i) 
																						{
																							clipboard_item = itaka_clipboard[i];
																							new_itaka_clipboard.push(clipboard_item);
																						}
																					}
																					
																					$.cookie('itaka_clipboard', JSON.stringify(new_itaka_clipboard), { expires: cookie_expiration, path: '/' });
																					$('div#schowek a.schowek strong').text(new_itaka_clipboard.length);
																					
																					if(new_itaka_clipboard.length == 0)
																					{																						
																						$('div#schowek div.schowek_content ul').slideUp(400, function() { 
																							$('div#schowek div.schowek_content').remove();
																							$('div#schowek a.schowek').removeClass('active');
																						});
																					}
																					else
																						$(this).parent().slideUp('fast');
																					
																					return false;
		});
	} 
	
	$('div#schowek a.schowek').click(function() {
		if($(this).hasClass('active'))
		{
			$('div#schowek div.schowek_content ul').slideUp(400, function() { 
				$('div#schowek div.schowek_content').remove();
				$('div#schowek a.schowek').removeClass('active');
			});
			
		}
		else
		{
			if(!$.cookie('itaka_clipboard'))
				return false;
			
			var itaka_clipboard = JSON.parse($.cookie('itaka_clipboard'));
			var new_itaka_clipboard = new Array();
			
			var today_date = new Date();
			var event_begin_date = new Date(); 
			
			for(var i in itaka_clipboard)
			{
				if(itaka_clipboard[i].event_begin.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/))
				{
					event_begin_arr = itaka_clipboard[i].event_begin.match(/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/);
					event_begin_date.setFullYear(event_begin_arr[1], event_begin_arr[2]-1, event_begin_arr[3]);
	
					if(today_date.valueOf() <= event_begin_date.valueOf())
					{
						clipboard_item = itaka_clipboard[i];
						new_itaka_clipboard.push(clipboard_item);
					}
				}
			}
			
			itaka_clipboard = new_itaka_clipboard;
			$.cookie('itaka_clipboard', JSON.stringify(itaka_clipboard), { expires: cookie_expiration, path: '/' });
			$('div#schowek a.schowek strong').text(itaka_clipboard.length);
			
			if(itaka_clipboard.length == 0)
				return false;
			
			$('div#schowek div.schowek_content').remove();
			$(this).after('<div class="schowek_content"><ul><li class="preloader">&nbsp;</li></ul></div>');
			result = clipboardOps.getContent(itaka_clipboard);
			$(this).addClass('active');
		}
		return false;
	});
});
	