var RollDrag=false;
var RollLock=false;
var RollMouseStart=null;
var RollMarginStart=null;
var MaxRollMargin=586;
var WindowHeight=0;
var WindowWidth=0;
var search=null;
var RollFx;
var Player;

window.addEvent('domready', function(){ 
	if(Browser.Engine.trident4) return false;
	
	/*
	if(Browser.Plugins.Flash.version < 10)
	{
		$('loader').getFirst().set('html','sophisti.tv wymaga przeglądarki flash w wersji conajmniej 10')
		
		
		
		return false;
	}
	*/
		
	$(document).addEvents({
		'mousewheel' : function(event){
			if(event.wheel < 0)
			{
				$('roll_right').fireEvent('click',event)
			}
			else
			{
				$('roll_left').fireEvent('click',event)
			}
		}
	})
	
	$('movie_url').addEvent('click',function(){
		this.select();
	});
	
	a=wyznacz_wymiary()
	
	$('clip_view').fade('hide')
	
	$('roll').addEvent('mousedown',function(){		
		if(RollFx) RollFx.cancel()
		RollDrag=true;
		RollLock=true;
		RollMouseStart=null;
		RollMarginStart=null;
		$(document).addEvent('mousemove',mouseTrace)
		$(document).addEvent('mouseup',function(){
			RollDrag=false;
			$(document).removeEvent('mousemove',mouseTrace)
			RemoveLock.delay(100);
		})
		return false;		
	})
	
	$('roll_container').addEvent('click',function(e){
		evt=new Event(e);
		if(!RollLock)
		{
			pos=evt.client.x-$('roll_container').getPosition().x-60
			am=parseInt($('roll').getStyle('margin-left'))	
			if(RollFx) RollFx.cancel()			
			RollFx = new Fx.Morph('roll', {duration: 750, transition: Fx.Transitions.Sine.easeOut});
			RollFx.start({ 'margin-left': [am, safeRange(pos,0,MaxRollMargin)] });
		}
	})
	
	$('roll_left').addEvent('click',function(){
		if(RollFx) RollFx.cancel()
		am=parseInt($('roll').getStyle('margin-left'))			
		RollFx = new Fx.Morph('roll', {duration: 750, transition: Fx.Transitions.Sine.easeOut});
		RollFx.start({ 'margin-left': [am, safeRange(am-100,0,MaxRollMargin)]});
	})
	
	$('roll_right').addEvent('click',function(){
		if(RollFx) RollFx.cancel()		
		am=parseInt($('roll').getStyle('margin-left'))		
		RollFx = new Fx.Morph('roll', {duration: 750, transition: Fx.Transitions.Sine.easeOut}); 		
		RollFx.start({ 'margin-left': [am, safeRange(am+100,0,MaxRollMargin)] });				
	})
	search=$('soph_search')
	search.set('s_value',search.get('value'))
	search.addEvent('click',function(){
		if(search.get('value') == search.get('s_value')) search.set('value','')
	})
	
	$$('.movie-holder').addEvents({
		'mouseover' : function(){ $(this).getElement('h2').addClass('invisible'); $(this).getElement('.desc').removeClass('invisible');},
		'mouseout' : function(){ $(this).getElement('h2').removeClass('invisible'); $(this).getElement('.desc').addClass('invisible');},
		'click' : function(event){
			event=new Event(event);
			element=$(event.target);	
			while(!element.hasClass('movie-holder'))
			{
				element=element.getParent()
			}
			
			if(element.hasClass('iplex'))
			{
				document.location.href=element.get('id')
			}
			
			item_id=element.get('id')
			LaunchMovie(item_id);
		}
	})
	
	$('close_clip_view').addEvent('click',function(){
		
		Player.toElement().stopMovie(); 
		if(Browser.Engine.trident)
			$('clip_view').fade('hide')
		else
			$('clip_view').fade('out')
		Player=null;
		})
	$('clip_view').addEvent('click',function(){
		Player.toElement().stopMovie(); 
		if(Browser.Engine.trident)
			$('clip_view').fade('hide')
		else
			$('clip_view').fade('out')
		Player=null;
		})
	$('clip_container').addEvent('click',function(event){x=new Event(event); x.stop();})
	
	if(AutoPlay)
	{
		LaunchMovie(AutoPlay.toString());
	}
	
	$('loader').fade('out',2500)
	Recursive();
});

