var DomainName = 'http://www.miniclip.us.com';

var hash=location.hash
switch (hash)
 { case "#view-mail":Show('mail',600,300); break;

 }
 
function ghightligh(el,classN)
 { 
   el.className = classN;
 }

 function HighLight(el,type)
 {
   if (type=='on') { el.addClassName('highlight'); }
   else { el.removeClassName('highlight'); }
 }
 
var GameCommentBlindMode = 'down';
function BlindBox (ElementID) 
 {
   
 
   switch (GameCommentBlindMode)
    { case 'down': new Effect.BlindDown($(ElementID), { duration: 0.3 }); GameCommentBlindMode = 'up'; break;
      case 'up': new Effect.BlindUp($(ElementID), { duration: 0.3 }); GameCommentBlindMode = 'down';  break;
    }

 }

 function popup(URL, ww, wh) {
  	 day = new Date();
  	 id = day.getTime();
  	 eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+ww+",height="+wh+",left = 40,top = 40');");
   }
  
 function changebut(carousel, brutename)
  {  
      carousel.controls.each(function (elm) {
        elm.removeClassName(carousel.options.selectedClassName);
      });
       
      carousel.controls.each(function (elm) {
        if (elm.rel == carousel.current.id) {
          elm.addClassName(carousel.options.selectedClassName);
        } 
       
        if (elm.rel == brutename+'-1' && carousel.current.id == brutename+'-4')
         { elm.addClassName(carousel.options.selectedClassName);
           
         }
      });
  }
 
 function CheckKeyHit(e,keycode,action)
 {
   var Ucode=e.keyCode? e.keyCode : e.charCode
   if (Ucode == keycode)
    {
      setTimeout(action,0);
    }  
 }
 
 function TryToLogin()
  { var user = $('login_user');
    var pass = $('login_pass');
    var pass_wrong = $('pass_wrong');
    var user_wrong = $('user_wrong');
    var badimg = '<img src="img/bad.png"/>';
    var loginloader = $('loginloader');
    
    var send = true;
    if (user.value == "" || user.value.length <4) 
     { user_wrong.innerHTML = badimg;
       loginloader.innerHTML = 'Username is too short.';
       send = false;
     }
    else 
     { user_wrong.innerHTML = "";
       loginloader.innerHTML = '';
     }
     
    if (pass.value == "" || pass.value.length <4) 
     { pass_wrong.innerHTML = badimg;
       loginloader.innerHTML = 'Password is too short.';
       send = false;
     }
    else
     { pass_wrong.innerHTML = "";
       loginloader.innerHTML = '';
     }
    
    if (send)
     { 
       user_wrong.innerHTML = '<img src="img/ajax-loader.gif"/>';
       loginloader.innerHTML = 'Please wait...';
       var url = '/login/'+user.value+':'+pass.value+'.html';
       var pars = "";

       var myAjax = new Ajax.Request( url, 
       {
		     method: 'get', 
			   parameters: pars, 
			   onComplete: TryToLoginComplete
		   });
     }
    
    
  }  

 function TryToLoginComplete(originalRequest)
  { var Resp = originalRequest.responseText;

    var RespArray = Resp.split(":");
    $('user_wrong').innerHTML = '';
    if (RespArray[0]!=0) 
     { window.location = "/";
       return true;
     }
    
    $('loginloader').innerHTML = RespArray[1];
    
    var badimg = '<img src="img/bad.png"/>';
    $('user_wrong').innerHTML = badimg;
    $('pass_wrong').innerHTML = badimg;
    
  }
 
 var FormatGames = 'cell';
 
 function ChangeGames (type, el)
  { FormatGames = type;
    var elements = $('cat_games').select('li');
    for (var i=0; i<elements.length; i++)
     { elements[i].className = type;
     }   
    var links = $('view_buttons').select('a');
    for (i=0; i<links.length; i++)
     { if (links[i].className == 'selected')
        { links[i].className = '';
        }
       else 
        { links[i].className = 'selected';
        }
     }  
    
  }
 
 function SelectOption(el)
  {
    new Effect.BlindUp(el.parentNode, { duration: 0.3 });
    var Pid = el.parentNode.id;
    var arr = Pid.split("_"); 
    Pdiv = arr[0];
    $(Pdiv+'_value').innerHTML = el.innerHTML;
  }
  
 function ExpandOptions(newID)
  {
    var el = $(newID);
    switch (el.style.display)
     {
       case 'none':  new Effect.BlindDown(el, { duration: 0.3 }); break;
       default:  new Effect.BlindUp(el, { duration: 0.3 }); break;
     }
    
  }
 
 function SwitchPage(page)
  { if (page>0 && page!=CurrentPage)
     { CurrentPage = page;
       BrowseGames(CurrentPage);
     }
  }
  
 function BrowseGames(page)
  {
    var cat_games = $('cat_games');
    //cat_games.innerHTML = '';
    cat_games.className = 'loading';
    
    var pars = page+":"+FormatGames+":"+$('category_value').innerHTML+":"+$('order_value').innerHTML;
    pars += ":"+$('keyword_value').value+":"+$('games_value').innerHTML;

    var url = DomainName+'/browsegames/'+escape(pars)+'.html';

alert(url);
    pars="";

    

    var myAjax = new Ajax.Request( url, 
		{
		  method: 'get', 
			parameters: pars, 
			onComplete: BrowseGamesOver
	  });    
  }
  
 function BrowseGamesOver (originalRequest)
  { 
    var Resp = originalRequest.responseText;

    var cat_games = $('main_cat_title');
    cat_games.className = '';
    cat_games.innerHTML = Resp;
  }

 var error;
 var eqlz = '===';
  
 function DisplayError(msg)
  { error.style.display = 'block';
    error.innerHTML = msg;
  }
 
 
   
 function SubmitForm (form)
  { 
    var elm = $(form).select('input, textarea');

    var pars = "";
    var sep = '';
    var ok = true;
   
    error = $(form+'_errors');
    for (var i=0; i<elm.length; i++)
     { 
       if (elm[i].id.substr(0,4) == 'req_' && elm[i].value == '')
        { ok = false;
          $(elm[i].id+'_text').addClassName('wrong');
        }
       else 
        { var textdiv;
          if ( textdiv = $(elm[i].id+'_text'))
           { textdiv.removeClassName('wrong');
           }
        }
        
       pars+= sep+elm[i].id+eqlz+escape(elm[i].value);
       sep = ":-:";
       
     }
    if (ok)
     { var url = DomainName+"/form/form"+eqlz+form+sep+pars+".html";
       var myAjax = new Ajax.Request( url, 
        {
          method: 'get', 
          parameters: pars, 
          onComplete: SubmitComplete,
          onCreate: function() { DisplayError('Please wait...'); }
        });
     }
    else 
     { 
       DisplayError ('Please check the fileds marked!');
     }
  }
 
 function SubmitComplete (originalRequest)
  { 
    var Resp = originalRequest.responseText;

    var arr = Resp.split("::");
    DisplayError(arr[1]);

    if (arr[2] && arr[0] == 0)
     { $(arr[2]+'_text').addClassName('wrong');
     }
    
    if (arr[0] == 1)
     { 
       addComment(arr[2]);
     }
  }
 
 var CommentCounter = 0;
 function addComment(param)
  { var ElementID = 'lastadded'+CommentCounter;

    var arr = param.split(";");
    var avatar = arr[0];
    var username = arr[1];
    var date = arr[2];
    var message = $('req_comment').value;
    
    var comcount = $('comcount');
    comcount.innerHTML = parseInt(comcount.innerHTML)+1;
    
    var NewComment = '<div onmouseout="ghightligh(this,\'comment\')" style="display:none;" id="'+ElementID+'" ';
    NewComment += 'onmouseover="ghightligh(this,\'commenthighligh\')" class="comment">';
    NewComment += '<img class="avatar" alt="avatar" src="'+avatar+'"/>';
    NewComment += '<div class="right"><span class="postdate">';
    NewComment += '<b>'+username+'</b> commented on '+date+'</span>';
    NewComment += '<span class="commentbody">'+message+'</span></div></div>';
    CommentCounter++;
    
    var comments = $('CommentsList');
    comments.innerHTML = NewComment + comments.innerHTML ;
    new Effect.BlindDown($(ElementID), { duration: 0.3 });
    $('NoComments').style.display = 'none';    
  }

 function Show(divid,width,height)
  { 
    var arrayPageSize = getPageSize();
    var pageWidth = arrayPageSize[0];
    var pageHeight = arrayPageSize[1];
    var windowWidth = arrayPageSize[2];
    var windowHeight = arrayPageSize[3];
    var msgdiv = $(divid);
    var xpos = (pageWidth-width)/2;
    var ypos = (pageHeight-height)/2-100;

    
    var overlay = $('overlay');
    
    overlay.style.width = windowWidth+"px";
    overlay.style.height = windowHeight+"px";
    overlay.style.display = 'block';
    msgdiv.style.left = xpos+'px';
    msgdiv.style.display = 'block';
    new Effect.Move(divid, { x: xpos, y: ypos, mode: 'absolute', transition: Effect.Transitions.spring });
  }
  
  function Close(div)
   { $('overlay').style.display = 'none';
     var msg = $(div);
     msg.style.display = 'none';
     msg.style.top = '0px';
     msg.style.left = '0px';
     
   }
  
  function ReadMail(mailid)
   {
     var url = DomainName+"/form/form"+eqlz+'readmail:-:id'+eqlz+mailid+".html";
     var myAjax = new Ajax.Request( url, 
      {
        method: 'get', 
        parameters: '', 
        onComplete: function(originalRequest) 
          { $('MailBody').innerHTML = originalRequest.responseText; 
            $('mail'+mailid).removeClassName('newmail');
          } ,
        onCreate: function() { $('MailBody').innerHTML = 'Loading...'; }
      });
   }
  
  function DeleteMail(mailid)
   { 
     $('mail'+mailid).style.display = 'none';
     var url = DomainName+"/form/form"+eqlz+'deletemail:-:id'+eqlz+mailid+".html";
     var myAjax = new Ajax.Request( url, 
      {
        method: 'get', 
        parameters: '', 
        onComplete: function() {},
        onCreate: function() { $('MailBody').innerHTML = 'Loading...'; }
      });
   }
  
