var orig = new Array();
var pics = new Array();
var stat = new Array();
var pic = new Array();
var rates = new Array();
var ratePath = "";
var rateExt = "";

//-- Form Functions ----------------------------------------------------------//

function checkLogin(main) {
	var allow = true;
	p = (main) ? 'loginpass' : 'smallpass';
	n = (main) ? 'loginname' : 'smallname';
	$(p).setStyle({backgroundColor:'#fff'});
	$(n).setStyle({backgroundColor:'#fff'});
	if($F(p).strip() == '') {
		$(p).activate().setStyle({backgroundColor:'#fcc'});
		allow = false;
	}
	if($F(n).strip() == '') {
		$(n).activate().setStyle({backgroundColor:'#fcc'});
		allow = false;
	}
	return allow;
}

function checkForgot() {
	var allow = true;
	var inputs = $('forgotForm').getInputs('text');
	inputs.reverse();
	inputs.each(function(n) {
		$(n).setStyle({backgroundColor:'#fff'});
		if($F(n).strip()=='') {
			$(n).activate().setStyle({backgroundColor:'#fcc'});
			allow = false;
		}
	});
	return allow;
}

function checkRegister() {
	var allow = true;
	$('registerAgree').up(0).setStyle({backgroundColor:'#fff'});
	if(!$F('registerAgree')) {
		$('registerAgree').up(0).setStyle({backgroundColor:'#fcc'});
		$('registerAgree').focus();
		allow = false;
	}
	var inputs = $('registerForm').getInputs('text');
	var passwords = $('registerForm').getInputs('password');
	passwords.reverse();
	passwords.each(function(n) { inputs.splice(1,0,n); });
	inputs.reverse();
	inputs.each(function(n) {		
		$(n).setStyle({backgroundColor:'#fff'});
		if(($F(n).strip()=='') && (!$(n).hasClassName('optional'))) {
			$(n).activate().setStyle({backgroundColor:'#fcc'});
			allow = false;
		}
	});
	return allow;
}

function checkUpload() {
	var allow = true;
	$('uploadform').getElements().each(function(n) {
		if($(n).hasClassName('required')) {
			$(n).up().up().setStyle({backgroundColor:(n.type=="checkbox"?'#fcc':'#fff')});
			if(((n.type=="checkbox") && !n.checked) || !n.value) {
				$(n).up().up().setStyle({backgroundColor:'#f33'});
				allow = false;
			}
		}
	});
	return allow;
}

function errorUpload(msg) {
	$('main').select('div#cookiemessage', 'div#forminfo').each(function(e) { e.remove(); });
 	var div = new Element('div', { 'id': 'cookiemessage' }).update(msg);
 	$('header').insert({after:div});
}

//-- Flash Functions ---------------------------------------------------------//

function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

//-- Category Functions ------------------------------------------------------//

function flipCatList(elm) {
	dls = elm.parentNode.childNodes;
	for(i=0;i<dls.length;i++) {
		if(dls[i].nodeName=="DL") {
			dls[i].className = (dls[i].id==elm.id) ? "active" : "none";
		}
	}
}

//-- Category Functions ------------------------------------------------------//

function flipBL(elshow, elhide) {
	$(elhide).hide();
	$(elshow).show();
	return false;
}

//-- Preview Functions -------------------------------------------------------//

function loadpic(url,code,j) {
	if(stat[code]) {
		Event.observe($(pic[code][j]),'error',function(e) { pic[code][j].src = "/gfx/abh.gif"; });
		pic[code][j].src=url;
	}
}

function changepic(i,code) {
	if(!stat[code]) return;
	if(i>=10) i = 0;
	if(pic[code][i].complete) {
		document.getElementById(code).src=pic[code][i].src;
		setTimeout("changepic("+(++i)+",'"+code+"')",750);
	} else {
		setTimeout("changepic("+(i)+",'"+code+"')",20);
	}
}

function previewScroll(i,e) {
	stat[i.id]=1;
	var ppexp = /(.+[0-9]{2}\/)([a-z]+)(_[a-z]+)(\.[a-z]+)?$/i;
	var parts = ppexp.exec(i.src.toString());
	var first = true;

	for(var j=0;j<10;j++) {
		if(pics[i.id][j]==1) {
			pic[i.id][j]=new Image();
			var path = parts[1]+"frames/"+parts[2]+"_"+((j==0)?"05":(j*10)+5)+parts[4];
			if(first) {
				first = false;
				loadpic(path,i.id,j);
			} else {
				setTimeout("loadpic('"+path+"','"+i.id+"',"+j+")",j*50);
			}
		}
	}
	changepic(0,i.id);
}

