/*
 * ternCal v2.0b - Calendar Javascript Document
 * http://www.ternstyle.us/ternCal
 *
 * Copyright (c) 2007-2010 Matthew Praetzel (ternstyle Inc.)
 * License: http://www.ternstyle.us/license.html
 *
 * Author: Matthew Praetzel
 *
 *
 */
 
(function($) {
		  
	ternCal = {
		
		q : '',
		elap : ternCal_root+'/wp-content/plugins/ternCal/calendar/images/loading.gif',
		wins : [],
		url : {},
		
		views : ['month','week','day'],
		view : 'month',
		isLoaded : false,
		mapsLoaded : false,
		selectedDay : null,
		selected : false,
		showing : false,
		
		defaults : {
			m : (new Date).getMonth()+1,
			d : (new Date).getDate(),
			y : (new Date).getFullYear(),
			view : 'month',
			calendar : 0,
			query : '',
			func : null
		},
		time : null,
		font_size : 10,
		windowSize : {},
		mouse : {},
		map : {
			node : null,
			locs : []
		},
		
		scripts : {
			js : [
				ternCal_root+'/wp-content/plugins/ternCal/calendar/js/ternCal-func.js',
				ternCal_root+'/wp-content/plugins/ternCal/calendar/js/ternMenus.jquery.js',
				ternCal_root+'/wp-content/plugins/ternCal/calendar/js/jquery-libs.min.js'
			],
			css : [
				ternCal_root+'/wp-content/plugins/ternCal/calendar/themes/ternCal.css',
				ternCal_root+'/wp-content/plugins/ternCal/calendar/themes/ternCal_'+ternCal_theme+'.css'
			]
		},
		scriptsLoaded : false,
		
		init :
		function (a) {
			var th = this;
			$.extend(ternCal,th.defaults,a);
			//
			$(document.body).elapsor({
				image : th.elap,
				color : '#fff',
				opacity : 85,
				func : function () {
					if(!th.scriptsLoaded) {
						th.loadScripts();
					}
					th.addToQueue(function () {
						th.q += '&ternCal=1';
						if(th.parseUrl()) {
							th.setVarsToUrl();
						}
						else {
							th.time = th.timeObject(th.m,th.d,th.y);
						}
						$.preloadCssImages();
						th.addToQueue(th.loadView);
						th.removeFromQueue();
					});
				},
				text : 'Loading Calendar...',
				text_style : {
					color : '#333',
					'font-size' : '11px'
				}
			});
		},
		loadScripts :
		function () {
			var th = this;
			for(k in th.scripts) {
				for(l in th.scripts[k]) {
					if(k == 'js') {
						eval("th.addToQueue(function () { $.getScript('"+th.scripts[k][l]+"',function () { th.removeFromQueue(); }); });");
					}
					if(k == 'css') {
						eval("th.addToQueue(function () { $.ajax({ cache : false,url : '"+th.scripts[k][l]+"',success : function () { $('head').append('<link>');$('head').children(':last').attr({ rel : 'stylesheet',type : 'text/css',href: '"+th.scripts[k][l]+"' });th.removeFromQueue(); } }); });");
					}
				}
			}
			if(ternCal_gapikey) {
				th.addToQueue(th.loadGoogleMaps);
			}
			th.addToQueue(function () { th.scriptsLoaded = true;th.removeFromQueue(); });
		},
		
		loadGoogleMaps : function () {
			var th = this;
			$('head').append('<script type="text/javascript" src="http://www.google.com/jsapi?key='+ternCal_gapikey+'"></script>');//.children(':last').attr({ type:'text/javascript',src:'http://www.google.com/jsapi?key='+ternCal_gapikey });
			
			//var d = new Date();
		//return Date.UTC(d.getUTCFullYear(),d.getUTCMonth(),d.getUTCDate());
			
			var t = (new Date())/1000;
			th.timer = setInterval(function () {
				if(window['google']) {
					clearInterval(th.timer);
					google.load('maps','3',{ callback:function(){},'other_params':'sensor=true' });
					th.timer = setInterval(function () {
						if(google.maps) {
							clearInterval(th.timer);
							th.removeFromQueue();
							th.mapsLoaded = true;
						}
						else if((t+20) < (new Date())/1000) {
							clearInterval(th.timer);
							th.removeFromQueue();
						}
					},100);
				}
				else if((t+20) < (new Date())/1000) {
					clearInterval(th.timer);
					th.removeFromQueue();
				}
			},100);
		},
		
		queue : [],
		addToQueue :
		function (f,a) {
			this.queue.push([f,a]);
			if(!this.iq) {
				this.startQueue();
			}
		},
		startQueue :
		function () {
			if(this.queue.length > 0) {
				this.iq = true;
				var a = this.queue[0][1] ? this.queue[0][1] : [];
				this.queue[0][0].apply(this,a);
				return;
			}
			this.iq = false;
		},
		removeFromQueue :
		function () {
			this.queue.splice(0,1);
			this.startQueue();
		}
	};

})(jQuery);