$(document).ready(function()
{
	// search form: begin
	if ( $("input#s").attr('value') != '' )
		$("div.search label").css("display","none");

	$("input#s").bind("focus", function() {
		$("div.search label").css("display","none");
	});

	$("input#s").bind("blur", function() {
		if ( this.value == "" ) $("div.search label").css("display","block");
	});
	// search form: end

	if ( document.getElementById("tip") != null ) {
		lightpos ();
		$("#tip").load("/tip.php");
	}
	else {
		$("body").css("background-image","none");
	}

	$("#moretip").bind("click", function() {
		$("#tip").load("/tip.php");
		return false;
	});

	$("#chip").bind("click", function() {
		if ( $("#css_lightness").attr("href") == "http://www.artgen.com.ua/wp-content/themes/artgen/day.css" ) {
			$("#css_lightness").attr("href","http://www.artgen.com.ua/wp-content/themes/artgen/night.css");
			$.cookie("ag_studio_lightness", "night", { expires:7, path: "/"});
		}
		else if ( $("#css_lightness").attr("href") == "http://www.artgen.com.ua/wp-content/themes/artgen/night.css" ) {
			$("#css_lightness").attr("href","http://www.artgen.com.ua/wp-content/themes/artgen/day.css");
			$.cookie("ag_studio_lightness", "day", { expires:7, path: "/"});
		}
		return false;
	});

	$("#lang").bind("click", function () {
		$("#overlay").fadeIn("fast");
		$("#color-flower").fadeIn("fast", function() {
			$("#color-flower .gold").fadeIn(0);
			$("#color-flower .red").fadeIn(80);
			$("#color-flower .violet").fadeIn(160);
			$("#color-flower .sky").fadeIn(240);
			$("#color-flower .sea").fadeIn(320);
			$("#color-flower .green").fadeIn(400);
		});

		return false;
	});

	$("#color-flower a").bind("click",function () {
		$("#css_color").attr("href","http://www.artgen.com.ua/wp-content/themes/artgen/"+$(this).parent().attr("className")+".css");
		$.cookie("ag_studio_color", $(this).parent().attr("className"), { expires:7, path: "/"});

		$("#overlay").css("display","none");
		$("#color-flower").css("display","none");

		return false;
	});

	$(window).resize(function(){
		if ( document.getElementById("tip") != null ) {
			lightpos();
		}
	});

	$("#light").draggable({
		zIndex:9999,
		drag: function(e,ui) {
			$("body").css("background-position", ( ui.position.left - 234 ) + "px " + ( ui.position.top - 262 ) + "px" );
		}
	});

});

function lightpos () {
	var delta = document.documentElement.clientWidth - document.getElementById('wrapper').clientWidth;
	var width = 0.61 * document.getElementById('tip').clientWidth;
	if ( $.browser.msie )
		width = 0.51 * document.getElementById('sidebar').clientWidth;
	var offset = 0;
	if ( delta > 0 ) offset = delta / 2 + width;
	else offset = width;

	$("#light").css("left",offset);
	$("#light").css("top","272px");
	$("#light").css("display","block");
	$("body").css("background-position",(offset-235)+"px 10px");
}