// JavaScript Document
$(document).ready(function(){

	//FORM FUNCTIONS		
	var v = jQuery("#message-form").validate({
		rules: {
			subject: {
				required: true,
				minlength: 2
			},
			email: {
				required: true,
				email: true
			},
			message: {
				required: true,
				minlength: 2
			},
		
			messages: {
				subject: "Please enter a subject",
				email: "Please enter a valid email",
				message: "Please enter a message"
			}
			
		},
		
		submitHandler: function(form) {
			$(form).ajaxSubmit({
				target: "#result"
				});
			}
		});
		
	
	// VERTICAL SCROLLABLE FUNCTIONS
	$("div.scrollable-credits").scrollable({
			vertical: true, 
			size: 1,
			clickable: false
		});
	
		$("div.scrollable-news").scrollable({
			vertical: true, 
			size: 2,
			clickable: false
		});
		
		$("div.scrollable-links").scrollable({
			vertical: true, 
			size: 2,
			clickable: false
		});
	
		// initialize scrollable video 
		$("div.scrollable-video").scrollable({
			vertical: true, 
			size: 1,
			clickable: false
		});
	
		// initialize scrollable audio 
		$("div.scrollable-audio").scrollable({
			vertical: true, 
			size: 3,
			clickable: false
		});
		
		// initialize scrollable gallery
		$("div.scrollable-gallery").scrollable({
			vertical: true, 
			size: 1,
			clickable: false
		});
		
		// initialize scrollable news article
		$("#scrollable-article").scrollable({
			vertical: true, 
			size: 1,
			clickable: false
		});
		
		  
		
	
			
			
	var $mainlist = $('#main-nav li');		
	var $sublist = $('#main-nav li ul');
	var $fatb = $('#feedback');
	$fatb.text('hi');
	$sublist.css('display', 'none');		
			
	$mainlist.mouseover(function(){
	$sublist.css('display', 'none');
	
	
	$sublist.removeClass('selected');
	$('ul', this).addClass('selected');
	
	$('ul.selected').css('display', 'block');
	//var $listItem = $(this);
		//$sublist.css('display', 'none');
		//$('ul',this).css('display', 'block');
		//$sublist.slideUp('slow', function(){
		
		//$('ul', this).slideDown('slow');
		
		//});
		
     });
		
	$sublist.mouseout(function(){
		$(this).css('display', 'none');
     });
		
	});



