/**
 * @name STG Presents User Interface
 * @version 2010.02.01
 * @author nick herold (nick@axisweb.com)
 * @copyright 2009 - Seattle Theatre Group
 * 
 * @type jQuery
 * @requires jquery v1.3.2
 * @requires jquery.ifixpng v3.1.2 (2008.09.01)
 * @requires jquery.preload v1.0.7 (2008.03.12)
 * @requires jquery.cycle (2009.01)
 * @requires jquery.jdMenu v1.4.1
 * @requires jquery.shadowbox v2.0
 * @requires jquery.validate v1.5.1
 * @requires jquery.jPlayer v0.2.2 beta
 * @requires jquery.media v0.92 (2009.09.24) -- currently unused
 * @desc jquery functions
 */

// shadowbox setup
Shadowbox.loadSkin('classic','/_ui/shadowbox/skin');

// preload rollover imgs with load event (fires before document.onready)
$(window).bind('load', function() {
	$('#hd-logo img, #hd-info img#donate, #nav-main img, #enews img, #blog img').each(function(elm) { 
		$.preload($(this).attr("src").replace(".png", "-over.png"));
	});
});

// init jQuery
$(document).ready(function() {

	// png fix for stupidass ie6, die already
	$.ifixpng('/_ui/img/x.gif');
	$('#hd-logo img, #ft img').ifixpng();


	// carousel flash insertion - not working
	/*$('#stgCarousel .carousel').flash({
		src:'http://gamesvertising.com/stg-carousel.swf',
		id:'stgCarousel-flash',
		class:'carousel',
		width:500,
		height:200,
		wmode:'transparent',
		allowscriptaccess:'always'
	});*/


	// sponsor slides
	$('#sponsors .slide').cycle({ fx:'fade', timeout:4000 });
	// edu slides
	$('.edu .slides, .edu .slides-right').cycle({ fx:'fade', timeout:3500, random:1 });


	// externalizes outside links
	$('a[href^="//"], a[href^="http://"], a[href^="https://"], a[href$=.pdf]').attr('target','_blank');


	// left col flyout menus
	$('ul.jd_menu').jdMenu({
		showDelay:100,
		hideDelay:750,
		disableLinks:false
	});


	// rollovers
	// $('#hd-venues.index img').css({'display':'none'}).fadeIn(900);
	$('#hd-logo img:first, #hd-info img#donate, #nav-main img, #enews img, #blog img').hover(
		function() {
			$(this).attr('src', $(this).attr('src').split('.p').join('-over.p'));
		}, function() {
			$(this).attr('src', $(this).attr('src').split('-over.p').join('.p'));
	});


	// homepage artist padding removal
	$('#upcoming-container-1 div:first, #upcoming-container-2 div:first').css({ 'padding-left':'0' });
	$('#upcoming-container-1 div:last, #upcoming-container-2 div:last').css({ 'padding-right':'0' });
	$('#nav-main ul li:last').css({ 'margin-bottom':'0' });


	// showpage height toggle
	$('#ReadMore').click(function() {
		$(this).fadeOut(100);
		$('.aText').animate({ height:'100%' }, { queue:true, duration:500 }).css({ 'overflow-y':'visible' });
	});


	// Shadowbox defaults
	var sbOptions = {
		flvPlayer:'/_ui/shadowbox/flvplayer.swf',
		continuous:true,
		handleOversize:'drag',
		displayCounter:false,
		autoplayMovies:true,
		animSequence:'sync',
		overlayColor:'#000',
		overlayOpacity:0.9,
		fadeDuration:0.3,
		resizeDuration:1.0,
		initialWidth:450,
		initialHeight:300
	};
    Shadowbox.init(sbOptions);


	// form validation
	$('#enews-signup').validate({
		rules: {
			email: {
				required:true,
				email:true
			},
			first:'required',
			last:'required'
		},
		messages: {
			email:'Please enter a valid Email address',
			first:'Please enter your First Name',
			last:'Please enter your Last Name'
		}
	});
	$('#group-rsvp').validate({
		rules: {
			group_name:'required',
			performance:'required',
			date_desired:'required',
			group_type:'required',
			contact:'required',
			email: {
				required:true,
				email:true
			},
			phone_daytime:'required'
		},
		messages: {
			group_name:'missing text',
			performance:'missing text',
			date_desired:'missing date &amp; time',
			group_type:'select a group type',
			contact:'missing text',
			email:'invalid email address',
			phone_daytime:'missing number'
		}
	});


	// konami code :D
	var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
	$(document).keydown(function(e) {
		kkeys.push( e.keyCode );
		if ( kkeys.toString().indexOf( konami ) >= 0 ){
			$(document).unbind('keydown',arguments.callee);
			$.getScript('http://www.cornify.com/js/cornify.js',function(){
				cornify_add();
				$(document).click(cornify_add);
				$(document).keydown(cornify_add);
			});
		}
	});
});


/**
 * @type JavaScript
 * @requires google.feeds
 * @desc global functions
 */

// load RSS via Google AJAX Feed API
function getRSS() {
	var feedControl = new google.feeds.FeedControl();

	feedControl.addFeed("http://feeds.feedburner.com/stgpresents?format=xml");
	feedControl.setNumEntries(5);
	feedControl.setLinkTarget(google.feeds.LINK_TARGET_BLANK);
	feedControl.draw(document.getElementById("feedControl"));
}
google.setOnLoadCallback(getRSS);

/**
 * @desc rotating shoutboxes
 */
var aShoutbox1 = new makeArray(//'<img src="/img/info/tickets-dot-com.png" width="91" height="91" alt="" />',
	'<img src="/img/info/stg-affinity.png" width="91" height="91" />',
	'<img src="/img/info/faq.png" width="91" height="91" />',
	'<img src="/img/info/internships.png" width="91" height="91" />',
	'<img src="/img/info/education-sm.jpg" width="91" height="91" />',
	'<img src="/img/info/pclub-sm.png" width="91" height="91" />'
	/*'<img src="/img/info/stg-curtain-call.png" width="91" height="91" />'*/);

var aUrls1 = new makeArray(//'/info/tickets-dot-com.asp',
	'/info/stg-affinity.asp',
	'/info/',
	'/about/job-openings.asp',
	'/education/',
	'/club/'
	/*'/info/stg-curtain-call.asp'*/);

var aShoutbox2 = new makeArray('<img src="/img/info/doors.png" width="191" height="91" alt="" />');

var aUrls2 = new makeArray('/doors/');


var iShoutbox1 = randNum(aShoutbox1.length);
var iShoutbox2 = randNum(aShoutbox1.length);
var iShoutbox3 = randNum(aShoutbox2.length);

while (iShoutbox2 == iShoutbox1) {
	iShoutbox2 = randNum(aShoutbox1.length);
}

var oShoutbox1 = "<a href=\'" + aUrls1[iShoutbox1] + "\'>" + aShoutbox1[iShoutbox1] + "</a>";
var oShoutbox2 = "<a href=\'" + aUrls1[iShoutbox2] + "\'>" + aShoutbox1[iShoutbox2] + "</a>";
var oShoutbox3 = "<a href=\'" + aUrls2[iShoutbox3] + "\'>" + aShoutbox2[iShoutbox3] + "</a>";

function makeArray() {
	this.length = makeArray.arguments.length
	for (var i = 0; i < this.length; i++)
	this[i + 1] = makeArray.arguments[i]
}
function randNum(num) {
	var now = new Date();
	var rand = Math.round(num * Math.cos(now.getTime()));
	if (rand < 0) rand = - rand; if (rand == 0) rand++;
	return rand;
}