/**
 * jQuery Log
 * Fast & safe logging in Firebug console
 *
 * @param mixed - as many parameters as needed
 * @return void
 *
 * @url http://plugins.jquery.com/project/jQueryLog
 * @author Amal Samally [amal.samally(at)gmail.com]
 * @version 1.0
 * @example:
 * 		$.log(someObj, someVar);
 * 		$.log("%s is %d years old.", "Bob", 42);
 * 		$('div.someClass').log().hide();
 */
(function(a){a.log=function(){if(window.console&&window.console.log){console.log.apply(window.console,arguments)}};a.fn.log=function(){a.log(this);return this}})(jQuery);


jQuery('body').addClass('has-js');

function getFlashMovieObject(movieName) {
  if (window.document[movieName]) {
    return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1) {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else {
  // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    return document.getElementById(movieName);
  }
  //return document.getElementsByTagName(movieName); // yeah, this don't work
}

function swfReset(toReset) {
	var swfToReset = toReset;
	var flashMovie=getFlashMovieObject(swfToReset);
	flashMovie.StopPlay();
	flashMovie.Rewind();
	jQuery.fn.SCT2_elementFocusState();
}
function swfStop(toStop) {
	var swfToStop = toStop;
	var flashMovie=getFlashMovieObject(swfToStop);
	flashMovie.StopPlay();
	jQuery.fn.SCT2_elementFocusState();
}
function swfPlay(toPlay) {
	var swfToPlay = toPlay;
	var flashMovie=getFlashMovieObject(swfToPlay);
	flashMovie.Play();
	jQuery.fn.SCT2_elementFocusState();
}
function swfRewind(toRewind) {
	var swfToRewind = toRewind;
	var flashMovie=getFlashMovieObject(swfToRewind);
	flashMovie.Rewind();
	jQuery.fn.SCT2_elementFocusState();
}


/*  ## CENTRE VERTICALLY  ------------------------------------------------------*/
jQuery.fn.vAlign=function() {
		return this.each(function(i) {
			var eh = jQuery(this).height();
			var ph = jQuery(this).parent().height();
			var mh = (ph-eh)/2;
			jQuery(this).css('margin-top', mh);
		}
	);
}; // End jQuery.fn.vAlign=function()

var SCT2_sctRunning = false;
var SCT2_flvCapable = false;
var SCT2_flvPlaying = false;
var SCT2_html5VideoCapable = false;
var SCT2_html5VideoPlaying = false;
var SCT2_swfCapable = false;
var SCT2_swfFirstRun = false;
var SCT2_swfPlaying = false;
var SCT2_swfMovie = "";
var SCT2_pointerHovering = false;
var SCT2_outgoingSlide = "";
var SCT2_incomingSlide = "";

jQuery.fn.SCT2_browserCapabilityCheck=function() {
	if(FlashDetect.versionAtLeast(9, 0)){
        SCT2_flvCapable = true;
    }
	if(FlashDetect.versionAtLeast(10, 0)){
        SCT2_swfCapable = true;
    }
} // End jQuery.fn.SCT2_browserCapabilityCheck=function()

jQuery.fn.SCT2_sctState=function() {
	// If anything is happening with either the pointer or a piece of embedded media at all pause the carousel
	if((SCT2_pointerHovering == true) || (SCT2_flvPlaying == true) || (SCT2_swfPlaying == true) || (SCT2_html5VideoPlaying == true)) {
		jQuery('.sct > div:first-child .sct-slides').cycle('pause');
		return;
	} else {
		jQuery('.sct > div:first-child .sct-slides').cycle('resume');
		return;
	}
} // End jQuery.fn.SCT2_sctState=function()
jQuery.fn.SCT2_elementFocusState=function() {
	if (document.activeElement.tagName == 'OBJECT') {
		document.activeElement.blur();
	}
	return;
} // End jQuery.fn.SCT2_elementFocusState=function()
jQuery.fn.SCT2_getSlideDuration=function(currElement, nextElement, opts, isForward) {
	var index = opts.currSlide;
	return SCT_slideDuration[index] * 1000;
} // End jQuery.fn.SCT2_getSlideDuration=function(currElement, nextElement, opts, isForward)
jQuery.fn.SWF_animationStart=function() {
	if ((SCT2_sctRunning == true) && (SCT2_swfFirstRun == false)) {
		SCT2_swfFirstRun = true;
		SCT2_swfPlaying = true;
		jQuery.fn.SCT2_elementFocusState();
		jQuery.fn.SCT2_sctState();
		return;
	}
} // End jQuery.fn.SWF_animationStart=function()
jQuery.fn.SWF_animationEnd=function() {
	if (SCT2_sctRunning == true) {
		SCT2_swfPlaying = false;
		jQuery.fn.SCT2_elementFocusState();
		jQuery.fn.SCT2_sctState();
		return;
	}
} // End jQuery.fn.SWF_animationEnd=function()
jQuery.fn.SCT2_beforeTransitionUpdate=function(outgoingSlide, incomingSlide) {
	SCT2_outgoingSlide = outgoingSlide;
	SCT2_incomingSlide = incomingSlide;
	if((SCT2_flvCapable == true) && (jQuery(SCT2_outgoingSlide).hasClass('flv-video'))) {
		jQuery(SCT2_outgoingSlide).find('.video a').flowplayer().each(function() {
		  // 'this' refers to the current Player instance
		  this.pause();
		  //this.seek(0);
		});
		SCT2_flvPlaying = false;
	}
	if((SCT2_swfCapable == true) && (jQuery(SCT2_outgoingSlide).is('.is-swf, .has-swf'))) {
		SCT2_swfOutgoingMovie = jQuery(SCT2_outgoingSlide).find('.swf object').attr('id');
		jQuery.fn.SWF_animationEnd();
		swfStop(SCT2_swfOutgoingMovie);
		SCT2_swfFirstRun = false;
	}
} // End jQuery.fn.SCT2_beforeTransitionUpdate=function(outgoingSlide, incomingSlide)
jQuery.fn.SCT2_afterTransitionUpdate=function(outgoingSlide, incomingSlide) {
	SCT2_outgoingSlide = outgoingSlide;
	SCT2_incomingSlide = incomingSlide;
	if((SCT2_flvCapable == true) && (jQuery(SCT2_outgoingSlide).hasClass('flv-video'))) {
		jQuery(SCT2_outgoingSlide).find('.video a').flowplayer().each(function() {
		  // 'this' refers to the current Player instance
		  this.unload();
		  //this.seek(0);
		});
		SCT2_flvPlaying = false;
	}
	if((SCT2_swfCapable == true) && (jQuery(SCT2_outgoingSlide).is('.is-swf, .has-swf'))) {
		SCT2_swfOutgoingMovie = jQuery(SCT2_outgoingSlide).find('.swf object').attr('id');
	}
	if((SCT2_swfCapable == true) && (jQuery(SCT2_incomingSlide).is('.is-swf, .has-swf'))) {
		SCT2_swfIncomingMovie = jQuery(SCT2_incomingSlide).find('.swf object').attr('id');
		swfRewind(SCT2_swfIncomingMovie);
		jQuery.fn.SWF_animationStart();
		swfPlay(SCT2_swfIncomingMovie);
	}
} // End jQuery.fn.SCT2_afterTransitionUpdate=function(outgoingSlide, incomingSlide)

jQuery.fn.SCT2_beforeTransition=function(curr,next,opts) {
	var currentSlide = opts.currSlide +1;
	SCT2_outgoingSlide = jQuery('.sct-wrapper .sct-slides .slide:nth-child('+ currentSlide +')');
	jQuery.fn.SCT2_beforeTransitionUpdate(SCT2_outgoingSlide,SCT2_incomingSlide);
} // End jQuery.fn.SCT2_beforeTransition=function(curr,next,opts)
jQuery.fn.SCT2_afterTransition=function(curr,next,opts) {
	//var currentSlide = opts.currSlide;
	SCT2_incomingSlide = jQuery(this);
	jQuery.fn.SCT2_afterTransitionUpdate(SCT2_outgoingSlide,SCT2_incomingSlide);
} // End jQuery.fn.SCT2_afterTransition=function(curr,next,opts)



/*  ## SET UP FLOWPLAYER FOR ALL FLV VIDEOS  ------------------------------------------------------*/

jQuery.fn.SCT2_flvSetup=function() {
	if (SCT2_flvCapable == true) {
		jQuery('.sct .flv-video').each(function(index) {
			
			var videoFile = jQuery(this).find('.slide-meta a.video-file').attr('href');
			var posterImage = jQuery(this).find('.slide-meta img.poster-image').attr('src');
			var videoContainer = jQuery(this).find('div.video');
			
			jQuery(videoContainer).addClass('video-enabled');
			
			var flowplayerPoster = '<a href="'+ videoFile +'" class="poster-link"><img src="'+ posterImage +'" class="poster-image"><span></span></a>';

			jQuery(this).find('.video').empty().append(flowplayerPoster);

			jQuery(videoContainer).children('a[href*=.flv]').flowplayer(
				{
					src: '/swf/flowplayer.commercial-3.2.4.swf',
					wmode: 'transparent'
				},
				{
					key: '#$2ac86d7410b959c562b',
					play:{
						label: null,
						replayLabel: null,
						url: '/images/layout/sct/sct-video-play.png',
						width: 73,
						height: 73
					},
					playlist:[{
						url:posterImage,
						scaling:'fit'
						},
						{
						url:videoFile,
						autoPlay:true,
						autoBuffering:false,
						scaling:'fit',
						onStart: function(clip) {
							SCT2_flvPlaying = true;
							jQuery.fn.SCT2_elementFocusState()
							jQuery.fn.SCT2_sctState()
						},
						onPause: function(clip) {
							SCT2_flvPlaying = false;
							jQuery.fn.SCT2_elementFocusState()
							jQuery.fn.SCT2_sctState()
						},
						onResume: function(clip) {
							SCT2_flvPlaying = true;
							jQuery.fn.SCT2_elementFocusState()
							jQuery.fn.SCT2_sctState()
						},
						onFinish: function(clip) {
							SCT2_flvPlaying = false;
							jQuery.fn.SCT2_elementFocusState()
							jQuery.fn.SCT2_sctState()
						}
					}],
					plugins:{
						controls:{
							bottom:0,
							fullscreen:'true',
							autoHide:'always',
							url: '/swf/flowplayer.controls-3.2.2.swf',
							backgroundGradient: 'none',
							backgroundColor:'rgba(0, 0, 0, 0.6)',
							bufferColor:'#a3a3a3',
							progressColor:'#4599ff',
							sliderColor: '#FFFFFF',
							sliderBorder: '1.5px solid rgba(160,160,160,0.7)',
							volumeSliderColor: '#FFFFFF',
							volumeBorder: '1.5px solid rgba(160,160,160,0.7)',
							volumeColor:'#4599ff',
							timeColor: '#ffffff',
							durationColor: '#ffffff',
							tooltipColor: 'rgba(255, 255, 255, 0.7)',
							tooltipTextColor: '#000000'
						}
					},
					clip:{
						scaling:"fit"
					},
					canvas:{
						backgroundGradient:'none',
						backgroundColor:'rgba(245,245,245,0)'
					}
				}
			); // End jQuery(videoContainer).children('a[href*=.flv]').flowplayer
		}); // End jQuery('.sct .flv-video').each(function(index)
	} // End if (SCT2_flvCapable == true)
} // End jQuery.fn.SCT2_flvSetup


jQuery.fn.SCT2_sctSetup=function() {
	jQuery('.sct').each(function(index) {
		if(jQuery(this).find('.sct-slides .slide').length > 1) {
			jQuery(this).addClass('sct-multislide');
			SCT2_sctRunning = true;
			jQuery(this).find('.sct-wrapper').append('<div class="sct-nav"><a class="prev">Previous slide</a><span class="slides"></span><a class="next">Next slide</a></div>');
		    jQuery(this).find('div:first-child div.sct-slides').cycle ({
				fx: 'scrollHorz',
				speed: 1000,
				timeoutFn: jQuery.fn.SCT2_getSlideDuration,
				pager: jQuery('.sct-nav .slides'),
				pagerAnchorBuilder: function(idx, slide) {
					return '<span class="slide"><img src="' + jQuery(slide).find('img.slide-thumbnail').attr('src') + '" class="unhover-show"><img src="' + jQuery(slide).find('img.slide-thumbnail').attr('src') + '" class="hover-show unhover-hidden"></span>';
				},
				pagerClick: function(zeroBasedSlideIndex, slideElement) {
					jQuery.fn.SCT2_sctState();
				},
				prev: jQuery('.sct-nav .prev'),
				next: jQuery('.sct-nav .next'),
				prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement) {
					jQuery.fn.SCT2_sctState();
				},
				containerResize: 1,
				before: jQuery.fn.SCT2_beforeTransition,
				after: jQuery.fn.SCT2_afterTransition
		    }) // End jQuery(this).find('div:first-child div.sct-slides').cycle
			jQuery(this).find('.sct-slides .slide').each(function(index){
				jQuery(this).removeClass('nonjs-hidden');
			}); // End jQuery(this).find('.sct-slides .slide').each(function(index)
			jQuery(this).find('.sct-nav .slides :not(.activeSlide) .hover-show').hover(
				function() {
					jQuery(this).siblings('.unhover-show').addClass('hover-hide');
					jQuery(this).parent().addClass('hover-parent');
					jQuery(this).removeClass('unhover-hidden').addClass('hovering').removeClass('unhover-hidden').stop(true, true).animate({
						opacity: 1,
						left: '-10px',
						height: '38px',
						width: '78px'
					}, 125, function() {
						// Animation complete.
					});
				},
				function() {
					jQuery(this).stop(true, false).animate({
						opacity: 0.5,
						left: '0px',
						height: '28px',
						width: '58px'
					}, 125, function() {
						jQuery(this).siblings('.unhover-show').removeClass('hover-hide');
						jQuery(this).removeClass('hovering').addClass('unhover-hidden');
						jQuery(this).parent().removeClass('hover-parent');
					});
				}
			); // End jQuery(this).find('.sct-nav .slides :not(.activeSlide) .hover-show').hover
			jQuery(this).find('.sct-nav .slides .activeSlide .hover-show').hover(
				function() {
					jQuery(this).parent().addClass('hover-parent');
					jQuery(this).removeClass('unhover-hidden').addClass('hovering').removeClass('unhover-hidden').stop(true, true).animate({
						opacity: 1,
						left: '-10px',
						height: '38px',
						width: '78px'
					}, 125, function() {
						// Animation complete.
					});
				},
				function() {
					jQuery(this).stop(true, false).animate({
						opacity: 1,
						left: '0px',
						height: '28px',
						width: '58px'
					}, 125, function() {
						jQuery(this).removeClass('hovering').addClass('unhover-hidden');
						jQuery(this).parent().removeClass('hover-parent');
						// Animation complete.
					});
				}
			); // End jQuery(this).find('.sct-nav .slides .activeSlide .hover-show').hover
		
			jQuery(this).find('.slide-meta').remove();
		
//			jQuery(this).find('.sct-wrapper').hover(
//				function() {
//					SCT2_pointerHovering = true;
//					jQuery.fn.SCT2_sctState()
//				},
//				function() {
//					SCT2_pointerHovering = false;
//					jQuery.fn.SCT2_sctState()
//				}
//				
//			); // End jQuery(this).find('.sct-wrapper').hover
			
			jQuery(this).find('.sct-wrapper').bind('hover',function(){
				SCT2_pointerHovering = true;
				jQuery.fn.SCT2_sctState()
			})
			.bind('hoverend',function(){
				SCT2_pointerHovering = false;
				jQuery.fn.SCT2_sctState()
			}); // End jQuery(this).find('.sct-wrapper').bind('hover',function()
			
		
		}; // End if(jQuery(this).find('.sct-slides .slide').length > 1) 
	}); // End jQuery('.sct').each(function(index)
} // End jQuery.fn.SCT2_sctSetup

jQuery(document).ready(function(){
	jQuery.fn.SCT2_browserCapabilityCheck();
	jQuery.fn.SCT2_flvSetup();
	jQuery.fn.SCT2_sctSetup();
});

jQuery(window).load(function(){
	if (jQuery.support.opacity) {
		jQuery('.sct-nav img.unhover-show').each(function(index){
			jQuery(this).pixastic('desaturate');
		});
	}
	jQuery('.sct-slides .slide').each(function(index){
		jQuery(this).vAlign();
	});
});