// SCROLLPANE FUNCTIONS
$(function() {
	var originalSizes = new Array();
	$('#contents').jScrollHorizontalPane({
	scrollbarHeight:32, 
	scrollbarMargin:0,
	animateInterval:20,
	dragMinWidth:134,
	dragMaxWidth:134
	//animateStep:3
	//resize:true
	});
	
	$('a.jumper').click(function(){
		var pos = $(this).attr('id');
		var c = $("#contents");
		c[0].scrollTo("li.pane:eq("+pos+")");
		return false;
	});
	
	//COLOURBOX FUNCTIONS
	//video
	$(".video-link").colorbox({
		opacity:0.80
	});
	
	//gallery - gema
	$("a[rel='gemma_factor']").colorbox({
		opacity:0.80
	});
	
	//gallery - publicity
	$("a[rel='publicity']").colorbox({
		opacity:0.80
	});
	
	//gallery - misc
	$("a[rel='misc']").colorbox({
		opacity:0.80
	});
	
	
	//news
	$(".article").colorbox({
		opacity:0.80
	});
	
	$(".article").colorbox({
		//onOpen:function(){ alert('onOpen: colorbox is about to open'); },
		//onLoad:function(){ alert('onLoad: colorbox has started to load the targeted content');},
		onComplete:function(){ 
			//alert('onComplete: colorbox has displayed the loaded content'); 
			//get height of all divs in items div
			//if they are larger than the maximum height we want eg 500px we setup a scroller
			//we also set the height of the container div
			var iCount = 0;
			//var conH = 500;
			
			//$("#scrollable-article").css('height', conH +'px');
			//$("#scrollable-article").height(conH +'px');
			//$("#article-wrapper").height(conH+50 +'px');
			var conH =  $("#scrollable-article").height();
			var $iDiv = $("#scrollable-article div.items div");
			
			$iDiv.each(function(){
				iCount += $(this).height();
			});
			
			//display scrollable only if height of items is greater than container div
			if ( iCount > conH ){
				//console.log('greater');
				// initialize scrollable news article
				$("#scrollable-article").scrollable({
					vertical: true, 
					size: 1,
					clickable: false
				});
				//show nav buttons
				$("#article-wrapper div#actions").show()
				
			} else{
				//hide nav buttons
				$("#article-wrapper div#actions").hide()
				//console.log('lesser');
			}
			
			//console.log(iCount);
			//console.log(conH);
			
		}
					
	});
	
	
	//TWITTER FUNCTIONS
	$("#twitter").getTwitter({
		userName: "anna_gilthorpe",
		numTweets: 5,
		loaderText: "&nbsp;",
		slideIn: false,
		showHeading: false,
		headingText: "Latest Tweets",
		showProfileLink: true
	});
	
		




	//JPLAYER FUNCTIONS
	// Local copy of jQuery selectors, for performance.
	//var jpPlayInfo = $("#play-info");
	var currentTrack = "None";
	var isPlaying = false;
	var currentDiv;
	
	/*
    $(".audio-link").each(function () {
		console.log($(this).attr("href"));
		$("#jquery_jplayer").jPlayer("setFile", $(this).attr("href"));
		//var tt = $("#jquery_jplayer").jPlayer("getData", "diag.totalTime");
		//var tt = $("#jquery_jplayer").jPlayer("getData", "diag.totalTime");
	});*/
	
  $("#jquery_jplayer").jPlayer({
		swfPath: "scripts/",
		customCssIds: true
	})
	.jPlayer("cssId", "play", "play")
	.jPlayer("cssId", "pause", "pause")
	.jPlayer("cssId", "stop", "stop")
	.jPlayer("cssId", "volumeMin", "vmin")
	.jPlayer("cssId", "volumeMax", "vmax")
	.jPlayer("cssId", "volumeBar", "vbar")
	.jPlayer("onProgressChange", function(lp,ppr,ppa,pt,tt) {
 		//jpPlayInfo.text("at " + parseInt(ppa)+"% of " + $.jPlayer.convertTime(tt) + ", which is " + $.jPlayer.convertTime(pt));
		
		$('span.duration', currentDiv).text($.jPlayer.convertTime(pt));
		
	});
	

  	$('.audio-link').click( changeTrack );
	
	//change track function
	function changeTrack(e) {
		
		currentDiv = $(this).parent();
		
		//get array key for current track
		var index = $(".audio-link").index(this);

		//console.log(index);
		
		//clicked on a new track
		if(currentTrack != $(this).attr('id')){
			
			//$("#jquery_jplayer").jPlayer( "clearFile" );

			
			$("#jquery_jplayer").jPlayer("setFile", $(this).attr("href")).jPlayer("play");
			currentTrack = $(this).attr('id'); 
			$("#trackname").text(currentTrack);
			//remove anchor elements classes
			$(".audio-link").removeClass('playing paused');
			$(this).addClass('playing');
			
			
			
		}else{//we have clicked the a track that is currently playing or paused
			if(isPlaying){
				//the track is playing, so we pause it
				$("#jquery_jplayer").jPlayer("pause");
				$(this).removeClass('playing').addClass('paused'); 
			}else{
			//the track is not playing, so we play it
				$("#jquery_jplayer").jPlayer("play");
				$(this).removeClass('paused').addClass('playing');;
			}
		}
		changeImg();
		$(this).blur();
		return false;
	}
	
	function changeImg() {
		//set all images to play, might need a filter to speed up processing
		$('.audio-link img')
		.attr({
 			src:'images/audio_play.gif',
			alt:'Play'
		});
		//set playing images
		$('.audio-link.playing img')
		.attr({
 			src:'images/audio_playing.gif',
			alt:'Playing'
		});
		//set paused images
		$('.audio-link.paused img')
		.attr({
 			src:'images/audio_play.gif',
			alt:'Play'
		});
	};
	
	//need to set play buton again once sound is complete
	$("#jquery_jplayer").jPlayer("onSoundComplete", function() {
		$(".audio-link").removeClass('playing');
		changeImg();
	});



	setInterval( function() {
 		if($("#jquery_jplayer").jPlayer("getData", "diag.isPlaying")){
    		$("#jpPlaying").text("Curent Track="+currentTrack+" Playing="+isPlaying);
			isPlaying = true;
  		} else {
    		$("#jpPlaying").text("Curent Track="+currentTrack+" Playing="+isPlaying);
			isPlaying = false;
  		}
	}, 500 );

//end of document ready
});
