var is_opera =  navigator.userAgent.indexOf('Opera') > -1;
var is_safari = navigator.userAgent.indexOf('AppleWebKit/') > -1;
var is_chrome = navigator.userAgent.indexOf('Chrome') > -1;
var is_firefox = navigator.userAgent.indexOf('Firefox') > -1;
var is_mac = navigator.platform.toLowerCase().indexOf('mac') > -1;
var is_linux = navigator.platform.toLowerCase().indexOf('linux') > -1;
var ie6 = false;
var ie7 = false;
var ie8 = false;
var ie9 = false;

$(document).ready(function() {

	$('a').map(function(i, el){
		if($(el).attr('rel') == 'external') {
			$(el).attr('target', '_blank');
		}
	})        

	if(is_safari) {
		$('body').addClass('browser_safari');
	}

	if(is_opera) {
		$('body').addClass('browser_opera');
	}
	
	$('#navigation a').bind('mouseover', function(event) {
		var parent_li = $(event.currentTarget).parent('li');
		$(parent_li).addClass('over');
	});
	
	$('#navigation a').bind('mouseout', function(event) {
		var parent_li = $(event.currentTarget).parent('li');
		$(parent_li).removeClass('over');
	});

    if(typeof portfolio_items == 'object') {
        portfolio_items.setup();
    }
	
	$('#layout').append('<div id="player-container"></div>');

	$("#player-container").jPlayer({
		swfPath: "_scripts/jquery/ui/sound/",
		solution: 'flash, html',
		volume: 0.6,
		supplied: "mp3"
	});			

});


var portfolio_items = {
    container: null,
	nav_items: [],
	decription_items: [],
	last_item: null,
	content_items: null,

    setup: function() {
		this.container = $('#featured-content');
		
		$(this.container).append('<span id="left_frame"></span>');
		$(this.container).append('<span id="right_frame"></span>');
		
		if(this.container.length == 0) return;

		this.initCarousel();
	
		this.nav_items = $('#featured-content .nav a');
		
		$(this.nav_items).bind('click', function(event) {
			event.preventDefault();
			var item_index = $(portfolio_items.nav_items).index(event.currentTarget);

			portfolio_items.loadTab(item_index);
        });
		
		this.loadTab(0);
    },
	
	loadTab: function(tab_no) {
		
			var related_a = this.nav_items[tab_no];

			var parent_li = $(related_a).parents('li')[0];
            if($(parent_li).hasClass('selected')) return;
			
			$("#player-container").jPlayer("clearMedia");
			
			$('#portfolio_decription').remove();

			$('#featured-content .nav li').removeClass('selected');
			$(parent_li).addClass('selected');

			var ajax_url = $(related_a).attr('rel') + '.php';
			
			$('#featured-content').addClass('loading');

			$('#play-button').remove();
			$.get(ajax_url, function(data){
				$('#featured-content').removeClass('loading');
				$('#portfolio_items_container').html(data);
				
				$('#project_description').html($('#portfolio_decription'));
				
				portfolio_items.decription_items = $('#project_description .item');
				portfolio_items.content_items = $('#portfolio-items li');
				$(portfolio_items.decription_items[0]).addClass('active');
				portfolio_items.last_item = 0;
				
				var items_no = $('#portfolio-items a').length;
				if(items_no >0) {
					$('#portfolio_items_container').append('<div id="play-button"><a href="#"></a></div>');

					$('#play-button a').bind('click', function(event) {
						
						event.preventDefault();
						
						var is_web = $('.portfolio-web');
						var is_video = $('.portfolio-video');
						var is_audio = $('.portfolio-audio');

						var related_a = $('a', portfolio_items.content_items[portfolio_items.last_item]);
						
						if(is_web.length == 1) {
							window.open($(related_a).attr('href'));
						} else if(is_video.length == 1) {
							$(related_a).trigger('click');
						} else if(is_audio.length == 1) {
							event.preventDefault();
							portfolio_items.playSound(related_a);
						}
					});						
					
				}
				
				setTimeout(function(){portfolio_items.initCarousel();}, 100);
			});		
	},
	
	playSound: function(related_a) {
		$('#player-container').jPlayer("setMedia", {
			mp3: $(related_a).attr('href')
		}).jPlayer("play");
	},
	
	initCarousel: function() {
		var thumbs_no = $('li', '#portfolio_items_container').length;
		
		function initContentLinks() {

			$('.jcarousel-skin-portfolio li a').bind('click', function(event) {
				event.preventDefault();
			});
			
			$(".portfolio-video li a").fancybox({
				'overlayShow'	:	false
			});
		}

		if(thumbs_no == 1) {
			jQuery('.jcarousel-skin-portfolio').jcarousel({itemFallbackDimension: 409});
			
			initContentLinks();
		} else {
			jQuery('.jcarousel-skin-portfolio').jcarousel({
				scroll: 1,
				wrap: 'circular',
				itemVisibleInCallback: {
					onAfterAnimation:  function() {
						
						initContentLinks();
					}
				},
				itemFallbackDimension: 409
			});
		}
		
		
		
		$('.jcarousel-prev').bind('click', function(event) {
			event.preventDefault();
			
			$("#player-container").jPlayer("clearMedia");
			
			portfolio_items.last_item -= 1;
			if(portfolio_items.last_item < 0) {
				portfolio_items.last_item = portfolio_items.decription_items.length - 1;
			}
			
			$(portfolio_items.decription_items).removeClass('active');
			$(portfolio_items.decription_items[portfolio_items.last_item]).addClass('active');			
        });
		
		$('.jcarousel-next').bind('click', function(event) {
			event.preventDefault();
			
			$("#player-container").jPlayer("clearMedia");
			
			portfolio_items.last_item += 1;
			if(portfolio_items.last_item > portfolio_items.decription_items.length - 1) {
				portfolio_items.last_item = 0;
			}
			
			$(portfolio_items.decription_items).removeClass('active');
			$(portfolio_items.decription_items[portfolio_items.last_item]).addClass('active');
        });
		
	}
}


