var loading_image = "_img/site/loading-bar-black.gif";
var related_crumb = new Array();
var debug = false;//true;//

/* LOGGER
*************************************************************/
function logger(message,level){
	if(level == null) level = 1;
	
	if(debug){
		if(level == 1){
			console.warn(message);
		}
		if(level == 2){
			console.info(message);
		}
		if(level == 3){
			console.debug(message);
		}
	}
}

/* NAVIGATION
*************************************************************/

var navigation = {
	_currentLength : 0, 	//length of the history object
	_currentURL : "",		//current url that is in the hashref in the url
	_default_goto : 'news', //default stage to move to after a certain time
	_default_time : 3, 		//seconds
	recent_hash : "", 		//used figure out the most recent hash
	link_timer : "",  		//figure out the most recent hash
	current_time : 0, 		//use this to figure out how much time has elapsed before a link has been clicked
	init : function(){ 		//Initialize the navigation functions
		//hijack the click event for each navigation item
		$("#navigation ul li a").each(function(){
			$(this).attr('href','#'+$(this).parent().attr('id'));		
		});
		
		$("#navigation ul li a").click(function(){
			//find the type of link that was clicked represented as type_nav so only grab the first half
			var parent_id = $(this).parent().attr('class').split("_")[0];
			
			track_link("/"+parent_id);
			
			if(parent_id != 'login'){
				//if search was clicked, stop and open the search bar, otherwise continue returning true;
				if(parent_id === "search"){
					navigation.show_search();
					return false;
				} else {
					$.history.add(parent_id);				
					return false;
				}
			}
		});
		//setup the button to close the search bar
		$("#search_close").click(function(){
			navigation.hide_search();
			return false;
		});
		
		/*
			setup the drag and drop feature of the navigation
		*/
		//setup each nav item as a droppable object using jQuery
		$("#navigation ul li a").droppable({
			accept: "#navigation div.nav_slider",
			tolerance: "touch",
			drop: function(ev, ui) {
				var parent_id = $(this).parent().attr('class').split("_")[0];
				if(parent_id != "search"){
					$.history.add(parent_id);
				} else {
					navigation.show_search();
				}
			}
		});
		
		//setup the slider as a draggable object using jQuery
		$("#navigation div.nav_slider").draggable({
			axis: 'x',
			helper: 'clone',
			opacity: 0.5
		});
		
		navigation.link_timer = setTimeout("navigation.update_stage()", 100); //setup a timer to go off in 1 millisecond to check the hash
	},
	move : function(current_hash){		//move the 'dot' to the appropriate navigation item based on its id
		if(current_hash){ //make sure to_id isn't null
			var tmp = current_hash.split('/');
			var selected_pod = "";
			if(tmp.length > 1){
				to_id = tmp[0];
				selected_pod = tmp[0] + "_" +tmp[1];
			} else {
				to_id = current_hash;
			}
			//find out a bit about the navigation element to move to
			var obj_left = parseInt($("#navigation li." + to_id + "_nav").offset().left); //get the left co-ordinate
			var mid_nav = parseInt($("#navigation li." + to_id + "_nav a").css('width')) / 2; //find its mid-point
			var offset =  parseInt($("#navigation div.nav_slider").css('width')) + (parseInt($("#navigation div.nav_slider").css('width')) / 2) - 6; //find out an amount to take off to properly place the 'dot' centered
			var move_to = obj_left + mid_nav - offset; //figure out exactly where to move the 'dot' to based on the previous calculations
				
			stage.kill_overlay(); //get rid of any overlayed content
			
			if(to_id != "search" && to_id != "login"){
				//if the navigation item that was clicked wasn't search or login, animate the movement
				$("#navigation div.nav_slider").animate({
					marginLeft : move_to,
					backgroundPosition: -90 - (obj_left - offset)
				}, {duration: 1500, easing: 'easeOutQuad'});
				//figure out the id of the stage selected based on the nav id
				var stage_id = $("#navigation li." + to_id + "_nav").attr('id').replace(/_/g, "-");
				stage.load(stage_id,selected_pod); //load 'er up
			} else {
				if(to_id == "search"){
				}
				if(to_id == "login"){
				}
			}
		}
	},
	show_search: function(){  //open the search bar
		stage.kill_overlay(); //get rid of any overlayed content
		
		$("#search_bar").slideDown({duration: 250, easing: 'easeOutQuad'});
		if(!($.browser.msie && $.browser.version.number == 6)){
			$("#stage").animate({
				paddingTop : 170
			}, {duration: 250, easing: 'easeOutQuad'});
		}
	},
	hide_search: function(){ // close the search bar
		stage.kill_overlay(); //get rid of any overlayed content
		
		$("#search_bar").slideUp({duration: 250, easing: 'easeOutQuad'});
		if(!($.browser.msie && $.browser.version.number == 6)){
			$("#stage").animate({
				paddingTop : 125
			}, {duration: 250, easing: 'easeOutQuad'});
		}
	},
	update_stage: function(){
		if($.history.getCurrent().length>0){ //check if the current history item has anything in it
			if($.history.getCurrent() != navigation.recent_hash){
				navigation.recent_hash = $.history.getCurrent(); //get the new hash
				navigation.move(navigation.recent_hash);//move the navigation to the right nav item
			}
		} else {
			//if no link has been clicked, the hash is empty to check to see if we need to go to the default item
			if(navigation.current_time >= (navigation._default_time * 1000)){
				$.history.add(navigation._default_goto);//location.href+="#"+navigation._default_goto;
			}
		}
		navigation.current_time += 100;
		navigation.link_timer = self.setTimeout("navigation.update_stage()", 100);
	}
}

