$j = jQuery.noConflict();

$j(document).ready(function(){
	Initialise();
});

//Initialise all javascript enhancements for the current page
function Initialise(){
	// Enable PNG transparency fix for IE6
	$j('body').supersleight({shim: assetPath+"images/x.gif"});
	
	startFlowplayer();
	$j('a.lightbox').lightBox();
}

function startFlowplayer(){
	$j("a[href$=.mp4]").each(function(){
		$f($j(this).attr("id"), {src: assetPath+"flowplayer/flowplayer.commercial-3.2.2.swf", wmode: 'opaque'}, {
			key: '#@d98aa64e1d2ee4a8ee3',
			
			play: {
				url: assetPath+"images/buttons/btn_play.png",
				width: 88,
				height: 87
			},
			
			// don't start automcatically
			clip: {
				autoPlay: true,
				autoBuffering: true
			},
		
			plugins: {controls: null},
			
			canvas: {
				borderRadius: 10,
				backgroundGradient: 'none',
				opacity: 0.5
			}
		}).controls($j(this).attr("id")+"-controls", {duration: 0});
	});	
}

function calendar_update(id,params) {
	$j.ajax({
		type: "GET",
		url: "/?func=square_articles/calendar_update",
		data: params,
		dataType: "text/html",
		success: function(data){
			$j("#"+id).html(data);
		}
	});		
}

function load_image(id,params) {
	$j('#viewer .preloader').height($j('#viewer img').height());
	$j('#viewer img').fadeOut('fast', function() { 
		$j('#viewer .preloader').fadeIn('fast', function() { 
			$j.ajax({
				type: "GET",
				url: "/?func=object&object=get_image",
				data: params,
				dataType: "text/html",
				success: function(data){
					var imagesdetails = data.split("|");
					var image = new Image();
					
					image.onload = function() {
						$j('#viewer img').attr('src', image.src);
						$j('#viewer a').attr('href', imagesdetails[1]);
						
						$j('#viewer .preloader').fadeOut('fast', function() { 
							$j('#viewer .preloader').height(0);
							$j('#viewer img').fadeIn('fast', function() { 
								$j('#viewer h3').html(imagesdetails[2]);
								$j('#viewer a').attr('alt', imagesdetails[2]);
								$j('#viewer a').attr('title', imagesdetails[2]);
								$j('a.lightbox').lightBox();
							});
						});
					}
					
					image.src = imagesdetails[0];
				}
			});				
		}) 
	} );
}

function load_video(id,params) {
	if($f().isLoaded()) {
		$f().stop();
		$f().unload();
	}
	
	$j('#viewer .preloader').height($j('#viewer .player').height());
	$j('#viewer .player').fadeOut('fast', function() { 
		$j('#viewer .preloader').fadeIn('fast', function() { 
			$j.ajax({
				type: "GET",
				url: "/?func=object&object=get_video",
				data: params,
				dataType: "text/html",
				success: function(data){
					var videodetails = data.split("|");
					var image = new Image();
					
					image.onload = function() {
						$j('#viewer .content').html('<div class="preloader" style="height: '+$j('#viewer .preloader').height()+'"></div><a href="'+videodetails[1]+'" class="player" id="videoplayer" style="display: none;"><img src="'+assetPath+'images/buttons/btn_play.png"/></a>');
						$j('#viewer .bottom').html('<div id="videoplayer-controls" class="controls"></div>');
						$j('#viewer .player').css('background-image', 'url('+image.src+')');
						//$j('#viewer .player').attr('href', videodetails[1]);
						
						$j('#viewer .preloader').fadeOut('fast', function() { 
							$j('#viewer .player').fadeIn('fast', function() { 
								$j('#viewer h3').html(videodetails[4]+'<a href="'+videodetails[3]+'" class="quicktime"></a><a href="'+videodetails[2]+'" class="windows"></a>');
								startFlowplayer();
							});
						});
					}
					
					image.src = videodetails[0];
				}
			});				
		}) 
	} );
}