
var SNIMU = 1, SDAYU = 2;
var rops = [];
var ofs = [];
var bs = [], cs=[], sub_bs=[];
var cur = {opt:0,type:0,subopt:0};
//var dquestion, dhide, dform;
var places_length;
var click_counter_iframe;


var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function getOfferEmail(str)
{
 	str = Base64.decode(str);
 	//return '<span class="nobr"><a ' + ' href="m' + 'ai' + 'l' + 't' + 'o' + ':' + str + '">' + str + '</a></span>';

 	return '<span class="nobr"><a title="Написать письмо" ' + ' href="m' + 'ai' + 'l' + 't' + 'o' + ':' + str + '"><img style="padding-right: 4px;" class="phoneicon" src="/img/icon-email.gif" alt="Электронная почта" width="18" height="13" />' + str + '</a></span>';
}

function getContactEmailCode()
{
	var e = 'm'+'a'+'i'+'l'+'t'+'o'+':'+'m'+'a'+'i'+'l'+'@'+'ede'+'m-n'+'a-y'+'ug'+'.'+'ru';
	var t = 'm'+'a'+'i'+'l'+'@'+'e'+'dem-na'+'-y'+'ug'+'.'+'ru';
	return '<span class="nobr"><a ' + ' href="' + e + '">' + t + '</a></span>';
}

function Prepare()
{
	var hash = window.location.hash;
	if(hash)
	{
		if(typeof(hash) == 'string')
		{
			hash = hash.substring(1);
			if(document.getElementById(hash))
			{
			 var highlighted_offer = document.getElementById(hash);
			 highlighted_offer.className = 'item highlighted_item';
			}
		}
	}

    click_counter_iframe = document.getElementById('iframe1');

}

function LoadOffers()
{
	places_length = places.length;// - 1;
	places_adler_length = places_adler.length;
	for(co in ops)
	{
		var cop = ops[co];
		if(cop.type == undefined)
		{
			cop.type = SDAYU;
		}
		if(e = document.getElementById('o' + cop.id))
		{
			cop.con = e;
		}
		rops[rops.length] = cop;
	}
	for(var i = 1; i <= places_length; i++)
	{
		bs[bs.length] = document.getElementById('s' + i);
	}

	for(var j = 1; j <= places_adler_length; j++)
	{
		sub_bs[sub_bs.length] = document.getElementById('sub_s' + j);
	}
	
	//for(var i = 1; i <= 3; i++)
	//{
	//	cs[cs.length] = document.getElementById('st' + i);
	//}
	

}
function CheckOb(ob,set)
{
	var result = true;
	for(every_property in set)
	{
		var set_value = set[every_property];
		if(set_value > 0)
		{
			var ob_value = ob[every_property];
			if(typeof(ob_value) == 'object')
			{
				var tresult = false;
				for(every_item in ob_value)
				{
					var every_value = ob_value[every_item];
					if(every_value == set_value)
					{
						tresult = true;
						break;
					}
				}
				result = (result && tresult);
			}
			else
			{
				result = (result && ob_value == set_value);
			}
		}
	}
	return result;
}
function Set(set)
{

// показываем подпанель
if(set.opt == 1)
{
//slidedown('selector_adler');
document.getElementById('selector_adler').style.display='';
}
else
{
//slideup('selector_adler');
document.getElementById('selector_adler').style.display='none';
}


	//var div_left_ad,div_main_ad_8,div_main_ad_16;
	if(document.getElementById('left_ad')){ var left_ad=document.getElementById('left_ad');}
	if(document.getElementById('main_ad_8')){ var main_ad_8=document.getElementById('main_ad_8');}
	if(document.getElementById('main_ad_16')){ var main_ad_16=document.getElementById('main_ad_16');}
	
	if(left_ad && main_ad_8 && main_ad_16)
	{
	 if(set.opt == 0 || (set.opt == 1 && set.subopt == 0))
	 {
	  left_ad.style.display='';
	  main_ad_8.style.display='';
	  main_ad_16.style.display='';
	 }
	 else
	 {
	  left_ad.style.display='none';
	  main_ad_8.style.display='none';
	  main_ad_16.style.display='none';
	 }
	}




	
	for(ob in rops)
	{
		var cob = rops[ob];
		if(cob.id && cob.opt && cob.type)
		{
			if(CheckOb(cob,set))
			{
				cob.con.style.display = '';
			}
			else{
				cob.con.style.display = 'none';
			}
		}
	}

// погода
if(bs[cur.opt] != set.opt)
{
	w1 = $('weather1');
	w3 = $('weather3');
	w4 = $('weather4');
	w6 = $('weather6');

	w1.className = 'weather'; w3.className = 'weather'; w4.className = 'weather'; w6.className = 'weather'; 

	if(set.opt == 1){w1.className='';}
	if(set.opt == 2){w3.className='';}
	if(set.opt == 3){w3.className='';}
	if(set.opt == 4){w4.className='';}
	if(set.opt == 5){w4.className='';}
	if(set.opt == 6){w6.className='';}
}

	
    bs[cur.opt].className = 'button';
    cur.opt = set.opt;
    bs[cur.opt].className = 'selected';


    if(cur.subopt != set.subopt && cur.opt == set.opt)
    {
	    if(sub_bs[cur.subopt])
	    {
		sub_bs[cur.subopt].className = 'button';
		//sub_bs[cur.subopt].onclick = function(){ Set( {type:0,opt:cur.opt,subopt:cur.subopt} ) };
	    }
	    cur.subopt = set.subopt;
	    if(sub_bs[cur.subopt])
	    {
		    sub_bs[cur.subopt].className = 'selected';
		    //sub_bs[cur.opt].onclick = '';
	    }
    }





// счетчик кликов в ифрейме
    var url='';
    if(set.opt > 0 && set.subopt == 0){url='click-counter/' + places_urls[set.opt] + '?nocache=' + Math.random();}
    if(set.opt == 1 && set.subopt > 0){url='click-counter/' + places_urls[set.opt] + '/' + places_adler_urls[set.subopt]  + '?nocache=' + Math.random();}
    if(set.opt == 0){url='click-counter/ALL?nocache=' + Math.random();}
    click_counter_iframe.src = url;


}

