(function ($) {
// VERTICALLY ALIGN FUNCTION
$.fn.vAlign = function() {
	return this.each(function(i){
	$(this).hide();
	var ah = $(this).height();
	var ph = $(this).parent().height();
	var mh = (ph - ah) / 2;
	$(this).css('margin-top', mh);
	$(this).show();
	});
};
})(jQuery);




      
      

$(document).ready(function(){


	$(".vertical").vAlign();
	
	$('#ynly').hover(function() {$('#ynly').addClass('itemHover');$('#chooser-info').addClass('ynlyHover');$.sound.play('/media/hover.wav');}, function() {$('#ynly').removeClass('itemHover');$('#chooser-info').removeClass('ynlyHover');});
	$('#dodp').hover(function() {$('#dodp').addClass('itemHoverError');$('#chooser-info').addClass('dodpHover');$.sound.play('/media/hover.wav');}, function() {$('#dodp').removeClass('itemHoverError');$('#chooser-info').removeClass('dodpHover');});

	$('.blink').blink({delay:1000});
    $('#taskbarClock').jclock();
 	Shadowbox.init({skipSetup: true,overlayOpacity:0.8,handleOversize:"drag"});
 	
	$('#extranetInbox').draggable({containment:'parent',handle:$('.appHeader')});
	$('#extranetProfiles').draggable({containment:'parent',handle:$('.appHeader')});
	$('#extranetSurveillance').draggable({containment:'parent',handle:$('.appHeader')});
	$('#extranetFeeds').draggable({containment:'parent',handle:$('.appHeader')});
	$('#extranetBook').draggable({containment:'parent',handle:$('.appHeader')});
	$('#extranetNotepad').draggable({containment:'parent',handle:$('.appHeader')});
	$('#extranetAccount').draggable({containment:'parent',handle:$('.appHeader')});
	
	$("#extranetLoginPassword").bind("keydown", function(e) {if (e.keyCode == '13'){login();}}); 
	
	$("#extranetInbox").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) {var currentZ = parseFloat($(this).css("zIndex"));largestZ = currentZ > largestZ ? currentZ : largestZ;});$("#extranetInbox").css("z-index", largestZ + 1);}); 
	$("#inboxIcon").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) {var currentZ = parseFloat($(this).css("zIndex"));largestZ = currentZ > largestZ ? currentZ : largestZ;});$("#extranetInbox").css("z-index", largestZ + 1);}); 
	$("#newMessagePopup").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) {var currentZ = parseFloat($(this).css("zIndex"));largestZ = currentZ > largestZ ? currentZ : largestZ;});$("#extranetInbox").css("z-index", largestZ + 1);}); 
	$("#taskbarMessage").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) {var currentZ = parseFloat($(this).css("zIndex"));largestZ = currentZ > largestZ ? currentZ : largestZ;});$("#extranetInbox").css("z-index", largestZ + 1);}); 
	
	
	$("#extranetProfiles").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetProfiles").css("z-index", largestZ + 1);}); 
	$("#profilesIcon").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetProfiles").css("z-index", largestZ + 1);}); 
	
	$("#extranetSurveillance").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetSurveillance").css("z-index", largestZ + 1);}); 
	$("#surveillanceIcon").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetSurveillance").css("z-index", largestZ + 1);}); 
	
	$("#extranetFeeds").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetFeeds").css("z-index", largestZ + 1); }); 
	$("#feedsIcon").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetFeeds").css("z-index", largestZ + 1); }); 

	$("#extranetBook").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetBook").css("z-index", largestZ + 1); }); 
	$("#bookIcon").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetBook").css("z-index", largestZ + 1); }); 

	$("#extranetNotepad").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetNotepad").css("z-index", largestZ + 1); });
	
	$("#extranetAccount").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetAccount").css("z-index", largestZ + 1); });
	$("#accountLink").bind("mousedown", function(e){var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetAccount").css("z-index", largestZ + 1); });



});



