// Open Modal popup of help width appropriate URL
var jQueryWindows = null;
var jQueryStartUpFucntions = [];
if (!window.HelpSystemAPI){
	window.HelpSystemAPI = {HelpSystemData:{}};	
}
$(document).ready(function(){
	jQueryWindows = new jQueryWindowsObjects();				   
	jQueryWindows.init({
		autoOpeners: [
			{id:"pg10", path:["/","/index.htm","/index.cfm"],	params:["showcookies=10"],	callback:function(autoOpenerId){ openHelp("top_help",autoOpenerId); }},
			{id:"pg2", path:["/","/index.htm","/index.cfm"],	params:["showcookies=2"],	callback:function(autoOpenerId){ openHelp("top_help",autoOpenerId); }},
			{id:"pg3", path:["/","/index.htm","/index.cfm"],	params:["showcookies=3"],	callback:function(autoOpenerId){ openHelp("top_help",autoOpenerId); }},
			{id:"pg4", path:["/","/index.htm","/index.cfm"],	params:["showcookies=4"],	callback:function(autoOpenerId){ openHelp("top_help",autoOpenerId); }},
			{id:"pg5", path:["/bizFromFiles.cfm"],  params:["step=upload"],callback:function(autoOpenerId){ openHelp("popup_upload",autoOpenerId); }},
			{id:"pg6", path:["/bizFromFiles.cfm"],  params:["step=crop"],callback:function(autoOpenerId){ openHelp("popup_crop",autoOpenerId); }},
			{id:"pg7", path:["/bizFromFiles.cfm"],  params:["step=preview"],callback:function(autoOpenerId){ openHelp("popup_preview",autoOpenerId); }}
		],
		bottomPanelHTML : '<label><input name="single" type="checkbox" /> '+HelpSystemAPI.HelpSystemDataLabels.singlePageDontShow+' </label><label><input name="all" type="checkbox" /> '+HelpSystemAPI.HelpSystemDataLabels.allPageDontShow+'</label>'
	});
	for (var i=0; i<jQueryStartUpFucntions.length;i++)
		jQueryStartUpFucntions[i]();
});
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
function jQueryWindowsObjects(){
	var self = this;
	
	this.cookiesPrefix = "auto_popup_";
	this.bottomPanelHTML = '<label><input name="single" type="checkbox" /> Don\'t show this page any more.  </label><label><input name="all" type="checkbox" /> Disable all the help popups on load</label>'
	
	this.dialogObjects = {};
	this.init = function(options){
		options = options || {};
		for (var i in options)
			this[i] = options[i];
			
		// init some defaults
		$.ui.dialog.defaults.modal = true;
		$.ui.dialog.defaults.bgiframe = true;
		$.ui.dialog.defaults.resizable = false;
		$.ui.dialog.defaults.draggable = false;
		
		this.checkForAutoOpenPopups();
	}
	this.closeWindowById = function(id){
		$("#"+id).dialog("close");	
	}
	this.openPopupById = function(id,opt,autoOpenerId){
		if (!HelpSystemAPI.HelpSystemData[id]) return;	
		var url = HelpSystemAPI.HelpSystemData[id].url;
		var title = HelpSystemAPI.HelpSystemData[id].title;
		var customType = HelpSystemAPI.HelpSystemData[id].customType||"default";
		var options = HelpSystemAPI.HelpSystemDataCustomTypes[customType];
			options.title = title;
		// some options can overwrite defaults	
		if (opt) for (var i in opt) options[i] = opt[i];
		this.openPopupJQuery(url,options,autoOpenerId);		
	}
	this.openPopupJQuery = function(src,options,autoOpenerId){
		options=options||{}; 
		var id  = "jPopup_"+(new Date()).valueOf();
		var d = document.body.appendChild(document.createElement("DIV"));
		d.style.overflow="hidden";
		$(d).html('<iframe width="100%" height="0px" frameborder="0" name="'+id+'" id="'+id+'" style="border:1px solid gray;"></iframe><div style="position:absolute;top:0px;left:0px;width:200%;height:200%;z-index:1000;display:none;background-color:gray;filter:alpha(opacity=10);-moz-opacity:0.1;opacity:0.1;"></div>');
		$(d).find("iframe").attr("src",src);
		this.dialogObjects[id] = d;
		
		if (options.modal) this.setVisibilityToAllSelectBoxes(false);
		options.close = function(){
			if (options.modal) self.setVisibilityToAllSelectBoxes(true);
			$(d).dialog("destroy");
			$(d).html("");
			d.style.display="none";
			self.dialogObjects[id] = null;
			
			// set cookies before close if they are
			if (autoOpenerId && self.cookies){
				if (self.cookies["all"]) self.setCookie(self.cookiesPrefix+"all","set");
				if (self.cookies["single"]) self.setCookie(self.cookiesPrefix+autoOpenerId,"set"); 
			}
		}
		options.resizeStart = function(event,ui){self._hideIframes();}
		options.resizeStop = function(event,ui){self._showIframes()}
		options.dragStart = function(event,ui){self._hideIframes();}
		options.dragStop = function(event,ui){self._showIframes()}
		options.open = function(event,ui){$(d).find("iframe").attr("height","100%");}
		
		if (autoOpenerId) options.buttons = {fake:"fake"};
		$(d).dialog(options,"open");	
		if (autoOpenerId) {
			self.cookies = {};
			$(d).nextAll(':last').html(self.bottomPanelHTML);;
			$(d).nextAll(':last').find("input").click(function(){
				self.cookies[this.name] = this.checked; 
			});
		}
	}
	
	
	this._hideIframes=function(){
		for (var i in this.dialogObjects)
			if (this.dialogObjects[i]) $(this.dialogObjects[i]).children().next().css("display","block");
	}
	this._showIframes=function(){
		for (var i in this.dialogObjects)
			if (this.dialogObjects[i]) $(this.dialogObjects[i]).children().next().css("display","none");
	}
	this.setCookie = function(name, value, expires, path, domain, secure) {
		 expires=expires||"Mon, 01-Jan-2070 00:00:00 GMT";	
		 document.cookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	}
	this.getCookie = function(name) {
		var cookie = " " + document.cookie;
		var search = " " + name + "=";
		var setStr = null;
		var offset = 0;
		var end = 0;
		if (cookie.length > 0) {
			offset = cookie.indexOf(search);
			if (offset != -1) {
				offset += search.length;
				end = cookie.indexOf(";", offset)
				if (end == -1) {
					end = cookie.length;
				}
				setStr = unescape(cookie.substring(offset, end));
			}
		}
		return(setStr);
	}
	
	// hide all the selectboxes because they are above the popup area. Then show them on close of popup.
	this.dialogObjectsModalCounter = 0;
	this.setVisibilityToAllSelectBoxes = function(mode){
		if (mode) this.dialogObjectsModalCounter--;else this.dialogObjectsModalCounter++;
		if (mode && this.dialogObjectsModalCounter) return;
		var s = document.getElementsByTagName("SELECT");
		if (s){
			for (var i=0; i<s.length; i++)
				s[i].style.visibility=(mode?"visible":"hidden");
		}		
		if (window.frames.frmDesign && window.frames.frmDesign.document.getElementsByTagName("SELECT")){
			var s = window.frames.frmDesign.document.getElementsByTagName("SELECT");
			for (var i=0; i<s.length; i++)
				s[i].style.visibility=(mode?"visible":"hidden");	
		}		
	}
	
	
	/*	Config for auto openers. 
		The first item that fits will be chosen. So if you have two modules with the same name but different params - write from the most filled one to the less one.
		( Sample:
		 		1) params:[]
				2) params:["param=1"]
				Result: the second item will be never chosen. As if the second fits, the first one will fit the page location too	
				
				1) params:["param=1"]
				2) params:[]
				Result: the second item will be chosen only if there is no param=1. If it is present - the first item will be chosen
		 )	
		Don't forget to insert ID. This id will be checked in the cookies for existance
	*/	
	/*
	this.autoOpeners = [
		{id:"pg1", path:["/","/index.htm","/index.cfm"],	params:["showcookies=1"],	callback:function(autoOpenerId){ openHelp("top_help",autoOpenerId); }},
		{id:"pg2", path:["/","/index.htm","/index.cfm"],	params:["showcookies=2"],	callback:function(autoOpenerId){ openHelp("top_help",autoOpenerId); }},
		{id:"pg3", path:["/","/index.htm","/index.cfm"],	params:["showcookies=3"],	callback:function(autoOpenerId){ openHelp("top_help",autoOpenerId); }},
		{id:"pg4", path:["/","/index.htm","/index.cfm"],	params:["showcookies=4"],	callback:function(autoOpenerId){ openHelp("top_help",autoOpenerId); }}
	];
	*/
	this.autoOpeners = [];
	this._arrayFind = function(ar,v, unesc){
		if (!unesc)	{
			for (var i=0; i<ar.length; i++)
				if (ar[i].toLowerCase()==v.toLowerCase()) return i;
		} else {
			for (var i=0; i<ar.length; i++)
				if (unescape(ar[i]).toLowerCase()==v.toLowerCase()) return i;
		}
		return -1;	
	}
	// get the first autoopener that fits the conditions and open.
	// But open only if there are no cookies "don't remind" for this page or "don't remind for all the pages"
	this.checkForAutoOpenPopups = function(){
		var c_all = this.getCookie(this.cookiesPrefix+"all");
		if (c_all) return;
		
		var path = window.location.pathname.toLowerCase();
		var params = window.location.search.toLowerCase().substr(1).split("&");
		var fitIndex=-1;
		for (var i=0; i<this.autoOpeners.length; i++){
			var rec = this.autoOpeners[i];
			if (this._arrayFind(rec.path,path)>-1){
				if (rec.params.length){
					var paramFound = true;	
					for (var j=0; j<rec.params.length; j++)
						if (this._arrayFind(params,rec.params[j],true)==-1){
								paramFound = false;
								break;
						}
					if (paramFound)	fitIndex = i;
				} else {
					fitIndex = i;
				}
			}
			if (fitIndex>-1) break;
		}
		if (fitIndex>-1){
			var config = this.autoOpeners[fitIndex];	
			var c = this.getCookie(this.cookiesPrefix+config.id);
			if (!c) config.callback(config.id);
		}
	}
}
function openHelp(id,autoOpenerId){
	if (!HelpSystemAPI.HelpSystemData[id]) return;	
	if (jQueryWindows){
		jQueryWindows.openPopupById(id,{},autoOpenerId);
		return;
	}
	
}
function openToolbar(id,w,h){
	if (!HelpSystemAPI.HelpSystemData[id]) return;
	if (jQueryWindows){
		jQueryWindows.openPopupById(id,{width:w,height:h});
		return;
	}
}

// open Flash document
function openFlash(url,title,autoOpenerId){
	if (jQueryWindows){
		var options = HelpSystemAPI.HelpSystemDataCustomTypes["swfPage"];
			options.title = (title||"Online Help Animation");
		jQueryWindows.openPopupJQuery(url,options,autoOpenerId);	
		return;
	}
}
function openFlashModal(url,title,autoOpenerId){
	if (jQueryWindows){
		var options = HelpSystemAPI.HelpSystemDataCustomTypes["swfPageModal"];
			options.title = (title||"Online Help Animation");
		jQueryWindows.openPopupJQuery(url,options,autoOpenerId);		
		return;	
	}
}

// open any document in custom size
function openHtm(url,title,autoOpenerId){
	if (jQueryWindows){
		var options = HelpSystemAPI.HelpSystemDataCustomTypes["htmlPage"];
			options.title = (title||"Online Help");
		jQueryWindows.openPopupJQuery(url,options,autoOpenerId);	
		return;
	}
}
function openZoom(url,title){
	if (jQueryWindows){
		var options = HelpSystemAPI.HelpSystemDataCustomTypes["zoom"];
			options.title = (title||"Zoom");
		jQueryWindows.openPopupJQuery(url,options);	
		return;
	}	
}


