var loadstatustext = "<img src='http://www.dursamj.mn/stx/images/loading4.gif' border=0 />"
var defaultcontentarray = new Object();
var mainContainer = '';
var bustcachevar = 1 //bust potential caching of external pages after initial request? (1=yes, 0=no)

function ajaxConnect()
{
  var xmlHttp;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return xmlHttp;
}

function afdsafds(){

  var xhr;
  xhr = ajaxConnect();

  var url = 'userupdate.php?rand=' + Math.round(100*Math.random()) + Math.round(100*Math.random()) + Math.round(100*Math.random()) + '&user_id=' + userid;

  xhr.open('GET', url, true);
  xhr.onreadystatechange = function()
  {
    if(xhr.readyState == 4 && xhr.status == 200) {
      document.getElementById('userUpdateDesc').innerHTML = xhr.responseText;
    }
    else
    {
      document.getElementById('userUpdateDesc').innerHTML = "<div align='center' style='width:100%; height:250px;'><br><br><br><br><br><br><br><img src='images/ajax-loader_circle.gif' align='absmiddle' /><div>";
    }
  }
  xhr.send(null);
}

function defaultPage(tabcontentid, tabnumber){
  var thetab=document.getElementById(tabcontentid).getElementsByTagName("a")[tabnumber]
  if (thetab.getAttribute("rel")){
    var str = thetab.getAttribute("rel").split('|')
    ajaxpage(str[1], str[0], thetab)
    loadobjs(thetab.getAttribute("rev"))
  }
}

function testTab(tabcontentid){

  mainContainer = document.getElementById(tabcontentid);

  var arguments = mainContainer.childNodes;


  for (var i=1; i<arguments.length; i++) { //loop through each TD element

    var tdobj = arguments[i];
    //var ulist = ulobj.getElementsByTagName("li")
    //alert(tdobj.innerHTML);
    if( tdobj.innerHTML.substr(0, 2) == '<A'){

      //alert(tdobj.innerHTML);

      var ulistlink=tdobj.getElementsByTagName("A")[0];

      ulistlink.index = i ;

      if (ulistlink.getAttribute("rel")){

        var str = ulistlink.getAttribute("rel").split('|')
        savedefaultcontent(str[0])

        ulistlink.onclick = function() {

          var str2 = this.getAttribute("rel").split('|')
          //alert(this.getAttribute("onclick"))
          //alert(str2[1]);

          ajaxpage(str2[1], str2[0], this);
          deselectTabs();
          selectTab(this.index);
          return false;
        }
      }
    }
  }
}

function ajaxpage(url, containerid, targetobj) {

  page_request = ajaxConnect();

  deselectTabs();  //deselect all tabs
  //selectTab(4);  //highlight currently clicked on tab send targetobj

  if (url.indexOf("#default")!=-1) { //if simply show default content within container (verus fetch it via ajax)
    document.getElementById(containerid).innerHTML=defaultcontentarray[containerid]
    return
  }

  document.getElementById(containerid).innerHTML = loadstatustext
  page_request.onreadystatechange = function() {
    loadpage(page_request, containerid)
  }
  if (bustcachevar) //if bust caching of external page
  bustcacheparameter = (url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()

  page_request.open('GET', url+bustcacheparameter, true)
  page_request.send(null)
}

function loadpage(page_request, containerid) {
  if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
    document.getElementById(containerid).innerHTML = page_request.responseText
  }
}

function savedefaultcontent(contentid){// save default ajax tab content

  if (typeof defaultcontentarray[contentid] == "undefined") //if default content hasn't already been saved
  defaultcontentarray[contentid] = document.getElementById(contentid).innerHTML
}

function deselectTabs(){
  var tdList = mainContainer.childNodes;
  for (var i=1; i<tdList.length - 4; i+=4)
  {
    tdList[i].className="statTabLeftPassive";
    tdList[i+1].className="statTabCenterPassive";
    tdList[i+2].className="statTabRightPassive";
  }
}

