/*
 * Dropdown menu jQuery plugin
 * 
 *  Author: David J. Bradshaw (EMC Conchango)
 *  Dependacies:
 * 		jquery-1.2.6.min.js
 * 		jquery.cookies.2.0.min.js
 * 		strings.XX.js
 */


// Create closed namespace for jQuery code.
(function($) {
	
	var fadeSpeed = 400;
	
	//create menu data storage object
	$.menus = {open:false};
	
	//Create jQuery plugin methods
	$.fn.extend({
		
		//attach javascript to open dropdown menu
		addDropDown	: function (menu){
			try{
				
				var $this 		=  $(this); 			//get jQuery object
				var id 			=  $this.attr('id');	//link id
				var dropDownId 	=  id + 'DropDown';		//dropdown id
				
				//Store menu data for later - we only process this if the link is clicked
				$.menus[id] = menu; 
				
				//Add fucntion to open menu to link
				$this.click(function(){
					
					var $link			=  $(this); 								//get jQuery object of link
					var menuSelector	=  '#' + $link.attr('id') + 'DropDown'; 	//dropdown css selector
					
					//If this is first run, create menu from stored data
					if(0 == $(menuSelector).length)
						addDropDown ($link);
					
					//open menu
					$(menuSelector).fadeIn(fadeSpeed,function(){$.menus.open=true});
					
					return false;
				});
			}
			catch(e){
				console.dir(e);
			}
		},
		
		//Handle country selection
		selectCountry  : function() {
			try{
				var retCode = false;
				
				var $this	 	= $(this);
				var toCountry 	= $this.find('span').html();
				var fromCountry = strings.defaultCountry; //Default option 
				var href		= $this.attr('href');
				
				//Create popup
				var html 	= '<form id="changeCountryConfirm" onsubmit="return false">'
								+ '<a href="#" onclick="jQuery(this).parent().fadeOut('+fadeSpeed+').remove();jQuery(\'.overlay\').remove();return false;" class="close">' + strings.close + '</a>'
								+ '<h3>' + strings.changeCountry.heading + '</h3>'
								+ '<p>' + strings.changeCountry.text + '</p>'
								+ '<input type="checkbox" id="makeDefault" />'
								+ '<input type="hidden" name="countryHref" id="countryHref" value="' + href + '" />'
								+ '<input type="hidden" name="countryName" id="countryName" value="' + toCountry + '" />'
								+ '<label for="cookieBox">' + strings.changeCountry.label + '</label>'
								+ '<p>' + strings.changeCountry.cookieText + '</p>'
								+ '<div><button class="primaryButtonSmall" onclick="jQuery(this).goCountry()"><span>'+strings.Continue+'</span></button>'
								+ '<button class="secondaryButtonSmall" onclick="jQuery(this).parent().parent().fadeOut('+fadeSpeed+').remove();jQuery(\'.overlay\').remove();return false;"><span>'+strings.Cancel+'</span></button></div>'
							+ '</form>';
				
				//Get Cookie
				var cookie = $.cookies.get('country');
				if (null != cookie) fromCountry = cookie;
				
				//inject to and from counties into popup html
				html = html.replace(/%TO%/g,toCountry).replace(/%FROM%/g,fromCountry);
				
				//inject html into page
				$('.overlay').css('opacity',0.4).appendTo('body');
				$('body').append(html);
				
				return retCode;
			}
			catch (e){
				console.dir(e);
			}
		},
		
		goCountry	: function(){
			try{
				var $this		= $(this);
				var href		= document.getElementById('countryHref').value;
				var name		= document.getElementById('countryName').value;
				var makeDefault = document.getElementById('makeDefault').checked;
				
				//Set cookie
				if (true == makeDefault)
					$.cookies.set('country',name);
				
				//Close popup
				//$this.parent().parent().remove();
				
				//Head off to foreign lands
				location.href = href;
				
				return this;
			}
			catch (e){
				alert(e);
				console.dir(e);
			}
		}
		
	});//End $.fn.extend()

	//create menu
	function addDropDown ($link){
		try{
			
			//Add menu section
			function addSection(section){
				
				//Add heading if in object
				function  heading(){
					return section.heading ? '<h5>' + section.heading + '</h5>' : '';
				}
				
				//Add menu items
				function items(){
					
					//format menu item
					function item(i){
						return	'<li><a href="'+ i.href + '"'
								+ (i.click ? 'onclick="' +i.click + '">' :'>')
								+ (i.image ? '<img src="'+i.image+'" alt="" />' : '')
								+ '<span>' + i.name + '</span>'
								+ '</a></li>';
					}
					
					var retStr = '';
					var len = section.items.length; //Number of items in this section
					
					//Loop through items
					for (var i=0; i != len ; i++)
						retStr += item(section.items[i]);
						
					return retStr;
				}
				
				//Create section
				return heading() + '<ul>' + items() +'</ul>';
			}
			
			var id 			= $link.attr('id');
			var dropDownId 	= id + 'DropDown';
			var menu 		= $.menus[id];
			var sectionsLen	= menu.sections.length;
			var i			= 0;
			
			//create menu div
			var menuStr = '<div id="' + dropDownId +'" name="' + dropDownId +'" class="dropDown '+menu.className+'">';
			
			//Add close link
			menuStr +='<a href="#" onclick="jQuery(this).parent().fadeOut('+fadeSpeed+');jQuery.menus.open = false;return false;" class="close">' + menu.close + '</a>';
			
			//Add headding
			menuStr +='<h4>' + menu.heading + '</h4>';
			
			//Add Sections	
			do 
				menuStr += addSection(menu.sections[i++]);
			while (i < sectionsLen);
			
			//close menu div	
			menuStr +='</div>';
			
			//inject menu after link.
			$link.after(menuStr);
		}
		catch(e){
			console.dir(e);
		}
	}
	
	// Add menu close handler to the body tag 
	$(document).ready(function(){ 
		$('body').click(function(){
			if (true == $.menus.open){
				$('.dropDown').fadeOut(fadeSpeed/2);
				$.menus.open = false;
			}
		})
	});
	
})(jQuery);