window.more_link = [ '', '' ];
window.playing = false;

function setupPage(){
	
	// highlight active menu item
	$.each( $("#nav li a"), function(){
		
		if ( $(this).text() == window.pagetitle )
			$(this).parent().addClass('active');
	});
	
	// set window title
	document.title = document.title + " :: " + window.pagetitle;
	
	// set page title
	$("#header .title img").attr('src', 'images/titles/' + window.pagetitle + '.png')
	
	// prepare playlist
	$.each( $("#playlist a"), function(){
		var id = $(this).attr('id');
		id = id.replace('i', '');
		
		var pageid = window.pageid.replace(".", "");
	
		$(this).prepend("<img src='/uploads/images/" + pageid + "." + id + "_thumb.jpg' />");

		$(this).append('<span class="play_overlay"></span>');
	});
	
	// make more link
	if ( window.more_link[0] != '' )
	{
		var link = "<a href='"+ window.more_link[1]+ "' >" + window.more_link[0] + "</a>";
		$("#more_link").html(link);
	}
}

function regularVideo(){
	
	if ( window.disableVideo === undefined){ window.disableVideo = false; }

	// insert close button (hidden)
	$("#preview").append("<a href='javascript:window.closeButton();' id='close-button'>Close video &nbsp;<img src='images/close.png'></a>");
	$("#close-button").hide();
	

	// playlist animation
	
	$('#playlist a').hover(function(){
				
		// don't do anything if a video is playing
		if ( !window.playing ) {
			
			// the About Flicks page doesn't have video
			if (!window.disableVideo)
			{
				$('.play_overlay', this).addClass('visible');
			} 
		
			// description
			var id = $(this).attr('id');
			id = id.replace('i', '');
			
			var description = $("#descriptions #description"+id).html();
			$('#rightpanel').html(description);
		
			// preview image
			var pageid = window.pageid.replace(".", "");
			$('#preview_image').html('<img src="/uploads/images/' + pageid + '.' + id + '.jpg" >').show();
		
		
		}
		

	}, function(){
		if (!window.playing) {
			$('.play_overlay', this).removeClass('visible');
		}
	});

	
	// hide preview on play
	
	$('#playlist a').click(function(e){
		
		// if is not playing
		if ( !window.playing && !window.disableVideo){
			
			window.playing = true;
			$('#preview_image').hide();
			$("#close-button").show();
			$("#playlist a").fadeTo('slow', 0.1);

		}
	});
	
	
	// close button
	window.closeButton = function(){
		window.playing = false;
		$("#close-button").hide();
		$("#playlist a").fadeTo('slow', 1);
		$('#preview_image').show();
		$f().stop();
	}

	$f("player", {src:"js/flowplayer.commercial-3.2.2.swf", wmode: 'opaque'}, {
		// product key from your account
		key: '#$9c22348d8aed7c7aca9',
		clip: {
			baseUrl: '/uploads/video/'
		},
		// change the default controlbar to modern
		plugins: {
			controls: {

				url: 'js/flowplayer.controls-3.2.1.swf',

				all:false,
				play:true,
				scrubber:true,
				fullscreen: true,

				backgroundColor: "transparent",
				backgroundGradient: "none",

				sliderColor: '#FFFFFF'

			}
		}

	// setup playlist plugin for the root entry of our playlist 
	}).playlist("#playlist");
	
	
}

function mobileVideo( browser ){
	$('#playlist a').each(function(){
		var link = $(this).attr('href');
		$(this).attr('href', '/mobile.php?v='+link+'&type='+browser);
	});
}

// start the magic

$(function(){
	
	setupPage();
	
	// check for mobile browsers
	var ua = navigator.userAgent.toLowerCase();
	var isAndroid = ua.indexOf("android") > -1;
	var isiOS = (ua.indexOf('iphone') > -1) || (ua.indexOf('ipad') > -1)
	var isMobile = ua.indexOf("mobile") > -1 ;
	
	
	if( isAndroid ) {
		mobileVideo('android');
	} 
	else if (isiOS) {
		mobileVideo('iOS');
	}
	else if (isMobile) {
		mobileVideo('mobile');
	} 
	else {
		regularVideo();
	}
	
});
