cp = function(load, vars)
{
	if (slideinter)
	{
		clearInterval(slideinter);
	}
	if (timeoutID)
	{
		clearInterval(timeoutID);
	}
	slideinter = false
	timeoutID = false
	
	var c = $('loader');
	var loading = $('loading');

	loading.appear({
		afterFinish: function(){
			if (load == 'index.php')
			{
				var body = $$('body');
				body = body[0];
				body.addClassName('splash');
			}
			else
			{
				var body = $$('body');
				body = body[0];
				body.removeClassName('splash');
			}
			
			new Ajax.Updater('loader', load, {
				evalScripts: true,
				parameters: 'ajax=true&'+vars,
				onComplete: function() { loading.fade({ delay: 0.05, afterFinish: function(){ c.appear(); } }); }
			});
		}
	});
	return false;
}

function changeBox()
{
	$('div1').style.display='none';
	$('div2').style.display='';
	$('password').focus();
}

function restoreBox()
{
	if(document.getElementById('password').value=='')
	{
	  document.getElementById('div1').style.display='';
	  document.getElementById('div2').style.display='none';
	}
}
 
var slideinter = false;
autostartslideshow = function(task)
{
	if (task == 'off')
	{
		if(slideinter)
		{
			/*$('slideshow_on').removeClassName('underline');
			$('slideshow_off').addClassName('underline');*/
			clearInterval(slideinter);
			slideinter = false;
		}
	}
	else
	{
		if(!slideinter)
		{
			/*$('slideshow_off').removeClassName('underline');
			$('slideshow_on').addClassName('underline');*/
			slideinter = setInterval('scrollDiv("auto")', 4500, 'auto');
		}
	}
}

cphoto = function(id)
{
	if(slideinter)
	{
		clearInterval(slideinter);
	}
	id = 'full_'+id;
	changephoto(id);
}

changephoto = function(id)
{
	ActiveScrollS = true;
	var current	= $$('.currentItem');
	var prevView	= current[0];
	id = $(id);
	prevView.removeClassName('currentItem');
	id.addClassName('currentItem');
	new Effect.Fade(prevView, { duration:0.5, afterFinish: 
	(
		function() {
			new Effect.Appear(id, { afterFinish: 
			(
				function() {
					ActiveScrollS	= false;
				}
			)});
		}
	)});
}



play_slideshow = function()
{
	autostartslideshow('on');
}

pause_slideshow = function()
{
	autostartslideshow('off');
}

var ActiveScrollS = false;
scrollDiv= function(direction)
{
	
	if (direction == 'auto')
	{
		var direction = 'fwd';
	}
	else if(slideinter)
	{
		/*$('slideshow_on').removeClassName('underline');
		$('slideshow_off').addClassName('underline');*/
		clearInterval(slideinter);
	}
	if (ActiveScrollS == false)
	{
		ActiveScrollS = true;
		var current	= $$('.currentItem');
		var prevView	= current[0];
		var fwdCheck = 1;
		var backCheck = 0;
		var siblings		= prevView.siblings();
		var siblings		= $('full_image').childElements();
		var totalLength		= siblings.length;
		
		if (direction == 'back') {
			var nextView	= prevView.previous();
			if (nextView == null)
			{
				nextView = siblings[totalLength-1];
			}
		}
		else if(direction == 'fwd') {
			var nextView	= prevView.next();
			if (nextView == null)
			{
				nextView = siblings[0];
			}
		}
		else {
			ActiveScrollS	= false;
			return false;
		}
		if (nextView != null)
		{
			prevView.removeClassName('currentItem');
			nextView.addClassName('currentItem');
			new Effect.Fade(prevView, { duration:0.5, afterFinish: 
			(
				function() {
					new Effect.Appear(nextView, { afterFinish: 
					(
						function() {
							ActiveScrollS	= false;
						}
					)});
				}
			)});
		}
		else
		{
			ActiveScrollS	= false;
			return false;
		}
	}
	return false;
}

showThumbs = function()
{
	Effect.Appear('thumbnails_overall', { duration: 0.5 });
}

hideThumbs = function()
{
	Effect.Fade('thumbnails_overall', { duration: 0.5 });
}

function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}

