var ytplayer = null;
function onYouTubePlayerReady(playerId) {
      ytplayer = document.getElementById("myytplayer");
      ytplayer.playVideo();
}

function getId(href){
	var id = href.substring(31);
	return id;
}

function loadMovieYt(id){
	var params = { allowScriptAccess: "always", autoPlay: true, allowFullScreen: true };
	var atts = { id: "myytplayer" };
    swfobject.embedSWF("http://www.youtube.com/v/"+id+"?enablejsapi=1&playerapiid=ytplayer&fs=1", "ytapiplayer", "640", "385", "8", null, null, 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();
		
		//set new movie title	
		dialog.dialog('option', 'title', title);
		
		if(dialog.dialog('isOpen') == true){
			ytplayer.loadVideoById(id);
			
			
		}else{
			// open the dialog
			$("#dialog").dialog('open');
			//load the movie html
			loadMovieYt(id);
		}	
		dialog.find('.player-description').html(description);
		return false;		
	});
}
	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);
				}
			});
		}
		/*
		content.append('<div class="slider-viewBox">')
		.find('.slider-viewBox').append('<div class="slider-content">')
		.find('.slider-content').append(data);
		var sliderContent = content.find('.slider-content');
		var viewBox = content.find('.slider-viewBox');
		
		if(sliderContent.height() > viewBox.height()){
			var slideMax = sliderContent.height()+ 50 - viewBox.height();
			content.prepend('<div class="panel-slider" style="height: '+ (viewBox.height() - 20)+'px; float:right">');
			
			content.find('.panel-slider').slider({
				orientation: "vertical",
				range: "min",
				min: 0,
				max: slideMax,
				value: slideMax,
				slide: function(event, ui) {
					var top = (ui.value - slideMax);
					sliderContent.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;

	//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');
		}
	);
	
	$("#menu li:first-child").addClass("menuFirst");
	$("#menu li:last-child").addClass("menuLast");

	$("#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');
			}
		}
	});
	// get the hash and go to the proper page		
  	var hash = document.location.hash;		
	
	var scrollTo = $("a[href='"+hash+"']").attr('step');
	stepcarousel.stepTo('viewBox', scrollTo);
	
	//get twitter feed
			var url = 'http://twitter.com/statuses/user_timeline/staticrevenger.json?callback=twitterCallback2&count=5';

  			var script = document.createElement('script');
  			script.setAttribute('src', url);
  			document.body.appendChild(script);	
  	
  	
	
});