function login(){
	
	$('#extranetLoginVideo').hide();
	
	if ($('#extranetLoginPassword').val() != '') {
		$('#extranetLogin').fadeOut('slow', function() {
			$('#extranetLoginPassword').val('');
			$.getJSON('/experience/test/', function(data) {
			
				//load the user details
				$.each(data.content.user, function(i,item){
					$("#extranetAccount .appHeader h2").html('USER ACCOUNT: <i>'+item.firstname.toUpperCase()+'.'+item.lastname.toUpperCase()+'</i>');
					$("#extranetAccount .appInner #userImage").attr("src",item.image);
					$("#extranetAccount .appInner h3").html(item.firstname + ' ' + item.lastname);
					$("#extranetAccount .appInner h4").html(item.role);
					$("#extranetAccount .appInner #userEmail").html('<a href="mailto:'+item.email+'">'+item.email+'</a>');
					$("#extranetAccount .appInner #userPhone").html('<a href="skype:'+item.phoneNumber+'?call">'+item.phoneNumber+'</a>');
				});

				//load the desktop documents and append the icons
				$.each(data.content.documents, function(i,item){
            		$('#documentIcons').append('<div class="desktopIcon" onclick="launchNotepad(\''+item.title.toUpperCase()+'\',\''+item.body+'\');"><img src="/images/extranet_document.gif" width="23" height="32" alt="'+item.title.toUpperCase()+'"/><br/>'+item.title.toUpperCase()+'</div>');
         		});
         		
         		//load the inbox
         		$.each(data.content.inbox, function(i,item){
         			$('#extranetInbox #inboxList').append('<div id="message'+this.id+'" class="inboxItem" onclick="launchMessage(\''+item.id+'\',\''+item.subject+'\',\''+item.sender+'\',\''+item.date+'\',\''+item.body+'\');"><div class="inboxSender">'+item.sender+'</div><div class="inboxSubject">'+item.subject+'</div><div class="inboxDate">'+item.date+'</div></div>');
         			if (item.isNew == 'true'){
         				$('#message'+item.id).addClass('isnew');
         				isNew = true;
         			};
         		});
         		
         		//load the profiles
         		$.each(data.content.profiles, function(i,item){
         			$('#profileCaro').append('<div id="profile'+this.id+'" class="profileItem" onclick="launchProfileDetail(\''+item.id+'\',\''+item.name+'\',\''+item.image+'\',\''+item.biog+'\');"><img src="'+item.thumbnail+'" alt="'+item.name+'" width="50" height="63"/></div>');
           			if (i == 0){launchProfileDetail(item.id,item.name,item.image,item.biog);};
           		});
         		
         		//load the feeds
         		$.each(data.content.feeds, function(i,item){
         				$('#extranetFeeds #feedList').append('<div id="feed'+this.id+'" class="feedItem '+item.type+'" onclick="launchFeedItem(\''+item.id+'\',\''+item.title+'\',\''+item.source+'\',\''+item.date+'\',\''+item.image+'\');"><img src="'+item.thumbnail+'" alt="'+item.title+'" width="80" height="60"/><div class="feedTitle">'+item.title+'</div><div class="feedSource">'+item.source+'</div><div class="feedDate">'+item.date+'</div></div>');
         		});
         		
         		//load the surveillance
         		$.each(data.content.surveillance, function(i,item){
         			$('#extranetSurveillance #mediaList').append('<div id="media'+this.id+'" class="mediaItem '+item.type+'" onclick="launchMedia(\''+item.id+'\',\''+item.type+'\',\''+item.file+'\');"><div class="mediaTitle">'+item.title+'</div></div>');
         			if (i == 0){launchMedia(item.id,item.type,item.file);};
         		});
         		
         		//display
         		$(".desktopIcon").bind("mousedown", function(e){;var largestZ = 1;$(".app").each(function(i) { var currentZ = parseFloat($(this).css("zIndex")); largestZ = currentZ > largestZ ? currentZ : largestZ; }); $("#extranetNotepad").css("z-index", largestZ + 1); });
         		if (isNew = true) {setTimeout("triggerNewMessage()",20000)};
         		
         		var so2 = new SWFObject('/media/player-licensed.swf','plv2','360','240','9');
  					so2.addParam('allowfullscreen','true');
 					so2.addParam('allowscriptaccess','always');
  					so2.addParam('wmode','transparent');
  					so2.addVariable('file','/media/OnlineBNCEb.f4v');
  					so2.addVariable('backcolor','009999');
  					so2.addVariable('autostart','true');
  					so2.addVariable('repeat','never');
  					so2.addVariable('stretching','fill');
  					so2.addVariable('controlbar','none');
  					so2.write('postLoginVideo');
  					
         		$('#extranetDesktop').fadeIn('slow');
         		
          
  			});
		});
	}
	else {
			$('#extranetLoginFields').fadeOut('slow', function() {
    			$('#extranetLoginError').fadeIn('slow', function() {
    				$('#extranetLoginPassword').val('');
    				setTimeout("$('#extranetLoginError').fadeOut('slow', function() {$('#extranetLoginFields').fadeIn('slow');$('#extranetLoginPassword').focus();});",4000);
  				});

  			});
		};
		
}