/* STAGE
*************************************************************/
var stage = {
	current_browser_width : 0,
	selected_stage : "",
	index_contents : "",
	stage_contents : "",
	has_index : false,
	total_selected_stage_pods : 0,
	stage_pod_meta : Array(),
	selected_stage_pods : Array(),
	load : function(stage_id, selected_pod){
		stage.kill_overlay();
		$("#stage").fadeOut(250, function(){
			stage.total_selected_stage_pods = 0;
			stage.selected_stage_pods = new Array();
			stage.stage_pod_meta = new Array();
			if(stage_id == "home"){
				$("body").animate({
					backgroundColor:  "#FFFFFF"
				},600);
				
				$("#stage").html('<div id="cag_logo"><img src="/_img/cag_logo.gif" width="405" height="45" alt="Contemporary Art Gallery" /></div>').fadeIn(250);
				
			} else {
				stage.show_loading();
				$.getJSON("/_views/stages/" + stage_id + ".json", function(data){
					stage.selected_stage = data;
					stage.stage_contents = "";
					var stage_pods = stage.selected_stage.pods;
					stage.total_selected_stage_pods = 0;
					
					$("body").animate({
						backgroundColor:  "#CCCCCC"
					},600);
					
					stage.kill_overlay();
					
					//add an index pod if one is defined in the JSON
					if(stage.selected_stage.index_pod.length > 0){
						if(stage.selected_stage.name == 'Learning'){
							stage.index_contents = stage.build_resource_index(stage.selected_stage.index_pod);
						} else {
							stage.index_contents = stage.build_index(stage.selected_stage.index_pod);
						}
						stage.has_index = true;
					} else {
						stage.has_index = false;
					}
					
					$("#stage").empty();	
					$.each(stage_pods, function(i,pod){
						if(pod.id.length > 0){
							stage.stage_pod_meta.push({"id":pod.id,"size":pod.size,"type":pod.type,"order":stage.total_selected_stage_pods});
							stage.total_selected_stage_pods +=1;
							stage.build(pod);
						}
					});
					
					setTimeout("stage.check_pod_status('" + selected_pod + "')", 100);

				});
			}
		});
	},
	kill_overlay: function(){ // kill any overlay content if it exists
		$("#pod_active").dialog("close"); //make sure there isn't a pod overlayed
		$("#pod_active").remove(); //remove any pod that is overlayed
		$("div.ui-dialog-overlay").remove();
		$("div.ui-dialog").remove();
		
	},	
	show_loading: function(){
		stage.kill_overlay();
	},
	check_pod_status: function(selected_pod){
		if(stage.total_selected_stage_pods <= stage.selected_stage_pods.length){
			stage.kill_overlay();
			stage.place_pods(selected_pod);
		} else {
			setTimeout("stage.check_pod_status('" + selected_pod + "')", 100);
		}
	},
	place_pods : function(selected_pod){
		
		stage.kill_overlay();
		
		//add regular pods
		column_width = parseInt($("#navigation").width()) - 30;
		small_pod = 176;
		large_pod = 356;
		xtralarge_pod = 708;
		if(column_width < xtralarge_pod){
			column_width = xtralarge_pod;
		}
		logger("Initial Column Width: " + column_width);
		stage.stage_contents = "<div id='stage_pods' style='float: left;width: " + column_width + "px;'>";
		if(stage.has_index){
				if(stage.selected_stage.name == 'Learning'){
					column_width = column_width - large_pod - 30;
				} else {
					column_width = column_width - small_pod - 30;
				}
			if(column_width <= xtralarge_pod){
				if(stage.selected_stage.name == 'Learning'){
					column_width = xtralarge_pod + large_pod + 30;
				} else {
					column_width = xtralarge_pod + small_pod + 30;
				}
			}
			stage.stage_contents += stage.index_contents;
			logger("Has Index, updated column width: " + column_width);
		}
		
		stage.stage_contents += "<div id='main_pod_column' class='pod_column' style='width:" + column_width + "px;'>";
		$("#stage").empty();
		var current_column = 0;
		var current_row = 1;
		var current_pod = 1;
		var current_large_pod = 1;
		pod_columns = new Array();
		row_width = 0;
		logger("Num pods: " + stage.selected_stage_pods.length);
		tmp_count = 0;
		$.each(stage.selected_stage_pods, function(i,pod){
			if(pod){
				pod_size = "";
				if(pod.indexOf('small"') > 0){
					row_width += small_pod;
					pod_size = 'small';
				} else if(pod.indexOf('xtra-large"') > 0){
					row_width += xtralarge_pod;
					pod_size = 'xtra-large';
				} else {
					row_width += large_pod;
					pod_size = 'large';
				}
				if(row_width >= column_width){
					row_width = 0;
					current_column = 0;
					current_row++;
				}
				logger("Number of Columns: " + pod_columns.length  + " Current Column: " + current_column + " Current Row: " + current_row);
				if(pod_columns.length <= current_column){
					if(pod_columns.length < 0){
						current_column == 0;
					} else {
						current_column = pod_columns.length;
					}
				}
				if(current_row == 1){
					pod_columns[current_column] = { 'size': pod_size, 'pods' : Array()};
					pod_columns[current_column].pods.push({ 'size': pod_size, 'contents':pod});
					
					current_column++;
				} else {
					if(pod_columns[current_column].size == 'xtra-large' && pod_size =='large'){
						pod_columns[current_column].pods.push({ 'size': pod_size, 'contents':pod});
						
						current_large_pod++;
						current_pod+=2;
						if(current_large_pod > 2){
							current_large_pod=1;
							current_pod=1;
							current_column++;
						}
					} else if(pod_columns[current_column].size == 'xtra-large' && pod_size =='small'){
						pod_columns[current_column].pods.push({ 'size': pod_size, 'contents':pod});
						current_pod++;
						if(current_pod > 4){
							current_large_pod=1;
							current_pod=1;
							current_column++;
						} else if (current_pod == 2){
							current_large_pod++;
						}
					} else if(pod_columns[current_column].size == 'large' && pod_size =='small'){
						pod_columns[current_column].pods.push({ 'size': pod_size, 'contents':pod});
						current_pod++;
						if(current_pod > 2){
							current_pod=1;
							current_column++;
						}
					} else if(pod_columns[current_column].size == 'small' && (pod_size =='large' || pod_size =='xtra-large')){
						if(current_column+1 < pod_columns.length){
							if(pod_columns[current_column+1].size == 'small'){
								logger('Large Pod in Small Column:' + pod_columns[current_column].size);
								more_pods = (pod_columns[current_column].pods.length > pod_columns[current_column+1].pods.length) ? pod_columns[current_column].pods.length : pod_columns[current_column+1].pods.length;
								new_pods = Array();
								for(p=0;p<more_pods;p++){
									if(pod_columns[current_column].pods.length >p){
										new_pods.push(pod_columns[current_column].pods[p]);
									}
									if(pod_columns[current_column+1].pods.length >p){
										new_pods.push(pod_columns[current_column+1].pods[p]);
									}
								}
								logger('Columns: ' + pod_columns.length);
								pod_columns.splice(current_column, 1);
								logger('After Slice Columns: ' + pod_columns.length);
								new_pods.push({ 'size': pod_size, 'contents':pod});
								pod_columns[current_column].size = pod_size;
								pod_columns[current_column].pods = new_pods;
							} else if(pod_columns[current_column+1].size == 'large' || pod_columns[current_column+1].size == 'xtralarge'){
								current_column++;
								pod_columns[current_column].pods.push({ 'size': pod_size, 'contents':pod});
							}
						} else {
							row_width = 0;
							current_column = 0;
							current_row++;
							pod_columns[current_column].pods.push({ 'size': pod_size, 'contents':pod});
						}
						
						current_column++;
					} else {
						pod_columns[current_column].pods.push({ 'size': pod_size, 'contents':pod});
						current_column++;
					}
					if(current_column >= pod_columns.length){
						row_width = 0;
						current_column = 0;
						current_row++;
					}
				}
			}
		});
		
		for(x=0;x<pod_columns.length;x++){
			stage.stage_contents += "<div style='float: left;'>";
			logger("Pods: " + pod_columns[x].pods.length + ' in a ' +pod_columns[x].size+' column.',2);
			num_small = 1;
			num_large = 1;
			for(y=0; y<pod_columns[x].pods.length;y++){
				
				if(pod_columns[x].size == pod_columns[x].pods[y].size){			
					num_small = 1;
					logger("Pod is same size as column.");
					stage.stage_contents += pod_columns[x].pods[y].contents;
					stage.stage_contents += "<br class='clear' />";
				} else if(pod_columns[x].size =='xtra-large' && pod_columns[x].pods[y].size =='large'){
					logger("Pod is small, column is large.");
					stage.stage_contents += pod_columns[x].pods[y].contents;
					if(num_large == 2){
						num_small = 1;
						num_large = 1;
						stage.stage_contents += "<br class='clear' />";
					} else{
						num_large++;
						num_small+=2;
					}
				} else if(pod_columns[x].size =='xtra-large' && pod_columns[x].pods[y].size =='small'){
					logger("Pod is small, column is large.");
					stage.stage_contents += pod_columns[x].pods[y].contents;
					if(num_small == 4){
						num_large = 1;
						num_small = 1;
						stage.stage_contents += "<br class='clear' />";
					} else if(num_small == 2){
						num_large++;
						num_small++;
					} else{
						num_small++;
					}
				} else if(pod_columns[x].size =='large' && pod_columns[x].pods[y].size =='small'){
					logger("Pod is small, column is large.");
					stage.stage_contents += pod_columns[x].pods[y].contents;
					if(num_small == 2){
						num_small = 1;
						stage.stage_contents += "<br class='clear' />";
					} else{
						num_small++
					}
				}
			}
			stage.stage_contents += "</div>";
		}
		stage.stage_contents += "</div><br class='clear' /></div>";
		
		
		$("#stage").html(stage.stage_contents).fadeIn(250, function(){
			pods.init();
			if(selected_pod){
				pods.select(selected_pod);
			}
		});
	},
	debug: function(){
		/*
		if($("body #msg").length <= 0){
			$("body").append("<p id='msg' style='position: absolute; top: 200px; left: 20px; border: 1px solid #000; background: #fff; font-size: 12px; padding: 10px; float: left;'></p>");
		}
		$("#msg").ajaxError(function(event, request, settings){
		   $(this).append("<li>Error requesting page " + settings.url + "</li>");
		});*/
	},
	build : function(pod){
		var stage_name = stage.selected_stage.name;
		var pod_string = "";
		var podID = pod.id.replace("//-/", "_");
		var tmp_image_size = "144";
		var image_size = "144";
		var youtube_base_dimensions = new Array(560, 349);
		var youtube_set = false;
		
		var youtube_dimensions = new Array(image_size,Math.round((image_size / youtube_base_dimensions[0]) * youtube_base_dimensions[1]) - 9);
		if(pod.size == 'large'){
			tmp_image_size = "320";
			image_size = "320";	
			youtube_dimensions = new Array(image_size,Math.round((image_size / youtube_base_dimensions[0]) * youtube_base_dimensions[1]) - 18);
		} else if(pod.size == 'xtra-large'){
			tmp_image_size = "672";
			image_size = "672";	
			youtube_dimensions = new Array(image_size,Math.round((image_size / youtube_base_dimensions[0]) * youtube_base_dimensions[1]) - 40);
		}
		if(debug){
			stage.debug();
		}
		
		$.getJSON("/_views/pods/" + podID + ".json", function(data){
			logger("Loaded:" + podID);
			current_pod = data;
			pod = "";
			for(x=0;x<stage.stage_pod_meta.length;x++){
				if(stage.stage_pod_meta[x].id == current_pod.id){
					pod = stage.stage_pod_meta[x];
				}
			}
			if(pod){
				podID = pod.id.replace("//-/", "_");
				if(pod.type=="generic" || pod.type=='news'){		
					pod_string += '<div id="' + stage_name + '_' + podID + '" class="pod_text ' + pod.size + '">';
					if(current_pod.images.length > 0){
						if(current_pod.images[0].src.length > 0){
							pod_image = "/_lib/thumb/phpThumb.php?w="+ tmp_image_size+"&q=90&iar=1&src=/_content/uploads/" + current_pod.images[0].id + "/" + current_pod.images[0].src;
							pod_string += '<div class="pod_image"><img src="' + pod_image + '" alt="' + current_pod.title + '" /></div>';
						}
					}
					pod_string += '<div class="pod_title">' + current_pod.title + '</div><div class="pod_content">' + current_pod.description + '</div></div>';
				} else {
					pod_string += '<div id="' + stage_name + '_' + podID + '" class="pod_shell ' + pod.size + '">';
					if(current_pod.youtube_id){
						if(current_pod.youtube_id.length > 0){
							var youtube_set = true;
							pod_string += '<div class="pod_image"><iframe width="' + youtube_dimensions[0] + '" height="' + youtube_dimensions[1] + '" src="http://www.youtube.com/embed/' + current_pod.youtube_id + '?version=3&autohide=1&showinfo=0" frameborder="0" allowfullscreen></iframe></div>';
						}
					} 
					if(!youtube_set) {
						if(current_pod.images.length > 0) {
							if(current_pod.images[0].src.length > 0){
								pod_image = "/_lib/thumb/phpThumb.php?w="+ tmp_image_size+"&q=90&iar=1&src=/_content/uploads/" + current_pod.images[0].id + "/" + current_pod.images[0].src;
							} else {
								pod_image = "/_img/pod/" + stage_name + "_" + pod.size + '.jpg';
							}
						} else {
							pod_image = "/_img/pod/" + stage_name + "_" + pod.size + '.jpg';
						}
						pod_string += '<div class="pod_image"><img src="' + pod_image + '" alt="' + current_pod.title + '" /></div>';
					}
					pod_string += '<div class="pod_title">' + current_pod.title + '</div><div class="pod_content">' + current_pod.summary + '</div></div>';
				}
				stage.selected_stage_pods[parseInt(pod.order)] = pod_string;
			}
		});
	},
	build_index : function(pod){
		var index_pod = '<div class="pod_column index">';
		var index_pod_content = '';
		index_pod += '<div id="' + stage.selected_stage.name.toLowerCase() + 'indexpod" class="pod_index">';
		index_pod += '<div class="pod_title">' + stage.selected_stage.name + ' Index:<br /><form action="#">';
		index_pod += '<select id="indexselector" onchange="onChangeIndex(\'' + stage.selected_stage.name + '\');">';
		index_pod += '<option value="-">Search By:</option>';
		var index_count = 0;
		$.each(pod, function(i,index){
			index_pod += "<option value='" + stage.selected_stage.name.toLowerCase() + "index_" + index.option.toLowerCase() + "'>" + index.option + "</option>";
			index_pod_content += "<ul class='sliding_menu " + stage.selected_stage.name.toLowerCase() + "indexpod' id='" + stage.selected_stage.name.toLowerCase() + "index_" + index.option.toLowerCase() + "' style='display:none;'>";
			
			index_count++;
			$.each(index.contents, function(i,content){
				if(content)	{				
					index_pod_content += "<li id='" + index.option.toLowerCase() + "_" + content.heading.toLowerCase() + "'><div class='title'>" + content.heading + "</div><div class='content'>";
					$.each(content.values, function(i,value){
						if(value){
							if(index.option.toLowerCase() == 'learning'){
								index_pod_content += "<a href='#' onclick='pods.select_lessonplan(\"" + stage.selected_stage.name.toLowerCase() + "_" + value.id.replace("//-/","_") + "\");return false;' id='" + stage.selected_stage.name.toLowerCase() + "_" + value.id.replace("//-/","_") + "'>" + value.title + "</a>";
							} else {
								index_pod_content += "<a href='#' onclick='pods.select(\"" + stage.selected_stage.name.toLowerCase() + "_" + value.id.replace("//-/","_") + "\");return false;' id='" + stage.selected_stage.name.toLowerCase() + "_" + value.id.replace("//-/","_") + "'>" + value.title + "</a>";
							}
						}
					});
					index_pod_content += "</div></li>";
				}
			});
			index_pod_content += "</ul>";
		});
		
		index_pod += '</select></form></div>';
		index_pod += index_pod_content;
		index_pod += '</div></div>';
		
		return index_pod;
	},
	build_resource_index : function(pod){
		var index_pod = '<div class="pod_column index large">';
		var index_pod_content = '';
		index_pod += '<div id="' + stage.selected_stage.name.toLowerCase() + 'indexpod" class="pod_index large">';
		index_pod += '<div class="pod_title">Learning Resources</div>';
		index_pod += '<div class="pod_content">';
		index_pod += '<p>Are you a teacher looking to further educate your class about one of our exhibitions? Or, maybe you are planning a field trip and would like some further guidance.</p>';
		index_pod += '<p>Teachers&rsquo; Guides support educators who wish to visit the CAG with their students or who wish to carry out lessons related to CAG exhibitions in their classrooms. They include artist biographies, thematic exhibition overviews, suggested points of discussion, as well as recommended readings and references.</p>';
		index_pod += '<p>Lesson Plans are designed to bring the resources of contemporary art and artists to diverse classrooms. It is our goal to accustom students of all ages to viewing contemporary art and to show that it can be used as a springboard in teaching a variety of subjects. Please feel free to adapt lessons to suit the specific needs of your class and curriculum.</p>';
		index_pod += '<p>Select an exhibition for related Teachers&rsquo; Guides and Lesson Plans:</p>';
		index_pod += '<form action="#">';
		index_pod += '<select name="exhibtion_index" id="exhibition_index" onchange="pods.select($(\'#exhibition_index option:selected\').val());">';
		index_pod += "<option value=''>Select an Exhibition</option>";
		var exhibition_listing = stage.selected_stage.index_pod[0].contents;
		$.each(exhibition_listing, function(i,content){
			if(content){
				$.each(content.values, function(i,value){
					if(value){
						index_pod += "<option value='" + stage.selected_stage.name.toLowerCase() + "_" + value.id.replace("//-/","_") + "'>" + value.title + "</option>";
					}
				});
			}
		});
		index_pod += '</select>';
		index_pod += "</form><br class='clear' />";
		
		index_pod += '<p>Search directly for Lesson Plans by subject:</p>';
		index_pod += '<form action="#">';
		index_pod += '<select name="subject_index" id="subject_index" onchange="pods.select($(\'#subject_index option:selected\').val());">';
		index_pod += "<option value=''>Select a Subject</option>";
		
		var subject_listing = stage.selected_stage.index_pod[1].contents;
		$.each(subject_listing, function(i,content){
			if(content){
				index_pod += "<optgroup label='" + content.heading + "'>";
				$.each(content.values, function(i,value){
					if(value){
						index_pod += "<option value='" + stage.selected_stage.name.toLowerCase() + "_" + value.id.replace("//-/","_") + "'>" + value.title + "</option>";
					}
				});
				index_pod += "</optgroup>";
			}
		});
		index_pod += '</select>';
		index_pod += '</form></div>';
		index_pod += index_pod_content;
		index_pod += '</div></div>';
		
		return index_pod;
	},
	build_huge : function(pod){
		var youtube_base_dimensions = new Array(560, 349);
		var podID = pod.id;	
		var youtube_set = false;		
		var huge_pod = '<div class="large_pod">'; //initialize the huge_pod variable
		
		// setup the related container and insert the related pods if there are any
		related_content = "";
		if(pod.related_pods.length > 0){
			related_content = '<div class="related_content"><h3>Related</h3><div class="related_container">';
			$.each(pod.related_pods, function(i,related){
				related_content += '<div class="pod_item">';
				relatedID = related.id.replace('//-/','_');
				related_type = related.title.split(':');
				$.getJSON("/_views/pods/" + related.id + ".json", function(data){});
				if(related.feature_image.length > 0){
					img_src = "/_lib/thumb/phpThumb.php?h=70&q=90&iar=1&src=/_content/uploads/" + related.feature_image;
				} else {
					img_src = "/_lib/thumb/phpThumb.php?h=70&q=90&iar=1&src=/_img/pod/" + related_type[0].toLowerCase() + "_small.jpg";
				}
				related_content += '<a href="#" onclick="pods.swap(\'' + related_type[0].toLowerCase().replace(' ','') + '_' + relatedID + '\');return false;" id="document_' + relatedID + '"><img src="' + img_src + '"  title="'+ related.title + '" alt="'+ related.title + '" height="70" /></a>';
				related_content += '</div>';
			});
			related_content += '</div></div>';
		}
		
		//add the related content generated above to the huge pod
		//huge_pod += related_content;
		
		//setup the pod content
		pod_content = '<div class="pod_content">';//add the container
		pod_content += '<h2>' + pod.title + '</h2>';//add in the title
		
		//google tracking
		track_link("/" + pod.type.toLowerCase() + "/" + pod.title);
		
		if(pod.artists.length > 0){
			pod_content += '<p>';
			current_artist = 0;
			$.each(pod.artists, function(i,artist){
										 
				$.getJSON("/_views/pods/" + artist.id + ".json", function(data){});
				pod_content += '<a href="#" onclick=\'pods.swap("artist_' + artist.id + '");return false;\'>' + artist.name + '</a>';
				current_artist ++;
				if(current_artist < pod.artists.length){
					pod_content += '<br />';
				}
			});
			pod_content += '</p>';
		}	
		//setup the dates
		if(pod.dates != "" && pod.dates != "{}"){
			pod_content += '<p>' + pod.dates  + '</p>';
		}
		pod_content += '<div id="large_pod_content"><div class="content_container">';
		pod_content += pod.description;
		pod_content += '</div></div>';
		pod_content += '</div>';//close the container	
		
		//add content to the huge pod
		huge_pod += pod_content;
		
		//setup the pod_media
		pod_media = '<div class="pod_media"><div class="pod_feature" id="featured_media">';
		if(pod.youtube_id){
			if(pod.youtube_id.length > 0){
				youtube_set = true;
				youtube_dimensions = new Array(550,Math.round((550 / youtube_base_dimensions[0]) * youtube_base_dimensions[1]) - 33);
				pod_media += '<iframe width="' + youtube_dimensions[0] + '" height="' + youtube_dimensions[1] + '" src="http://www.youtube.com/embed/' + pod.youtube_id + '?version=3&autohide=1&showinfo=0" frameborder="0" allowfullscreen></iframe>';
			}
		} 
		if(!youtube_set){
			if(pod.url.length > 0){
				img_src = "/_lib/thumb/phpThumb.php?w=550&h=360&far=L&bg=FFFFFF&q=90&src=/_content/uploads/" +  pod.images[0].id + "/" + pod.images[0].src;
				media_url = "/_content/uploads/"+pod.id+"/" + pod.url;
				pod_media += "<a href='" + media_url + "'>&nbsp;</a>";

			} else if(pod.images.length > 0){
				img_src = "/_lib/thumb/phpThumb.php?w=550&h=360&far=L&bg=FFFFFF&q=90&src=/_content/uploads/" +  pod.images[0].id + "/" + pod.images[0].src;
				pod_media += "<img src='" + img_src + "' height='360' alt='" + pod.images[0].tombstone + "' />";
			}
		}
		pod_media += '</div>';
				
		// Setup in the media strip and tombstones, and add the images if there are more than one
		tombstones = '<div class="pod_tombstones">';	
		strip_container = '<div class="pod_strip_container">';
		if(pod.images.length > 1){
			strip_container += '<div class="pod_media_strip">';
			current_image = 1;			
			$.each(pod.images, function(i,image){
				if(image){
					imgID = image.id.replace("//-/", "_");
					strip_container += "<div class='pod_item";
					tombstones += '<div class="tombstone" id="tombstone_' + imgID + '" style="display: '; 
					if(current_image == 1){
						strip_container += " active";				
						tombstones += 'block;">';
					} else {
						tombstones += 'none;">';
					}
					strip_container += "'>";
					tombstones += image.tombstone + "</div>";
					img_lnk = "/_lib/thumb/phpThumb.php?w=550&h=360&far=L&bg=FFFFFF&q=90&src=/_content/uploads/" + image.id;
					img_src = "/_lib/thumb/phpThumb.php?h=70&iar=1&q=90&src=/_content/uploads/" + image.id;
					strip_container += '<a href="' + img_lnk + '/' + image.src + '" id="thumb_' + imgID + '"><img src="' + img_src + '/' + image.src + '" height="70" alt="' + image.tombstone + '"/></a>';
					strip_container += "</div>";
					current_image++;
				}
			});
			
			strip_container += '</div>';
		} else if(pod.images.length == 1) {
			image = pod.images[0];
				if(image){
					imgID = image.id.replace("//-/", "_");
					tombstones += '<div class="tombstone" id="tombstone_' + imgID + '" style="display: block;">';
					tombstones += image.tombstone + "</div>";
				}			
		}
		strip_container += '</div><div class="media_slider" id="media_slider"><div class="slide_container"><div class="ui-slider-handle"></div></div></div>';
		tombstones += '</div>';
		
		pod_media += tombstones;
		pod_media += strip_container;
		
		//add the related content generated above to the huge pod
		pod_media += related_content;
		
		pod_media += "</div>";
	
		huge_pod += pod_media + '</div>';

		return huge_pod;
	},
	build_resource : function(pod){
		var podID = pod.id;															  
		var huge_pod = '<div class="large_pod resource">'; //initialize the huge_pod variable
		
		// setup the related container and insert the related pods if there are any
		related_content = '<div class="related_content"><div class="related_container">';
		if(pod.related_pods.length > 0){
			$.each(pod.related_pods, function(i,related){
				related_content += '<div class="pod_item">';
				relatedID = related.id.replace('//-/','_');
				related_type = related.title.split(':');
				$.getJSON("/_views/pods/" + related.id + ".json", function(data){});
				if(related.feature_image.length > 0){
					img_src = "/_lib/thumb/phpThumb.php?h=70&q=90&iar=1&src=/_content/uploads/" + related.feature_image;
				} else {
					img_src = "/_lib/thumb/phpThumb.php?h=70&q=90&iar=1&src=/_img/pod/" + related_type[0].toLowerCase() + "_small.jpg";
				}
				related_content += '<a href="#" onclick="pods.swap(\'' + related_type[0].toLowerCase().replace(' ','') + '_' + relatedID + '\');return false;" id="document_' + relatedID + '"><img src="' + img_src + '"  title="'+ related.title + '" alt="'+ related.title + '" height="70" /></a>';
				related_content += '</div>';
			});
		}
		related_content += '</div></div>';
		
		//add the related content generated above to the huge pod
		huge_pod += related_content;
		
		//setup the pod content
		pod_content = '<div class="pod_content">';//add the container
		pod_content += '<h2>Learning Resources</h2>';
		pod_content += '<div id="large_pod_content"><div class="content_container">';
		if(pod.images.length > 0){
			img_src = "/_lib/thumb/phpThumb.php?w=180&iar=1&q=90&src=/_content/uploads/" +  pod.images[0].id + "/" + pod.images[0].src;
			pod_content += "<img src='" + img_src + "' alt='" + pod.title + "' />";
		}
		pod_content += '<h3>Exhibition</h3>';//add in the exhibition title
		pod_content += pod.title;
		pod_content += '<h3>Summary</h3>';//add in the summary
		pod_content += pod.description;
		pod_content += "<p><a href='#' onclick=\"pods.swap('exhibition_" + pod.exhibition_id + "'); return false;\">View Exhibition</a></p>";
		pod_content += '</div></div>';
		pod_content += '</div>';//close the container	
		
		//add content to the huge pod
		huge_pod += pod_content;
		
		//setup the pod_media
		var pod_education = '<div class="pod_education"><div id="education_resources">';
		
		pod_education += '<h3>Teachers&rsquo; Guide:</h3>';
		pod_education += '<p>Teachers&rsquo; Guides support educators who wish to visit the CAG with their students or who wish to carry out lessons related to CAG exhibitions in their classrooms. They include artist biographies, thematic exhibition overviews, suggested points of discussion, as well as recommended readings and references.</p>';
		if(pod.teachers_guide.length > 0){
			pod_education += '<p class="links">';
			$.each(pod.teachers_guide, function(i,teachers_guide){
				pod_education += '<a href="/_content/uploads/'+teachers_guide.id+'/'+teachers_guide.url.replace("'","\\'")+'" onclick="track_link(this.href);">'+teachers_guide.title+'</a>';
			});
			pod_education += '</p>';
		}
		
		pod_education += '<br /><br /><br /><br /><h3>Lesson Plans:</h3>';
		pod_education += '<p>CAG lesson plans are designed to bring the resources of contemporary art and artists to diverse classrooms. It is our goal to accustom students of all ages to viewing contemporary art and to show that it can be used as a springboard in teaching a variety of subjects. Please feel free to adapt lessons to suit the specific needs of your class and curriculum.</p>';
		if(pod.teachers_guide.length > 0){
			pod_education += '<p class="links" id="lessonplan_links">';
			$.each(pod.lesson_plans, function(i,lessonplan){
				pod_education += '<a href="/_content/uploads/'+lessonplan.id+'/'+lessonplan.url.replace("'","\\'")+'" onclick="lesson_plan.init(\'' + lessonplan.id + '\', \''+lessonplan.url+'\'); return false;" id="plan_' + lessonplan.id + '">'+lessonplan.title+'</a>';
			});
			pod_education += '</p>';
		}
		
		pod_education += '</div>';
		
		pod_education += '<div id="educational_form" style="display: none;">';
		pod_education += '<form name="edu_form" id="lessonplan_form">';
		pod_education += '<h3>Download Lesson Plan:</h3>';
		pod_education += '<p>The Contemporary Art Gallery gathers the names and contact information of educators or persons using our education resource documents for internal statistical purposes only. Your name and email address is confidential and will not be made available to the public in any way.</p>';
		pod_education += '<p><label>Name:</label><input type="text" name="txt_name" id="full_name" value="" /></p>';
		pod_education += '<p><label>Email:</label><input type="text" name="txt_email" id="email_address" value="" /></p>';
		pod_education += '<p><label>&nbsp;</label><a href="#" id="resource_submit"> SUBMIT </a></p>';
		pod_education += '</form>';
		pod_education += '</div>';
		
		pod_education += '</div>';
	
		huge_pod += pod_education + '</div>';
		
		return huge_pod;
	},
	build_lessonplan : function(pod){
		var podID = pod.images[0].id;
		var huge_pod = '<div class="large_pod">'; //initialize the huge_pod variable
		
		// setup the related container and insert the related pods if there are any
		related_content = '<div class="related_content"><div class="related_container">';
		if(pod.related_pods.length > 0){
			$.each(pod.related_pods, function(i,related){
				related_content += '<div class="pod_item">';
				relatedID = related.id.replace('//-/','_');
				related_type = related.title.split(':');
				$.getJSON("/_views/pods/" + related.id + ".json", function(data){});
				img_src = "/_lib/thumb/phpThumb.php?h=70&q=90&iar=1&src=/_content/uploads/" + related.feature_image;
				related_content += '<a href="#" onclick="pods.swap(\'' + related_type[0].toLowerCase().replace(' ','') + '_' + relatedID + '\');return false;" id="document_' + relatedID + '"><img src="' + img_src + '"  title="'+ related.title + '" alt="'+ related.title + '" height="70" /></a>';
				related_content += '</div>';
			});
		}
		related_content += '</div></div>';
		
		//add the related content generated above to the huge pod
		huge_pod += related_content;
		
		//setup the pod content
		pod_content = '<div class="pod_content">';//add the container
		pod_content += '<h2>' + pod.title + '</h2>';//add in the title
		//tracking code
		track_link("/education/educational_resource/"+pod.title);
			
		if(pod.artists.length > 0){
			pod_content += '<p>';
			current_artist = 0;
			$.each(pod.artists, function(i,artist){
										 
				$.getJSON("/_views/pods/" + artist.id + ".json", function(data){});
				pod_content += '<a href="#" onclick=\'pods.swap("artist_' + artist.id + '");return false;\'>' + artist.name + '</a>';
				current_artist ++;
				if(current_artist < pod.artists.length){
					pod_content += '<br />';
				}
			});
			pod_content += '</p>';
		}	
		//setup the dates
		if(pod.dates != "" && pod.dates != "{}"){
			pod_content += '<p>' + pod.dates  + '</p>';
		}
		pod_content += '<div id="large_pod_content"><div class="content_container">';
		pod_content += pod.description;
		pod_content += '</div></div>';
		pod_content += '</div>';//close the container	
		
		//add content to the huge pod
		huge_pod += pod_content;
		
		//setup the pod_media
		pod_media = '<div class="pod_media"><div class="pod_feature" id="featured_media">';
		if(pod.images.length > 0){
			img_src = "/_lib/thumb/phpThumb.php?w=550&h=360&far=L&bg=FFFFFF&q=90&src=/_content/uploads/" +  pod.images[0].id + "/" + pod.images[0].src;
			pod_media += "<img src='" + img_src + "' height='360' alt='" + pod.images[0].tombstone + "' />";
		}
		pod_media += '</div>';
				
		// Setup in the media strip and tombstones, and add the images if there are more than one
		tombstones = '<div class="pod_tombstones">';	
		strip_container = '<div class="pod_strip_container">';
		if(pod.images.length > 1){
			strip_container += '<div class="pod_media_strip">';
			current_image = 1;
			$.each(pod.images, function(i,image){
				imgID = image.id.replace("//-/", "_");
				strip_container += "<div class='pod_item";
				tombstones += '<div class="tombstone" id="tombstone_' + imgID + '" style="display: '; 
				if(current_image == 1){
					strip_container += " active";				
					tombstones += 'block;">';
				} else {
					tombstones += 'none;">';
				}
				strip_container += "'>";
				tombstones += image.tombstone + "</div>";
				img_lnk = "/_lib/thumb/phpThumb.php?w=550&h=360&far=L&bg=FFFFFF&q=90&src=/_content/uploads/" + image.id;
				img_src = "/_lib/thumb/phpThumb.php?h=70&iar=1&q=90&src=/_content/uploads/" + image.id;
				strip_container += '<a href="' + img_lnk + '/' + image.src + '" id="thumb_' + imgID + '"><img src="' + img_src + '/' + image.src + '" height="70" alt="' + image.tombstone + '"/></a>';
				strip_container += "</div>";
				current_image++;
			});
			strip_container += '</div>';
		}
		strip_container += '</div><div class="media_slider" id="media_slider"><div class="slide_container"><div class="ui-slider-handle"></div></div></div>';
		tombstones += '</div>';
		
		pod_media += tombstones;
		pod_media += strip_container + "</div>";
	
		huge_pod += pod_media + '</div>';

		return huge_pod;
	}
}

