$('html').addClass('js');

// Click block interfering with playVideo
html5VideoInit = false;

function loadFlowplayer(){
	flowplayer(
		"flowholder",{src: "tpl/js/flowplayer-3.2.7-0.swf", wmode: 'transparent'},
		{
			clip:{
				autoPlay:true,
				autoBuffering:true,
				scaling:'orig'
				//url:'../geisha/assets/video/1295522483.flv',
				//coverImage: 'geisha/assets/video/stills/1295522483.003.jpg',
			},
			screen:{
				width:555, height: 418, top:'50%', right:0
			},
			canvas:{
				backgroundColor: "#000",
				backgroundGradient: "none"
			}
			/*
			plugins:{
				audio:{
					url: 'tpl/js/flowplayer/flowplayer.audio-3.2.1.swf'
				}
			}
			*/
		}
	).onLoad(function(){
		var t=setTimeout("$('#vid-question').slideDown(500)", 800);
	}).onBegin(function(){
		//$('#video-area').css('background','none');
	});
}// Change to animate? jerky with padding at bottom loaded after animation


function playVideo(context, render){
	var infoBar = context.parent().parent(),
	t;
	infoBar.animate({
		'marginBottom':-infoBar.outerHeight()
	}, function(){
		infoBar.css('display', 'none');
		if(render === 'flash'){
			//console.log('in flash')
			t=setTimeout("loadFlowplayer()", 200);
			//$('#flowholder').fadeOut();
		}else if(render === 'html5'){
			//videoElem = $('video#html5video');
			$('#html5video').hide();
			$('#flowholder').fadeOut('slow', function(){
			$('#html5video').show();
				html5VideoInit = true;
				t=setTimeout("$('#vid-question').slideDown(500, function(){$('video#html5video')[0].play();})", 400);
			});
		}		
	});
	var vid=($('#flowholder').attr('rel'));
	$.get('inc/ajax.php?method=t&v='+vid);
	return false;
}


function videoControl(video){
	//video.paused ? video.play() : video.pause();
	return false;
}

	
/*-------------
triggerBlocks
------------------------*/

// Jon White 07/02/2011
// Methods for binding achor events to parent elements

// 18/02/2011 - Added freeze param to stop window.location.

// Use the class 'click-block' to mark triggering block elements
// All events are bound to the 'chain' anchor within the target block
// Use the 'arm' method to bind, passing a jQuery element
// Specify 'click', 'hover' or 'both' (default) for the type of events to add


// Binder Object
var triggerBlock = {
	arm: function(block, type, freeze){
		var self = this;
		switch (type){
			case "both":
				block.bind('click', function(e){self.tripClick($(this), 'a.chain', freeze, e);})
				.bind('mouseover', function(){
					self.tripHover($(this), 'a.chain');
					block.css('cursor', 'pointer');
				})
				.bind('mouseout', function(){self.tripHover($(this), 'a.chain')});
			break;
			case "click":
				block.bind('click', function(e){self.tripClick($(this), 'a.chain', freeze, e);})
				.bind('mouseover', function(){block.css('cursor', 'pointer');});
			break;
			case "hover":
				block.bind('mouseover', function(){
					self.hover(block, 'a.chain');
					block.css('cursor', 'pointer');
				})
				.bind('mouseout', function(){self.tripHover($(this), 'a.chain')});
			break;
			default:
				block.bind('click', function(e){self.tripClick($(this), 'a.chain', freeze, e);})
				.bind('mouseover', function(){
					self.tripHover($(this), 'a.chain');
					block.css('cursor', 'pointer');
				})
				.bind('mouseout', function(){self.tripHover($(this), 'a.chain')});
				//console.log($(this));	
			break;
		}
	},
	tripClick: function(context, trigger, freeze, e){
	if(context.data('bound') !== 'true'){
			context.find(trigger).bind('click', function(e){
				e.stopPropagation();
				!freeze ? window.location=this.href : null;
				// !freeze ? console.log('nofreeze') : console.log('freeze');
				context.data('bound', 'true');
				//return false;
			});			
		}
		context.find(trigger).click();
	},
	tripHover: function(context, trigger){context.find(trigger).toggleClass('selected');}
}


