/** 
* @projectDescription 	SELFAdDirectory extension for eZ Publish 4.0
* 						SELFAdDirectory extension for eZ Publish 4.0
* 						Written by Piotr Karaś, Copyright (C) SELF s.c.
* 						http://www.mediaself.pl, http://ez.ryba.eu
* 						This program is free software; you can redistribute it and/or
* 						modify it under the terms of the GNU General Public License
* 						as published by the Free Software Foundation; version 2 of the License.
*
* 						This program is distributed in the hope that it will be useful,
* 						but WITHOUT ANY WARRANTY; without even the implied warranty of
* 						MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* 						GNU General Public Lic	ense for more details.
*
* 						You should have received a copy of the GNU General Public License
* 						along with this program; if not, write to the Free Software
* 						Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* @author	Paweł Graczyk p.graczy@mediaself.pl
* @version	0.1 
*/

$(document).ready(
function(){
	var ajaxURL = $('#basket_ajax_url').html();
	var removeText = $('#remove_text').text();
	var offerLimit = parseInt($('#offers_limit_in_a_basket').html());
	var addToABasketText = $('#add_to_a_basket_text').text();
	$('.job_offer_draggable').css('cursor', 'move');
	$('.add_item_to_basket').css('cursor', 'move');

	
	$('.add_item_to_basket').one('click', jobOfferAdd );
	$('.job_offer_delete').one('click', jobOfferDelete);
	
	var basket = new Basket();

	/**
	* Create a new instance of Offer.
	*
	* @classDescription	Offer is created from JSON. It geather data about one job offer.
	* @type {Object} 
	* @constructor	
	*/
	function Offer(ad_id, reference_number, name, town, lead, url){
		this.ad_id = ad_id;
		this.reference_number = reference_number;
		this.name = name;
		this.town = town;
		this.lead = lead;
		this.url = url;
	};	

	/**
	* Create a new instance of Basket.
	*
	* @classDescription	Basket is an Object storing data in offers array.
	* @type {Object}
	* @constructor	
	*/
	function Basket(){
		this.offers = Array();
	};
	
	/**
	* Prototyped from Basket object.
	* Takes care of view when offer is added to a basket.
	*/
	Basket.prototype.add = function(jobOfferId){
		$('#no_offers').remove();
		if(this.offers.length <= offerLimit){
			
			for(offer in this.offers){
				if(this.offers[offer].ad_id == jobOfferId){
					var offer = this.offers[offer]; 
				}
			}
			$('#job_offers_basket').append('<div class="job_offer" id="job_offer_basket_id_' + offer.ad_id +'"><span class="job_offer_name"><span><a href=' + offer.url + 
					'>'+offer.name.substr(0,23)+'</a></span><span class="job_offer_delete" id="job_offer_delete_id_' +
					offer.ad_id+'"> ' + removeText + '</span><span class="clear">&nbsp;</span></span></div>');
			$('.job_offer_delete').one('click',{id: offer.ad_id}, jobOfferDelete);
				$('#add_offer_to_basket_id_' + offer.ad_id).remove();
		}	
		
		if(this.offers.length >= offerLimit){
			$('.text > h5').after('<p id="max_offers">' + addToABasketText + '</p>');
		}
	};
	
	/**
	* Prototyped from Basket object. 
	* Takes care of view when offer is removed from a basket.
	*/
	Basket.prototype.remove = function(jobOfferId){
		// It is necessary to redisplay a basket when element is deleted due to Opera behaviour		
		$('#job_offers_basket').empty();
		$('#job_offer_basket_list_id_' + jobOfferId).remove();
		
		for(offer in this.offers){
			$('#job_offers_basket').append('<div class="job_offer" id="job_offer_basket_id_' + this.offers[offer].ad_id +'"><span class="job_offer_name"><span><a href=' + this.offers[offer].url + 
				'>'+this.offers[offer].name.substr(0,23)+'</a></span><span class="job_offer_delete" id="job_offer_delete_id_' +
				this.offers[offer].ad_id+'"> ' + removeText + '</span><span class="clear">&nbsp;</span></span></div>');
		}
		$('.job_offer_delete').one('click', jobOfferDelete );
		
		//offer list
		$('#job_offer_id_' + jobOfferId).children('.box_content').prepend('<div id="add_offer_to_basket_id_' +
				+ jobOfferId + '" class="add_item_to_basket"><span>Dodaj do koszyka</span></div>');
		//offer full
		if(typeof($('.job_offer_full > a')) == 'object' && $('.job_offer_full > a').attr('id') != undefined){		
			if($('.job_offer_full > a').attr('id').substr(17) == jobOfferId){
				$('.header').before('<div id="add_offer_to_basket_id_' + jobOfferId + '" class="add_item_to_basket"><span>Dodaj do koszyka</span></div>');
			}
		}
		
		$('.add_item_to_basket').one('click', jobOfferAdd );
		
		if(this.offers.length < offerLimit){
			$('#max_offers').remove();
		}
		if(this.offers.length == 0){
			$('#job_offers_basket').before('<p id="no_offers">Brak ofert.</p>');
		}
	};

	/**
	* Prototyped from Basket object. 
	* Initialisate basket.
	*/
	Basket.prototype.refill = function(json){
		this.offers = Array();
		for(jsonOffer in json.offers){
			this.offers.push(new Offer( json.offers[jsonOffer].ad_id, json.offers[jsonOffer].reference_number, 
										json.offers[jsonOffer].name, json.offers[jsonOffer].town, 
										json.offers[jsonOffer].lead, json.offers[jsonOffer].url));
		}
	};

	//Draggable UI
	$('.job_offer_draggable').draggable({
		revert: true,
		revertDuration: 300,
		opacity: '0.6',
		helper: 'clone',
		zIndex: 100,
    	start: function(event, ui){
			$('.basket_droppable').addClass('drag_in_progress');
		},
		stop: function(event, ui){
			$('.basket_droppable').removeClass('drag_in_progress');
			}
	});
	
	//Droppable UI
	$('.basket_droppable').droppable({
		accept: '.job_offer_draggable',
		hoverClass: 'DroppableIsOverDraggable',
		tolerance: 'touch',
		over: function(event, ui){
			ui.helper.css('opacity', '0.9');
		},
		drop: function(event, ui){
			var link = ui.draggable.children('.box_content').children('.add_item_to_basket').children('span').text();
			var jobOfferId = ui.draggable.attr('id').substr(13);
			var jobOfferUrl = ui.draggable.children('.box_content').children('.view_item:first').children('label').children('a').attr('href');
			$.ajax({
				type: 'get',
				url: ajaxURL,
				data: {jobOfferID: jobOfferId, jobOfferURL: jobOfferUrl, activity: 'add'},
				dataType: 'json',
				async: false,
				success: function(json, textStatus){
					if(json.success){
						basket.refill(json);
						basket.add(jobOfferId)
			     	}
				},
				error: function (XMLHttpRequest, textStatus, errorThrown){
					alert(textStatus);
				}
			});
			$(ui.helper).fadeOut();
		}
	});

	//Deletes job offer from the basket (by button).
	function jobOfferDelete(){
		var jobOfferId = $(this).attr('id');
		var id = jobOfferId.substr(20);
		$.ajax({
			type: 'get',
			url: ajaxURL,
			data: {jobOfferID: id, activity: 'delete'},
			dataType: 'json',
			async: false,
			success: function(json, textStatus){
	     		if(json.success){
					basket.refill(json);
	     			basket.remove(id)
	     		}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown){
				alert(textStatus);
			}
		});
	};
	
	//Adds job offer to the basket (by button).
	function jobOfferAdd(){
		var jobOfferId = $(this).attr('id');
		var id = jobOfferId.substr(23);
		var jobOfferUrl = $("#job_offer_url_id_" + id).attr('href');
		$.ajax({
			type: 'get',
			url: ajaxURL,
			data: {jobOfferID: id, jobOfferURL: jobOfferUrl, activity: 'add'},
			dataType: 'json',
			async: false,
			success: function(json, textStatus){
				if(json.success){
	     			basket.refill(json);
	     			basket.add(id)
		     	}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown){
				alert(textStatus);
			}
		});
	};
});