function displayVideo(video_path, video_title, video_desc) {
	var flashvars = {};
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	// CUSTUMIZABLE PARAMETERS ///////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	
	// video width
	var stageW = 590;
	
	// video height		NOTE: you should include the control bar height
	var stageH = 380;
	
	///////////////////////////////////////////////////////////////////////
	// PATHS //////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////

	// image path
	flashvars.imagePath			= "images/logo.png";

	// video path
	flashvars.videoPath 		= video_path; //"videos/preview.flv";

	// video title
	flashvars.title 			= video_title; //"Video Title goes here";

	// video description
	flashvars.description 		= video_desc; //"Some description over this video! \nAlmost all control buttons can easily be turn on or off.";
				
	///////////////////////////////////////////////////////////////////////
	// VIEW CONTROLS ////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////

	// view information button 		 NOTE: this will display the title and description of the video
	flashvars.viewInfoButton 		=	"true";

	// view fullscreen button		( true / false )
	flashvars.viewFullscreenButton 	=	"true";

	// view scale button			( true / false )
	flashvars.viewScaleButton 		=	"true";

	// view volume controls			( true / false )
	flashvars.viewVolumeControls 	=	"true";

	// view video time				( true / false )
	flashvars.viewTime				=	"true";

	// view big middle button		( true / false )
	flashvars.viewBigPlayButton 	=	"true";
	
	// view right click menu		( true / false )
	flashvars.viewRightClickMenu 	=	"true";
	
	////////////////////////////////////////////////////////////////////////
	// MOUSE FUNCTIONS //////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////

	// mouse hide							( true / false )
	flashvars.mouseHide				=	"true";

	// mouse hide after # (seconds)		NOTE : Must be a hole number !
	flashvars.mouseHideTime			=	"3";

	// double click for toggle size view	( true / false )
	flashvars.doubleClick			=	"true";

	// click the video for play/pause		( true / false )
	flashvars.oneClick				=	"true";
	
	////////////////////////////////////////////////////////////////////////
	// KEYBOARD FUNCTIONS ///////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////

	// play/pause on SPACE key 		( true / false )
	flashvars.spaceKey				=	"true";
	
	////////////////////////////////////////////////////////////////////////
	// VIDEO FUNCTIONS ///////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////

	// video loop				( true / false )
	flashvars.videoLoop			=	"false";

	// video auto play			( true / false )
	flashvars.videoAutoPlay		=	"true";
	
	// video buffer time		( seconds )
	flashvars.videoBufferTime	=	"0.1";
	
	// timeline interval	
	flashvars.tlInterval		=	"100000";

	// sound volume at start 		NOTE :	1=Max	0=Min
	flashvars.soundVolume		=	"0.8";

	// size the video starts at
	// can be set to 1, 2 and 3
	// 1 for narmol size view
	// 2 for aspect view 
	// 3 for full size view
	flashvars.fullSizeView		=	"1";
	
	////////////////////////////////////////////////////////////////////////
	// VISUAL APPEARANCE  /////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	
	// spacing between the controls
	flashvars.spacing 			=	"10";
	
	// control bar height		( height )
	flashvars.controlHeight		=	"25";
	
	// vulume scrub lenght 		( lenght )
	flashvars.volumeLengthW		= 	"50";
	
	// controls background		( colors / alphas )
	flashvars.color1 			= 	"0x2e2e2e";
	flashvars.color2 			= 	"0x000000";
	flashvars.alpha1 			= 	"1";
	flashvars.alpha2 			= 	"1";
	
	// controls border			( color / alpha )
	flashvars.borderColor 		= 	"0x555555";
	flashvars.borderAlpha		= 	"1";
	
	// time view ////////////////////////////////////
	// time view background		( colors / alphas )
	flashvars.timeColor1		= 	"0x333333";
	flashvars.timeColor2		= 	"0x111111";
	flashvars.timeAlpha1		= 	"1";
	flashvars.timeAlpha2		= 	"1";

	// time view text color		( color )
	flashvars.timeTextColor1	= 	"0xffffff";
	flashvars.timeTextColor2	= 	"0x888888";

	
	// scrubber /////////////////////////////////////////////////////
	// scrubber height			( height )
	flashvars.scrubberHeight 	=	"3";

	// scrubber background 		( color / alpha )
	flashvars.scrubberColor1	= 	"0x333333";
	flashvars.scrubberAlpha1	= 	"1";

	// scrubber					( color / alpha )
	flashvars.scrubberColor2	= 	"0x47d2ff";
	flashvars.scrubberAlpha2	= 	"1";

	// scrubber glow filter		( color / alpha )
	flashvars.filterColor		= 	"0x0066ff";
	flashvars.filterAlpha		= 	"1";
	
	// control buttons	///////////////////////////////////////////////
	// control buttons color	( color )
	//flashvars.buttonColor		= 	"0x000000";
	
	// info view /////////////////////////////////////////////////////
	// title color					( color )
	flashvars.titleColor			=	"0x47d2ff";

	// description color			( color )
	flashvars.descriptionColor		=	"0xD3D3D3";

	// info background				( color / alpha )
	flashvars.infoBackgroundColor	=	"0x000000";
	flashvars.infoBackgroundAlpha	=	"0.5";
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////////////////////////////////
	
	var params = {};
	params.scale = "noscale";
	params.allowfullscreen = "true";
	params.salign = "tl";
	params.bgcolor = "000000";
	
	var attributes = {};
	swfobject.embedSWF("_scripts/jquery/ui/video-player.swf", "video-object", stageW, stageH, "9.0.0", false, flashvars, params, attributes);	
}