/* PODS
*************************************************************/
var pods = {
	strip_width : 0,
	init : function(){
		$("#stage div.pod_shell").bind("mouseenter",function(){
			$(this).children("div.pod_title").addClass("active");
			$(this).children("div.pod_content").slideDown(250);
		}).bind("mouseleave",function(){
			$(this).children("div.pod_title").removeClass("active");
			$(this).children("div.pod_content").slideUp(250);
		}).click(function(){
			/* removed due to redundant fade of stage
			var selected = $(this).attr('id').toLowerCase().split("_");
			var new_url = selected[0] + "/" + selected[1];
			$.history.add(new_url);
			navigation.recent_hash = new_url;
			*/
			pods.select($(this).attr('id'));
		});
		
		$.accordian('ul.sliding_menu > li', '#nav_a', {
			showSpeed:150,
			hideSpeed:150
		});
	},
	select : function(pod_id){
		stage.kill_overlay();
		related_crumb = new Array();
		related_crumb.push(pod_id);
		
		pod_type = pod_id.split("_")[0];
		pod_id = pod_id.split("_")[1].replace(/_/g,"-");
		
		if(debug){
			stage.debug();
		}
		$.getJSON("/_views/pods/"+pod_id+".json", function(data){
			if(data.url.length == 0 || data.type == 'media'){
				if(pod_type == 'learning'){
					huge_pod = stage.build_resource(data);
				} else {
					huge_pod = stage.build_huge(data);
				}
				stage.kill_overlay();
				$("#stage").append("<div id=\"pod_active\">" + huge_pod + "</div>");
				$("#pod_active .pod_media_strip a").click(function(){
					$("#pod_active .pod_media_strip .pod_item.active").fadeTo(250, .75).removeClass("active");
					$(this).parent().addClass("active").fadeTo(250,1);
					tombstone_id = $(this).attr('id').split("thumb_")[1];
					$("div.pod_tombstones div.tombstone").hide();
					$("#tombstone_" + tombstone_id).show();
					pods.select_media(this.href);
					return false;
				});
				$("#pod_active .pod_media_strip a").mouseover(function(){
					if(!$(this).parent().hasClass("active")){
						$(this).parent().fadeTo(150,1);
					}
				});
				$("#pod_active .pod_media_strip a").mouseout(function(){
					if(!$(this).parent().hasClass("active")){
						$(this).parent().fadeTo(150,.75);
					}
				});
				
				$("#featured_media a").click(function(){
					return false;
				});
				
				
				if($.browser.msie && $.browser.version.number==6){
					dialogue_top = 120;
				} else {
					dialogue_top = parseInt($("#stage").css("paddingTop"));
				}
				if(pod_type == 'learning'){
					$("#pod_active").dialog({
						modal: true,
						height: 800,
						width: 800,
						position: [50, dialogue_top],
						background: "#FFFFFF",
						overlay: {
							opacity: 0.5,
							background: "black"
						}
					});
				} else {
					$("#pod_active").dialog({
						modal: true,
						height: 640,
						width: 800,
						position: [50, dialogue_top],
						background: "#FFFFFF",
						overlay: {
							opacity: 0.5,
							background: "black"
						}
					});
				}
				$(".ui-dialog-overlay").click(function(){
					stage.kill_overlay();
				});
				
				$("#pod_active .pod_media_strip .pod_item").fadeTo(250, .65);
				$("#pod_active .pod_media_strip .pod_item.active").fadeTo(250, 1);
				
				pods.setup_thumb_images();
				pods.setup_related();
				pods.setup_media();
				pods.setup_selected_content();
			} else {
				window.open(data);
			}
		});
	},
	select_lessonplan : function(pod_id){
		stage.kill_overlay();
		related_crumb = new Array();
		related_crumb.push(pod_id);
		
		pod_type = pod_id.split("_")[0];
		pod_id = pod_id.split("_")[1].replace(/_/g,"-");
		
		if(debug){
			stage.debug();
		}
		$.getJSON("/_views/pods/"+pod_id+".json", function(data){
			if(data.url.length == 0){
				lessonplan = stage.build_lessonplan(data);
				stage.kill_overlay();
				
				$("#stage").append("<div id=\"pod_active\">" + lessonplan + "</div>");
				
								
				if($.browser.msie && $.browser.version.number==6){
					dialogue_top = 120;
				} else {
					dialogue_top = parseInt($("#stage").css("paddingTop"));
				}				
				$("#pod_active").dialog({
					modal: true,
					height: 640,
					width: 800,
					position: [50, dialogue_top],
					background: "#FFFFFF",
					overlay: {
						opacity: 0.5,
						background: "black"
					}
				});
				$(".ui-dialog-overlay").click(function(){
					stage.kill_overlay();
				});
				
			} else {
				window.open(data);
			}
		});
	},
	swap : function(pod_id, pos){
		if(pos == null){
			pos = -1;
		}
		pod_type = pod_id.split("_")[0];
		pod_id = pod_id.split(pod_type+"_")[1].replace(/_/g,"-");
		
		//setup breadcrumbs
		if(pos >=0){
			related_crumb = related_crumb.slice(0,pos);
		} else {
			related_crumb.push(pod_type+"_"+pod_id);
		}
		
		$.getJSON("/_views/pods/"+pod_id+".json", function(data){
			if(data.url.length == 0){
				if(data.type == 'educationalresource'){
					huge_pod = stage.build_resource(data);
				} else {
					huge_pod = stage.build_huge(data);				
				}
				document.getElementById("pod_active").innerHTML = huge_pod;
				
				$("#pod_active .pod_media_strip a").click(function(){
					$("#pod_active .pod_media_strip .pod_item.active").fadeTo(250, .75).removeClass("active");
					$(this).parent().addClass("active").fadeTo(250,1);
					tombstone_id = $(this).attr('id').split("thumb_")[1];
					$("div.pod_tombstones div.tombstone").hide();
					$("#tombstone_" + tombstone_id).show();
					pods.select_media(this.href);
					return false;
				});
				$("#pod_active .pod_media_strip a").mouseover(function(){
					if(!$(this).parent().hasClass("active")){
						$(this).parent().fadeTo(150,1);
					}
				});
				$("#pod_active .pod_media_strip a").mouseout(function(){
					if(!$(this).parent().hasClass("active")){
						$(this).parent().fadeTo(150,.75);
					}
				});
				
				$("#featured_media a").click(function(){
					return false;
				});
				
				$("#pod_active .pod_media_strip .pod_item").fadeTo(250, .65);
				$("#pod_active .pod_media_strip .pod_item.active").fadeTo(250, 1);
				
				pods.setup_thumb_images();
				pods.setup_related();
				pods.setup_media();
				pods.setup_selected_content();
			} else {
				window.open("/_content/uploads/" + data.id + "/" + data.url);
			}
		});
	},
	setup_thumb_images : function(){
		pods.strip_width = 0;
		$("#pod_active .pod_media_strip a img").each(function(){
			pods.strip_width += $(this).width();
		});
		pods.strip_width += 50;
		$("#pod_active .pod_media_strip").css('width',parseInt(pods.strip_width) + 'px')
		
		if($("#pod_active .pod_media_strip").width() > 550){
			$("#media_slider").slideDown(150);
			$("#media_slider .slide_container").slider({
				slide: function(e,ui){
					total_scroll = parseInt($("#pod_active .pod_strip_container").css("width")) - (parseInt(pods.strip_width));
					percent_scrolled = ui.value / 100;
					$("#pod_active .pod_media_strip").css("left", 0 - (Math.abs(total_scroll) * percent_scrolled));
				}
			});
		}
	},
	setup_related : function(){
		$(".ui-dialog-titlebar").html("<a class=\"ui-dialog-titlebar-close\" href=\"#\" onclick='$(\"#pod_active\").dialog(\"destroy\"); $(\"#pod_active\").remove();return false;'><span>X</span></a>");
		logger("# of related items: " + $("#pod_active .related_container .pod_item").length);
		/*
		if($("#pod_active .related_container .pod_item").length > 0){
			$(".ui-dialog-titlebar").append("<a href=\"#\" onclick=\"$('#pod_active .related_content').slideToggle(250); return false;\"><img src='/_img/pod/dot_down.gif' width='20' height='20' title='Related Info' /></a><span class=\"related_label\">Related</span>");
			$('.related_label').show();
		}*/
		if(related_crumb.length > 1){
			crumb_string = "<div class=\"paging\">";
			for(x=0;x<related_crumb.length;x++){
				var crumb = related_crumb[x];
				var crumb_type = related_crumb[x].split("_")[0];
				if(x > 0){
					crumb_string += "<span> / </span>";
				}
				if(x < (related_crumb.length-1)){
					crumb_string += "<a href=\"#\" id=\"crumb_"+crumb+"\" onclick=\"pods.swap('"+crumb+"', "+(x+1)+"); return false;\">" + crumb_type.substr(0,1).toUpperCase()+crumb_type.substr(1) +"</a> ";
				} else {
					crumb_string += crumb_type.substr(0,1).toUpperCase()+crumb_type.substr(1);
				}
			}
			crumb_string += "</div>";
			$(".ui-dialog-titlebar").append(crumb_string);
		}
	},
	setup_selected_content : function(){
		
		if($("#pod_active #large_pod_content div.content_container").height() > $("#pod_active #large_pod_content").height()){
			pages_needed = Math.ceil($("#pod_active #large_pod_content div.content_container").height() / 313);
			var paging_links = "<div class=\"paging\">";
			for(x=0;x<pages_needed;x++){
				current_page = ((x + 1) / 10 > 0 ) ? x + 1 : '0' + (x + 1);
				current_move = (x * $("#pod_active #large_pod_content").height()) - 13;
				paging_links += "<a href='#' onclick='pods.move_pod_content(\"";
				if(x == 0){
					paging_links += "0\", $(this)); return false;' class='active'";
				} else {
					paging_links += "-" + current_move + "px\", $(this)); return false;'";					
				}
				paging_links += ">" + current_page + "</a>";
			}
			paging_links += "</div>";
			$("#pod_active div.pod_content").append(paging_links);
		}
	},
	move_pod_content : function(amount, a_obj) {
		$("#pod_active div.pod_content div.paging a").removeClass('active');
		
		$(a_obj).addClass("active");
		$("#pod_active #large_pod_content div.content_container").css("top", amount);
		return false;
	},
	select_media : function(href){
		var isImage = (href.indexOf(".png") > 0 || href.indexOf(".jpg") > 0 || href.indexOf(".gif") > 0 || href.indexOf(".PNG") > 0 || href.indexOf(".JPG") > 0 || href.indexOf(".GIF") > 0);
		if(!isImage){
			$("#featured_media").html("<a href=\""+href+"\">&nbsp;</a>");
			$("#featured_media a").media( { height: 300, autoplay: true } );
		} else {
			$("#featured_media").html("<img src=\""+href+"\" height=\"360\" border=\"0\" />");
		}
	},
	setup_media : function(){
		var href = ($("#featured_media a").attr('href')) ? $("#featured_media a").attr('href') : ".gif";
		var isImage = ( href.indexOf(".png") > 0 || href.indexOf(".jpg") > 0 || href.indexOf(".gif") > 0 || href.indexOf(".PNG") > 0 || href.indexOf(".JPG") > 0 || href.indexOf(".GIF") > 0);
		if(!isImage){
			$("#featured_media a").media( { width: 500, height: 340, autoplay: true } );
		}
	},
	email_lessonplan: function()
	{
    input_vars = $("#editform input").serialize() + "&" + $("#editform select").serialize();
		document_title = $('#documenttitle').val();
		email_address = $('#emailaddress').val();
		$.post($('#editform').attr('action'),input_vars,function(data){
			stage.kill_overlay();
			alert("The lesson plan:\n     " + document_title + "\nhas been emailed to you at:\n     " + email_address);
		});
	}
}
	