function LaunchMovie(item_id)
{
	data=Movies[item_id]
	
	ad_rand = Math.floor (Math.random() * Campaigns.length); 
	banner=Campaigns[ad_rand];
	
	data['b_file']=banner['b_file']
	data['b_height']=banner['b_height']
	data['b_click']=banner['b_click']
	
	clip_width=parseInt(data.width)
	clip_height=parseInt(data.height)
	$('movie_title').set('html',data.title)
	$('clip_container').setStyle('width',clip_width+'px')
	
	params={menu:false,allowScriptAccess:'always',allowFullScreen:true}
	
	p_url='http://media.sophisti.pl/stv/movieplayer.swf?a='+now
	p_url2='http://media.sophisti.pl/stv/movieplayer2.swf'
	
	if(DebugMode) url=p_url2
	else url=p_url
	
	Player=new Swiff(url,{
		width: clip_width,
		height: clip_height,
		vars : data,
		container : $('player_widget_container'),
		id: 'player_window',
		allowScriptAccess: 'always',
		properties: params,
		params: params
		
	});
	
	$('movie_url').set('value','http://sophisti.tv/'+data['id']+'.html')
	
	$('clip_view').fade('in'); 
	PlayMovie.delay(1000);
}

function mouseTrace(evt){
	if(!RollMouseStart) RollMouseStart=evt.page.x
	if(!RollMarginStart) RollMarginStart=parseInt($('roll').getStyle('margin-left'))
	przesuniecie=RollMarginStart+(evt.page.x-RollMouseStart)
	$('roll').setStyle('margin-left', safeRange(przesuniecie,0,MaxRollMargin))
	return false;
}

function PlayMovie()
{
	if(Player)	
	Player.toElement().playMovie()
	else
	PlayMovie.delay(250);
}

function RemoveLock()
{
	RollLock=false;
}

var cc_height;
var FullScreenMode=false;
var PlayerFx;
var PaddingrFx;
function FullScreen()
{
	pw=$('player_window')
	playerWidth=parseInt(pw.get('width'))
	playerHeight=parseInt(pw.get('height'))
	playerHeight_video=playerHeight-68	
	diff_he=$('clip_container').getHeight()-playerHeight
	diff_wi=$('clip_container').getWidth()-playerWidth
	if(FullScreenMode)
	{		
		PlayerFx = new Fx.Morph('player_window', {duration: 1500, transition: Fx.Transitions.Sine.easeOut}); 		
		PlayerFx.start({ 'width': playerWidth,height:  playerHeight});
		//$('clip_container').setStyle('padding','30px')
		
	}
	else
	{
		//AR
		//max szerokosc
		max_szer=WindowWidth-diff_wi
		max_wys=WindowHeight-diff_he
		fitToWidth=true;
		//czy sie zmiesci na szer
		asp=max_szer/playerWidth
		newWidth=max_szer
		newHeight=playerHeight*asp
		if(newHeight > max_wys) fitToWidth=false
		
		//$('clip_container').setStyle('padding','0px')
		
		if(!fitToWidth)
		{
			newHeight=max_wys
			newWidth=(max_wys/playerHeight)*playerWidth
		}
		PlayerFx = new Fx.Morph('player_window', {duration: 1500, transition: Fx.Transitions.Sine.easeOut}); 		
		PlayerFx.start({ 'width': newWidth,height:  newHeight});
	}
}

function Recursive()
{
	if($(window).getHeight() != WindowHeight) wyznacz_wymiary()
	ww=WindowWidth-300
	przesuniecie=Math.ceil((parseInt($('roll').getStyle('margin-left'))/588)*(parseInt($('movie-inner-container').getStyle('width'))-ww))
	$('movie-inner-container').setStyle('left',-przesuniecie)	
	Recursive.delay(40)
}


function wyznacz_wymiary()
{	
	WindowHeight=parseInt($(window).getHeight());
	WindowWidth=parseInt($(window).getWidth());
	
	$('clip_view').setStyle('height',WindowHeight)
	$('clip_view').setStyle('width',WindowWidth)
	
	wymiar_kontenera=WindowHeight-80
	$('movie-container').setStyle('height',wymiar_kontenera)
	$('movie-inner-container').setStyle('height',wymiar_kontenera)
	ilosc_w_poziomie=3
	ilosc_per_poziom=Math.ceil($$('#movie-inner-container > div').length/ilosc_w_poziomie)	
	$('movie-inner-container').setStyle('width',ilosc_per_poziom*240)
	return true;
}

function safeRange(range,min,max)
{
	if (range < min) return min;
	if (range > max) return max;
	return range;
}