// Global Shortcut Functions
function $e(text) { return document.getElementById(text); }

// Framework Definition
var aether = {
 site_url: "http://www.stitch-n-keep.com",
 _vars: [],

 // Browser Related Functions
 getScreenXY: function() {
        var myWidth = 0, myHeight = 0;
        if ( typeof( window.innerWidth ) == 'number' ) {
                // Non-IE
                myWidth = window.innerWidth;
                myHeight = window.innerHeight;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                // IE 6+ in 'standards compliant mode'
                myWidth = document.documentElement.clientWidth;
                myHeight = document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                //IE 4 compatible
                myWidth = document.body.clientWidth;
                myHeight = document.body.clientHeight;
        }
        return [ myWidth, myHeight ];
 },
 getCookie: function(Name) {
	var search = Name + "=";
	var returnvalue = "";
	if (document.cookie.length > 0) {
	        offset = document.cookie.indexOf(search);
        	if (offset != -1) {
                	offset += search.length;
	                end = document.cookie.indexOf(";", offset);
        	        if (end == -1) end = document.cookie.length;
                	returnvalue = unescape(document.cookie.substring(offset, end));
	        }
	}
	return returnvalue;
 },

 // Library Functions
 load: function(path,loading) {
	if (loading == undefined) { loading = true; }

	var head = document.getElementsByTagName('head')[0];
	if (!head) { return false; }
	if (loading) $("aether_loading").show('fast');
        script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = aether.site_url+path;
        head.appendChild(script)
	if (loading) $("aether_loading").hide('fast');
	return true;
 },
 setVar: function(k,v) {
	aether._vars[k] = v;
 },
 getVar: function(k) {
	return (aether._vars[k] ?aether. _vars[k] : undefined);
 },

 // Modal Box Functions
 showModal: function(div,html,w,h) {
	// Development Notice
//	if (console != undefined && console.log != undefined) {
//		console.log('deprecated function aether.showModal called, use aether.dialog() instead');
//	}

	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({"width":maskWidth,"height":maskHeight});

	$('#mask').fadeIn('fast');
	$('#mask').fadeTo(800,0.7);

	var winH = $(window).height();
	var winW = $(window).width();

	if (w == undefined || w > $(window).width()) var w = $(window).width() - 50;
	if (h == undefined || h > $(window).height()) var h = $(window).height() - 50;

	$('#'+div).css({"width":w,"height":h});
	$('#'+div).css('top',  document.body.scrollTop + (winH/2-$('#dialog').height()/2));
	$('#'+div).css('left', winW/2-$('#dialog').width()/2);
	$('#'+div).html(html);
	$('#'+div).draggable();
	$('#'+div).show('fast');
	return false;
 },
 closeModal: function(div) {
	$('#'+div).hide('fast');
	$('#'+div).html('');
	$('#mask').hide('fast');
	return false;
 },
 ConfirmDelete: function(title,txtmsg,lnk_ok,lnk_cancel) {
	if (!title) { title = "Confirm delete?"; }
	if (!txtmsg) { txtmsg = "Are you sure you want to delete?"; }
	if (!lnk_ok) { alert('Missing paramater 3: lnk_ok'); return false; }
	lnk_ok = "parent.location.href='"+lnk_ok+"';";

	if (lnk_cancel == undefined) lnk_cancel = "aether.closeModal('dialog');";
	else lnk_cancel = "parent.location.href='" + lnk_cancel + "';";

	var skinpath = aether.getVar('skin_baseurl') + "/" + aether.getVar('skin');
	aether.showModal('dialog',""
		+ "<div style='position:relative;width:441px;height:295px;background: url(" + skinpath + "/img/delete-dialog.png) no-repeat'>"
		+ "<div style='position:absolute;top:35px;left:80px;height:70px;width:343px;font-size:10px;'>"+title+"</div>"
		+ "<div style='position:absolute;top:115px;left:16px;width:408px;height:88px;font-size:12px;padding:5px;font-family:courier;'>"+txtmsg+"</div>"
		+ "<div style='position:absolute;top:215px;left:16px;height:20px;width:408px;text-align:center;font-weight:bold;color:red;font-size:10px;'></div>"
		+ "<div style='position:absolute;top:256px;left:280px;height:25px;width:145px;'>"
			+ "<img onclick=\""+lnk_cancel+"\" src='" + skinpath + "/img/cancel-button.png' border=0 onmouseover=\"this.style.cursor='pointer';\" onmouseout=\"this.style.cursor='normal';\"> "
			+ "<img onclick=\""+lnk_ok+"\" src='" + skinpath + "/img/delete-button.png' border=0 onmouseover=\"this.style.cursor='pointer';\" onmouseout=\"this.style.cursor='normal';\">"
		+ "</div>"
		+ "</div>",441,295);
	return false;
 },
 photoGallery: function(title,images) {
	if (typeof images != 'object') { return false; }

	html  = "<div style='width:100%;height:100%;border:0px;background-color:white;color:black;'>";
	html += "<div style='width:100%;height:22px;background-color:black;color:white;font-weight:bold;text-align:center;'>" + title + "</div>";
	for (i in images) { html += "<img src='" + images[i] + "'>"; }
	html += "<br><br><center>[ <a href='#' onclick=\"return aether.closeModal('dialog');\">Close Gallery</a> ]</center>";
	html += "</div>";

	aether.showModal('dialog',html,($(window).height() - 50),($(window).width()-50));
	return false;
 },
 photoPreview: function(title,url,w,h) {
	html  = "<div style='width:100%;height:100%;border:0px;background-color:white;color:white;'>";
	html += "<div style='width:100%;height:22px;background-color:black;color:white;font-weight:bold;text-align:center;'>" + title + "</div>";
	html += "<img src='" + url + "' title='"+title+"'>";
	html += "<div style='width:100%;height:22px;background-color:white;color:black;font-weight:bold;text-align:center;'>[ <a href='#' onclick=\"return aether.closeModal('dialog');\">Close Preview</a> ]</div>";
	html += "</div>";

	aether.showModal('dialog',html,w,h+22+22);
	return false;
 },

 // Input Sanity Checking
 allowkeys: function(type,element,e) {
	if (window.event && e) { keynum = e.keyCode; }
	else if (e && e.which) { keynum = e.which; }
	else { return true; }
	keychar = String.fromCharCode(keynum);

	switch (type) {
	 case 'int':
	 case 'number':
		numcheck = /[^0-9?\x08\x09]/;
		return !numcheck.test(keychar);
	 default:
		return true;
	}
 },
 dialog_cache: {},
 dialog: function(opt) {
	if (opt == undefined || typeof opt != 'object' || opt.url == undefined) { return false; }

	// Error Checking / set defaults
	if (opt.title == undefined) { opt.title = 'Unknown Dialog'; }
	if (opt.width == undefined) { opt.width = 640; }
	if (opt.height == undefined) { opt.height = 'auto'; }
	if (opt.id == undefined) { opt.id = 'default'; }
	if (opt.resizable == undefined) { opt.resizable = false; }
	if (opt.draggable == undefined) { opt.draggable = false; }
	if (opt.cachable == undefined) { opt.cachable = false; }
	if (opt.buttons == undefined) { opt.buttons = {}; }

	var obj = $("<div id='aether_dialog_"+opt.id+"'><div style='text-align:center;'><b>Loading Please wait...</b><br/><img src='/static/img/ajax_loading.gif' alt='Loading...'/></div></div>").attr('title',opt.title).appendTo('body').dialog({
		autoOpen:	true,
		modal:		true,
		draggable:	opt.draggable,
		resizable:	opt.resizable,
		width:		opt.width,
		height:		opt.height,
		minWidth:	(opt.width == 'auto' ? false : opt.width),
		minHeight:	(opt.height == 'auto' ? false : opt.height),
		title:		opt.title,
		bgiframe:	true,
		stack:		true,
		close:		function() { $(this).remove(); },
		buttons:	opt.buttons
	});

	if (opt.cachable && aether.dialog_cache[opt.id] != undefined) {
		obj.html(aether.dialog_cache[opt.id]);
		if (typeof opt.onOpen == 'function') { opt.onOpen(); }
		return;
	}

	$.ajax({
		type: 'get',
		url:	opt.url,
		dataType: 'html',
		success: function(data,textStatus,xtmp) {
			if (opt.cachable) { aether.dialog_cache[opt.id] = data; }
			obj.html(data);
			if (typeof opt.onOpen == 'function') { opt.onOpen(); }
			return;
		},
		error: function(xtmp,textStatus,errorThrown) {
			obj.html("Failed to download dialog contents - please close and try again<br/><br/>("+xtmp.status+" "+xtmp.statusText+")");
			return;
		}
	});
 }
};
aether.load('/ajax?r=skin_loadjs',false);


$(function(){
 $('body').append("<div id='commonDialog' class='ui-helper-hidden'></div>");
 $('#commonDialog').dialog({
	autoOpen:	false,
	modal:		true,
	draggable:	false,
	resizable:	false,
	minWidth:	450,
	width:		450,
	bgiframe:	true
 });
 $('#ui-dialog-title-commonDialog').parent().find('.ui-dialog-titlebar-close').hide();
});
