$(document).ready(function() {

	var hash = document.location.hash;
	/* faq */

	$("div.faq ul:not(ul.contact-box) li").children().hide();
	$("div.faq h5").show().click(function() {
		if($(this).siblings().css("display") == "none") {
			$(this).parent().siblings().find("p").hide();
			$(this).siblings().show();
		} else {
			$(this).siblings().hide();
		}
	});
	if(document.location.search.substr(0, 9) == "?csearch=") {
		var searchTerm = unescape(document.location.search.substr(9)).toLowerCase();
		var Term = $('*', 'body')
		.andSelf()
		.contents()
		.filter(function(){
		return this.nodeType === 3;
		})
		.filter(function(){
		return this.nodeValue.toLowerCase().indexOf(searchTerm) != -1;
		})
		.filter(function(){
		var nn = $(this).parent().get(0).nodeName;
		if( nn == 'P' || nn == 'H3' || nn == 'H5' ) {
		return true;
		}
		return false;
		})[0];

		if(Term) {
			Term = $(Term).parent().get(0);

			if(Term.nodeName == 'P') {
				$(Term).parent().children('h5').siblings().show();
				$('html, body').animate({
					scrollTop : $(Term).parent().offset().top
				}, 1);
			} else if(Term.nodeName == 'H5') {
				$('html, body').animate({
					scrollTop : $(Term).offset().top
				}, 1);
				$(Term).siblings().show();
			} else {
				$('html, body').animate({
					scrollTop : $(Term).offset().top
				}, 1);
			}
		}
	}

	/* help */
	$("ul.help > li").children().hide();
	$("ul.help h4").show().click(function() {
		if($(this).siblings().css("display") == "none") {
			$(this).siblings().show();
		} else {
			$(this).siblings().hide();
		}
	});
	/* gallery function */
	$("ul#gallery a").click(function(e) {
		e.preventDefault();
		$("#gallery-view").remove();
		$("ul#gallery").after('<img src="' + this.href + '" alt="" id="gallery-view">');
		return false;
	});
	/* videos */
	if($("#video.dba-partnership").length) {
		swfobject.embedSWF("/assets/common/swf/vw_player.swf", "video", "351", "308", "9", null, {}, {
			quality : "high",
			align : "middle",
			wmode : "transparent",
			allowScriptAccess : "always"
		}, {});
	}

});



/* run iphone app video in colorBox panel */

VideoJS.setupAllWhenReady();
asset_host=location.protocol+"//"+location.host;

// Having the HTML within this piece of js is a bit icky, so find a way of not having it in here in future.

video_player = '<div class="video-js-box">' + 
					'<video id="example_video_1" class="video-js" width="960" height="540" controls="controls" preload="auto" poster="/assets/common/videos/apps/app_poster.jpg">' + 
						'<source src="/assets/common/videos/apps/FullAdComp-VerticalApp_960x540-2.mp4" type="video/mp4" />' + 
						'<source src="/assets/common/videos/apps/FullAdComp-VerticalApp_960x540-2.webm" type="video/webm" />' + 
						'<source src="/assets/common/videos/apps/FullAdComp-VerticalApp_960x540-2.ogv" type="video/ogg" />' + 
				  
						'<object id="flash_fallback_1" class="vjs-flash-fallback" width="960" height="540" type="application/x-shockwave-flash"	data="/assets/common/swf/flowplayer-3.2.7.swf">' + 
							'<param name="movie" value="/assets/common/swf/flowplayer-3.2.1.swf" />' + 
							'<param name="allowfullscreen" value="true" />' + 
							'<param name="wmode" value="transparent" />' + 
							'<param name="flashvars" value=\'config={"playlist":["'+asset_host+'/assets/common/videos/apps/app_poster.jpg", {"url": "'+asset_host+'/assets/common/videos/apps/FullAdComp-VerticalApp_960x540.flv","autoPlay":false,"autoBuffering":true}]}\' />' + 
							'<img src="'+asset_host+'/assets/common/videos/apps/app_poster.jpg" width="960" height="540" alt="Poster Image" title="No video playback capabilities." />' + 
						'</object>' + 
					'</video>' + 
				'</div>';

$(document).ready(function() {
	
	/* so can be hidden if no JS, initial state of the play button is hidden */
	$("div.mobileApps .play_video, div.mobileApps #view_apps_advert").show();
	
	$("div.mobileApps .play_video img").mouseover(function() {
		if (!$(this).get(0).style.filter){
		$(this).animate({
			opacity : 1
		}, 400);
		}
	}).mouseout(function() {
		if (!$(this).get(0).style.filter){
		$(this).animate({
			opacity : 0.7
		}, 400);
		}
	}).click(function(e) {
		e.preventDefault();
		$("#cboxClose").addClass("video_panel_closer").get(0).title = "Close";
		$.colorbox({
			top : 26,
			close : "",
			html : video_player,
			width : 1010,
			height : 595,
			onClosed : function() {
				$("#cboxClose").removeClass("video_panel_closer");
			}
		}, function() {
			/* when colorbox is loaded activate HTML5 player */
			VideoJS.setupAllWhenReady();
		});
	});
});

