$(document).ready(function(){
	
	$("div.sidebar-open").bind('click', function(){ 
		openSidebar(1500); 
	});
	$("div.sidebar-close").bind('click', function(){ 
		$("div.sidebar-right div.sidebar-close").fadeOut(function(){ $(this).parent().animate({width:0, opacity: 'hide'}, 2500); });
		$("div.sidebar-open").fadeIn(2500);
	});
	
	openSidebar(500);
	
	// on Safari, hide our styles in favor of the WebKit style.
	if($.browser.safari){
		$("#search-theme-form *").css('background', 'transparent');
	}else{
		// Search field grey 'Serach...' text autohide
		var SF = $("#edit-search-theme-form-1");
		var SFdefault = "Suche...";
		SF.val(SFdefault).addClass("graySearch");
		SF.focus(function(){
			if(SF.hasClass('graySearch'))
				SF.val("").removeClass("graySearch");
		}).blur(function(){
			if(SF.val() == '')
				SF.val(SFdefault).addClass("graySearch");
		});
	}
});
function openSidebar(speed){
	$("div.sidebar-open").fadeOut(speed);
	$("div.sidebar-right div.sidebar-close").hide();
	$("div.sidebar-right").width(0).animate({width:200, opacity: 'show'}, speed, function(){ $("div.sidebar-right div.sidebar-close").fadeIn(); });
}

var header = {
	open: function(size){
		$("div.content-header").animate({height: size+'px'}, 2500);
		$("div.content-body").animate({top: (80+size)+'px'}, 2500);
	},
	close: function(){
		$("div.content-header").animate({height: '200px'}, 1000);
		$("div.content-body").animate({top: '280px'}, 1000);
	},
	content: function(html, width, height){
		$("div.content-header .content").hide().width(width).height(height).css('left', (800 - width)/2).html(html).slideDown(2500);
	},
	clear: function(){
		$("div.content-header .content").slideUp(1000, function(){$(this).html('EMPTY');});
		header.close();
	}
}

var movie = {
	m1: null,
	width: 800,
	height: 400,
	url: '',
	image: '',
	initialized: false,
	initialize: function(callback){
		if(movie.initialized == false){
			//$.getScript(pathToTheme+"/flvplayer/swfobject.js",function(){
				movie.initialized = true;
			//});
		}	
		if(typeof callback == 'function') callback();
	},
	create: function(){
		if(movie.initialized == true){
			if($.browser.msie){
				$('.gloss').hide();
			}
			header.open(movie.height );
			header.content('<div id="player">Player HERE!</div>', movie.width, movie.height );
			movie.m1 = new SWFObject(pathToTheme+'/flvplayer/betaplayer.swf','videoPlayer',movie.width,movie.height,'9');
			movie.m1.addParam("wmode", "transparent");
			movie.m1.addParam("bgcolor","transparent");
			movie.m1.addParam('allowscriptaccess','always');
			movie.m1.addParam('allowfullscreen','true');
			movie.m1.addVariable('javascriptid','videoPlayer');
			movie.m1.addParam('flashvars','&file='+movie.url+'&image='+movie.image+'&backcolor=000000&frontcolor=EEEEEE&lightcolor=666666&screencolor=transparent&controlbar=over&autostart=true&screenalpha=0');
			movie.m1.write('player');
		}
	},
	setParams: function(url, width, height, image){
		movie.url = url;
		movie.image = image;
		if(width > 100 && width < 800) movie.width = 800;
		if(height > 20 && height < 600) movie.height = height;
	},
	close: function(){
		header.clear();
		m1 = null;
		url = ''; image = '';
		width = 800; height = 400;
		$('.gloss').fadeIn(1000); // restore gloss
	}
}

var player;
function playerReady(obj) {
	var id = obj['id'];
	var version = obj['version'];
	var client = obj['client'];
	//alert('the videoplayer '+id+' has been instantiated');
	player = document.getElementById(id);
	player.addModelListener('STATE',"evaluateState"); 
};
function evaluateState(state){
	if(state.newstate == 'IDLE' || state.newstate == 'COMPLETED'){ // The Event for Stop is called IDLE
		movie.close(); 
	}
	//$("div.content-header .header-title").text(state.newstate);
	if(!$.browser.msie){
		if(state.newstate == 'PLAYING')
			$('.gloss').slideUp(500);
		if(state.newstate == 'PAUSED')
			$('.gloss').slideDown(1000);
	}
}