function fixOnMouseOut(element, event) {
	var current_mouse_target = null;
	if( event.toElement )
	{				
		current_mouse_target 			 = event.toElement;
	}
	else if (event.relatedTarget)
	{				
		current_mouse_target 			 = event.relatedTarget;
	}
	
	if( !is_child_of(element, current_mouse_target) && !is_child_of($('thumbnails_overall'), current_mouse_target) && element != current_mouse_target && $('thumbnails_overall') != current_mouse_target ) {
		hideThumbs();
	}
}

var timeoutID = false;
scrollauto = function(direction, div)
{
	timeoutID = setInterval("scrollDivH('"+direction+"', '"+div+"')", 500); 
}

cancelscroll = function()
{
	clearInterval(timeoutID);
	timeoutID = false;
}

var ActiveScroll = false;
scrollDivH= function(direction, div)
{
	if (ActiveScroll == false)
	{
		var current	= $$('.currentItemThumb');
		var prevView	= current[0];

		var fwdCheck = 13;
		var backCheck = 0;
		var prevLength		= prevView.previousSiblings().length;
		var nextLength		= prevView.nextSiblings().length;

		if (direction == 'back') {
			var nextView	= prevView.previous();
			var leftCheck	= backCheck;
			var itemLength	= prevLength;
		}
		else if(direction == 'fwd') {
			var nextView	= prevView.next();
			var leftCheck	= fwdCheck;
			var itemLength	= nextLength;
		}
		else {
			ActiveScroll	= false;
			return false;
		}
		
		if (nextView != null)
		{
			if( itemLength > leftCheck )
			{
				prevView.removeClassName('currentItemThumb');
				nextView.addClassName('currentItemThumb');
				var prevViewOffset	= prevView.cumulativeOffset();
				var nextOffset	= nextView.cumulativeOffset();
				var scrolltopixle	= (nextOffset[0] - prevViewOffset[0])*14;
				
				new Effect.Move($(div),{x: -scrolltopixle, y: 0, duration: 1, mode:'relative', afterFinish: 
					( function() { ActiveScroll	= false; return false; } )
				});
			}
			else
			{
				ActiveScroll	= false;
				return false;
			}
		}
		else
		{
			ActiveScroll	= false;
			return false;
		}
	}
	return false;
}

formSend = function(form, page)
{
	var fields = form.serialize();
	cp(page, fields);
	return false;
}

function doClear(theText)
{
	if (theText.value == theText.defaultValue)
	{
		theText.value = ""
	}
	else if (theText.value == '')
	{
		theText.value = theText.defaultValue;
	}
}

var playing = false;
var playerid = false;
var autostart = false;
var force_start = false;
var playthis = '0';

Event.observe(window, 'load', function() {
	var flashvars = {
		file: './music/playlist.xml',
		autostart: playing,
		shuffle: false,
		displayclick: 'none',
		icons: false,
		repeat: 'always',
		stretching: 'none',
		item: playthis
	};
	var params = { allowscriptaccess: 'always', allowfullscreen: 'false' };
	var attributes = {	id: 'musicPlayerC',	name: 'musicPlayerC' };
	swfobject.embedSWF("./flash/player.swf", "musicPlayer", "240", "40", "9.0.0","./flash/expressInstall.swf", flashvars, params, attributes);
	/*var playerid = document.getElementById('musicPlayerC');*/
});

function playerReady(obj)
{
	playerid = document.getElementById(obj['id']);
}

control_music = function(action, item)
{
	if (action == 'start')
	{
		if (playing == false)
		{
			if (!playerid)
			{
			
				playthis = item;
				playing = true;
			}
			else
			{
				playing = true;
				playerid.sendEvent('PLAY', true);
				playerid.sendEvent('ITEM', item);
			}
		}
	}
	else if (action == 'auto')
	{
		if (playing == false)
		{
			control_music('play');
		}
		else
		{
			control_music('stop');
		}
	}
	else if (action == 'play')
	{
		if (playing == false)
		{
			/*$('switchPlay').innerHTML = 'mute';*/
			playerid.sendEvent('PLAY', true);
			playing = true;
		}
	}
	else if (action == 'stop')
	{
		if (playing == true)
		{
			/*$('switchPlay').innerHTML = 'play';*/
			playerid.sendEvent('PLAY', false);
			playing = false;
		}	
	}
	else if (action == 'change')
	{
		if (playing == false)
		{
			control_music('play');
		}
		playerid.sendEvent('ITEM', action);
	}
	return false;
}
