/*
 * jQuery plugin to create wide variety of modal popup windows on the site
 * 
 * 		$("body").delegate(".modalPopup", "click", function(){
 *			$(this).sdiModals();
 *			return false;
 *		});
 *
 * behavior determined with classes:
 * frag = use a fragment of the target url, which should be identified by id="modalFragment"
 * wideDlg = dimensions 740 x 500
 * narrowDlg = dimensions 420 x 400
 * 
 * provide a title in the link to determine the heading of the popup
 *
 * use title="Attraction Detail" to popup attraction tabs WITH rel="tab-id" to specify which tab to show
 *
 *
 */
(function( $ ){

	$.fn.sdiModals = function(options) {
		var settings = {
				trackingPageName: "Undefined",
				autoOpen: false,
				resizable: true,
				draggable: false,
				modal: true,
				position: "center",
				closeText: 'close ' + $("<div/>").html('&#215;').text()
		};
		if (options) { 
	        $.extend(settings, options);
	    };
		
	    var rel = "";
		if($("#modalPopup").length == 0){
			$("body").append('<div id="modalPopup" class="hide"><div id="modalPopupContent" class="clearfix"></div></div>');
		};
		$("#modalPopup").dialog(settings);
	
		return this.each(function(){
			var thisLink = $(this);
			var myTitle = thisLink.attr("title") ? thisLink.attr("title") : "";
			var contentToLoad = thisLink.attr("href");
			rel = thisLink.attr("rel");
			if (thisLink.hasClass("frag")){
				contentToLoad = contentToLoad + " #modalFragment";
			}else if(thisLink.hasClass("attractionModal")) {
				contentToLoad = contentToLoad + "?modal=true";
			}
			if (thisLink.hasClass("wideDlg")){
				myWidth = 900;
				myHeight = 500;
			} else if (thisLink.hasClass("narrowDlg")) {
				myWidth = 420;
				myHeight = 400;
			} else {
				myWidth = 740;
				myHeight = 500;
			};
			
			if(sdi && sdi.google) {
				try {
					sdi.google.trackEvent(settings.trackingPageName, thisLink.attr("title"), '');
				} catch(err) {}
			}
			
			$("#modalPopupContent").load(contentToLoad, function(responseText, textStatus, XMLHttpRequest){
				$('#modalPopup')
					.dialog({
						width: myWidth,
						height: myHeight,
						title: myTitle
						})
					.dialog("open");
				$("#modalPopupContent").show();
				
				$( "#tabs" ).tabs().show();
				if(rel != ""){
					tabToShow = $("#"+rel);
					$( "#tabs" ).tabs().tabs("select",$("#tabs .ui-tabs-panel").index(tabToShow));
				}
				
			});
		});
	};
})( jQuery );
