/**
 * Custom Javascript
 * Must add jquery before this file
 */

jQuery(document).ready(function(){
	var urlAjax = $("meta[name=url]").attr('content') + '/?do=ajax';
	//alert('test');
	jQuery('#searchform').autoLabel();
	//$('.auto_label').autotooltip();
	jQuery('#unsubscribe').autoLabel();
	
	if ($('#jobalertwidget #submit_job_alert').length != 0){
		$('#jobalertwidget #submit_job_alert').click( function(){
			var current 		= $(this),
				container 		=  $('#jobalertwidget'),
				keyword 			= container.find('input[name=keyword]'),
				email 			= container.find('input[name=email]'),
				name 				= container.find('input[name=name]'),
				catid 			= container.find('select[name=catid]'),
				keyword_val 	= keyword.val(),
				keyword_title	= keyword.attr('title');
				email_val 		= email.val(),
				email_title		= email.attr('title');
				catid_val 		= catid.val(),
				name_val 		= name.val(),
				name_title		= name.attr('title');
				errorB = false;
				
			container.find('.message').html("");
			
			//validate
			if (catid_val == "0"){
				catid.addClass('error');
				errorB = true;
			}
			else{
				catid.removeClass('error');
			}
			if (email_val == "" || email_val == email_title){
				email.addClass('error');
				errorB = true;				
			}
			else{
				email.removeClass('error');
			}
			if (name_val == "" || name_val == name_title){
				name.addClass('error');
				errorB = true;	
			}
			else{
				name.removeClass('error');
			}
			
			if (keyword_val == keyword_title){
				keyword_val = "";
			}
			
			if (!errorB){
				//disable button first
				current.attr('disabled', 'disabled');
			
				//do ajax
				$.ajax({
					type: 'post',
					url : urlAjax,
					data: ({
						action : 'job_alert',
						ajax : 'true',
						keyword : keyword_val,
						name : name_val,
						email : email_val,
						catid : catid_val
					}),
					beforeSend : function(){
						},
					success : function(data){
						if (data.success)
							{
								var divMsg = container.find('.message');
								divMsg.html(data.messageData);
							}
						else
							alert(data.messageData);
						
						current.removeAttr('disabled');
					}
				});
			}
		} );
	}
});
/**
 * plugin
 */


(function($){
	jQuery.fn.autoLabel = function(options){
		/**
		 * auto label
 		 * @param applyClass
 		 * @param submitClass
 		 * @param edited class that confirmed item edited
		 */
		var defaults =
		{
			applyClass : '.auto_label',
			submitClass : '.submit',
			edited : 'edited'
		};
		
		var opts = jQuery.extend(defaults, options);
		
		return this.each(function(){
			var container = jQuery(this),
				items = container.find(opts.applyClass),
				submitElement = container.find( opts.submitClass );
				
			items.each(function(){
				var current = jQuery(this),
					value = current.val(),
					title = current.attr('title');
				if (value == '' || value == title){
					current.removeClass( opts.edited );
					current.val(title);
				}
				else if (!current.hasClass( opts.edited )) {
					current.addClass( opts.edited );			
				}
			});
			
			items.focusin(function(){
				var current = jQuery(this),
					value = current.val();
				if (!current.hasClass( opts.edited ) ){
					current.addClass( opts.edited )
					current.val('');
				}
			});
			
			items.focusout(function(){
				var current = jQuery(this),
					value = current.val(),
					title = current.attr('title');
				if (value == '' || value == title){
					current.removeClass( opts.edited );
					current.val(title);
				}
			});
			
			container.submit(function(){
				items.each(function(){
					var current = jQuery(this),
						value = current.val(),
						title = current.attr('title');
					if (value == title){
						current.val('');
					}
				});
			});
			
			submitElement.click(function(){
				items.each(function(){
					var current = jQuery(this),
						value = current.val(),
						title = current.attr('title');
					if (value == title){
						current.val('');
					}
				});
			});
			
		});
}
})(jQuery);

	/**
	* auto label
	* @param applyClass
	* @param submitClass
	* @param edited class that confirmed item edited
	*/
(function($){
	$.fn.autotooltip = function(opt){
		var defaults =
		{
			top: 0,
			left: 0
		};
		
		var options = $.extend(defaults, opt);
		
		return this.each(function(){
			var current = $(this),
			title = current.attr('title'),
			width = current.width(),
			height = current.height(),
			mouse_x = 0;
			mouse_y = 0;
			
			current.parent().css('position','relative');			
			
			current.hover( function(e){
				var mouse_y = e.pageY ;
					mouse_x = e.pageX ;
					
				current.attr('title', '');				
				$('body').prepend( $('<p>').addClass('t-auto-tootlip').html(title).offset( {top: mouse_y + options.top , left: mouse_x + options.left}).delay(300).animate({opacity: 1}, 'slow') );
				
			}, function(e){
				var mouse_x = e.pageX,
					mouse_y = e.pageY;				
				current.attr('title', title);
				$('.t-auto-tootlip').remove();
				
			} );
			
		})
	}
})(jQuery);