/* LESSON PLAN RELATED FUNCTIONS
*************************************************************/
var lesson_plan = {
	form_id: "lessonplan_form",
	id: "",
	selected_lessonplan: "",
	cookie: "",
	init: function(i, u)
	{
		lesson_plan.id = i;
		lesson_plan.selected_lessonplan = u;
		lesson_plan.cookie = new cookie("localhost");
		if(lesson_plan.cookie.checkCookie('resource_name')){
			lesson_plan.track_download(lesson_plan.cookie.getCookie('resource_name'), lesson_plan.cookie.getCookie('resource_email'))
		} else {
			$('#education_resources').hide();
			$('#educational_form').show();
			
			track_link("/educational_resources/open_form/" + lesson_plan.selected_lessonplan);
			$("#resource_submit").unbind('click');
			$("#resource_submit").click(function(){
				track_link("/educational_resources/form_submit/" + lesson_plan.selected_lessonplan);
				lesson_plan.email();
				return false;
			});
		}
	},
	validate_email: function(s)
	{

		// Check for correct email
		reEmail = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

		if (!reEmail.test(s)) {
		  return false;
		}

		return true;
	},
	track_download: function(name, email)
	{
		input_vars = "name=" + name +"&email="+ email + "&lesson_plan=" + lesson_plan.id;
		
		//google tracking
		track_link("/_content/uploads/" + lesson_plan.id + "/" + lesson_plan.selected_lessonplan);
		
		$.post("/_lib/track_download.php",input_vars,function(data){
			//stage.kill_overlay();
			window.open("/_content/uploads/" + lesson_plan.id + "/" + lesson_plan.selected_lessonplan);
		});
	},
	email: function()
	{
		email_address = $('#email_address').val();
		full_name = $('#full_name').val();
		valid_email = lesson_plan.validate_email(email_address);
		
		if(valid_email && full_name.length > 0){
			lesson_plan.cookie.setCookie('resource_name', full_name); 
			lesson_plan.cookie.setCookie('resource_email', email_address);
			$('#education_resources').show();
			$('#educational_form').hide();
			lesson_plan.track_download(full_name,email_address);
		} else {
			(!valid_email) ? $('#email_address').parent().children('label').css('color', 'red') : $('#email_address').parent().children('label').css('color', '#999999');
			(full_name.length <= 0) ? $('#full_name').parent().children('label').css('color', 'red') : $('#full_name').parent().children('label').css('color', '#999999');
		}
	}
}

