/**
 * This create a popup from an a-tag.
 * example $('a.alertBox').popup()
 * note:don't use any class that starts with popup this conflicts with markup2.js stuff
 * to close the popup window simple add the class ui-popup-close to a div or an a tag.
 * By default this will stop the event propagation. If you need to perform another action 
 * i.e. go to the link in the a tag add the class ui-popup-do-link
 * author:jkoberstein
 * options
 *   preload:	if true the ajax call will be sent on document ready. Default is false
 *   background: if true a the background div will be used.
 *   url: if set the popup will go to this url. you may also set the url attribute on the a tag 
 *   		itself to have a different popup link that the default no javascript link. 
 *   		e.g. <a href="pageView" url="popupView">link</a>
 *   class: if set the popup frame will have this class
 */
(function( $ ){
	$.fn.popup = function( options ) {
		var settings = {
			'preload'	: false,
			'background': true
		};
		this.each(function() {
			if ( options ) { 
				$.extend( settings, options );
			}
			if(settings.cmd && settings.cmd=='close'){
				ajaxPopupJQueryAddon.togglePopup($(this).attr('popupId'),true,false,settings.event);
			}
			var href=$(this).attr("href");
			if(settings.url){
				href=settings.url;
			} else if( $(this).attr("url") ){
				href=$(this).attr("url");
			}
			
			//create the popup.
			var popupId;
			if(href.indexOf("#") >= 0){
				popupId = href.substring(href.indexOf("#")+1);
				ajaxPopupJQueryAddon.createPopup(popupId);
				ajaxPopupJQueryAddon.initPopupEvents(popupId);
			} else {
				if(!!$(this).attr('popupId')){
					popupId = $(this).attr('popupId')
				} else if(ajaxPopupJQueryAddon.hrefs[href]){
					popupId = ajaxPopupJQueryAddon.hrefs[href];
				} else {
					popupId = "ui-popup-"+ajaxPopupJQueryAddon.idCount;
					ajaxPopupJQueryAddon.idCount = ajaxPopupJQueryAddon.idCount + 1;
				}
				ajaxPopupJQueryAddon.hrefs[href] = popupId;
				$(this).attr("href","#"+popupId);
				$(this).attr("url",href);
				ajaxPopupJQueryAddon.createPopup(popupId,href);
				if(settings.preload){
					//trigger the load right away
					ajaxPopupJQueryAddon.togglePopup(popupId,settings.background,false);
				}
			}
			$(this).attr('popupId',popupId);
			ajaxPopupJQueryAddon.popupIds[popupId] = settings.close || true;
			$(this).click(function(clickEvent){
				ajaxPopupJQueryAddon.togglePopup(popupId,settings.background,true,clickEvent);
				clickEvent.preventDefault();
				return false;
			});
		});
		ajaxPopupJQueryAddon.hashChange();
		return this;
	};
	
	ajaxPopupJQueryAddon = {};
	ajaxPopupJQueryAddon.idCount = 0;
	ajaxPopupJQueryAddon.activePopup = '';
	ajaxPopupJQueryAddon.hrefs = {};
	ajaxPopupJQueryAddon.popupIds = {};
	ajaxPopupJQueryAddon.hashChange = function(){
		var hash = location.hash.replace( /^#/, '' );
		if(hash.length>4){
			hash = hash.substring(4);
		}
		var active = ajaxPopupJQueryAddon.activePopup;
		if(active.length>4){
			active = active.substring(4);
		}
		if(hash!=active){
			//hide the active window if the hash has changed
			if(active!='none' && active.length>0){
				ajaxPopupJQueryAddon.togglePopup(active,true,false);
			}
			//show the new popup
			if(ajaxPopupJQueryAddon.popupIds[hash]){
				ajaxPopupJQueryAddon.togglePopup(hash,true,true);
			}
		}
	}
	if($(window).hashchange){
		$(window).hashchange( ajaxPopupJQueryAddon.hashChange );
	}
	ajaxPopupJQueryAddon.createPopup = function(popupId, href){
		if (!$('#ui-popup-background').length) {
			$('body').append($('<div id="ui-popup-background"></div>'));
			$("#ui-popup-background").hide();
		}
		if (!$('#'+popupId).length) {
			url = '';
			if(!!href){
				url = " url='"+href+"'";
			}
			$('body').append($('<div id="'+popupId+'"'+url+'></div>'));
			//$('#'+popupId).hide();
		}
	};
	ajaxPopupJQueryAddon.initPopupEvents = function(popupId){
		$('#'+popupId+' .ui-popup-close').click(function(event){
			ajaxPopupJQueryAddon.togglePopup(popupId,true,false,event);
			if(!$(this).hasClass('ui-popup-do-link')){
				event.preventDefault();
			}
		});
	};
	ajaxPopupJQueryAddon.togglePopup = function(popupId, useBackground, show, event){
		if($("#"+popupId).length===0){
			return;
		}
		href = $("#"+popupId).attr('url');
		if(!!href){
			$("#"+popupId).attr('url','');
			$('#'+popupId).load(href, function() {
				ajaxPopupJQueryAddon.initPopupEvents(popupId);
				ajaxPopupJQueryAddon.togglePopup(popupId,useBackground,show, event);
			});
			return;
		}
		if(show){
			var popup = $('#'+popupId);
			if(popup.length>0 && $.trim(popup.html())){
				window.location.hash = ajaxPopupJQueryAddon.activePopup = "loc-"+popupId;
				if(useBackground){
					$('#ui-popup-background').show();
				}
				$('#'+popupId).show();
				$('#'+popupId).addClass("ui-popup");
			}
		} else {
			if(typeof ajaxPopupJQueryAddon.popupIds[popupId] == 'function'){
				ajaxPopupJQueryAddon.popupIds[popupId](event);
			}
			window.location.hash = ajaxPopupJQueryAddon.activePopup = 'none';
			$('#ui-popup-background').hide();
			$('#'+popupId).hide();
		}
	};
	
})( jQuery );
