var checkboxHeight = "17";
var radioHeight = "25";
var selectWidth = "190";

var Custom = {
	init: function() {
			var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				if(!inputs[a].getAttribute("disabled")) {
					span[a].onmousedown = Custom.pushed;
					span[a].onmouseup = Custom.check;
				} else {
					span[a].className = span[a].className += " disabled";
				}
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				if(!inputs[a].getAttribute("disabled")) {
					inputs[a].onchange = Custom.choose;
				} else {
					inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
				}
			}
		}
		document.onmouseup = Custom.clear;
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
			AjaxDeleteFromCompare(element.value.substring(7));
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			AjaxAddToCompare(element.value.substring(7));
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}
//window.onload = Custom.init;

var form_all ='';

$(document).ready( function () {
	form_all = $('#podbor').html();
	onload = Custom.init;
	set_form();
	var logo_max = 0;
	var n_logo = $('#logo_table td.logo_img').size();
	for (i=1; i<=n_logo; i++)
	{
		//$('#logo_table td:nth-child('+i+')').find('.logo_big').attr({'height_on':$(this).height()});
		var logo_big_h = $('#logo_table td.logo_img:nth-child('+i+')').find('.logo_big').height();
		var logo_small_h = $('#logo_table td.logo_img:nth-child('+i+')').find('.logo_small').height();
		if (logo_big_h>logo_max) logo_max = logo_big_h;
    	$('#logo_table td.logo_img:nth-child('+i+')').find('.logo_big').attr({'height_on':logo_big_h});
		$('#logo_table td.logo_img:nth-child('+i+')').find('.logo_small').attr({'height_on':logo_small_h});
	}
	$(function(){
		$('#logo_table .logo_img a').hover(function(){
		var w = $(this).children('img').attr('max_w');
		$(this).children('img').stop().animate({width:w+"px"}, 400);
	}, function(){
		var w = $(this).children('img').attr('max_w')/2;
		$(this).children('img').stop().animate({width:w+"px"}, 400); });
	});

	//$('#logo_table td').find('.logo_small').attr({'height_on':$(this).height()});
	/*
		$('#logo_table td').hover(

			function () {
			var ln = $(this).attr('ln');

			//$('#logo_table .logo_img').find('img.logo_small').stop(true, true);
			//$('#logo_table .logo_img').find('img.logo_big').stop(true, true);

			if ($('#logo_table .logo_img[ln='+ln+']').find('img.logo_small').css('display')!='none')
			{

			var img_height_big = $('#logo_table .logo_img[ln='+ln+']').find('img.logo_big').attr('height_on');
			var img_height_small = $('#logo_table .logo_img[ln='+ln+']').find('img.logo_small').attr('height_on');



			$('#logo_table .logo_img[ln='+ln+']').find('img.logo_big').css({'display':'none'});


				$('#logo_table .logo_img[ln='+ln+']').find('img.logo_small').css({'display':'inline'}).animate({'height':img_height_big+'px'}, 0, function() {
							//$('#logo_table .logo_img[ln='+ln+']').find('img.logo_big').css({'display':'inline'});
//							$('#logo_table .logo_img[ln='+ln+']').find('img.logo_small').css({'display':'none'});
							//$('#logo_table .logo_img[ln='+ln+']').find('img.logo_small').css({'height':img_height_small+'px'});
							$(this).parent('a').find('img.logo_big').css({'display':'inline'});
							$(this).css({'display':'none'});
							$(this).css({'height':img_height_small+'px'});


					});
			}
			//$('#logo_table .logo_img[ln='+ln+']').find('img').css({'height':img_height+'px'});
		},
		function () {
			var ln = $(this).attr('ln');

			$('#logo_table .logo_img').find('img.logo_small').stop(true, true);
			$('#logo_table .logo_img').find('img.logo_big').stop(true, true);
		if ($('#logo_table .logo_img[ln='+ln+']').find('img.logo_big').css('display')!='none')
			{
			var img_height_big = $('#logo_table .logo_img[ln='+ln+']').find('img.logo_big').attr('height_on');
			var img_height_small = $('#logo_table .logo_img[ln='+ln+']').find('img.logo_small').attr('height_on');




			$('#logo_table .logo_img[ln='+ln+']').find('img.logo_small').css({'display':'none'});
			$('#logo_table .logo_img[ln='+ln+']').find('img.logo_big').css({'display':'inline'}).animate({'height':img_height_small+'px'}, 0, function() {

						$(this).parent('a').find('img.logo_small').css({'display':'inline'});
							$(this).css({'display':'none'});
							$(this).css({'height':img_height_big+'px'});


						//$('#logo_table .logo_img[ln='+ln+']').find('img.logo_small').css({'display':'inline'});
						//$('#logo_table .logo_img[ln='+ln+']').find('img.logo_big').css({'display':'none'});
						//$('#logo_table .logo_img[ln='+ln+']').find('img.logo_big').css({'height':img_height_big+'px'});

				});
			//$('#logo_table .logo_img[ln='+ln+']').find('img').css({'height':img_height+'px'});
			}
		});

	*/

	$('.left_arrow').mouseover( function () {
		$(this).css({'background-position': '-36px -156px'});
	});
	$('.left_arrow').mouseout( function () {
		$(this).css({'background-position': '-9px -156px'});
	});
	$('.right_arrow').mouseover( function () {
		$(this).css({'background-position': '-70px -156px'});
	});
	$('.right_arrow').mouseout( function () {
		$(this).css({'background-position': '-97px -156px'});
	});
	$("#timer_out").countdown(new Date($("#timer_out").attr('time_y'), $("#timer_out").attr('time_m')-1, $("#timer_out").attr('time_d'), $("#timer_out").attr('time_h'), $("#timer_out").attr('time_i'), $("#timer_out").attr('time_s')), {prefix:'', finish: 'Время истекло!'});
	$('#menu_table td').mouseover( function () {
		var top = $(this).offset().top+27;
		var left = $(this).offset().left+1;
		if ($(this).attr('class')=='last')
			left = left+$(this).width()-$('#subm'+$(this).attr('num')).width()-9;
		$('#subm'+$(this).attr('num')).css({'display':'block', 'top':top+'px', 'left':left+'px'});
		$('#subm'+$(this).attr('num')).parent('td').css({'position':'absolute', 'z-index': '401'});
	});
	$('#menu_table td').mouseout( function () {
		$('#subm'+$(this).attr('num')).css({'display':'none'});
		$('#subm'+$(this).attr('num')).parent('td').css({'position':'static', 'z-index': '0'});
	});
	$('.hover_table tr').hover ( function() {
		$(this).addClass('on');
	}, function () {
		$(this).removeClass('on');
	});

	//$('.hover_cross').columnHover({hoverClass:'on', eachCell:true });
	$('.hover_cross').tableHover({cellClass: 'on_h', colClass: 'on', rowClass: 'on_h',   ignoreCols: [1, 6]});
	$('#podbor_butt_target').click( function()
	{
		if($(this).html()=='развернуть')
		{
			$(this).html('свернуть');
			$('#podbor_po_cele').fadeIn();
		}
		else
		{
			$(this).html('развернуть');
			$('#podbor_po_cele').fadeOut();
		}
		return false;
	});

	$('#podbor_butt_params').click( function()
	{
		if($(this).html()=='развернуть')
		{
			$(this).html('свернуть');
			$('#podbor_po_params').fadeIn();
		}
		else
		{
			$(this).html('развернуть');
			$('#podbor_po_params').fadeOut();
		}
		return false;
	});

	$('.block_out:not(.on)').hover(
		function () {
			//$(this).removeClass('block_blue');
			$(this).addClass('on');
		},
		function () {
			$(this).removeClass('on');
			//$(this).addClass('block_blue');
		});

	$('.block_new:not(.on):not(.off)').hover(
		function () {
			//$(this).removeClass('block_blue');
			$(this).addClass('on');
		},
		function () {
			$(this).removeClass('on');
			//$(this).addClass('block_blue');
		});

	$('#my_star').click( function (e) {
		var x_onobj = e.pageX-$(this).offset().left
		if (x_onobj>0)
		{
			if ((x_onobj>0) && (x_onobj<5))
			{
				removeAllStars(this);
				$(this).addClass('stars_0');
			}
			if ((x_onobj>5) && (x_onobj<20))
			{
				removeAllStars(this);
				$(this).addClass('stars_1');
			}
			if ((x_onobj>20) && (x_onobj<40))
			{
				removeAllStars(this);
				$(this).addClass('stars_2');
			}
			if ((x_onobj>40) && (x_onobj<60))
			{
				removeAllStars(this);
				$(this).addClass('stars_3');
			}
			if ((x_onobj>60) && (x_onobj<80))
			{
				removeAllStars(this);
				$(this).addClass('stars_4');
			}
			if ((x_onobj>80) && (x_onobj<100))
			{
				removeAllStars(this);
				$(this).addClass('stars_5');
			}
		}
	});
	$('#prod_img_list img:not(#big_img_prod a img)').hover ( function () {
		$('#big_img_prod img').attr({'src': $(this).attr('src_big')});
		$(this).css({'outline':'1px solid #ff0000'});
	},
	function () {
//			$('#big_img_prod img').attr({'src': $('#big_img_prod').attr('defoult_src')});
		$(this).css({'outline':'none'});
	});

	$(function(){
	  $("a[rel^='prettyPhoto']").prettyPhoto();
	});

	/*
		$('.arrow').click( function () {
			var move_div = $(this).attr('move_div');
			var move_arr = parseInt($(this).attr('move_arr'));
			var move_shag =125;
			var movie_to = parseInt($('#'+move_div).css('left')) - (move_shag*move_arr);
			$('#'+move_div).stop(true, true);
			//	alert (movie_to+'|'+move_arr);
			if (((movie_to<move_shag) && (move_arr<0)) || ((movie_to>-762) && (move_arr>0)))
			$('#'+move_div).animate({'left':movie_to+'px'}, 500, function () {});
		});
	*/

	$(".delete-from-basket").click(function(){
		var id=$(this).attr("id").substring(6);
		$.post("/ajax/deletefrombasket.php", {id:id},function(data){
			if(data=="")
				document.location.search="";
			else
			{
				$("#cost").html(data);
				$("#tr"+id).remove();
			}
		});
		return false;
	});

	$(".delete-from-basket").live("click", function(){
		var id=$(this).attr("id").substring(6);
		$.post("/ajax/deletefrombasket.php", {id:id},function(data){
			if(data=="")
			{
				document.location.search="";
				$("#tr"+id).remove();
				$("#cost").html("");
			}
			else
			{
				$("#cost").html(data);
				$("#tr"+id).remove();
			}
		});
		return false;
	});

	$("#table-order input").keydown(function(){
		var t=0;
		$("#table-order input").each(function(){
			if($(this).val()!="")
				t++;
		});
		if(t!=5)
		{
//			alert(t);
			$("#orderButton").attr("disabled","disabled");
		}
		else
			$("#orderButton").attr("disabled","");
	});

});

