//$.AjaxifyDebug = true;

var timer;
var numberTimer;

$(document).ready(function() {   
 
 adVisibility();
	
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
     //hide bar for iphone
	if (document.getElementById){
		obj = document.getElementById('barcontainer');
		obj.style.display = "none";
	}


} else {


     var blnPinScrollbar = false;
     e = document.getElementById('chatwindow');

     $('#chatsubmit').ajaxify({
              target:'#chatwindow+',
              link:'http://uwadb.com/chat/chat.php?action=post',
              forms:'#chatform',

              loading_img:'http://uwadb.com/chat/loading.gif',
              loading_target:'#loading',

              onStart:function(options){

                            //if the scrollbar is at the very bottom before updating the div, then set the pin variable to true
                            if(e.scrollHeight - e.scrollTop < 220){
                                          blnPinScrollbar = true;
                            } else {
                                          blnPinScrollbar = false;
                            }
              },

              onSuccess:function(options,data){
                     obj = document.getElementById('chattext');
                     obj.value = "";

                     //if the pin variable is true, go ahead and scroll down, otherwise, don't touch the scroll position
                     if(blnPinScrollbar){
                            e.scrollTop = e.scrollHeight + 100;
                     }
              }
      });

      $('.gamelink').ajaxify({
              target:'false',

              tagToload:{
                  '#game':'#gamediv',
                  '#width':'#gamewidth',
                  '#height':'#gameheight'
              },

              onSuccess:function(op){
                      $('#gamediv').modal({
                             onShow: function (dialog) {
                                    chatHide();

                                    gameContainer = document.getElementById('simplemodal-container');
                                    
                                    gameWidth = document.getElementById('width');
                                    gameHeight = document.getElementById('height');

                                    gameContainer.style.height = gameHeight.innerHTML + "px";
                                    gameContainer.style.width = gameWidth.innerHTML + "px";

                                    $(window).trigger('resize.simplemodal');


                             }
                      });
              }
      });

menuFix();
numberUpdate();

}

});

function menuFix(){
           gamewindow = document.getElementById('gamemenu');

      if (window.innerHeight < 630) {
           gamewindow.style.height = (window.innerHeight - 70) + "px";
           gamewindow.style.overflowY = "scroll";
           gamewindow.style.overflowX = "hidden";
      } else {
           gamewindow.style.height = "";
           gamewindow.style.overflowY = "";
           gamewindow.style.overflowX = "";
      }
}

function chatUpdate(){

     $().ajaxify({
              link:'http://uwadb.com/chat/chat.php?action=read',
              target: '#chatwindow+',
              method:'POST',
              loading_img:'http://uwadb.com/chat/loading.gif',
              loading_target:'#hidden',
              event:false,

              onStart:function(options){

                            //if the scrollbar is at the very bottom before updating the div, then set the pin variable to true
                            if(e.scrollHeight - e.scrollTop < 220){
                                          blnPinScrollbar = true;
                            } else {
                                          blnPinScrollbar = false;
                            }
              },

              onSuccess:function(options,data){
                     //if the pin variable is true, go ahead and scroll down, otherwise, don't touch the scroll position
                     if(blnPinScrollbar){
                            e.scrollTop = e.scrollHeight + 100;
                     }
              }
      });

timer = setTimeout ( "chatUpdate()", 5000);

}

function numberUpdate(){

     $().ajaxify({
              link:'http://uwadb.com/chat/chat.php?action=number',
              target: '#chatnumber',
              method:'POST',
              loading_target:'#hidden',
              event:false
      });

numberTimer =  setTimeout ( "numberUpdate()", 5000);

}

function showhidechat(){
	if (document.getElementById){

		obj = document.getElementById('chatcontainer');

		if (obj.style.display == "none"){
			chatShow();
		} else {
			chatHide();
			chatLogoff();
			chatClear(); //clear chat every time you hide the window

		}
	}
}

function chatClear(){

	obj = document.getElementById('chatwindow');

  while (obj.childNodes[0])
  {
    obj.removeChild(obj.childNodes[0]);
  }

}

function chatShow(){

	obj = document.getElementById('chatcontainer');
	buttonobj = document.getElementById('chatbutton');
	abuttonobj = document.getElementById('chatabutton');

	obj.style.display = "";
	buttonobj.style.backgroundColor = "#ffffff";
	abuttonobj.blur();
	chatUpdate();
}

function chatHide(){

	obj = document.getElementById('chatcontainer');
	buttonobj = document.getElementById('chatbutton');
	abuttonobj = document.getElementById('chatabutton');

	obj.style.display = "none";
	buttonobj.style.backgroundColor = "";
	abuttonobj.blur();
	clearTimeout(timer);
}

function chatLogoff(){

     $().ajaxify({
              link:'http://uwadb.com/chat/chat.php?action=logoff',
              target: '#hidden',
              method:'POST',
              loading_target:'#hidden',
              event:false
      });

}

function adVisibility(){
	$().ajaxify({
		link:'http://uwadb.com/chat/showad.php',
		target: '#adshow',
		method:'POST',
		loading_img:'http://uwadb.com/chat/loading.gif',
		loading_target:'#hidden',
		event:false,

		onSuccess:function(options,data){

			if (document.getElementById){
			adsetting = document.getElementById('adshow');

				if(adsetting.innerHTML == "show"){
					adbox = document.getElementById('guestadboxtop');
					adbox.style.display = "";					
				}
			}		
		}
	});
}

//scroll fix for IE
window.onload = function ()
		{
			if(document.all) {
				var codeFix = document.all.tags("div");
				for (var codeI = 0; codeI < codeFix.length; codeI++)
				{
					if (codeFix[codeI].className == "scroll" && (codeFix[codeI].scrollWidth > codeFix[codeI].clientWidth || codeFix[codeI].clientWidth == 0))
					codeFix[codeI].style.height = (codeFix[codeI].clientHeight + 34) + "px";
				}
			}
		}