$(function(){	
	
	// Test the browser for html5 video
	var html5Video = $('video').first().attr('ended');
	
	/*-------------
	Flow player
	------------------------*/
	$('p.play a').bind('click', function(){
		if(html5Video != undefined){
			if(!html5VideoInit){
				playVideo($(this), 'html5');
			}
		}else{
			playVideo($(this), 'flash');
		}
		return false;
	});
	
	/*----------------------
	Javascript form elements
	---------------------------------------*/
	$(function(){
		
		//------ Tag multiselect replacement
		
		$('#f-ask div.tags ul li a').live('click', function(){
			var context = $(this);
			var select = context.parent().parent().parent().find('#f-tags');
			if (!context.hasClass("selected")){
				select.find('option[value="' + context.text() + '"]').attr("selected","selected");
			}else{
				select.find('option[value="' + context.text() + '"]').attr("selected","");
			}
			context.toggleClass("selected");
			return false;
		});
	
		//------ Dropdown replacements 
		
		$('form select.dropdown').next().find('li:first');
		var dList = $('form select.dropdown').next().find('li:first').append('<ul class="off"></ul>');
		
		$('form select.dropdown').find('option').each(function(i){
			/*i===0 ? $(this).parent().next().find('li a').text($(this).text()) : null;
			if(i>0){
				$(this).parent().next().find('ul').append('<li><a href="#">' + $(this).text() + '</a></li>');
			}*/
			
			if($(this).is(':selected')){
				$(this).parent().next().find('li:first a:first').text($(this).text());
			}else{
				$(this).parent().next().find('ul').append('<li><a href="#">' + $(this).text() + '</a></li>');
			}

			
		});

		dList.bind('click', function(){
			var context = $(this);
			context.find('ul').toggleClass('off');
			return false;
		});
		
		
		dList.find('ul li a').bind('click', function(e){
			var context=$(this);
			var select = context.parent().parent().parent().parent().prev();
			select.find('option[title="' + context.text() + '"]').attr('selected', 'selected');
			select.next().find('li:first a:first').text(context.text());
			if(select.next().hasClass('auto-click')){
				select.next().next().click();
			}
			//e.stopPropagation();
		});
	
	});
	

	
	//-------------- Arm triggers
	
	triggerBlock.arm($('#video-area'), null, true);
	triggerBlock.arm($('#dash-questions li.click-block:not(.follow)'), null, true);
	triggerBlock.arm($('#dash-questions li.click-block.follow'));
	
	
	/* --------------------
	Social Sharing
	-----------------------*/
	$('#share li.more a:first').bind('click', function(){
		var context = $(this),
		li=context.parent(),
		container = context.parent().find('.web2');
		
		if (container.hasClass('shown')){
			li.animate({width: 20});
			container.fadeOut('fast');
			container.removeClass('shown');
		} else {
			li.animate({width: 124});
			container.fadeIn('slow');
			container.addClass('shown');
		}
		return false;
	});		
	
	
	/*-------------
	Tiny labels
	------------------------*/
	$(function(){
		var label = $('form label.tiny').css('display', 'none');
		label.each(function(){
			var lbl=$(this);
			lbl.next().val(lbl.text()).bind('focus', function(){
				var txt=$(this);
				txt.val() === lbl.text() ? txt.val('') : null;
				// This is a domain specific augmentation
				if (lbl.attr('id', 'f-ssearch-keywords')){
					txt.next().css({
						'backgroundColor': '#FFFFFF',
						'backgroundPosition': '-77px 0'
					}).end().data('ssearch', 'true');
				}
			});
			lbl.next().bind('blur', function(){
				var txt=$(this); 
				txt.val() === lbl.text() || txt.val() === '' ? txt.val(lbl.text()) : null;
				// This is a domain specific augmentation
				if (txt.data('ssearch') === 'true'){
					txt.next().css({
						'backgroundColor': '#575659',
						'backgroundPosition': '8px 0'
					}).data('ssearch', 'false');
				}
			});
		});
	});
	
	

});