//-- Rating Functions --------------------------------------------------------//

function rateClear(t) {
	if(t) {
		clearTimeout(t);
		t=0;
	}
}

function rateMouseOver(e) {
	rateClear(rates[e[1]][0]);
	for(i=1;i<rates[e[1]].length;i++) {
		$(rates[e[1]][i][0]).src = ratePath+(i<=e[3]?"4":"0")+rateExt;
	}
}

function rateMouseOut(e) {
    rates[e[1]][0] = setTimeout("rateReset("+e[1]+")",1000);
}

function rateClick(e) {
	if(!rates[e[1]]) return false;
	new Ajax.Request(xmlSource+'add-rate', {
		method: 'get',
		parameters: { cid: e[2], rate: e[3] },
		onComplete: function(t) {
			rates[e[1]] = null;
			r = $(e[0]).up().next('span');
			if(t.responseText == "login") {
				window.location.href = "/login";
			} else {
				r.update('<br>'+t.responseText).show();
				new Effect.Fade($(r),{ delay: 5, duration: 0.5 });
			}
		}
	});
}

function rateReset(e) {
	rateClear(rates[e][0]);
	for(i=1;i<rates[e].length;i++) {
		$(rates[e][i][0]).src = rates[e][i][1];
	}
}

//-- Favorite Functions ------------------------------------------------------//

function favClick(a) {
	var afexp = /^addfav-([0-9]{1,})$/i;
	var parts = afexp.exec(a.id);
	if(!parts[1]) return false;
	new Ajax.Request(xmlSource+'add-favorite', {
		method: 'get',
		parameters: { cid: parts[1] },
		onComplete: function(t) {
			if(t.responseText == "login") {
				window.location.href = "/login";
			} else {
				a.replace(t.responseText);
			}
		}
	});
}

//-- Freinds Functions ------------------------------------------------------//

function addFriend(uid) {
	$('frequest'+uid).hide();
	new Ajax.Request(xmlSource+'add-friend', {
		method: 'post',
		parameters: { uid: uid },
		onComplete: function(t) {
			if(t.responseText != "error") {
				$('frequest'+uid).innerHTML = t.responseText;
			}
			$('frequest'+uid).show();
			return false;
		}
	});
}

//-- Init Functions ----------------------------------------------------------//

function init_preview() {
	$$('img.scroll').each(function(n) {
		orig[n.id] = n.src;
		stat[n.id]=0;
		pic[n.id]=new Array();
		pics[n.id]=new Array(1,1,1,1,1,1,1,1,1,1);
		Event.observe($(n),'mouseover',function(e) { previewScroll(n,e); });
		Event.observe($(n),'mouseout',function(e) {
			stat[n.id]=0;
			n.src = orig[n.id];
		});
	});
}

function init_rates() {
	var rsexp = /^star-([0-9]{4})-([0-9]{1,})$/i;
	var riexp = /^star-([0-9]{4})-([0-9]{1,})-([0-9]{1})$/i;
	var rpexp = /(.+)[0-9]{1}(\.[a-z]{3})$/i;
	$$('span.ratingStars.active').each(function(s) {
		span = rsexp.exec(s.id);
		rates[span[1]] = new Array();
		s.select('img.rateStar').each(function(i) {
			if(!ratePath || !rateExt) {
				path = rpexp.exec(i.src.toString());
				ratePath = path[1];
				rateExt = path[2];
			}
			img = riexp.exec(i.id);
			rates[span[1]][img[3]] = new Array(i.id,i.src.toString());
			Event.observe($(i),'mouseover',function(e) { rateMouseOver(riexp.exec(Event.element(e).id)); });
			Event.observe($(i),'mouseout',function(e) { rateMouseOut(riexp.exec(Event.element(e).id)); });
			Event.observe($(i),'click',function(e) { rateClick(riexp.exec(Event.element(e).id)); });
 		});
	});
}

function init_favorites() {
	$$('a.addfav').each(function(a) {
		Event.observe($(a),'click',function(e) {
			favClick($(a));
			Event.stop(e);
		 });
	});
}

//-- Global onload -----------------------------------------------------------//
document.observe("dom:loaded", function() {
	$$('input.readonly').each(function(n) { n.onclick = function() { this.activate(); }});
	$$('#logo').each(function(n) { n.onclick = function() { window.location.href = "/"; }});
	init_preview();
	init_rates();
	init_favorites();
});