$.fn.media.defaults.flvPlayer= '/mediaplayer.swf';
$.fn.media.defaults.mp3Player= '/mediaplayer.swf';

$.accordian = function(items, first, options) {

	var active = first;
	var running = 0;

	var titles = options && options.titles || '.title';
	var contents = options && options.contents || '.content';
	var onClick = options && options.onClick || function(){};
	var onShow = options && options.onShow || function(){};
	var onHide = options && options.onHide || function(){};
	var showSpeed = options && options.showSpeed || 'slow';
	var hideSpeed = options && options.hideSpeed || 'fast';

	$(items).not(active).children(contents).hide();
	$(items).not(active).each(onHide);
	$(active).each(onShow);

	$(items).children(titles).click(function(e){
		var p = $(contents, this.parentNode);
		$(this.parentNode).each(onClick);
		
		if (!p.is(":hidden")) return false;
		running = 2;

		$(active).children(contents).not(':hidden').slideUp(hideSpeed, function(){--running;});
		p.slideDown(showSpeed, function(){--running;});

		$(active).each(onHide);
		active = '#' + $(this.parentNode)[0].id;
		$(active).each(onShow);

		return false;
	});

};

function onChangeIndex(stage)
{
	if($('#indexselector').val().length > 0){
		$('div.pod_index ul').each(function(){
				$(this).css('display','none');
			});
		$("#" + $('#indexselector').val()).css('display','block');	
	}
}

function track_link(url){
	try {
		if(pageTracker){
			pageTracker._trackPageview(url);
		}
	} catch(err) {}
}


	
/* DOCUMENT READY FUNCTION
*************************************************************/
$(document).ready(function(){
	pods.init();
	navigation.init();   
	stage.current_browser_width = $('body').width();
	$(window).bind("resize", function(){ 
		if(stage.current_browser_width != $('body').width()){
		  stage.current_browser_width = $('body').width();
		  if(stage.stage_contents.length > 0){
			  stage.place_pods();
		  }
		}
   });

});
