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;


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);}