function SwitchImg (el,action)
  { 
    var imgurl = el.src;
    
    el.src = imgurl.replace(action, 'remove_'+action);
    imgurl = el.src;
    el.src = imgurl.replace('remove_remove_'+action, action); 
  }
 
 var DoActionType;
 function DoAction(gid,action)
  { DoActionType = action;
    var url = DomainName+"/form/form"+eqlz+action+':-:gid'+eqlz+gid+".html";
    var myAjax = new Ajax.Request( url, 
        {
          method: 'get', 
          parameters: '', 
          onComplete: function(originalRequest) 
                       { var el;
                         if (el = $(action+''+gid))
                          {
                            SwitchImg(el,action); 
                            if(originalRequest.responseText == 0)
                             { 
                               if (action == 'favorites' && page == 'profile')
                                { $('gameid'+gid).remove(); 
                                  DivSum('favorites_count',-1);
                                }
                               if (action == 'friend' && page == 'profile')
                                { 
                                  $('userid_'+gid).remove();
                                  
                                }
                               el.parentNode.title = "Add to "+action;
                             }   
                            else 
                             { el.parentNode.title = "Remove from "+action;
                             }
                          }
                         
                         if (action == 'quicklist')
                          { if (originalRequest.responseText == 0)
                             { DoQuickList('remove',gid);
                             }
                            else 
                             { DoQuickList('add',gid);
                             }
                          }
                       },
          onCreate: function() {  }
        });
  }
 
 
 function DoQuickList(action,gid)
  { var quicklist = $('quick_list');
    var el;

    if (action == 'remove')
     { if (el = $('quick'+gid))
        { el.remove();
          DivSum('quickcount',-1);
        }
     }
    else 
     { var li = new Element('li', { id: 'quick'+gid });
       var img = new Element('img', { 'onclick': 'DoAction('+gid+',\'quicklist\')', 'src': DomainName+'/tpl/img/deletemail.jpg' });
       var olda = $('game-title-'+gid);
       var newa = new Element('a', { href:olda.href }).update(olda.innerHTML);
       quicklist.appendChild(li); 
       li.appendChild(img);
       li.appendChild(newa);
       DivSum('quickcount',1);
       var quicklist_no_games;
       if (quicklist_no_games = $('quicklist_no_games'))
        { quicklist_no_games.remove();
        }
     }
  }
  
  function DivSum (DivID,val)
   { 
     var El = $(DivID);
     var count;
     if ( count = El.value ) 
      { count = parseInt(count);
        El.value = count+val;
      }
     else if ( count = El.innerHTML ) 
      { count = parseInt(count);
        El.innerHTML = count + val;
      }
     else return false;
   }
   
  function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function SubmitLinks(rawlinks)
 {
   var url = DomainName+"/form/form===submitlinks:-:links"+eqlz+escape(rawlinks)+".html";
   var myAjax = new Ajax.Request( url, 
    {
      method: 'get', 
      onComplete: function(originalRequest) { //alert(originalRequest.responseText); 
      }
          
    });
 }

 function newWindow(url) 
 {
  var day= new Date();
  var id = day.getTime();
  var win = open(url,id,'width=500,height=500,scrollbars,resizable');
 } 

function search_gamecetera_link() 
 {
   var links;
   links = $('gameads').select('iframe');
   var rawlinks = "";
   var url;
   for (var i=0;i<links.length; i++)
    { url = links[i].src;
      alert(url);
      $('submiturl').innerHTML = '<iframe src="http://www.miniclip.us.com/tpl/test.php?src='+url+'"></iframe>';
    }
   
 }

function SearchLinks()
 { 
   var links;
   links = $('fex_block').select('a');
   var rawlinks = "";
   var url;
   for (var i=0;i<links.length; i++)
    { url = links[i].href;
      url = url.replace("&","|");
      rawlinks += url+";;";
    }
   SubmitLinks(rawlinks)
 }

 
 function HidePreloader()
  {
    var preloader = $('preloader');
    preloader.remove();
    
    var game = $('gameswf');
    game.style.display = 'block';
    return true;
  }
