$(document).ready(function() {
	/*
		SET UP IF EXISTS
	*/
	$('.ifExists').each(function() {
		if ((!$(this).attr('rel')) || ($(this).attr('rel') == '/echotools/images/spacer.gif') || $(this).attr('rel') == '0') {
			$(this).remove();
		}	
	});
	
	$('a.theatreLink').each(function() {
		if ($(this).attr('rel') == '0') {
			var link = $(this);
			link.replaceWith("" + link.text() + "");
		}	
	});
	
	
	
	
	ajaxify();
	
});


/**
 * AJAX Stuff
 */
var closeButtonString = '<div class="ajaxBoxClose"><a href="#"><img src="/images/icon_close.gif" alt="close" />close</a></div>';
 
/**
 * Do all the setup for AJAX requests/interactivity
 */
function ajaxify () {
	
	$(".icons").each(function (i) {
		
		$(this).children('div').each ( function (d) {
			/*
			// Looks better without the hover action, imho
			$(this).hover(
				function () {
					$(this).addClass("iconover");
				},
				function () {
					$(this).removeClass("iconover");
				}
			);
			*/
			if ($(this).children('a:first').attr('rel') != "schedule" && $(this).children('a:first').attr('rel')) {
				$(this).click(function () {
					$(".icons").children('div').removeClass("active").removeClass("iconover");
					
					var type = $(this).attr('class').replace(/icon_/, "").replace(/ ?iconover ?/, "");
					var emek = $(this).children('a:first').attr('rel');
					var container = $(".dynamicContentContainer").get(i);
					
					getContent(	type, emek, container );
					
					$(this).addClass("active");
					
					return false; 
			
				});
			}
		
		});
	
	});
	
}
 
/**
 * Magical function that makes the async. request for content and sticks it back into the HTML on load.
 * @param {String} type
 * @param {Number} emek
 * @param {DOMElement} container
 */ 
function getContent(type, emek, container) {
	$(container).html("<p>loading...<br /></p>"); // stick some HTML into the container to show that things are happening.
	$.get( "/includes/dynamiccontent.php?type="+type+"&emek="+emek, null, function(data){
		updatePage(data, container);
		$(container).attr('class', 'dynamicContentContainer'); // remove any added classes to avoid styling conflicts of loaded elements
		enhance(type, container);
	},"string"); 
}

/**
 * Once the content has loaded, add it to the page with the necessary extras.
 * @param string data
 * @param object container
 */
function updatePage (data, container) {
	$(container).html(closeButtonString+data);
	$(container).children(".ajaxBoxClose").click(function () {
		$(this).parent().empty()
						.attr('class', 'dynamicContentContainer');	
		return false;
	});
}

/**
 * Since the content was loaded via AJAX, sprinkle some JQuery on it to make it prettier/usabler
 * @param {String} type The type of content you want to enhance
 * @param {DOMElement} container An HTML element that contains the stuff.
 */
function enhance(type, container) {
	switch(type) {
		case "photos":
			
			if ($(container).children('.photoset').children().length) {
				$(container).addClass('ajaxPhotos');
				$(container).children(".photoset").append('<div class="largeView">&nbsp;</div>');
				$(container).children(".photoset").append('<div class="thumbBg">&nbsp;</div>')
				
				$(container).children(".photoset").children('.thumbBg').fadeTo(0, 0);
				
				$(container).children(".photoset").hover(function(){
					$(this).children('.thumbBg').fadeTo('fast', 0.5);
				}, function(){
					$(this).children('.thumbBg').fadeTo('fast', 0);
				});
				
				// setup the thumbnails
				$(container).children(".photoset").children(".one_photo").each(function(i){
					var cssObj = {
						display: "block",
						width: "32px",
						height: "32px",
						position: "absolute",
						bottom: '10px',
						left: ((i * 42) + 5) + "px"
					} // 32px + 5px initial margin + 10px recurring margin
					$(this).css(cssObj);
					$(this).click(function(){
						$(this).siblings('.one_photo').fadeTo('fast', 0.8);
						$(this).fadeTo('fast', .4).blur();
						return showFullSizeImage($(this).children('a').attr('href'), $(container).children('.photoset').children('.largeView').get(0));
					});
					if (i == 0) {
						$(this).click();
					}
				});
			} else {
				$(".ajaxBoxClose").remove();
				$(container).children('.photoset').replaceWith('<p class="nocontent"><strong>No photos available.</strong></p>');
			}
			break;
		case "listen":
			if ($(container).children('.audioTracks').children("ul").children().length) {
				$(container).addClass('ajaxAudio');
				
				$(container).children('.audioTracks').find('li').each(function () {
					
					$(this).click(function () {
					
						launchAudioPlayer(container, $(this).children('a').attr('href'), $(this).children('a').html());
					
						return false;
					
					});
					
				});
				
				$(container).children('.audioTracks').find('li:first').click();
				
			} else {
				$(".ajaxBoxClose").remove();
				$(container).children('.audioTracks').html('<p class="nocontent"><strong>No audio tracks available.</strong></p>');
			}
			break;
		case "video":
			if	($(container).children('.videoset').children().length) {
				$(container).addClass('ajaxVideo');
				
			} else {
				$(".ajaxBoxClose").remove();
				$(container).children('.videoset').replaceWith('<p class="nocontent"><strong>No videos available.</strong></p>');
			}
			break;
	}
}

/* PHOTO ENHANCEMENT FUNCTIONS */
/**
 * Receive a .one_photo image path and display the href value as the full size image
 * @param {String} path
 * @param {DOMElement} container element to receive the fullsize image
 */
function showFullSizeImage (path, container) {	
	$(container).html(makeImageTag(path));
	return false;
}

/**
 * Receive a string, use the string to create a valid IMG tag
 * @param {String} href
 */ 
function makeImageTag (href) {
	return '<img src="'+href+'" alt="" />';
}


/* AUDIO PLAYER ENHANCEMENT FUNCTIONS */

/**
 * If absent, adds an mp3 player swf. Otherwise, sets the mp3 player track to that selected by the user
 * @param {DOMElement} container
 * @param {String} mp3
 * @param {String} title
 */
function launchAudioPlayer(container, mp3, title) {
		
	if (!$(container).children('.audioPlayer').length) {
		$(".audioPlayer").remove(); // out with the old
		$(container).prepend('<div id="audioPlayer" class="audioPlayer">Loading audio player...</div><br />');
		
		var fo = new SWFObject("/swf/InlineMp3Player.swf", "inlineMp3Player", "441", "50", "9", "#000000");
		fo.addParam("wmode", "opaque");
		fo.write("audioPlayer");
		
	}
	try {
		setTimeout(function(){
			thisMovie("inlineMp3Player").updatePlayerTrack(mp3, title)
		}, 1000);
	} catch (e) {
		// suck it up, big boy.
	}
	
}
/**
 * Macromedia-provided function to access the SWF embed across all browsers
 * @param {String} movieName
 */
function thisMovie(movieName) {
     if (navigator.appName.indexOf("Microsoft") != -1) {
         return window[movieName];
     } else {
         return document[movieName];
     }
 }
