/**
 * shoppingListManager

 *
 * @package    mivilagunk
 * @subpackage Site
 * @author     Szijártó Tamás ( szicsu ) <szicsu@jquery.hu>
 * @version    SVN: $Id: $
 */
shoppingListManager = new function(){
	var self = this;
	var bModified = false;
	
	this.init = function(){
		
		// INGREDIENT INIT
		recipeUploadManager.setAppContainer($('#jsIngredientContainer'));
		$('.jsIngredientItem').each(function(){
			return new IngredientItem( $(this), {mustMatch:true, selectFirst: true});
		});
		
		//item delete
		$('.jsShoppingListItemContainer .jsDelItem').bind('click', function(){
			
			if( $('.jsShoppingListItemContainer .jsDelItem').size() == 1 ) return false;
			
			var oParent = $(this).parents('.jsShoppingListItemContainer');
			bModified = true;
			
			if( $('.jsDelItem', oParent).size() < 2 ){
				oParent.remove();
			}
			else{
				$(this).parent('li').remove();
			}
			
			return false;
		});
		
		// modified
		$('#jsShoppingListForm').find('input, select, textarea').bind('change', function(){
			bModified = true;
			return true;
		});
		
		this.initPrint();
		this.initMail();
		this.initView();
	}
	
	this.checkModified = function(){
		
		if( bModified ){
			alert('A változások nincsenek mentve!');
			return true;
		}
		else{
			return false;
		}
	}
	
	this.initPrint = function(){
		
		$('.jsShoppingListPrint').bind('click', function(){
			
			if( self.checkModified() ){
				return false;
			}
			
			info(__('Please wait, shopping list printing in process!'));			
			
			$.get($(this).attr('href'), function(html){
				dialogManager.close();
				$(html).jqprint({ operaSupport: true });
			})
			
			
			
			return false;
		});
	}
	
	this.initMail =  function(){
		
		$('.jsShoppingListSend').bind('click', function(){
			
			if( self.checkModified() ){
				return false;
			}
			else{
				self.doSend( $(this).attr('href') );
			}
			return false;
		});
	}
	
	this.initView = function(){
		
		$('.jsShoppingListView').bind('click', function(){
			
			if( self.checkModified() ){
				return false;
			}
			
			var aObj = $(this);
			
			dialogManager.open({
				title: __('Shopping List'),
				width: 670,
				height: 620,
				text: '<iframe src="'+aObj.attr('href')+'" style="width:620px;height:600px;border:0px;"></iframe>'
			});
			
			return false;
		});
		
	}
	
	this.doSend = function( sUrl ){
		
		$.ajax({ 
			url: sUrl,
			dataType: 'html', 
			success: function( ret ){ 
				dialogManager.open({
					title: __('Send Shopping List'),
					height: 240,
					text: ret
				},function( dialogObj ){
					dialogObj
						.find('#forward_mail_link').val( window.location.pathname ).end()
						.find('form').YggdrasilValidation({
							validateUrl: Site.Helper.getSluggedUrl('/ajax/validation')
						},function( event, yggObj ){
							
							$.ajax({ 
								url: yggObj.element.attr('action'),
								dataType: 'html', 
								type: "POST",
								data:  Site.Helper.getFormData( yggObj.element ),
								success: function( ret ){ 
									info( ret );
								}
							});
							
							event.preventDefault();
							
							return false;
						});
					
				});
				
				Site.Init.validationInit();
			} 
		});
	}
	
	this.doAdd = function( aObj ){
		
		$.ajax({ 
			url: aObj.attr('href'),
			dataType: 'html', 
			success: function( ret ){ 
				dialogManager.open({
				   title: __('Add Shopping List'),
//					title: aObj.text(),
					height: 240,
					text: ret
				},function( dialogObj ){
					dialogObj
						
						//select onchange
						.find('#shopping_list_add_shopping_list').bind('change', function(){
							
							var oObj = $('#jsNameContainer', dialogObj);

							if( $(this).val() == 'new_item' ){
								oObj.show();
							}
							else{
								oObj.hide().find('input').val('');
							}
						}).end()
						
						//Add, Add&Edit
						.find('#jsAddEdit, #jsAdd').bind('click', function(){
							
							var oForm = dialogObj.find('form');
							if( this.id == 'jsAddEdit' ){
								oForm.data('navigate', true);
							}

							oForm.trigger('submit');
							return false;
						}).end()
						
						//Form submit
						.find('form').YggdrasilValidation({
							// debug: true,
							// debugLevel:2,
							validateUrl: Site.Helper.getSluggedUrl('/ajax/validation')
						},function( event, yggObj ){
							
							$.ajax({ 
								url: yggObj.element.attr('action'),
								dataType: 'json', 
								type: "POST",
								data:  Site.Helper.getFormData( yggObj.element ),
								success: function( ret ){ 
									if( ret.success && ret.url ){

										if( yggObj.element.data('navigate') ){
											location.href = ret.url;
										}
										else{
											//dialogManager.close();
											info( __('Recipe add to shopping list success') );
										}
										
									}
									else{
										alert( ret.error );
									}
								}
							});
							
							event.preventDefault();
							return false;
						});
				});
			} 
		});
	}
}
