var prefix="http://motif.creativez.com/";
var player=null;
var current=1;
var pspeed=5000;
var frame, up, down, bar, scroll, slidesize, dragmin, dragmax, timeout,dragflag,dragdelta,overflag,buttonflag,speed;
var mediaslide = "down";
var px =   [0, 130,  190,  220, 255, 275, 290, 300, 305, 305];

function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
}
function createPlayer() {
	var flashvars = {file:"files/music/playlist.xml", autostart:"false", repeat:"always", controlbar:"none", volume:"60"}
	var params = {allowfullscreen:"true", allowscriptaccess:"always"}
	var attributes = {id:"mpl", name:"mpl"}
	swfobject.embedSWF("player.swf", "player", "1", "1", "9.0.115", false, flashvars, params, attributes);
}
function init(){
	createPlayer();
	dragflag=dragdelta=overflag=buttonflag=speed=0;
	setTimeout("nextphoto()", pspeed);
	frame=document.getElementById("content");
	up=document.getElementById("scrollup");
	down=document.getElementById("scrolldown");
	bar=document.getElementById("scrollbar");
	scroll=document.getElementById("scroll");
    setDefault();

	up.onmouseover=function(){overflag=1;up.style.backgroundImage="url(img/up-o.gif)";if(!buttonflag){speed=1;timeout=setTimeout("move();",50);};}
	up.onmouseout=function(){overflag=0;if(buttonflag!=1){up.style.backgroundImage="url(img/up.gif)";speed=0;clearTimeout(timeout);};}
	up.onmousedown=function(){buttonflag=1;up.style.backgroundImage="url(img/up-o.gif)";speed=10;}
	down.onmouseover=function(){overflag=-1;down.style.backgroundImage="url(img/down-o.gif)";if(!buttonflag){speed=-1;timeout=setTimeout("move();",50);};}
	down.onmouseout=function(){overflag=0;if(buttonflag!=-1){down.style.backgroundImage="url(img/down.gif)";speed=0;clearTimeout(timeout);};}
	down.onmousedown=function(){buttonflag=-1;down.style.backgroundImage="url(img/down-o.gif)";speed=-10;}
	document.onmouseup=function(){speed=overflag;buttonflag=dragflag=dragdelta=0;if(overflag!=1)up.style.backgroundImage="url(img/up.gif)";if(overflag!=-1)down.style.backgroundImage="url(img/down.gif)";if(!overflag)clearTimeout(timeout);}
	document.onmousemove=function(e){if(dragflag){if(document.selection!==undefined&&document.selection.empty!==undefined)document.selection.empty();var t=0;if(!e)e=window.event;if(e.pageY)t=e.pageY;else if(e.clientY)t=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;if(t<dragmin+dragdelta)t=dragmin+dragdelta;else if(t>dragmax+dragdelta)t=dragmax+dragdelta;t=t-dragmin-dragdelta;bar.style.top=t+'px';var h=Math.max(0,frame.scrollHeight-frame.clientHeight);frame.scrollTop=Math.floor(h*t/slidesize);};}
	bar.onmousedown=function(e){dragflag=1;var y=0;if(!e)e=window.event;if(e.pageY)y=e.pageY;else if(e.clientY)y=e.clientY+document.body.scrollTop+document.documentElement.scrollTop;dragdelta=y-bar.offsetTop;}
	linkup();

	var s = document.getElementById("submit");
	s.onmouseover=function(){this.className+=" submit-o";}
	s.onmouseout=function(){this.className=this.className.replace(" submit-o", "");}
}
function slideup(step){
	if(step===undefined||step===null)step=0;
	if(!step)mediaslide='up';
	if(mediaslide=='up'){
		document.getElementById("media").style.top = -1*px[step]+'px';
		if (step < 8) setTimeout("slideup("+(step+2)+");", 60);
	}
}
function slidedown(step){
	if(step===undefined||step===null)step=0;
	if(!step)mediaslide='down';
	if(mediaslide=='down'){
		document.getElementById("media").style.top = -1*(305-px[step])+'px';
		if (step < 8) setTimeout("slidedown("+(step+2)+");", 60);
	}
}

function linkup(){
	hEls=document.getElementsByTagName('a');
	for(var j=0,jlen=hEls.length;j<jlen;j++){
		if(hEls[j].href.indexOf(prefix, 0)>=0 && hEls[j].href.substr(prefix.length,5) != 'files'){
			hEls[j].onclick=function(){go(this.href.substr(prefix.length));return false;}
		}
	}
}
function reply(){
	var name = document.getElementById('name').value;
	var comment = document.getElementById('comment').value;
	x_ajax_reply(name, comment, updateReply);
	return false;
}
function updateReply(result){
	if (result){
		document.getElementById('comment').value = '';
		go('replies');
	} else {
		replyError();
	}
}
function replyError(){
submit
}

function replyError(step){
	if(step===undefined||step===null)step=0;
	var s = document.getElementById("submit");
	if (step%2==0) s.className = s.className.replace("submit", "subm-err");
	else s.className = s.className.replace("subm-err", "submit");

//	if (step%2==0) s.className += " submit-err";
//	else s.className = s.className.replace(" submit-err", "");


	if (step < 5) setTimeout("replyError("+(step+1)+");", 100);
}

function setDefault(){
	if(frame.clientHeight>=frame.scrollHeight){bar.style.display=up.style.display=down.style.display="none";scroll.style.visibility="hidden";}else{bar.style.display=up.style.display=down.style.display="block";scroll.style.visibility="visible";if(frame.addEventListener)frame.addEventListener('DOMMouseScroll',wheel,false);frame.onmousewheel=wheel;}
	slidesize=scroll.clientHeight-down.clientHeight-up.clientHeight-bar.clientHeight;
//	dragmin=scroll.offsetTop+up.clientHeight; dragmax=dragmin+slidesize;
	dragmin=up.clientHeight; dragmax=dragmin+slidesize;
	updateBar();
}
function go(url){
	document.getElementById('content').innerHTML = '<div class="loading"><img src="img/loading.gif" alt="Loading..." /></div>';
	if (mediaslide == "up") document.getElementById("media").style.top = "-305px";
	x_ajax_go(url, updateContent);
}
function updateContent(data){
	document.getElementById('content').innerHTML = data[1];
	linkup();
	setDefault();
	if (data[0]=='replies' && mediaslide!='up') setTimeout("slideup();", 300);
	if (data[0]!='replies' && mediaslide!='down') setTimeout("slidedown();", 300);
}

function nextphoto(){
	setTimeout("nextphoto()", pspeed);
	current=current==10?1:current+1;
	document.getElementById("photo").src="files/photos/"+current+".jpg";
}
function handle(delta) {var sign=delta<0?1:-1;frame.scrollTop += sign*17;updateBar();}
function wheel(event){var delta=0;if(!event)event=window.event;if(event.wheelDelta){/* IE/Opera */delta=event.wheelDelta/120;/*if(window.opera)delta=-delta;*/}else if(event.detail)delta=-event.detail/3;/*Mozilla*/if(delta)handle(delta);if(event.preventDefault)event.preventDefault();event.returnValue=false;}
function move(){if(speed){frame.scrollTop-=speed;updateBar();setTimeout("move();",50);};}
function updateBar(){
	var h=Math.max(0,frame.scrollHeight-frame.clientHeight);
	var t=Math.floor(frame.scrollTop*slidesize/h);
	if(bar.style.display=='block')bar.style.top=t+'px';
}
if (window.addEventListener)
	window.addEventListener("load", init, false);
else if (window.attachEvent && !window.opera)
	window.attachEvent("onload", init);
