/*	wt_univ.js	*/
/*	Universal js/jq functions for Woodford Theater	*/
/*	Created: 2011-10-13	*/
/*	Modified: 2011-12-20*/
/*	© 2011 Woodford County Theater & RLK Marketing; All rights reserved.	*/

var basepath = "/";
var cur = 3;
var dataObj;
var gllry = 'Past Productions';
var isIE = ($.browser.msie) ? true : false;

function doQuery (func, args, callback, path) {
	if (args) {
		args.func = func;
	} else {
		args = {"func" : func};
	}
	if (callback && callback.indexOf ('.') > -1) {
		cb = callback.split ('.');
		callback = 'document.getElementById("tktframe").contentWindow.' + cb[1];
	}
	$.ajax ({
		type: 'POST',
		url: basepath + '_element/doQuery.php',
		data: args,
		async: false,
		success: function (data) {
			if (callback) {
				eval (callback + "(" + data + ")");
			} else {
				dataObj = $.JSON.decode (data);
				return dataObj;
			}
		}
	});
}


function loadHome () {
	$('*:not("#wtModal")').click (function () {
		$('#wtModal').hide();
	});
	drawNav ('mainNav', 'navList', 'listTab', '', true);
	loadShows ();
	loadGalleryLinks ();
	loadSBGallery ();
	$('#hm-gllry-img').click (function () {
		$('#sbPalette').show();
		loadGallery ('stgGallery0', 'gallery0', gllry);
		});
	$('#wtCalSm, #hm-cal-img').click (function () {
//		$('#sbPalette').show();
		loadContent ('calendar');
	});
}

function drawNav (menu, menuID, menuClass, path, animate) {
	doQuery ('getNav', {'menu' : menu}, '', path);
	menuObj = dataObj;
	dataObj = {};
	var temp1 = {};
	var temp2 = {};
	var parent = '';
	$(menuObj).each (function (i, m) {
		if (m.parent != parent) {
			temp1[m.dispOrder] = m;
			} else {
			if (!temp2[m.parent]) temp2[m.parent] = [];
			temp2[m.parent].push(m);
		}
		parent = m.parent;
	});
	nav2 = $('<ul>').attr('id', menuID).appendTo ("#wtNav");
	$.each (temp1, function () {
		var subNav;
		btn = $('<li>').addClass(menuClass);
		$('<a>').attr({'href' : '#', 'rel' : this.rel}).text(this.dispText).appendTo(btn);
		par = this.parent;
		if (temp2[par]) {
			if (!subNav) subNav = $('<ul>').attr ('class', "submenu").appendTo(btn);
			$.each (temp2[this.parent], function (i3, m3) {
				btn2 = $('<li>');
				$('<a>')
					.attr({'href' : '#', 'rel' : this.rel, "onClick" : "$(this).parent().parent().hide(400)"})
					.text(this.dispText)
					.appendTo(btn2);
				$(subNav).append(btn2);
			});
			
		}
		$(nav2).append(btn);
		subNav = '';
	});
	
	//Menu animation
	if (animate) {
		$('#' + menuID + ' ul').css({display: "none"}); //Fix Opera
		$('#' + menuID + ' li[class="listTab"]').hover(
			function() {  
                $(this).addClass('addPosition');
				$(this).find('a').stop().animate({'width' : "185"});
				$(this).find('ul:first').css({visibility : "visible", display : "none"}).show(400);
			},
			function() {
				$(this).find('ul:first').css({visibility : "hidden"}).hide(400);
				$(this).find('a').stop().animate({'width' : "175"});
                $(this).removeClass('addPosition');
			}
		);
		// Maintain hover color for parent
		$('#' + menuID + ' li[class="listTab"] ul li').hover(
			function () {
				$(this).parent().parent().children('a').addClass('linkHover');
			},
			function () {
				$(this).parent().parent().children('a').removeClass('linkHover');
			}
		);
	}	
	$('#' + menuID + ' a')
		.click (function () {
			loadContent ($(this).attr ('rel'));
		});
	
}

function loadShows () {
	$('#sbPallete').hide();
	doQuery ('getShows');
	shows = dataObj;
	dataObj = {};
	$('#wtStage').empty().css('background-color', '#FFF')
	$('<div>').attr ('id', 'showcontent').css ('overflow-x', 'hidden').appendTo ('#wtStage');
	$(shows).each (function () {
//		if (this.close < this.today) header = "PRESENTED IN " + this.close.getMonth();
	});
	$.get('_view/html/shows.html', function(data) {
		$('#wtStage').empty().css('background-color', '#FFF').html (data);
		$('.shownavtitle').hover(
			function () { $(this).addClass ('active'); }, 
			function () { $(this).removeClass ('active'); }
		);
		$('.shownavtitle').click (function () {
			$('.shownavtitle').removeClass ('selected');
			$(this).addClass ('selected');
			swapShow ($(this).index());
		}); 
		$('.shownavtitle').eq(cur).addClass ('selected');
		$('.showimg')	.eq(cur).animate ( {'left' : 9}, 750 ).addClass('onStage');
		$('.showtext')	.eq(cur).animate ( {'right' : 9}, 750 );
		$('.showheader').eq(cur).fadeIn(600);
	});
	$('#wtStageFooter').text ('Woodford Theatre 2011-2012 Season!');
}

