/**
 * $Workfile: jquery.advancedoptions.js $
 * $Revision: 3 $
 * $Modtime: 11/11/09 10:33 $
 * $Author: Aamir.afridi $
 *
 * jQuery Advanced Options Plugin
 * Copyright (c) 2009 Astun Technology (http://www.isharemaps.com)
 * @name $.advancedoptions
 * @cat Plugins/Wrapper
 * @author Aamir Afridi / info@aamirafridi.com
 * @wiki http://intranet.astun.local/docs/development:advancedoptions
 */


/**
 * Convert simple and advanced search panel into a way where all the advanced options will be hidden and will be show on demand by click 'Advanced' link.
 *
 * @example $('#element').advancedoptions();
 * @desc This will search for 2 divs inside consider first div is having simple options and the 2nd and last one has the advanced options. The advanced search will be hidden and will be shown on demand.
 * @options 
 *		simpleDivSelector: '', 					//The selector for the simple div
 *		advancedDivSelector: '', 				//The selector for the advanced div
 *		simpleDivClass: '.simpleCssClass',		//Css class for the simple div
 *		advancedDivClass: '.advancedCssClass',	//Css class for the advanced div
 *		animationSpeed: 1000 / 'fast',			//The speed of animation either as integer in milliseconds or as a string like 'slow'/'normal'/'fast'
 *		advancedLinkClass: '', 					//Css class for the advanced link/button
 *		advancedLable: 'Advanced', 				//Label/Text for the advanced link/button
 */



(function($) {
	$.fn.advancedoptions = function(options) {
		// Extend the options if any provided
			var o = $.extend({}, $.fn.advancedoptions.defaults, options);
		//Iterate and reformat each matched element
    	return this.each(function() {
			var
			//Save this
				$this = $(this),
			//Get the simple and advanced div
				$simpleElement = (o.simpleDivSelector!='' && $(o.simpleDivSelector).length!=0) ? $(o.simpleDivSelector) : $this.find('div:first'),
				$advancedElement = (o.advancedDivSelector!='' && $(o.advancedDivSelector).length!=0) ? $(o.advancedDivSelector) : $this.find('div:last');
			//Add classes
				$simpleElement.addClass(o.simpleDivClass);
				$advancedElement.addClass(o.advancedDivClass);
			//Hide the advanced div, make its width a little bit smaller than the simple div and centered it
				$advancedElement
					.width(parseInt($simpleElement.width(),10)-25)
					.hide()
					.css('margin','0 auto');
			//Create 'Advanced' button or link
				$advancedLink = $('<div/>')
								.appendTo($this)
								.append(
									$('<a/>')
										.html(o.advancedLable)
										.prepend(
											$('<span/>')
												.addClass('ui-icon ui-icon-triangle-1-s')
												.css({'float':'right', 'margin-top':-1})
										)
										.attr({'href':'#', 'title':o.advancedTitle})
										.addClass('ui-state-default ui-corner-bottom ui-shadow '+o.advancedLinkClass)
										.bind('mouseenter mouseleave',function(){ $(this).toggleClass('ui-state-hover') })
										.css({'float':'right', 'margin-right':30, 'margin-bottom':8, 'padding':5,'border-top':'none', 'text-decoration':'none'})
										.width(($.browser.msie) ? 90 : 'auto')//Quick fix for ie. 90 should be dynamic according to the lenght of the string in o.advancedTitle
										.click(function(){
											var $icon = $(this).find('.ui-icon');
											//Change the icon when it start animation
											$icon.addClass('ui-icon-triangle-1-w');
											$advancedElement.slideToggle(o.animationSpeed,
																			function(){
																				$icon
																					.removeClass('ui-icon-triangle-1-w')
																					.toggleClass('ui-icon-triangle-1-s')
																					.toggleClass('ui-icon-triangle-1-n')
																			});
											return false;
										})
								);
		});
	}//End of Plugin
	
	// Public: plugin defaults options
	$.fn.advancedoptions.defaults = {
		simpleDivSelector: '',
		advancedDivSelector: '',
 		simpleDivClass: '.atSimpleSearch',
 		advancedDivClass: '.atAdvancedSearch',
 		animationSpeed: 1000,
 		advancedLinkClass: 'atAdvancedLink',
		advancedLable: 'Advanced',
		advancedTitle: 'Toggle advanced search mode'
	};
	
})(jQuery);