function removeAllStars(obj_now)
{
	$(obj_now).removeClass('stars_0');
	$(obj_now).removeClass('stars_1');
	$(obj_now).removeClass('stars_2');
	$(obj_now).removeClass('stars_3');
	$(obj_now).removeClass('stars_4');
	$(obj_now).removeClass('stars_5');
}
function set_form()
{
$( ".slider").slider({
			range: true,
			create: function( event, ui ) {
				var min_now = parseFloat($(this).attr("min"))
				var max_now = parseFloat($(this).attr("max"));


				if ($(this).attr("min_now")) min_now = parseFloat($(this).attr("min_now"));
				if ($(this).attr("max_now")) max_now = parseFloat($(this).attr("max_now"));

						$(this).slider({
							range: true,
							min: parseFloat($(this).attr("min")),
							max: parseFloat($(this).attr("max")),
							values: [min_now, max_now]});

				$( "#"+$(this).attr('id')+"_ot" ).val(min_now);
				$( "#"+$(this).attr('id')+"_do" ).val(max_now);


				$( "#"+$(this).attr('id')+"_out" ).find('.inf_ot').html(parseFloat($(this).attr("min")));
				$( "#"+$(this).attr('id')+"_out" ).find('.inf_do').html(parseFloat($(this).attr("max")));

				if ($(this).attr("min_now"))
					$( "#"+$(this).attr('id')+"_out" ).find('.inf_ot').html(parseFloat($(this).attr("min_now")));
				else
					$( "#"+$(this).attr('id')+"_out" ).find('.inf_ot').html(parseFloat($(this).attr("min")));
				if ($(this).attr("max_now"))
					$( "#"+$(this).attr('id')+"_out" ).find('.inf_do').html(parseFloat($(this).attr("max_now")));
				else
					$( "#"+$(this).attr('id')+"_out" ).find('.inf_do').html(parseFloat($(this).attr("max")));
			},
			slide: function( event, ui ) {

				$( "#"+$(this).attr('id')+"_ot" ).val(ui.values[ 0 ]);
				$( "#"+$(this).attr('id')+"_do" ).val(ui.values[ 1 ]);


				$( "#"+$(this).attr('id')+"_out" ).find('.inf_ot').html(ui.values[ 0 ]);
				$( "#"+$(this).attr('id')+"_out" ).find('.inf_do').html(ui.values[ 1 ]);

			}
		});

		$('select').selectmenu();
}

function clear_form()
		{
		//document.forms['podbor'].reset();
		$('#podbor').html('');
		$('#podbor').html(form_all);


		set_form();

		//Custom.init;
		}