function logout(){
	$('#extranetDesktop').fadeOut('slow', function() {
			document.location.href = '/';
			
	});
}


function launchInbox(){
	closeMessage();
	$("#extranetInbox").fadeIn('slow');
	$("#inboxIcon").attr("src",'/images/extranet_inbox_on.gif');
	clearNewMessage();
	
}

function closeInbox(){
	$("#extranetInbox").fadeOut('slow');
	closeMessage();
	$("#inboxIcon").attr("src",'/images/extranet_inbox_off.gif');
}

function launchMessage(id,subject,sender,date,body){
	$("#inboxList").fadeOut('slow', function() {
		$("#messageSubject").html(subject);
		$("#messageSender").html(sender);
		$("#messageDate").html(date);
		$("#messageBody").html(body);
		$("#inboxMessage").fadeIn('slow');
		$('#message'+id).removeClass('isnew');
	});
	
}

function closeMessage(){
	$("#inboxMessage").fadeOut('fast', function() {
		$("#inboxList").fadeIn('fast');
	});
	
}

function launchProfiles(id){

	if (id != '') {
		$("#extranetProfiles").fadeIn('fast',function() {
			$('#profile'+id).click();
		});
	} else {
		$("#extranetProfiles").fadeIn('slow')
	};

	$("#profilesIcon").attr("src",'/images/extranet_profiles_on.gif');
}

function closeProfiles(){
	$("#extranetProfiles").fadeOut('slow');
	$("#profilesIcon").attr("src",'/images/extranet_profiles_off.gif');
}

function launchProfileDetail(id,name,image,biog){
		$('.profileItem').removeClass('on');
		$('#profile'+id).addClass('on');
		$("#profileImage").attr("src",image);
		$("#profileDetail h3").html(name);
		$("#profileInfo").html(biog);
}



function launchSurveillance(){
	$("#extranetSurveillance").fadeIn('slow');
	$("#surveillanceIcon").attr("src",'/images/extranet_surveillance_on.gif');
}

function closeSurveillance(){
	$("#extranetSurveillance").fadeOut('slow', function() {
		$("#mediaSpace").html('<img src="/images/extranet_surveillance_select.gif" width="360" height="240" alt="PLEASE SELECT A SURVEILLANCE SOURCE">');
		$('.mediaItem').removeClass('on');
	});
	$("#surveillanceIcon").attr("src",'/images/extranet_surveillance_off.gif');
}

function launchMedia(id,type,file){
		if (type == 'VIDEO'){
			var so = new SWFObject('/media/player-licensed.swf','mpl','360','240','9');
  			so.addParam('allowfullscreen','true');
 			so.addParam('allowscriptaccess','always');
  			so.addParam('wmode','transparent');
  			so.addVariable('file',file);
  			so.addVariable('backcolor','009999');
  			so.addVariable('autostart','true');
  			so.addVariable('repeat','always');
  			so.addVariable('stretching','fill');
  			so.addVariable('controlbar','none');
  			so.write('mediaSpace');
		};
		
		if (type == 'IMAGE'){
			var so = new SWFObject('/media/player-licensed.swf','mpl','360','240','9');
  			so.addParam('allowfullscreen','true');
 			so.addParam('allowscriptaccess','always');
  			so.addParam('wmode','transparent');
  			so.addVariable('file',file);
  			so.addVariable('backcolor','009999');
  			so.addVariable('stretching','fit');
  			so.addVariable('controlbar','none');
  			so.addVariable('autostart','true');
  			so.write('mediaSpace');
		};
		
		if (type == 'AUDIO'){
			var so = new SWFObject('/media/player-licensed.swf','mpl','360','240','9');
  			so.addParam('allowfullscreen','true');
 			so.addParam('allowscriptaccess','always');
  			so.addParam('wmode','transparent');
  			so.addVariable('file',file);
  			so.addVariable('image','/images/extranet_player_audio.gif');
  			so.addVariable('backcolor','009999');
  			so.addVariable('autostart','true');
  			so.addVariable('repeat','always');
  			so.addVariable('controlbar','none');
  			so.write('mediaSpace');
		};
		
		
		$('.mediaItem').removeClass('on');
		$('#media'+id).addClass('on');
}