function selectTab(id){
  var tdList = mainContainer.childNodes;
  tdList[id-1].className="statTabLeftActive";
  tdList[id].className="statTabCenterActive";
  tdList[id+1].className="statTabRightActive";
}
function universalNewsTabState(name,tabid,count)
{
  //alert(name+'-'+tabid+'-'+count);
  for(i=1; i<=count; i++)
  {
    var elem = 'c'+ name + i;
    var elemid = 'c'+ name + tabid;
    if( elem == elemid )
    {
      document.getElementById( 'l' + name + i ).className = 'news_tab_active1';
      document.getElementById( 'c' + name + i ).className = 'news_tab_active2';
      document.getElementById( 'r' + name + i ).className = 'news_tab_active3';
    }
    else
    {
      document.getElementById( 'l' + name + i ).className = 'news_tab_normal1';
      document.getElementById( 'c' + name + i ).className = 'news_tab_normal2';
      document.getElementById( 'r' + name + i ).className = 'news_tab_normal3';
    }
  }
}
function universalNewsSubTabState(name,tabid,count)
{
  for(i=1; i<=count; i++)
  {
    var elem = 'c'+ name + i;
    var elemid = 'c'+ name + tabid;
    if( elem == elemid )
    {
      document.getElementById( 'l' + name + i ).className = 'news_tab_sub_menu1';
      document.getElementById( 'c' + name + i ).className = 'news_tab_sub_menu2';
      document.getElementById( 'r' + name + i ).className = 'news_tab_sub_menu3';
    }
    else
    {
      document.getElementById( 'l' + name + i ).className = 'news_tab_sub_menu_normal1';
      document.getElementById( 'c' + name + i ).className = 'news_tab_sub_menu_normal2';
      document.getElementById( 'r' + name + i ).className = 'news_tab_sub_menu_normal3';
    }
  }
}
function nutagNameSelect(name,id,count)
{
  for(i=1; i<=count; i++)
  {
    var elem = i;
    var elemid = id;
    if( elem == elemid )
    {
      document.getElementById( name + i ).className = 'nutag_name_active';
    }
    else
    {
      document.getElementById( name + i ).className = 'nutag_name_normal';
    }
  }
}
function showhide(name,id,count)
{
  for(i=1; i<=count; i++)
  {
    var elem = i;
    var elemid = id;
    if( elem == elemid )
    {
      document.getElementById( name + i ).style.display = 'inline';
    }
    else
    {
      document.getElementById( name + i ).style.display = 'none';
    }
  }
}
//AJAX CONTROLLER
function makeRequest(url, container)
{
  //alert(url+container);
  var http_request = false;
  if (window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) {
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  http_request.onreadystatechange = function() { alertContents(http_request, container) };
  //url = url + '&rand = ' + parseInt(Math.random()*999999999999999);
  url = url;
  http_request.open('GET', url, true);
  http_request.send(null);
}

function alertContents(http_request, container)
{
  if (http_request.readyState == 4)
  {
    if (http_request.status == 200)
    {
      document.getElementById(container).innerHTML=http_request.responseText;
    }
    else
    {
      //alert("Problem");
    }

  }
  else
  {
    document.getElementById(container).innerHTML = "<div style='padding:5px;' align='center'><img src='http://localhost/hiimori.mn/images/ajax-loader_circle.gif' /></div>";
  }
}
function tabState(tabid)
{
  for(i=1; i<=7; i++)
  {
    var elem = 'ttabc'+ i;
    var elemid = 'ttabc'+ tabid;
    if( elem == elemid )
    {
      document.getElementById( 'ttabl' + i ).className = 'brownTabLeftActive';
      document.getElementById( 'ttabc' + i ).className = 'brownTabCenterActive';
      document.getElementById( 'ttabr' + i ).className = 'brownTabRightActive';
    }
    else
    {
      document.getElementById( 'ttabl' + i ).className = 'brownTabLeftPassive';
      document.getElementById( 'ttabc' + i ).className = 'brownTabCenterPassive';
      document.getElementById( 'ttabr' + i ).className = 'brownTabRightPassive';
    }
  }
}
function ntabState(tabid)
{
  for(i=1; i<=7; i++)
  {
    var elem = 'nttabc'+ i;
    var elemid = 'nttabc'+ tabid;
    if( elem == elemid )
    {
      document.getElementById( 'nttabl' + i ).className = 'brownTabLeftActive';
      document.getElementById( 'nttabc' + i ).className = 'brownTabCenterActive';
      document.getElementById( 'nttabr' + i ).className = 'brownTabRightActive';
    }
    else
    {
      document.getElementById( 'nttabl' + i ).className = 'brownTabLeftPassive';
      document.getElementById( 'nttabc' + i ).className = 'brownTabCenterPassive';
      document.getElementById( 'nttabr' + i ).className = 'brownTabRightPassive';
    }
  }
}
function wmakeRequest(tabid, url)
{

  wtabState(tabid);
  return true;
  var http_request = false;
  if (window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) {
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (!http_request) {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  alert(tabid + '-' + url);
  http_request.onreadystatechange = function() { walertContents(http_request) };
  url = url + '&rand = ' + parseInt(Math.random()*999999999999999);
  //url = 'http://{/literal}{$serverName}{$baseUrl}{literal}/home/ajax/naadamresult/id/{/literal}{$naadam.id}{literal}/nas/' + id + '/' + parseInt(Math.random()*999999);
  http_request.open('GET', url, true);
  http_request.send(null);
}

function walertContents(http_request)
{
  if (http_request.readyState == 4)
  {
    if (http_request.status == 200)
    {
      document.getElementById('naadamResult').innerHTML=http_request.responseText;
    }
    else
    {
      //alert("Problem is occured.");
    }

  }
  else
  {
    document.getElementById('naadamResult').innerHTML = 'loading ...';
  }
}

function wtabState(tabid)
{
  for(i=1; i<=6; i++)
  {
    if( i == tabid )
    {
      document.getElementById( 'tabStatus' + i ).className = 'videoTabActive';
    }
    else
    {
      document.getElementById( 'tabStatus' + i ).className = 'videoTabPassive';
    }
  }
}
function findPosX(obj)
{
  var curleft = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curleft += obj.offsetLeft
      obj = obj.offsetParent;
    }
  }
  else if (obj.x)
  curleft += obj.x;
  return curleft;
}

function findPosY(obj)
{
  var curtop = 0;
  if (obj.offsetParent)
  {
    while (obj.offsetParent)
    {
      curtop += obj.offsetTop
      obj = obj.offsetParent;
    }
  }
  else if (obj.y)
  curtop += obj.y;
  return curtop;
}
var moves;
function toRight()
{
  var remain = $('thumbnails').scrollHeight;
  if(remain-300 > $('thumbnails').scrollTop)
  	$('thumbnails').scrollTop += 2;
  else
	return false;
}
function toLeft()
{
  if(0 < $('thumbnails').scrollTop)
  	$('thumbnails').scrollTop -= 2;
  else
	return false;
}
function moveThumb(dir, state)
{
  if(state == 'over')
  	{
  	if(dir == 'left')
  		{
  		moves = setInterval('toLeft()', 10);
  		}
  	else
  		{
  		moves = setInterval('toRight()', 10);
  		}		
  	}
  else
  	clearInterval(moves);
}
function CommentUpdate(){
	var xhr;
	xhr = ajaxConnect();
  var state = 'send';
	user_id = 0;
	var text = document.getElementById('comment_text').value;
	var obj_type = document.getElementById('obj_type').value;
	var obj_id = document.getElementById('obj_id').value;
	document.getElementById('comment_text').value='';
  var url = 'http://www.hiimori.mn/home/ajax/comment/rand/' + Math.round(100*Math.random()) + Math.round(100*Math.random()) + Math.round(100*Math.random()) + '/state/' + state + '/user_id/' + user_id + '/obj_type/' + obj_type + '/obj_id/' + obj_id + '/text/' + encodeURI(text);
	
   	xhr.open('GET', url, true);
   	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4 && xhr.status == 200) {
			document.getElementById('commentContainDiv').innerHTML = xhr.responseText;
		}
		else
		{
		document.getElementById('commentContainDiv').innerHTML = '<div style="padding:2px; height: 20px" align="center"><img src="http://www.hiimori.mn/images/ajax-loader_circle.gif" /></div>' + document.getElementById('commentContainDiv').innerHTML;
		}		 
	}
   	xhr.send(null);
	document.getElementById('text').focus();
}
var message="ХWWW.HIIMORI.MN сайтнаас зөвшөөрөлгүй хуулбарлан ашиглахыг хориглоно!";
function click(e) {
if (document.all) {
if (event.button==2||event.button==3) {
alert(message);
return false;
}
}
if (document.layers) {
if (e.which == 3) {
alert(message);
return false;
}
}
}
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=click;