var siteUrl = 'supercevirmen.com';

$(document).ready(function(){
	$('.dialog', '#dialogs').dialog({ autoOpen: false, draggable: false, position:['center', 200], modal: true });
});

/* Site-wide javscript
----------------------------------*/
site = {
	
};

/* Dialog Boxes
----------------------------------*/
dialog = {
	showThis : function(name, path, opts){
		dialog.loading();
		opts = (util.isset(opts)) ? opts : "";
		var id = (util.isset(opts.id)) ? opts.id : 'dialog';
		if(opts.remote){
			$("#"+id)
				.load('http://'+siteUrl+'/'+path.toLowerCase()+'/'+name.toLowerCase(), null, function(){
					dialog.loaded(id);
					$('#'+id).dialog('open');
					if(opts.resize){
						//formAction.resizeFormShell(".blue_block-content");
						//formAction.resizeFormShell(".orange_block-content");
					}
					if(opts.onload){
						opts.onload();
					}
				})
				.dialog({modal:true, resizeable:false, autoOpen:false, position:['center',150]});
		}
		else{
			$('#'+name).dialog('open');
		}	
	},
	showIdentify : function(){
		if(!($('#email').is(':visible'))){
			dialog.loading();
			$("#dialog")
				.load('http://'+siteUrl+'/user/id/identify', function(){
					dialog.loaded();
					$('#dialog').dialog('open');
					formAction.resizeFormShell(".blue_block-content");
					$('#email', '#dialog')
					.keyup(identity.watchIdentityInput);
				})
				.dialog();
		}
	},
	showMakeALink : function(){
		dialog.closeAll();
		var trs = tran.transCSV();
		var src = tran.setFromTextLang;
		var txt = $('#fromText').val();
		if(trs && src && txt){
			$('#makeALinkLoading').show();
			$('#ntTransLink').val('');
			$('#shortLink').val('');
			$.get('http://supercevirmen.com/ajax/shortlink.php', {trs:trs, src:src, txt:txt}, function(data){
				$('#ntTransLink').val(data.shortLink);
				$('#shortLink').val(data.unu);
				$('#makeALinkLoading').hide();
			}, 'jsonp');
			$('#makeALink').dialog('open');
			
			dialog.openedList.push('makeALink');
		}
	},
	loading : function(){
		$('#dialog_loading').dialog('open');
		dialog.openedList.push('dialog_loading');
	},
	loaded : function(id){
		dialog.close('dialog_loading');
		dialog.openedList.push(id);
	},
	close : function(id){
		$('#'+id).dialog('close');
		var tmp = [];
		for(i in dialog.openedList){
			if(dialog.openedList[i] != id){
				tmp.push(id);
			}
		}
		dialog.openedList = tmp;
	},
	closeAll : function(){
		for(i in dialog.openedList){
			dialog.close(dialog.openedList[i]);
		}
	},
	openedList : []
};

/* Utilities
----------------------------------*/
util = {
	/* Functions */
	emailIsValid : function(str){
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}
		if (str.indexOf(at,(lat+1))!=-1){
		  return false
		}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false
		}
		if (str.indexOf(dot,(lat+2))==-1){
			return false
		}
		if (str.indexOf(" ")!=-1){
			return false
		}
 		return true;
	}, 
	emailIsDone : function(str){
		var domain = str.substring(str.length, str.length-5);
		var acTLDs = util.autoCompleteTLDs;
		for(var c in acTLDs){
			if(domain.indexOf('.'+acTLDs[c]) > 0){
				return true;
			}
		}
		return false;
	},
	addOnLoad : function(fnc){
		if(window.onload){
			var existing = window.onload;
			window.onload = function(){
			fnc();
			existing();
			}
		}
		else{
			window.onload = fnc;
		} 
	},
	isset : function(variable){
		return ( !(variable === undefined) );
	},
	getKeyCode : function(e){
		return ((window.event) ? event.keyCode : e.charCode);
	},
	cmpObj : function(a){
		  var o = {};
		  for(var i=0;i<a.length;i++)
		  {
			o[a[i]]='';
		  }
		  return o;
	},
	compact : function(ar, i){
		var compacted = [];
		if(util.isset(i)){
			ar[i] = null;
		}
		for(j in ar){
			if(util.isset(ar[j]) && (ar[j] != null)){
				compacted.push(ar[j]);
			}
		}
		return compacted;
	},
	getCaretPosition : function(elm){
		var iCaretPos = 0;
		if (document.selection) {
			elm.focus ();
			var oSel = document.selection.createRange ();
			oSel.moveStart ('character', -elm.value.length);
			iCaretPos = oSel.text.length;
		}
		else if (elm.selectionStart || elm.selectionStart == '0'){
			iCaretPos = elm.selectionStart;
		}
		return (iCaretPos);
	},
	createCookie : function(name,value,days){
		if(days){
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else{
			var expires = "";
		}
		value = escape($.toJSON(value));
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie : function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return $.evalJSON((unescape(c.substring(nameEQ.length,c.length))));
		}
		return null;
	},
	eraseCookie : function(name){
		createCookie(name,"",-1);
	},
	/* Variables */
	autoCompleteTLDs : [
		"com", "net", "org", "in", "uk", "fr", "it"
	]
};

String.prototype.ucfirst = function(){
    var f = this.charAt(0).toUpperCase();
    return f + this.substr(1);
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
String.prototype.addSlashes = function(){
	var str='';
	str=this.replace(/\\/g,'\\\\');
	str=this.replace(/\'/g,'\\\'');
	str=this.replace(/\"/g,'\\"');
	str=this.replace(/\0/g,'\\0');
	return str;
}
String.prototype.stripSlashes = function(){
	var str='';
	str=this.replace(/\\'/g,'\'');
	str=this.replace(/\\"/g,'"');
	str=this.replace(/\\\\/g,'\\');
	str=this.replace(/\\0/g,'\0');
	return str;
}


