﻿var slowdown = 0;
var defaultCity = 'Гомель';
var currentCity = defaultCity;
var wetdata = 0;
window.addEvent('domready', function(){
  cw_showWeather();
  cw_loadAfisha(0,'Кино');
  // cw_showCurrencies();
  // set currency
  // set weather
	var url = document.location.href;
	if(url.indexOf('/event/') != (-1))
  		jQuery('#titlePoster').text('Скоро');
});     
/*
$('#cw_curCity').ready(function(){
  cw_weatherSetCurCity();
});
*/
//-------------------------------------
//aCtualNews block
//Load block with actual news
//load block with last news comments
window.addEvent('domready', function(){
	AN_load();
	LC_load();
});

//function load new table with actual news
function AN_load(){
	blockId = 'actualNewsDiv';
	jQuery.get(
			DIR_WEB_ROOT + '/ajax/actualNews/AN_receiver.php',{type:'news'}, function(requestResult){
			jQuery('#actualNewsDiv').html(requestResult);	
		}	
	);

}

//--------------------------------------

//--------------------------------------
//lastNewsComments block

function LC_load(){
	jQuery.get(
			DIR_WEB_ROOT +  '/ajax/lastComments/LC_receiver.php',{type:'lastNewsComments'}, function(requestResult){
			jQuery('#lastNewsCommentsDiv').html(requestResult);		
		}
	);
}
//--------------------------------------


function cw_showWeather(city){
  if (city){
    currentCity = city;
  } else {
    currentCity = defaultCity;
  }
	jQuery('#cw_informer_fuel').css('display', 'none');
	jQuery('#cw_header_container').removeClass('pogoda_top3');
  if(jQuery('#cw_informer_weather_cont').css('display') == 'none'){
    var fromWindow = 'cw_informer_currency_cont';
  } else {
    var fromWindow = 'cw_informer_weather_cont';
  }
  jQuery('#cw_header_container').removeClass('pogoda_top2');
  jQuery('#cw_informer_loading').show(slowdown);
  jQuery('#'+fromWindow).hide(slowdown, function(){
    jQuery.get(DIR_WEB_ROOT + '/plugins/addblocks/include/ajax/weather.php', {action:'showWeather', city: currentCity, security_ls_key: LIVESTREET_SECURITY_KEY}, function(resp){
      jQuery('#cw_informer_weather_cont').html(resp);
      jQuery('#cw_informer_loading').hide(slowdown, function(){
        jQuery('#cw_informer_weather_cont').show(slowdown);
      });
    });
  });
  return false;
}

function cw_showCurrencies(){
	jQuery('#cw_informer_fuel').css('display', 'none');
	jQuery('#cw_header_container').removeClass('pogoda_top3');
	jQuery('#cw_header_container').addClass('pogoda_top2');
	jQuery('#cw_informer_loading').show(slowdown);
	jQuery('#cw_informer_weather_cont').hide(slowdown, function(){
    jQuery.get(DIR_WEB_ROOT + '/plugins/addblocks/include/ajax/currencies.php', {security_ls_key: LIVESTREET_SECURITY_KEY}, function(resp){
      jQuery('#cw_informer_currency_cont').html(resp);
      jQuery('#cw_informer_loading').hide(slowdown, function(){
        jQuery('#cw_informer_currency_cont').show(slowdown);
      });
    });
  });
  return false;
}

function cw_showFuel(){
jQuery('#cw_header_container').removeClass('pogoda_top2');
jQuery('#cw_informer_weather_cont').css('display', 'none');
jQuery('#cw_informer_currency_cont').css('display', 'none');
  jQuery('#cw_header_container').addClass('pogoda_top3');
  jQuery('#cw_informer_loading').show(slowdown);
  jQuery('#cw_informer_fuel').hide(slowdown, function(){
    jQuery.get(DIR_WEB_ROOT + '/plugins/addblocks/include/ajax/fuel.php', {security_ls_key: LIVESTREET_SECURITY_KEY}, function(resp){
      jQuery('#cw_informer_fuel').html(resp);
		
      jQuery('#cw_informer_loading').hide(slowdown, function(){
        jQuery('#cw_informer_fuel').show(slowdown);
      });
    });
  });
  return false;
}

function cw_showWeatherCities(){
  jQuery('#cw_informer_weather_cont').hide(slowdown, function(){
    jQuery.get(DIR_WEB_ROOT + '/plugins/addblocks/include/ajax/weather.php', {action: 'showCities', city: currentCity, security_ls_key: LIVESTREET_SECURITY_KEY}, function(resp){
      jQuery('#cw_informer_weather_cont').html(resp);
      jQuery('#cw_informer_weather_cont').show(slowdown);
    });
  });
  return false;
}

function cw_weatherSetCurCity(){
  jQuery('#cw_curCity').text(currentCity);
  return false;
}

function wbbclose(){
  jQuery('#fw_box').hide();
  jQuery('#fw_backbox').hide();
}

function cw_showWeatherDetailed(){
  var isie=navigator.userAgent.toLowerCase().match(/msie/i)?'':'slow';
  if(!ge('fw_box'))return false;
  ge('fw_close').onclick=wbbclose;
  ge('fw_backbox').onclick=wbbclose;
  jQuery.get(DIR_WEB_ROOT + '/plugins/addblocks/include/ajax/weather.php', {action: 'showDetails', city: currentCity, security_ls_key: LIVESTREET_SECURITY_KEY}, function(resp){
    wetdata=eval(resp);
    for(i in wetdata){weatherFor(i);break;}
    jQuery('#fw_backbox').show();
    jQuery('#fw_box').show(isie);
  });
  return false;
}

