var siteUrl = 'tumbledesign.com';

$(document).ready(function(){
	site.showOnPage();
	setTimeout(function(){
		var screen = util.windowDimensions();
		if(screen[0] > 1600){
			return false;
		}
		$('#mastheadImg')
			.attr('src', site.mastheadSrc)
			.load(function(){
				$('#mastheadImg')
				$(this)
					.hide()
					.fadeIn(400,
						function(){
							setTimeout(
								function(){
									$('#logoWhite')
										.fadeTo(350, 1.0);			
								}, 5);
							}
							);
				site.loadOthers();
			});
		}, 200);
});

/* Site-wide javscript
----------------------------------*/
site = {
	setOnPage : function(){
		for(i in site.pages){
			if(document.title.toLowerCase().indexOf(site.pages[i]) > -1){
				site.page = site.pages[i];
				break;
			}
			else{
				site.page = 'blog';
			}
		}
	},
	showOnPage : function(){
	 	$('#'+site.page+"-link").addClass("onPageStyle");		
	},
	grabMasthead : function(){
		var screen = util.windowDimensions();
		site.mastheadSrc = 'http://znd.tumbledesign.com/image/masthead/name/'+site.page
		+'/width/'+screen[0]+'/shift/'+site.shifts[site.page];
		var img = new Image();
		img.src = site.mastheadSrc;
	},
	loadOthers : function(){
		for(i in site.pages){
			var screen = util.windowDimensions();
			var img = new Image();
			img.src = 'http://tumbledesign.com/image/masthead/name/'+site.pages[i]
						+'/width/'+screen[0]+'/shift/'+site.shifts[site.pages[i]];
		}
	},
	mastheadSrc : '',
	page : '',
	msgSet : 0,
	alreadyOver : 0,
	pages : {
		"about" : "about", 
		"projects" : "projects",
		"press" : "press",
		"contact" : "contact",
		"blog" : "blog"
	},
	shifts : {
		"about" : 12,
		"projects" : 53,
		"press" : 33,
		"contact" : 27,
		"blog"  : 36
	},			
};

/* Dialog Boxes
----------------------------------*/
dialog = {
	showThis : function(name, path, opts){
		dialog.closeAll();
		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.onload){
						opts.onload();
					}
				})
				.dialog({modal:true, resizeable:false, autoOpen:false, position:['center',150]});
		}
		else{
			$('#'+name).dialog('open');
			if(opts.onload){
				opts.onload();
			}
			dialog.loaded(name);
		}	
	},
	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 */
	addOnLoad : function(fnc){
		if(window.onload){
			var existing = window.onload;
			window.onload = function(){
			fnc();
			existing();
			}
		}
		else{
			window.onload = fnc;
		} 
	},
	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;
	},
	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=/";
	},
	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;
	},
	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;
	},
	eraseCookie : function(name){
		createCookie(name,"",-1);
	},
	formToJson : function(id){
		var data = {};
		var form = util.getForm(id);
		for(i in form.elements){
			data[form.elements[i].name] = form.elements[i].value;
		}
		return data;
	},
	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);
	},
	getForm : function(id){
		for(i in document.forms){
			if(document.forms[i].id == id){
				return document.forms[i];
			}
		}
	},
	getKeyCode : function(e){
		return ((window.event) ? event.keyCode : e.charCode);
	},
	idData : function(id){
		id = id.split('-');
		return id[1];
	},
	isset : function(variable){
		return ( !(variable === undefined) );
	},
	labeledInput : function(t, l){
		t
			.focus(function(){
				if(t.val()==l){
					t.val('');
				}
			})
			.blur(function(){
				if(t.val()==''){
					t.val(l);
				}
			});
	},
	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;
	},
	scrollY : function(){
			var scrOfY = 0;
			if(typeof(window.pageYOffset) == 'number') {
				//Netscape compliant
				scrOfY = window.pageYOffset;
			}
			else if(document.body && (document.body.scrollLeft || document.body.scrollTop)){
				//DOM compliant
				scrOfY = document.body.scrollTop;
			}
			else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)){
				//IE6 standards compliant mode
				scrOfY = document.documentElement.scrollTop;
			}
			return scrOfY;
		},
	scrollHeight : function(){
		var scrOfY = 0;
		if( typeof(window.scrollMaxY) == 'number') {
			//Netscape compliant
			scrOfY = window.scrollMaxY;
		}
		else if(document.body && (document.body.scrollLeft || document.body.scrollTop)){
			//DOM compliant
			scrOfY = document.body.scrollHeight;
		}
		else if(document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop)){
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollHeight;
		}
		return scrOfY;
	},
	windowDimensions : function(){
		var width = 0, height = 0;
		if(typeof(window.innerWidth) == 'number'){
			//Non-IE
			width = window.innerWidth;
			height = window.innerHeight;
		} 
		else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
			//IE 6+ in 'standards compliant mode'
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		} 
		else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
			//IE 4 compatible
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}
		return [ width , height ];
	},
	/* 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;
}

site.setOnPage();
site.grabMasthead();

