if( navigator.userAgent.match( /(opera mini|ipod|iphone|android|webOS|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|hiptop|\bpda|psp|treo|nokia|blackberry)/i ) ) {
	var largeLen = parseInt( screen.width > screen.height ? screen.width : screen.height );
	if( largeLen > 0 && largeLen < 1024 ) {
		location.replace( "http://m.vivid.com/" );
	}
}
if( navigator.userAgent.match(/googletv/i) ){
	location.replace( "http://chrome.vivid.com/" );
}
	
var featBoxPgCt = 0

function mediaEndMovie( playerId, notice ) {
	mediaEnd( playerId, notice, 'joinForm' );
}
function mediaEndCeleb( playerId, notice ) {
	mediaEnd( playerId, notice, 'celebForm' );
}
var fplyrSettings = false;

function mediaEnd( playerId, notice, formName ) {

	if( !formName )
		formName = 'joinForm';

	var dC	= $( '#' + playerId ).fPlayer('get','dimClass');

	if ( dC && $('body').hasClass(dC) )
		toggleLights();

	$('<input style="height:1px;width:1px;border:0px;position:fixed;left:0px;top:0px;">').insertAfter( '#' + playerId ).focus().remove();

	var plyr	= $( '#' + playerId ),
		orig	= $('#hiddenFeatureFlv').html(),
		uiSizes	= plyr.fPlayer('get','uiSizes'),
		hide	= uiSizes[0].hide,
		show	= uiSizes[0].show,
		lastCSS	= uiSizes[ plyr.fPlayer('get','curSize') ? plyr.fPlayer('get','curSize') : 0 ],
		mainCSS	= uiSizes[0]
	;
	fplyrSettings = plyr.fPlayer('get', false);
	
	if( notice == 1 ) {
		$('#' + formName + ' .return').remove();
		$('#' + formName + ' .specialNotice').text('Sorry, you have reached your preview limit.');
	}

	if( lastCSS.css != mainCSS.css ) {
		$( '#featureMedia' ).switchClass( lastCSS.css, mainCSS.css, 300, function(){
			if ( mainCSS.show )
				$( mainCSS.show ).fadeIn(300);
			if ( mainCSS.hide )
				$( mainCSS.hide ).fadeOut(300);
			plyr.replaceWith( orig );
			$('#featureMedia .player').attr('id','fPlayer');
			$( '#' + formName ).show();
			$( '#' + playerId ).hide();
		});
	} else {
		plyr.replaceWith( orig );
		$('#featureMedia .player').attr('id','fPlayer');
		$('#' + formName ).show();
		$( '#' + playerId ).hide();
	}
}

function hidePlayerForms(){
	$('#joinForm').hide();
	$('#celebForm').hide();
	$('#fPlayer').show();
	
	$('#fPlayer').fPlayer(fplyrSettings);
	initFeatFade();
}

function fadeFeatured(div){
	if( curFade ) 
		clearTimeout(curFade);
	curFade = false;
	if( ( div - 0 ) == div ) {
		$('#fPlayer > ul > li').stop(true,true).each(function(i){
			if ( i > featBoxPgCt-(div) && i != 0){
				$(this).fadeOut(300);
			} else {
				$(this).show().fadeTo(300,1);
			}
			/*
			if( i == ( featBoxPgCt - div ) && $(this).find('[media]').length ){
				var curTrailer = player.playlist[ $(this).find('[media]').attr('media') ];
				if( curTrailer.end.autoJS == 'mediaEndCeleb' ) {
					$('#celebForm > div.celeb').css( 'background-image', "url(" + curTrailer.end.img + ")" );
				}
			}*/
		});
		$($('#fPlayer > .thumbs > div').removeClass('active').get(div)).addClass('active');
	} else {
		curFade = setTimeout(function(){
			if( $('#fPlayer > ul > li:visible').length <= 1 ) 
				$('#fPlayer > ul > li').show();
			$('#fPlayer > ul > li:visible:last').each(function(){
				$(this).dequeue();
				curFeat = $(this).index()-1;
				curFeatPg = $('#fPlayer > .thumbs > div [media]').length-(curFeat);
				if( !curFeat )
					curFeatPg = 0;
				$($('#fPlayer > .thumbs > div').removeClass('active').get(curFeatPg)).addClass('active');
				$(this).fadeOut(900,function(){
					if(curFade) fadeFeatured();
				});
			});
		},10000);
	}
}

function initFeatFade(){
	$('#fPlayer').hover(function(){
		if(curFade) clearTimeout(curFade);
		curFade=false;
	},function(){
		fadeFeatured();
	});
	fadeFeatured();
}


function celebLoad(pg,obj){
	pg++;
	if( $(obj).hasClass('prev') || $(obj).hasClass('next') ) {
		$('#dynCelebs .pagination li').each(function(i){
			if( $(this).find('span').length ){
				pg = i;
				if( $(obj).hasClass('prev') ) pg--;
				else pg++;
			}
		});
	}
	if( pg > $('#dynCelebs .pagination li').length - 2 ) pg = 1;
	if( pg < 1 ) pg = $('#dynCelebs .pagination li').length - 2;
	$('#dynCelebs .celebs').each(function(i){
		if(		i >= ( ( pg - 1 ) * 3 )
			&&	i <= ( ( pg * 3 ) - 1 )
			) {
			$(this).delay(300).fadeIn(300);
		}
		else $(this).fadeOut(300);
	});
	$('#dynCelebs .pagination li').each(function(){
		$(this).find('span').remove();
		$(this).find('a').show();
	});
	$('#dynCelebs .pagination li:eq('+(pg)+')').each(function(){
		$(this).find('a').hide();
		$(this).append('<span>'+(pg)+'</span>');
	});
	return false;
}

$(document).ready(function(){

	$('#dynCelebs .pagination li:eq(1)').each(function(){
		$(this).find('a').hide();
		$(this).append('<span>1</span>');
	});

	$("span.tipTrigger").colorbox({href:"/inl.tip/"});

	featBoxPgCt = $('#fPlayer > ul > li').length;

	for( page = 0; page <= featBoxPgCt - 1; page++ ) {
		$('#fPlayer > .thumbs').append("<div onclick='fadeFeatured("+page+");'>"+(page+1)+"</div>");
	}

	$('#fPlayer > ul > li:last').clone().insertBefore('#fPlayer > ul > li:first');
	$('#fPlayer > .thumbs div:first').addClass('active');
	
	$('body').append("<div id='hiddenFeatureFlv' style='display:none;'></div>");
	$('#fPlayer').clone().appendTo('#hiddenFeatureFlv');
	$('#hiddenFeatureFlv #fPlayer').removeAttr('id');
	
	$(window).load(initFeatFade);
});

