/**
 * --------------------------------------------------------------------
 * jQuery-Plugin "jmenu.jQuery.js"
 * by Scott Jehl, scott@filamentgroup.com
 * http://www.filamentgroup.com
 * reference article: http://www.filamentgroup.com/lab/update_date_range_picker_with_jquery_ui/
 * demo page: http://www.filamentgroup.com/examples/jmenu/
 * 
 * Copyright (c) 2008 Filament Group, Inc
 * Dual licensed under the MIT (filamentgroup.com/examples/mit-license.txt) and GPL (filamentgroup.com/examples/gpl-license.txt) licenses.
 *
 * Dependencies: jquery, jquery UI datepicker, date.js library (included at bottom), jQuery UI CSS Framework
 * Changelog:
 * 	10.23.2008 initial Version
 *  11.12.2008 changed dateFormat option to allow custom date formatting (credit: http://alexgoldstone.com/)
 *  01.04.09 updated markup to new jQuery UI CSS Framework
 *  01.19.2008 changed presets hash to support different text 
 * --------------------------------------------------------------------
 */
jQuery.fn.zonepicker = function(settings){
	var rangeInput = jQuery(this);
	
	//defaults
	var options = jQuery.extend({
		presetRanges: [
			{text: 'Today', ida:'0' },
			{text: 'Last 7 days', ida:'0'},
			{text: 'Month to date', ida:'0' },
			{text: 'Year to date', ida:'0' }
			//extras:
			//{text: 'Yesterday', dateStart: 'yesterday', dateEnd: 'yesterday' },
			//{text: 'Tomorrow', dateStart: 'Tomorrow', dateEnd: 'Tomorrow' },
			//{text: 'Ad Campaign', dateStart: '03/07/08', dateEnd: 'Today' },
			//{text: 'Last 30 Days', dateStart: 'Today-30', dateEnd: 'Today' },
			//{text: 'Next 30 Days', dateStart: 'Today', dateEnd: 'Today+30' },
			//{text: 'Our Ad Campaign', dateStart: '03/07/08', dateEnd: '07/08/08' }
		], 
		//presetRanges: array of objects for each menu preset. 
		//Each obj must have text, dateStart, dateEnd. dateStart, dateEnd accept date.js string or a function which returns a date object
		presets: {
			//specificDate: 'Specific Date', 
			//allDatesBefore: 'All Dates Before', 
			//allDatesAfter: 'All Dates After', 
			//dateRange: 'Date Range'
		},
		doneButtonText: 'Done',
		closeOnSelect: true, //if a complete selection is made, close the menu
		arrows: false,
		posX: null,
		autoCompleteExtra:'',
		posY: null,
		appendTo: 'body',
		ant:'',
		onSelect: function() {},
		onClose: function(){},
		onOpen: function(){}
	}, settings);
	
	//alert ('ceva');

	//custom datepicker options, extended by options
	
	//build picker and 
	var rp = jQuery('<div class="ui-jmenu ui-widget ui-helper-clearfix ui-widget-content ui-corner-bottom" style="border:solid 1px #ffcc66;border-top:none;"></div>');
	
	var rpPresets = (function(){
		var ul = jQuery('<ul class="ui-widget-content"></ul>').appendTo(rp);
		if (options.presets) {
		var x=0;
		jQuery.each(options.presets, function(key, value) {
			jQuery('<li class="ui-jmenu-'+ key +' preset_'+ x +' ui-helper-clearfix ui-corner-all"><span class="ui-icon ui-icon-triangle-1-e"></span><a href="#">'+ value +'</a></li>')
			.appendTo(ul);
			x++;
		});
		}
		jQuery.each(options.presetRanges,function(){
			//alert (this.ida);
			if (this.separator) jQuery('<hr style="margin:2px 0 2px 0">').appendTo(ul);
			else {
			jQuery('<li id="'+this.ida+'" class="ui-jmenu-'+ this.text.replace(/ /g, '') +' ui-corner-all"><a href="#" >'+ this.text +'</a></li>')
			.appendTo(ul);
			}
		});
		
		
		ul.find('li').hover(
				function(){
					jQuery(this).addClass('ui-state-hover');
				},
				function(){
					jQuery(this).removeClass('ui-state-hover');
				})
			.click(function(){
			if (rp.find('li').is('.ui-state-active')) rp.find('li').removeClass('ui-state-active');
				//alert (rp.html());
				//if (rp.find('.ui-state-active').removeClass('ui-state-active');
				//alert(rp.find('.ui-jmenu').is('.ui-state-active')); 
				//alert (this.innerHTML);
				jQuery(this).addClass('ui-state-active').clickAct();
				//alert (jQuery(this).attr('class'));
				return false;
			});
		return ul;
	})();
	//alert (rpPresets);
	function showRP(){
		rp.slideDown(5);
		if ($(document).find('.ui-daterangepicker').is(':visible')) $(document).find('.ui-daterangepicker').hide();
		options.onOpen();
	}
	function hideRP(){
		rp.fadeOut(10, function(){ options.onClose(); });
	}
	function toggleRP(){
		if(rp.is(':visible')){ hideRP(); }
		else { showRP(); }
	}
	
						
	//preset menu click events	
	jQuery.fn.clickAct = function(){
		
	if(jQuery(this).is('.ui-jmenu-dateRange')){
		rp.find('.menur-end').show(100, function(){
		rpPickers.show();
		rpPickers.find('#sr').focus(function(){rpPickers.find('#sr').val('');}).keyup(function(ev) {
		//alert (rangeInput.attr('href')+'?vl='+rpPickers.find('#sr').val());
		$.get(rangeInput.attr('href')+'?vl='+rpPickers.find('#sr').val(), function(data){
		//alert (data);
		rp.find('.lista-tari').empty();
		rp.find('.lista-tari').html(data);
		
		//jQuery(this).find('.lista-tari').empty();
		//
		//ceva.find('li').addClass('ui-state-active');
		//alert (ceva.find('li'));
		});
		});
		});
		}
		else {
			//alert ($(this).text());
			
			rp.find('.menur-end').hide(100, function(){
			rpPickers.hide();
			});
			rp.hide();
			
			rangeInput.val($(this).text());
			//alert($(this).attr('id'));
			if (options.onSelect) options.onSelect();
			rangeInput.attr('iid',$(this).attr('id'));
			//alert (rp.find('.ui-state-active').text());
			hideRP();
		}
		
		return false;
	}	
	
	//options.ACextraParams = (settings) ? jQuery.extend(autocomplete.options, settings.autoCompleteExtra) : autocomplete;
	//alert (options.autoCompleteExtra.ia);
	//picker divs
	if (options.presets) {
	var rpPickers = jQuery('<div class="ranges ui-widget ui-corner-all ui-helper-clearfix"><div class="menur-end"><span class="title-end ui-widget-header" style="text-align:left"><input type="text" class="textbox ui-state-active ui-corner-all" style="width:11em;font-size:1em" id="sr" value="Cautare rapida"></span></div></div>').appendTo(rp);
	//$('#sr').focus(function(){alert('ceva')});
	var content_url = rangeInput.attr('href');
	var ceva = jQuery('<div class="ui-widget ui-widget-content lista-tari" style="padding:3px;overflow-y:scroll;height:15em;width:19em"></div>').appendTo(rpPickers.find('.menur-end'));
	//alert (options.autoCompleteExtra);
	menuContent = $.get(content_url, function(data){
		jQuery(data).appendTo(ceva);
		//ceva.find('li').addClass('ui-state-active');
		//alert (ceva.find('li'));
		});
	//rpPickers.find('.lista-tari li').addClass('ui-state-active');
	//var i=0; 
	
	/* var doneBtn = jQuery('<button class="btnDone ui-state-default ui-corner-all">'+ options.doneButtonText +'</button>')
	.click(function(){
		//rp.find('.ui-datepicker-current-day').trigger('click');
		//alert (rpPickers.find('.agn').attr('iid'));
		hideRP();
	})
	.hover(
			function(){
				jQuery(this).addClass('ui-state-hover');
			},
			function(){
				jQuery(this).removeClass('ui-state-hover');
			}
	)
	.appendTo(rpPickers); */
	}
	
	//var buton = jQuery('<div><button class="ui-state-default">Ok</button></div>').appendTo(rpPickers.find('.menur-end'));
	// rpPickers.find('.menur-start, .menur-end').datepicker(options.datepickerOptions);
	// rpPickers.find('.menur-start').datepicker('setDate', inputDateA);
	// rpPickers.find('.menur-end').datepicker('setDate', inputDateB);
	//rpPickers.find('.menur-start').html('Ceva');
	//rpPickers.find('.menur-end').datepicker('option', 'minDate', inputDateB);
	
	//alert (inputDateA);
	//rpPickers.find('.menur-end').datepicker('option', 'minDate', inputDateA);
	
	
	
	//inputs toggle rangepicker visibility
	jQuery(this).click(function(){
		toggleRP();
		return false;
	});
	//hide em all
	//rp.css('display','block');
	rpPickers.css('display', 'none');
		//inject rp
	jQuery(options.appendTo).append(rp);
	
	//wrap and position
	rp.wrap('<div class="ui-jmenucontain"></div>');
	if(options.posX){
		rp.parent().css('left', options.posX);
	}
	if(options.posY){
		rp.parent().css('top', options.posY);
	}

	//add arrows (only available on one input)
	

	jQuery(document).click(function(){
		if (rp.is(':visible')) {
			hideRP();
			//alert('xxx');
		}
	}).keyup(function(e){if (e.keyCode == 27 || e.keyCode == 9) {if (rp.is(':visible')) {
			hideRP();
			//alert('xxx');
		}} }); 

	rp.click(function(){return false;}).hide();
	return this;

	
	}

function action(obj) {
	$('#teritoriu').val(obj.text());
	$('#vf_t').val(obj.text());
	$(document).click();
	//if (options.onSelect) options.onSelect();
	//alert ();
}
