// Инициализируем таблицу перевода
var trans = [];
for (var i = 0x410; i <= 0x44F; i++)
  trans[i] = i - 0x350; // А-Яа-я
trans[0x401] = 0xA8;    // Ё
trans[0x451] = 0xB8;    // ё

// Сохраняем стандартную функцию escape()
var escapeOrig = window.escape;

// Переопределяем функцию escape()
window.escape = function(str)
{
  var ret = [];
  // Составляем массив кодов символов, попутно переводим кириллицу
  for (var i = 0; i < str.length; i++)
  {
    var n = str.charCodeAt(i);
    if (typeof trans[n] != 'undefined')
      n = trans[n];
    if (n <= 0xFF)
      ret.push(n);
  }
  return escapeOrig(String.fromCharCode.apply(null, ret));
}


function toggleConfigurator()

{

	var configuratorHeader = $('#configurator');

	var configuratorBody = $('#configurator_body');



	configuratorHeader.toggleClass('open');

	configuratorHeader.toggleClass('close');



	configuratorBody.css('display', 'none' == configuratorBody.css('display') ? 'block' : 'none' );



	$.cookie('configurator_state', 'none' == configuratorBody.css('display') ? 1 : 0, { expires: 7, path: '/'});

}



function toggleDetails()

{

	var detailsHeader = $('#det');

	var detailsBody = $('.parameters');



	$("img#pic").attr("src", '/i/descr_up.gif' == $("img#pic").attr("src") ? '/i/descr_r.gif' : '/i/descr_up.gif');



	detailsBody.css('display', 'none' == detailsBody.css('display') ? '' : 'none' );



	$.cookie('details_state', 'none' == detailsBody.css('display') ? 1 : 0, { expires: 7, path: '/'});



	$.cookie('details_pic', '/i/descr_up.gif' == $("img#pic").attr("src") ? 0 : 1, { expires: 7, path: '/'});

}

function compareAdd(obj, id) {

var str = $.cookie('compare'+obj);

if (!str || !isInArray(str,id))

{
$("#comp"+id).attr("checked",true);
$("#cmp"+id).addClass("inCompareLink");
$.cookie('compare'+obj, $.cookie('compare'+obj)+id+'|', {expires: 7, path: '/'});

	//	show box
	$(".search").css("background","transparent url(/i/s_input_bg.gif) no-repeat scroll 100% 0");
	$(".compare").slideDown("slow");
	$("#compareLink").load("http://uv.by/cms/modules/shop/?act=compared&obj="+obj, {}, function(){}).attr("href","http://uv.by/comp/?obj="+obj);
}

else

{
$("#cmp"+id).removeClass("inCompareLink");
$("#comp"+id).attr("checked",false);
$.cookie('compare'+obj, $.cookie('compare'+obj).replace(id+'|',""), {expires: 7, path: '/'});
$("#compareLink").attr("href","http://uv.by/comp/?obj="+obj);
$("#compareLink").load("http://uv.by/cms/modules/shop/?act=compared&obj="+obj, {}, function(){

	if ($("#compareLink").html()==0){
			$(".search").css("background","transparent url(/i/s_input_bg_m.gif) no-repeat scroll 100% 0");
			$(".compare").slideUp("slow");
	}

});
}


}

function isInArray(str, valueToSearch)
{
	arrayToSearchIn = str.split("|");
	for (var i = 0; i < arrayToSearchIn.length; i++)
	{
		if(arrayToSearchIn[i] == valueToSearch)
			return true;
	}
	return false;
}



