var ytplayer = null;
var currentPlayer = "";

function onYouTubePlayerReady(playerId) {
      ytplayer = document.getElementById("myytplayer");
      ytplayer.playVideo();
}
function vimeo_player_loaded(playerId){
	
}
function getId(href){
	var id = href.substring(31);
	return id;
}

function loadMovieYt(id, type){
	var params = null;
	var flashvars = null;
	var params = { allowScriptAccess: "always", autoPlay: true};
	var atts = { id: "myytplayer" };
	var url ="";
	
	if(type == "vi"){
		var params = { allowScriptAccess: "always"};
		flashvars = {
	        clip_id: id,
	        autoplay: 1,
			js_api: 1, // required in order to use the Javascript API
			js_onLoad: 'vimeo_player_loaded', // moogaloop will call this JS function when it's done loading (optional)
			js_swf_id: 'moogaloop' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
	    };
	    url = "http://www.vimeo.com/moogaloop.swf";
	}else{
		
		url = "http://www.youtube.com/v/"+id+"?enablejsapi=1&playerapiid=ytplayer";
	}
	
    swfobject.embedSWF(url, "ytapiplayer", "640", "385","8","expressInstall.swf", flashvars, params, atts);
}



function getThumbYt(id, size){
	var html = '<img src="http://img.youtube.com/vi/'+id+'/0.jpg" alt="Play Video" width="'+ size +'" /><br />';
	return html;
}
function navArrow(position){
		$('#navArrow').animate({'left': position}, 525);
}


function ytVideoClicks(videoLink){
	videoLink.click(function(){
		//gather variables
		var id = $(this).attr('id');
		var title = $(this).text();
		var dialog = $("#dialog");
		var description = $(this).next().html();
		var type = $(this).attr('type');
		
		//set new movie title	
		dialog.dialog('option', 'title', title);
		
			if(dialog.dialog('isOpen') == true){
				if(type == 'vi'){
					dialog.prepend('<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>').find("#myytplayer").remove();
					loadMovieYt(id, type);
				}else if(currentPlayer == type){
					ytplayer.loadVideoById(id);
				}else{
					dialog.prepend('<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>').find("#myytplayer").remove();
					loadMovieYt(id, type);
			
				}
			}else{
				// open the dialog
				$("#dialog").dialog('open');
				//load the movie html
				loadMovieYt(id, type);
			}
		currentPlayer = type;
		
		dialog.find('.player-description').html(description);
		return false;		
	});
}

function viVideoClicks(){
	
}

	function sizeCheck(content){
		if(content[0].scrollHeight > content[0].clientHeight){
			content.attr('class','slider-content').wrap('<div class="slider-viewbox" />');
			var viewbox = content.parent();
			var slideMax = content.height() + 50 - viewbox.height();
			
			viewbox.prepend('<div class="panel-slider" />');
			
			viewbox.find('.panel-slider').slider({
				orientation: "vertical",
				range: "min",
				min: 0,
				max: slideMax,
				value: slideMax,
				slide: function(event, ui){
					var top = (ui.value - slideMax);
					content.css("top", top);
				}
			});
		}

	}

		//carousel
		stepcarousel.setup({
			galleryid: 'viewBox', //id of carousel DIV
			beltclass: 'belt', //class of inner "belt" DIV containing all the panel DIVs
			panelclass: 'panel', //class of panel DIVs each holding content
			panelbehavior: {speed:1000, wraparound:false, persist:true},
			defaultbuttons: {enable: false, moveby: 1, leftnav: ['img2/arrowl.png', 30, 435], rightnav: ['img2/arrowr.png', -50, 435]},
			statusvars: ['statusA', 'statusB', 'statusC'], // Register 3 "status" variables
			contenttype: ['inline'] // content type <--No comma following the very last parameter, always!
		});

$(document).ready(function(){
	var i = 1;
	//animations
	$('#awards').fadeIn(4000);
	$('#culture').fadeIn(1000);
	

	//sendform
	$('form#sendForm').submit(function(){
		var formData = $('form#sendForm input').serialize() + '&' + $('form#sendForm textarea').serialize();
			$.post('utilities/mail.php', formData, function(data){
				var form = $('form#sendForm').html();
				$('#emailForm p').html(data);
			});
			return false;
	});
	
	
	//take care of the menu attribute steps
	$(".stepLink").each(function(){
		href = $(this).attr('href');
		
		$("a[href='"+href+"']").attr("step", i).addClass("panelLink");
		
		i++;
	});
	
	//menu clicks
	$(".panelLink").click(function(){
		step = $(this).attr("step");
		stepcarousel.stepTo('viewBox', step);
		width = $("#menu a").css('width');
		width = width.substring(0,3);
		arrow = 0;
		if (step < 5){
			arrow = (step*width) - (width/1.7);
		}else{
			arrow = (step*137) + (width/2.3);
		}
		navArrow( arrow + 'px');
		return false;
	});
	// pannel content and layout
	//$('.belt').css('background', '#000000 url(../img2/belt.jpg) repeat-x 0px 20px');
	$('.stepcarousel').css('width', '720px');
	
	$.each($('.panel'), function(){
			sizeCheck($(this).find('.content'));
	});
	
	//video clicks and player
	ytVideoClicks($('.yt-link, .yt-link-music'));
	$('.player-show').toggle(
		function(){
			$(this).text('less info').next().slideDown('slow');
		},
		function(){
			$(this).text('more info').next().slideUp('slow');
		}
	);
	

	$("#dialog").dialog({
		autoOpen: false,
		position: "top",
		width: 690,
		resizable: false,
		beforeclose: function(){
				$(this).prepend('<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.</div>').find("#myytplayer").remove();
		},
		buttons: {
			"Close": function(){
				$(this).dialog('close');
			},
			"Share": function(){
				
			}
		}
	});
	//adjust the feed entries to fit the space
	if($(".twitter-feed").height() > 234){
		$(".twitter-feed").find('.follow').prev().remove();
	};
	
  	
  	// get the hash and go to the proper page		
  	var hash = document.location.hash;		
	
	$("a[href='"+hash+"']").trigger('click');
	
});