function SetType(j)
{
    for(co in rops)
    {
    	var cop = rops[co];
    	if(cop.id && cop.opt)
    	{
	    	if(j > 0)
	    	{
				var cc = cop.type;
				if(typeof(cc) == 'object')
				{
					var rs = 'none';
					for(aa in cc)
					{
						var bb = cc[aa];
				    	if(bb == j)
				    	{
				    		rs = '';
							break;
				    	}
					}
					cop.con.style.display = rs;
				}
				else
				{
			    	if(cop.type == j)
			    	{
			    		cop.con.style.display = '';
			    	}
			    	else
			    	{
			    		cop.con.style.display = 'none';
			    	}
				}
		    }
		    else
		    {
	    	    cop.con.style.display = '';
		    }
	   }
    }
    //cs[cur.type].className = 'button';
    //cur.type = j;
	//cs[cur.type].className = 'selected';
}
function SetOption(i)
{
    for(co in rops)
    {
    	var cop = rops[co];
    	if(cop.id && cop.opt)
    	{
	    	if(i > 0)
	    	{
				var cc = cop.opt;
				if(typeof(cc) == 'object')
				{
					var rs = 'none';
					for(aa in cc)
					{
						var bb = cc[aa];
				    	if(bb == i)
				    	{
				    		rs = '';
							break;
				    	}
					}
					cop.con.style.display = rs;
				}
				else
				{
			    	if(cop.opt == i)
			    	{
			    		cop.con.style.display = '';
			    	}
			    	else
			    	{
			    		cop.con.style.display = 'none';
			    	}
				}
		    }
		    else
		    {
	    	    cop.con.style.display = '';
		    }
	   }
    }
    bs[cur.opt].className = 'button';
    cur.opt = i;
	bs[cur.opt].className = 'selected';
}

function $(s){return document.getElementById(s);}