function $c(type,params){
	if(type=="#text"||type=="#"){
		return document.createTextNode(params);
	}else if(typeof(type)=="string"&&type.substr(0,1)=="#"){
		return document.createTextNode(type.substr(1));
	}else{
		var node=document.createElement(type);
	}
	for(var i in params)if(i=="kids"){
			for(var j in params[i]){
					if(typeof(params[i][j])=='object'){
				node.appendChild(params[i][j]);
					}
			}
	}else if(i=="#text"){
		node.appendChild(document.createTextNode(params[i]));
	}else if(i=="#html"){
		node.innerHTML=params[i];
	}else if(i=="onclick"){
		node.onclick=params[i];
	}else{
		node.setAttribute(i,params[i]);
	}
	return node;
}

function ge(e)
{
  return document.getElementById(e);
}

function deleteRows(table) 
{
  var i=table.children.length;
  while(i--)table.removeChild(table.children[i]);
}

function weatherFor(cday)
{
  var tbl_days=ge('tbl_days');
  deleteRows(tbl_days);
  var tr=$c('tr',{'align':'middle'});
  for(day in wetdata)tr.appendChild($c('td',{id:day,'#text':day,'onclick':function(){weatherFor(this.id)},'class':(cday==day?'wstab':'wtab')}));
  tbl_days.appendChild(tr);

  var tbl_weather=ge('tbl_weather');
  deleteRows(tbl_weather);
  tbl_weather.appendChild($c('tr',{id:'wrwhead',align:'bottom',kids:{
    'time':$c('td',{width:60}),
    'img':$c('td',{width:80}),
    'state':$c('td',{width:110,'#html':'<br>Характер-ки погоды'}),
    'temp':$c('td',{width:110,'#html':'<br>Температура воздуха'}),
    'atm':$c('td',{width:110,'#html':'<br>Атмосф. давление'}),
    'wind':$c('td',{width:100,'#html':'<br>Ветер'}),
    'hum':$c('td',{width:110,'#html':'<br>Влажность'}),
  }}));

  var dt=wetdata[cday];
  var first=true;
  var time={3:'Ночь',9:'Утро',15:'День',21:'Вечер'};
  for(tm in time){
    var i=dt[tm];
    if(!i)continue;
    if(first)first=false;else{
      tbl_weather.appendChild($c('tr',{align:'middle',kids:{
//        'hr':$c('td',{'colspan':7,'#html':'<font color="#CCCCCC">__________________________________________________________________________________________________________________</font>'})
        'hr':$c('td',{'colspan':7,'#html':'<hr style="border-color:#CCC">'})
      }}));
    }
    var ic=i.c.split('|');
    if(ic[0]=='03') if(tm==3||tm==21)ic[0]='14';

    tbl_weather.appendChild($c('tr',{'class':'wtrowx',align:'middle',kids:{
      'time':$c('td',{'#text':time[tm],'class':'wfrcl'}),
      //'img':$c('td',{kids:{0:$c('img',{'src':'/ajax/pogoda/images/empty.gif','class':'wtr wt'+i.c})}}),
      'img':$c('td',{kids:{0:$c('img',{width:'75px',height:'75px','src':'/ajax/pogoda/images/'+ic[0]+'.png'})}}),
      'state':$c('td',{width:100,'#text':ic[1]}),
      'temp':$c('td',{'#html':(i.t>0?'+':'')+Math.round(i.t)+'&#176;'}),
      'atm':$c('td',{'#text':i.p}),
      'wind':$c('td',{'#text':i.w+' м/сек, '+i.r}),
      'hum':$c('td',{'#text':i.h+'%'}),
    }}));
  }
}

function thumbsImg(obj, width, height)
{
  var x_ratio = width / obj.width;
  var y_ratio = height / obj.height;

  var ratio = Math.min(x_ratio, y_ratio);
  var use_x_ratio = x_ratio<y_ratio ? 1 : 0;

  var w = use_x_ratio ? width : Math.ceil(obj.width * ratio);
  var h = !use_x_ratio ? height : Math.ceil(obj.height * ratio);

  obj.style.width=w;
  obj.style.height=h;
}

function cw_loadAfisha(sid,filter,noooo)
{
  var lis=ge('afisha_but').getElementsByTagName('li');
  for(var li=0; li<lis.length; li++){
    var ifis=lis[li].innerHTML.split(' ')[1]==filter;
    if(ifis){
      if(lis[li].id=="cur"&&!noooo){
        if(ifis)filter='';
        lis[li].id="";
      }else lis[li].id="cur";
    }else lis[li].id='';  
  }
  //if(!filter)lis[lis.length-1].id='cur';

	var title ='Афиша';
		var url = document.location.href;
	if(url.indexOf('/event/') != (-1))
  		title = 'Скоро';
  jQuery.get(DIR_WEB_ROOT + '/ajax/afisha/afisha.php', {action:'showItem', sid:sid,title:title, f:filter}, function(resp){
    ge('afisha_block').innerHTML=resp;
  });
  return false;
}