function loadSBGallery () {
	doQuery ('getGallery', {'dir' : gllry}, '', '');
	imgs = dataObj;
	dataObj = {};
	thisgllry = $('<ul>').attr ('id', 'gallery0').addClass ('sbGallery').appendTo ('#sbGallery');
	$.each (imgs, function (i, img) {
		//	Ignore filenames that start with "."
		if (img.indexOf ('.') != 0) {
			img.replace (' ', '%20');
			im = $('<img>').attr({
				'src' : basepath + 'user_files/images/galleries/' + gllry + '/' + img,
				'align' : 'center'
			});
			$('<li>').append (im).appendTo (thisgllry);
		}
	});
	$('#sbGallery').easySlider({
		auto: true, 
		continuous: true,
		controlsShow: false,
		prevText: "",
		nextText: "",
		pause: 5000,
		speed: 1200
	});
}

function loadGallery (elm, div, dir) {
	$('#wtStage').empty().css ('background-color', '#000');
	$('.gallery').remove();
	galDiv = $('<div>').attr ({ 'id' : elm, 'class' : 'stgGallery' }).appendTo ('#wtStage');
	dir = (!dir) ? gllry : dir;
	control = (div == "sbGallery") ? false : true;
	doQuery ('getGallery', {'dir' : dir}, '', '');
	imgs = dataObj;
	dataObj = {};
	thisgllry = $('<ul>').attr ('id', div).addClass ('gallery').appendTo (galDiv);
	$.each (imgs, function (i, img) {
		//	Ignore filenames that start with "."
		if (img.indexOf ('.') != 0) {
			img.replace (' ', '%20');
			im = $('<img>').attr({
				'src' : basepath + 'user_files/images/galleries/' + dir + '/' + img,
				'align' : 'center'
			});
			$('<li>').append (im).appendTo (thisgllry);
		}
	});
	$(galDiv).easySlider({
		auto: true, 
		continuous: true,
		controlsShow: control,
		prevText: "",
		nextText: "",
		pause: 5000,
		speed: 1200
	});
}

function loadGalleryLinks () {
	doQuery ('getGallery', {'dir' : ''}, '', '');
	gals = dataObj;
	links = $("<ul>").empty();
	$.each (gals, function (i, gal) {
		if (gal.indexOf ('.') != 0) {
			gal.replace (' ', '%20');
			link = $('<a>')
				.attr({ 'href' : "#", "onClick" : "loadGallery ('stgGallery" + i + "', 'gallery" + i + "', '" + gal + "')" })
				.css ({ "color" : "#fff" })
			 	.text(gal);
			$('<li>').append (link).appendTo (links);
		}
	});
	$('#sbPalette').empty().append (links);
}

function swapShow (idx) {
	if (idx != cur) {
		$('.showimg').eq(cur).animate ({'left' : -440}, 750, function () {
			$('.showimg')	.removeClass('onStage');
			$('.showimg').eq(idx).show().animate ( {'left' : 9}, 750 ).addClass('onStage');
			});
		$('.showtext').eq(cur).animate ({'right' : -340}, 750, function () {
			$('.showtext')	.eq(cur).removeClass('onstage');
			$('.showtext').eq(idx).show().animate ( {'right' : 9}, 750 );
			});
		$('.showheader').eq(cur).fadeOut(750, function () {
			$('.showheader').eq(idx).fadeIn(750);
			});
	}
	cur = idx;
}

function usermsg (bOK) {
	if (bOK == 1) {
		alert ("Update successful.");
	} else {
		alert ("An error has occurred ...");
	}
	wait();
}

function vldtInput (val, type) {
	switch (type) {
		case "int" :
			if (typeof (val) != 'number') {
				alert ("Please enter a number ...");
				return false;
			} else {
				return true;
			}
		break;
	}
}


function jsDate (dt, tm) {
	d = dt.split("-");
	t = (tm) ? tm.split(":") : [0, 0, 0];
	return new Date (d[0], d[1] - 1, d[2], t[0], t[1], 0, 0);
}


function formatDate (dt, tm) {
	dd = jsDate (dt, tm);
	dd = dd.toString().split (" ");
	fmt = dd[0] + ", " + dd[2] + " " + dd[1] + " " + dd[3];
	if (tm) {
		hr = (t[0] > 12) ? t[0] - 12 : t[0];
		ampm = (t[0] > 12) ? "pm" : "am";
		fmt += " at " + hr + ":" + t[1] + " " + ampm;
	}
	return fmt;
}

function wait () {
	$('#wait').toggle();
}


	
	
	
	
	
	
	
	
	
	
