/* Music Styles */
var styles = new Array('all','pop','rock','electro','other','hiphop','remix');
var music_style;
var bg;
$(document).ready(function(){
	if (!$.browser.msie || $.browser.version >= 7) {
		music_style = document.body.className;
		bg = false;//(music_style!='');
		style(music_style,1);

		for(var i=0;i<styles.length;i++){
			if(styles[i]!=music_style){
				$("#"+styles[i]).mouseover(function(){style(this.id,1);});
				$("#"+styles[i]).mouseout(function(){style(this.id,0);});
			}
		}
	}
});
function style(s,flag){
	var mnu;

	if(s!=''){
		mnu = this.document.getElementById(s);
		if(mnu)
			mnu.src = mnu.src.substr(0,mnu.src.length-5) + flag + '.png';
	}

	if(bg)
		this.document.body.className = (flag==1)? s : music_style;

	if(s!=music_style && music_style!=''){
		flag = (flag==0)? 1 : 0;
		mnu = this.document.getElementById(music_style);
		if(mnu)
			mnu.src = mnu.src.substr(0,mnu.src.length-5) + flag + '.png';
	}
}
/* AJAX: PHP SETTINGS */
var PHP_GET_CAPTURE = '/captcha';
var PHP_SUBSCRIBE = '/subscribe/add';
var PHP_RATE = '/works/addvote';
var PHP_REPLY = '/works/addcomment';

/* AJAX: Capture */
//var reload_capture = 1;
var capture_id;
function changeCapture(id){
	capture_id = id;
	//makeGETRequest(PHP_GET_CAPTURE, 'reload_capture='+reload_capture, onChangeCapture);
	var r= Math.floor(Math.random()*10000);
	document.getElementById(capture_id).src = '/captcha?'+r;
}
/*function onChangeCapture(){
	if (http_request.readyState == 4){
		if (http_request.status == 200){
			result = http_request.responseText;
			var r= Math.floor(Math.random()*10000);
			document.getElementById(capture_id).src = '/captcha?'+r;
			reload_capture = 1;
		}
	}
}*/
/* AJAX: Subscription */
function wndSubscribe(){
	//reset
	this.document.getElementById('msgMailErr').className = 'hidden';
	this.document.getElementById('mail').value = '';

	//pos & show subscribe wnd
	var pos = $("#btnSubscribe").position();
	var left = pos.left;
	var wnd = this.document.getElementById('subscribe');
	wnd.style.left = left-27+'px';
	wnd.className = '';
}
function subscribe(){
	var poststr = ParamPrepare('mail')+'&'+ParamPrepare('subscribe_id');
	makeGETRequest(PHP_SUBSCRIBE,poststr,onChangeSubscribe);
}
function onChangeSubscribe(){
	if (http_request.readyState == 4){
		if (http_request.status == 200){
			result = http_request.responseText;
			document.getElementById('msgMailErr').innerHTML = result;
			document.getElementById('msgMailErr').className = '';
		}
	}
}
/* AJAX: Rate */
function wndRate(){
	//reset
	this.document.getElementById('msgErrCapture').className = 'hidden';
	this.document.getElementById('captureCode').value = '';

	//hide reply wnd
	if(this.document.getElementById('reply').className!='hidden'){
		this.document.getElementById('reply').className='hidden';
	}

	//new capture
//	reload_capture = 0;
	changeCapture('imgCapture');

	//pos & show rate wnd
	var pos = $("#btnRate").position();
	var left = pos.left;
	var top = pos.top;
	var wnd = this.document.getElementById('rate');
	wnd.style.left = left-20+'px';
	wnd.style.top = top-7+'px';
	wnd.className = '';
}
function rate(){
	var poststr = ParamPrepare('captureCode')+'&'+ParamPrepare('rate_id');
	makeGETRequest(PHP_RATE,poststr,onChangeRate);
}
function onChangeRate(){
	var results = new Array('Код не подходит','Спасибо, ваш голос учтен','Вы уже проголосовали за эту работу');
	if (http_request.readyState == 4){
		if (http_request.status == 200){
			result = http_request.responseText;
			if(result!=1){
				document.getElementById('msgErrCapture').innerHTML = results[result];
				document.getElementById('msgErrCapture').className = "";
				changeCapture('imgCapture');
			}
			else{
				var count = Number(document.getElementById('count').innerHTML) + 1;
				document.getElementById('count').innerHTML = count;
				document.getElementById('rate').className = 'hidden';
			}
		}
	}
}
/* AJAX: Reply */
function wndReply(){
	//reset
	var ele;
	ele = document.getElementById('errReply'); if (ele) ele.className = 'hidden';
	ele = document.getElementById('username'); if (ele) ele.value = '';
	ele = document.getElementById('capture2'); if (ele) ele.value = '';
	ele = document.getElementById('usermsg'); if (ele) ele.value = '';

	//hide rate wnd
	ele = document.getElementById('rate');
	if (ele && ele.className != 'hidden') {
		ele.className='hidden';
	}

	//new capture
//	reload_capture = 0;
	changeCapture('replyCapture');

	//pos & show reply wnd
	var pos = $("#btnReply").position();
	var left = pos.left;
	var top = pos.top;
	var wnd = this.document.getElementById('reply');
	if (wnd) {
		wnd.style.left = left-20+'px';
		wnd.style.top = top-7+'px';
		wnd.className = '';
	}
}
function reply(){
	if((this.document.getElementById('username').value!='')&&(this.document.getElementById('capture2').value!='')&&(this.document.getElementById('usermsg').value!='')){
		var poststr = ParamPrepare('username')+'&'+ParamPrepare('capture2')+'&'+ParamPrepare('usermsg')+'&'+ParamPrepare('id');
		makeGETRequest(PHP_REPLY,poststr,onChangeReply);
	}
	else{
		this.document.getElementById('errReply').innerHTML = 'Заполните обязательные поля';
		this.document.getElementById('errReply').className = '';
	}
}
function onChangeReply(){
	if (http_request.readyState == 4){
		if (http_request.status == 200){
			result = http_request.responseText;
			if(result==0){
				document.getElementById('errReply').innerHTML = 'Код картинки не подходит';
				document.getElementById('errReply').className = '';
				changeCapture('replyCapture');
			}
			else{
				document.getElementById('comments').innerHTML += result;
				document.getElementById('reply').className = 'hidden';
			}
		}
	}
}
/* AJAX: Lib Functions */
var http_request = false;
function makeGETRequest(url, parameters, afteraction){
	http_request = false;
	if (window.XMLHttpRequest){
		http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType){
				http_request.overrideMimeType('text/html');
			}
	}
	else if (window.ActiveXObject){
		try{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		}
			catch (e){
			try{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
		}
	}
	if (!http_request) {
		return false;
	}
	http_request.onreadystatechange=afteraction
	http_request.open("GET",url+'?'+parameters,true)
	http_request.send(null)
}
function ParamPrepare(id){
	var value = 'not_found';
	var input = this.document.getElementById(id);
	if(input){
		if((input.type=='radio')||(input.type=='checkbox')){
			value = id+'='+encodeURI(input.checked);
		}
		else{
			value = id+'='+encodeURI(input.value);
		}
	}
	return value;
}
