window.onload = function() {
  var content_more = $('content_text_more');
  if (content_more) content_more.hide();
  
  $(document).observe('click', respondToClick);
};

function respondToClick(event) {
  var search_form = $('search_form_block');
  var element = Event.element(event);
  if (element.id != 'search_ico' && element.id != 'q' && element.id != 'search_form_note' && element.id != 'search_form_img' && element.id != 'search_form_title') {
    if (search_form.visible()) search_form.hide();
  }
}

function show_content_text_more()
{
  var more = $('content_text_more');
  var link_1 = $('content_text_more_link_1');
  var link_2 = $('content_text_more_link_2');
  if (more.visible()) { 
    more.hide(); 
    link_1.show();
    link_2.hide();
  }
  else {
    more.show();
    link_2.show();
    link_1.hide();
  }
}

function getViewportSize() { 
	var size = [0, 0]; 
	if (typeof window.innerWidth != "undefined") { 
		size = [window.innerWidth, window.innerHeight];
	} 
	else if (typeof document.documentElement != "undefined" && typeof document.documentElement.clientWidth != "undefined" && document.documentElement.clientWidth != 0) {
		size = [document.documentElement.clientWidth, document.documentElement.clientHeight]; 
	}
	else {
		size = [document.getElementsByTagName("body")[0].clientWidth, document.getElementsByTagName("body")[0].clientHeight]; 
	}
	return size; 
}
function createFullBrowserFlash() {
	swfobject.createCSS("html", "height:100%;");
	swfobject.createCSS("body", "height:100%;");
	swfobject.createCSS("#container", "margin:0; width:100%; height:100%; min-width:1000px; min-height:800px;");
	window.onresize = function() {
		var el = document.getElementById("container");
		var size = getViewportSize(); 
		el.style.width = size[0] < 1024 ? "1000px" : "100%";
		el.style.height = size[1] < 800 ? "800px" : "100%";
	};
	window.onresize();
}	
function extract_key()
{
  if (window.location.href.indexOf('message') != -1)
  {
    var arr = window.location.href.split('/');
    return arr[arr.length - 1];
  }
  return null;
}
function findPos(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		
		return [curleft, curtop];
	}

function show_cloud(event)
{
  var cloud = $('account_cloud');
  var text = $('container_text');
  
  cloud.appear();
  text.fade({ queue: 'front', duration: 0.2 });
}

function hide_cloud(event)
{
  var cloud = $('account_cloud');
  var text = $('container_text');
  
  text.appear();
  cloud.fade({ queue: 'front', duration: 0.2 });
}

var old_state = -1;

var RequestAjaxCnt = {activeRequestCount:0};

Ajax.Responders.register({
    onCreate: function(ajax) {
    	if(ajax.options.invisible) return;
    	RequestAjaxCnt.activeRequestCount++;
    },
    onComplete: function(ajax) {
    	if(ajax.options.invisible) return;
    	RequestAjaxCnt.activeRequestCount--;
    }
});

function handlerAccount(t)
{
  if (RequestAjaxCnt.activeRequestCount != 1) return;
  
  try {
      // returns HTML Object or HTML Collection
      // and attempts to loop through return UNDEFINED or NULL
	    if (t.readyState == 4 && t.status == 200) {
		    var xmlDoc = t.responseXML;
		    
		    if (xmlDoc != null)
		    {
		      var account = xmlDoc.getElementsByTagName('account');
		      if(account == null) return;
		      
		      var state_node = account[0].getElementsByTagName('state')[0];
		      if (state_node == null) return;
		      
		      var offset_node = account[0].getElementsByTagName('offset')[0];
		      if (offset_node == null) return;
		      
		      var state_value = parseInt(state_node.childNodes[0].nodeValue);
		      var offset_value = parseInt(offset_node.childNodes[0].nodeValue);
		      
		      if (flash == null) {
		        flash = swfobject.getObjectById("flash_video");
		        //document.getElementById('flash_video');
		      }
		      
		      if (old_state != state_value)
		      {
		        flash.status(state_value, offset_value);
		        old_state = state_value;
		        
		        if (periodical != null && (state_value == 6 || state_value == 4 || state_value == 5)) 
		        {
		          periodical.stop();
		        } 
		      }
      	}
      }

  } catch(e) {
     //alert(e.message);
  }  
}

function remote_link(event) 
{
  var element = Event.element(event);
  	new Ajax.Request(element.href, {asynchronous:true, evalScripts:true, parameters:{}, method:'get'});
 	 return false;
};

function show_table(table_id, elem)
{  
  var link = $(elem);
  var flag = link.className.include('left');
  $(table_id).toggle();
  link.className = flag ? 'more arr-down' : 'more arr-left';  
}
