var showDefault = false;
var initCallbacks = new Array();

$(document).ready(function() {
	for(var i = 0; i < initCallbacks.length; i++) {
		initCallbacks[i]();
	}
	try {
		if ($('#production_list').length > 0) {
			getList('production', 'production_list');
		}
		if ($('#services_list').length > 0)  {
			getList('productionservices', 'services_list');
		}

		if ($('#fb_like_holder').length > 0) {
			appendFB();
		}
		/*$('div#production_list a.b').click(function(obj) {
			alert(obj.html());
			var data = getContent(this);
			obj.append('<span id="open_span_this">'+data+'</span>');
			return false;
			});*/
	} catch(e) {
		
	}
	
	//write div-s into urls
	$('div.mitekst').click(function() {
		$(window.location).attr('href', $(this).find('a').attr('href'));
	});
});

function addShow(alias) {
	showDefault = alias;
}

function addInitCallback(callback) {
	initCallbacks.push(callback);
}

function setActiveMenuItem(page) {
	alert(page);
	alert('active');
}
function getList(container, results) {
	$.getJSON('/ajax/list/'+container, function(data) {
		$('div#'+results).html(' ');
		$('div#'+results).html(data['result']);
		setTimeout(function() {
			if (showDefault) {
				show(showDefault);
				showDefault = false;
			} 
		}, 250);
		return;
	});
}
function getContent(url) {
	alert('showtime - ajax'+url);
	$.getJSON('/ajax/content/'+url, function(data) {
		if(data['error']) {
			return data['error'];
		}
		return data['result'];
	});
}

function show(alias) {
	$("div[id^='data_']").hide();
	//$('#link_'+alias).click(function() {hide(alias);return false;});	
	$("div[id^='close_']").hide();
	$("div[id^='open_']").show();	
	
	$("div#open_"+alias).hide();
	$("div#close_"+alias).show();
	$('#data_'+alias).slideDown('slow');
}

function hide(alias) {
	$("div[id^='data_']").hide();
	
	$("div#open_"+alias).show();
	$("div#close_"+alias).hide();

}
function appendFB() {
	$('#fb_like_holder').html(' ');
	//$('#fb_like_holder').html('<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.rudolfkonimoisfilm.com&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=dark&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe>');
}



/*
 * vimeo hack----------------------------------------------------------------------------------
 */
var moogaloop = false;
var vimeoUserName = 'rudolf';

var videos;
var currentVideo = 0;
var maxVideos = 0;
var callback = 'showVideos';
// Set up the URLs
var url = 'http://www.vimeo.com/api/v2/album/257238/videos.json?callback=' + callback;

// This function loads the data from Vimeo
function initVideos() {
	var js = document.createElement('script');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', url);
	document.getElementsByTagName('head').item(0).appendChild(js);
}
function showVideos(videosResponse) {
	videos = new Array(videosResponse.length);
	maxVideos = videosResponse.length-1;
	for (var i = 0; i < videosResponse.length; i++) {
		videos[i] = videosResponse[i].id;
	}
	showNextVideo();
}

function showNextVideo() {
	$('#vimeo_player_holder').html('Loading...');
	if (currentVideo >= maxVideos) {
		currentVideo = 0;
	}
	$.getJSON('/ajax/video/getLast', function(data) {
		if(data['video']) {
			if (data['video'] > currentVideo && data['video'] < maxVideos) {
				currentVideo = data['video'];
			}
		}
		runTheVideo(videos[currentVideo]);
		currentVideo = currentVideo + 1;
		$.getJSON('/ajax/video/setLast/'+currentVideo);
	});	
}


	function vimeo_on_finish(swf_id) {
		if (currentVideo == 0 && maxVideos == 0) {
			return;
		} else {
			showNextVideo();
		}
	}
	function vimeo_player_loaded(){
		$('#vimeo_player_holder').flash(function(){
			this.api_addEventListener("onFinish", "vimeo_on_finish");
			this.api_setVolume(1);
		});

	}

 function runTheVideo(video_id){ 
	if(!$.flash.available){
	  $('#vimeo_player_holder').html('<div id="install_flash">This content Requires the Flash Player. <a target="_blank" href="http://www.adobe.com/go/getflashplayer">Download Flash Player</a>.</div>');
	}else{
	  $('#vimeo_player_holder').html(' ');
	} 
	// For more Swiff documentation visit: http://mootools.net/docs/core/Utilities/Swiff
	$('#vimeo_player_holder').flash({
		swf: 'http://vimeo.com/moogaloop.swf',
		width: 640,
		height: 360, 
		flashvars:{
			clip_id: video_id,
			show_portrait: 0,
			show_byline: 0,
			show_title: 1,
			autoplay: 1,
			js_api: 1, // required in order to use the Javascript API
			js_onLoad: 'vimeo_player_loaded', // moogaloop will call this JS function when it's done loading (optional)
			js_swf_id: 'moogaloop' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
	    },
		params: {allowscriptaccess: 'always',allowfullscreen: 'true'}
	}, function(){"loading"});

}
