$(function() {

	// second menu
	$(".plus span, .mines span")
	.each(function() {
		if (this.parentNode.className == "plus") { this.parentNode.className = "mines"; $(this).append("<div class=\"li-marker\">+</div>"); }
		else { this.parentNode.className = "plus"; $(this).append("<div class=\"li-marker\">-</div>"); };
	})
	.click(function() {
		if (this.parentNode.className == "plus") { this.parentNode.className = "mines"; $(".li-marker", this).html("+"); }
		else { this.parentNode.className = "plus"; $(".li-marker", this).html("-"); };
	});

	// spoyler
	$(".spoyler .spoyler-head").click(function() {
		if (!$(this).hasClass("act")) 
		{
			$(".spoyler-content", this.parentNode).show();
			$(this).addClass("act");
		}
		else
		{
			$(".spoyler-content", this.parentNode).hide();
			$(this).removeClass("act");
		}
	});
	
	
	// ol list
	$("#content ol")
	.each(function() {
		var listNum = 1;
		$("li", this)
		.each(function() {
			this.innerHTML = '<div class="ol-content">'+this.innerHTML+'</div>';
			$(this).prepend('<div class="ol">'+listNum+'.</div>');
			listNum++;
		});
	});
	
	
	// email form
	$("#formSend input[type=text]")
	.each(function() { 
		if (this.value == '' || this.value == this.title) {
			this.value = this.title;
			this.style.color="#999999";
		}
	})
	.focus(function() {
		if (this.value == this.title) { this.value = ''; this.style.color="#000000"; }
	})
	.blur(function() {
		if (this.value == '') { this.value = this.title; this.style.color="#999999"; }
	}); 
	
	$("#formSend textarea")
	.each(function() { 
		if (this.innerHTML == '' || this.innerHTML == this.title) {
			this.innerHTML = this.title;
			this.style.color="#999999";
		}
	})
	.focus(function() { 
		if (this.innerHTML == this.title) { this.innerHTML = ''; this.style.color="#000000"; }
	})
	.blur(function() { 
		if (this.value == "") { this.innerHTML = this.title; this.style.color="#999999"; }
	}); 
	
	
	$(".sub").click(function() {
	    if ($(this).hasClass("off")) return false;
	});
	
	$("#formOrder input").change(function() {
	    chechFormOrder();
	});
	
	$("#formOrder textarea").change(function() {
	    chechFormOrder();
	});
	
	chechFormOrder();
	
	
	
	// fancybox
	$("a.zoom").fancybox({
		'titleShow'     : true
	});
	
	/*
	// li border
	$("#menu_second > ul > li, #content ul > li, #bottom ul > li").append('<div class="li_border">&nbsp;</div>');
	$("#menu_second > ul > li:last-child .li_border, #content ul > li:last-child .li_border, #bottom ul > li:last-child .li_border").remove();
	$(".li_border").css('opacity', '0.05');
	*/
 
});

var statForm = false;

function chechFormOrder() {
	
	statForm = true;

	$("#formOrder textarea").each(function() {
	    if (this.value == "" || this.value == this.title) statForm = false;
	});	
	
	$("#formOrder input").each(function() {
	    if (this.value == "" || this.value == this.title) statForm = false;
	});
	
	if (statForm) $(".sub").removeClass("off");
	else $(".sub").addClass("off");
}


function fancyBX(obj)
{
	$.fancybox(
		{
			'autoScale': false,
			'width'			:474,
			'height'		:173,
			'padding'		: 0,
			'onStart': function() {
				$("#fancybox-outer-bottom, #fancy-bg-n, #fancy-bg-ne, #fancy-bg-e, #fancy-bg-se, #fancy-bg-s, #fancy-bg-sw, #fancy-bg-w, #fancy-bg-nw").hide();
				$("#fancybox-outer").css('background-image', 'none').css('border-style', 'none').css('background-color', 'transparent');
			},
			'content': $(obj).html()
		}
	);
}

// comments
function showBlock(obj)
{ 
	$(obj).toggle();
}

var captcha = true;

function formAddComment(path)
{
	path = path + '&key=' + document.getElementById("key").value + '&str=' + document.getElementById("str").value + '&captcha=' + captcha + '&fio=' + document.getElementById("add_fio").value + '&text=' + str_replace ("\n", " ", document.getElementById("add_text").value);
	
	$.getJSON(path, 
		{}, 
		function(json) {
			
			
			if (json.err) 
			{
				document.getElementById('form_messages').innerHTML = json.err;
				reloadCaptcha();
			}
			else 
			{
				document.getElementById('add_fio').value='';
				document.getElementById('add_text').value='';
				document.getElementById('form_messages').innerHTML = json.mess;
				captcha = true;
			}
		/*	if (!json.captcha) 
			{
				$("#recaptcha_area").hide();
			}
			else 
			{
				$("#recaptcha_area").show();
				javascript:Recaptcha.reload ();
			}
			captcha = json.captcha;
			if (json.err) document.getElementById('form_messages').innerHTML = json.err;
			else 
			{
				document.getElementById('form_messages').innerHTML = json.mess;
				document.getElementById('add_fio').value='';
				document.getElementById('add_text').value='';
				$("#recaptcha_area").show();
				javascript:Recaptcha.reload ();
				captcha = true;
			}*/
		}
	);
}

function reloadCaptcha()
{
	$.getJSON('/captcha_img/captcha.php', 
		{}, 
		function(json) {
			if (json.html) document.getElementById('captcha_img').innerHTML = json.html;
			if (json.str) document.getElementById('str').value = json.str;
		}
	);
}


function change_captcha_img(key_str, obj)
{
	$(".captcha_img").removeClass('current');
	document.getElementById('key').value = key_str;
	$(obj).addClass('current');
}


function str_replace ( search, replace, subject ) {	// Replace all occurrences of the search string with the replacement string
	// 
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Gabriel Paderni

	if(!(replace instanceof Array)){
		replace=new Array(replace);
		if(search instanceof Array){//If search	is an array and replace	is a string, then this replacement string is used for every value of search
			while(search.length>replace.length){
				replace[replace.length]=replace[0];
			}
		}
	}

	if(!(search instanceof Array))search=new Array(search);
	while(search.length>replace.length){//If replace	has fewer values than search , then an empty string is used for the rest of replacement values
		replace[replace.length]='';
	}

	if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
		for(k in subject){
			subject[k]=str_replace(search,replace,subject[k]);
		}
		return subject;
	}

	for(var k=0; k<search.length; k++){
		var i = subject.indexOf(search[k]);
		while(i>-1){
			subject = subject.replace(search[k], replace[k]);
			i = subject.indexOf(search[k],i);
		}
	}

	return subject;

}