function launchFeeds(){
	$("#feedList").hide();
	$("#feedLoading").show();
	$("#extranetFeeds").fadeIn('slow');
	$("#feedsIcon").attr("src",'/images/extranet_feeds_on.gif');
	setTimeout("$('#feedLoading').fadeOut('slow', function() {$('#feedList').fadeIn('slow');});",5000)
}

function closeFeeds(){
	$("#extranetFeeds").fadeOut('slow', function() {
		$("#feedList").hide();
		$("#feedLoading").show();
	});
	$("#feedsIcon").attr("src",'/images/extranet_feeds_off.gif');
}

function launchFeedItem(id,title,source,date,file){
	Shadowbox.open({content: file, player: "img"});
}

function launchBook(){
	$("#extranetBook").fadeIn('slow');
	$("#bookIcon").attr("src",'/images/extranet_book_on.gif');
}

function closeBook(){
	$("#extranetBook").fadeOut('slow', function(){
			$('#bookingFrame').attr('src','/experience/hwts-client/');
	});
	$("#bookIcon").attr("src",'/images/extranet_book_off.gif');
}

function launchNotepad(docTitle,docBody){
	$("#extranetNotepad .appHeader h2").html('RESTRICTED DOCUMENT: <i>'+docTitle+'</i>');
	$("#extranetNotepad .appInner").html(docBody);
	$("#extranetNotepad").fadeIn('slow');
}

function closeNotepad(){
	$("#extranetNotepad").fadeOut('slow');
}

function launchAccount(){
	$("#extranetAccount").fadeIn('slow');
}

function closeAccount(){
	$("#extranetAccount").fadeOut('slow');
}

function closeWelcome(){
	$("#extranetWelcome").fadeOut('slow');
}

function triggerNewMessage(){
	$.sound.play('/media/new_email.wav');
	$("#taskbarMessage").show();
	$("#newMessagePopup").fadeIn('slow');
	setTimeout("$('#newMessagePopup').fadeOut('slow');",20000)
}

function clearNewMessage(){
	$("#taskbarMessage").hide();
	$("#newMessagePopup").fadeOut('slow');
}


function getTicketingAvailability(){


$.ajax({
  url: 'https://services.hitwave.net/hwts/?hwtsAction=avails&bID=2&eventID=103&format=JSON',
  dataType: 'jsonp',
  success: function(data) {
	
	prevperfID = '';
	prevperfDate = '';

		$.each(data.availability, function(i,item){
	
		if (parseFloat(item.AVAILABILITY) < 10){perfstatus = 'amber';statusTitle='LIMITED';};
		if (parseFloat(item.AVAILABILITY) == 0) {perfstatus = 'red';statusTitle='SOLD OUT';};
		if (parseFloat(item.AVAILABILITY) >= 10){perfstatus = 'green';statusTitle='AVAILABLE';};
	
		
			if (prevperfDate == item.PERFDATE) {$('#availability'+prevperfID+' .timeslots').append('<div class="time '+perfstatus+'"><a href="/experience/?screen=booking&amp;perfID='+item.PERFID+'" title="'+statusTitle+'">'+item.PERFTIME+'</a></div>');}
			if (prevperfDate != item.PERFDATE) {$('#availability').append('<div id="availability'+item.PERFID+'" class="bookingDate"><div class="date">'+item.PERFDATE+'<span> - '+item.VENUENAME+'</span></div><div class="timeslots"><div class="time '+perfstatus+'"><a href="/experience/?screen=booking&amp;perfID='+item.PERFID+'" title="'+statusTitle+'">'+item.PERFTIME+'</a></div></div></div>');prevperfID = item.PERFID;}

			prevperfDate = item.PERFDATE;
       });
       
       $("#availabilityLoader").fadeOut('fast', function() {
       	$("#availability").show();
        });
  
  
	
	 }
});

}



function stateMonitor(obj){
	if (obj.id == 'plv'){
		if(obj.newstate == 'COMPLETED'){$('#extranetLoginVideo').fadeOut('fast', function(){$('#extranetLogin').fadeIn('slow');$('#extranetLoginPassword').focus();});}
	};
	if (obj.id == 'plv2'){
		if(obj.newstate == 'COMPLETED'){$('#extranetWelcome').fadeOut('fast');}
	};

};

function playerReady(obj){
	player = document.getElementById(obj.id);
		player.addModelListener('STATE', 'stateMonitor');
};