$(document).ready(function(){
	$('#bannerplace').cycle({
		fx:     'scrollHorz',
		prev:   '#banner_left',
		next:   '#banner_right',
		speed:  1000,
		timeout: 0,
		easing: 'easeInOutQuint'
	});
	$("#bannerplace").css("background-color","black");
	$("#bannerplace").css("width","100%");


	$(".polezno_in").click(function(){
		$("#polezno_block").load('http://uv.by/polezno-znat/?mainpage=1');
		return false;
	});
	$(".search a").click(function(){
		$("#searchinput").val($(this).html());
		return false;
	});


	//	reviews menu
	$(".mmenu").hide();
	$(".mmenu a:first").show();
		//	let's show menu
		$("#selectedCat").click(function(){
			$(".mmenu").slideDown("fast");
			return false;
		});
		$(".selectedImg").click(function(){
			$(".mmenu").slideDown("fast");
			return false;
		});
		//	selected!
		$(".PartOfMenu a").click(function(){
			$("#selectedCat").html($(this).html()).show();
			$(".mmenu").slideUp("fast");
				//	ajax loader
					//	save developers, install BROWSER!
					if($.browser.msie){
						var xURL = $(this).html();
						//xURL = xURL.replace(" ", "%20");
						document.location = 'http://uv.by/reviews/?k='+escape(xURL);
					} else {
						//	thank You
						$("#block2loadreview").load('http://uv.by/reviews-ajax/?main_page=1&caller=ajax&k=' + escape($(this).html()));
					}
			return false;
		});
			//	highlight
			$(".menuRow").hover(
			function () {
        			$(this).addClass("active");
      		},
      		function () {
        			$(".mmenu tr").removeClass("active");
     			}
     			);

		//	delete compare
		$("#cancelCompare").click(function(){
			$(".in-compare a").removeClass("inCompareLink");
			$.cookie('compare'+$(this).attr("obj"), "", {expires: 7, path: '/'});
			$(".compare").slideUp("fast");
			$(".search").css("background","transparent url(/i/s_input_bg_m.gif) no-repeat scroll 100% 0");
			$("body :checkbox").attr("checked", false);
			return false;
		});


     //	save developers, install BROWSER!
     if (jQuery.browser.msie) {
		if (parseInt(jQuery.browser.version)=="6"){
            $("#left_top").css("background-image","url(i/menu_left_top.gif)");
            $(".centerTop").css("background-image","url(i/menu_top.gif)");
            $("#right_top").css("background","transparent url(i/menu_right_top.gif) no-repeat scroll 0 0");

            $(".centerLeft").css("background","url(i/menu_left.gif)");
            $(".centerRight").css("background","url(i/menu_right.gif)");

            $("#left_bot").css("background","transparent url(i/menu_left_bot.gif) no-repeat scroll 0 0");
            $(".centerBottom").css("background-image","url(i/menu_bot.gif)");
            $("#right_bot").css("background","transparent url(i/menu_right_bot.gif) no-repeat scroll 0 0");
		}
	 }



});


function zvonok(){
	$.floatbox({
        content: $("#zvonokform").html(),
        fade: true
    });
}


function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
}

function str2int(x){
	x = str_replace(' ','', x);
	return x;
}

function setDiskont() {
	var diskont = $("#DiskType").val();
	var price = $("#PriceSum").html();
	if (diskont!="0") {
		price= str2int(price);
		var newprice = price - price*0.01*diskont;
		/*
			newprice = roundMe(newprice, -2);
		*/
		$("#AddItToMe").html("");
		//	cathca!
		$("#AddItToMe").append('Введите <b>персональный номер карты</b>: <br><input type="Text" id="DiskontID" name="DiskontID">');
		$("#diskontPrice").html("Цена с учётом дисконта: <b>"+newprice+"</b> руб.");

	} else {
		$("#AddItToMe").html("");
		$("#diskontPrice").html("");
	}
}

function roundMe ( val, precision ) {
	return parseFloat(parseFloat(val).toFixed(precision));
}


function zakaz(){
	$("#dName").remove();
	$("#dprice").remove();
	$("#did").remove();
	$("#asform").append("<input id='dName' type='hidden' name='dname' value='"+$("#DiskType").val()+"'>");
	$("#asform").append("<input id='dprice' type='hidden' name='dprice' value='"+$("#diskontPrice b").html()+"'>");
	$("#asform").append("<input id='did' type='hidden' name='did' value='"+$("#DiskontID").val()+"'>");
	$.floatbox({
        content: $("#zakazForm").html(),
        fade: true
    });



}


function checkphone(currForm)
{
	var numericExpression = /^[0-9]+$/;
    var phone = currForm.as_Mail.value;
	
      if(phone.match(numericExpression)){
      	return true;
      } else {
        alert("Телефон должен состоять только из цифр");
         return false;
      }
	
}

function checkKey(ev)
{
	var key = ev.keyCode;
	if(key == 0)
	{
		key = ev.which;
	}

    if(((key > 47) && (key <58)) || (key == 8) || (key == 46) || (key == 9) || (key == 37) || (key == 39))
    {
    	return true;
    }
    else
    {
    	return false;
    } 
}
