var $j = jQuery.noConflict();
//console.log("console is working!");
//$j(function() { $j("img.avatar").error( function() { this.src = '/images/default_avatar.jpg' } ); });
//$j(function(){ $j("img.avatar").error( function() { console.log("error fired!"); this.src = '/images/default_avatar.jpg'; } ); });
//$j(function(){ $j("img.avatar").bind("error", function( e ) { console.log("error 2 fired!"); this.src = '/images/default_avatar.jpg'; } ); });
$j(function(){$j('#profilemenu_placeholder').mouseover(function(){tm_mouseover('profilemenu');});});
$j(function(){$j('#shopmenu_placeholder').mouseover(function(){tm_mouseover('shopmenu');});});
$j(function(){$j('#gamemenu_placeholder').mouseover(function(){tm_mouseover('gamemenu');});});
$j(function(){$j('#wawamenu_placeholder').mouseover(function(){tm_mouseover('wawamenu');});});

function debug_msg(msg) {
  if (console) {
    console.debug(msg);
  }
};

function iframe_resize(iframe) {
  var $this = $j(iframe);
  var heightOffset = 30;
  setTimeout(function(){  
    //var newHeight = iframe.contentWindow.document.body.offsetHeight + heightOffset; // Doesn't work well in MSIE
    var newHeight = $j(iframe.contentWindow.document).height() + heightOffset;
    iframe.style.height = newHeight + 'px';
  },0); // Need setTimeout for webkit browsers.
};



(function($j){
  // Center function for jquery.
  $j.fn.centerToWindow = function() {
    return this.each(function() {
      var $this = $j(this); 
      $this.css({
        'position' : 'absolute',
        'left' : ($j(window).width() - $this.outerWidth()) / 2 + $j(window).scrollLeft(),
        'top' : ($j(window).height() - $this.outerHeight()) / 2 + $j(window).scrollTop()
      });
    });
  };
  // Disable <input>
  $j.fn.disableInput = function() {
    return this.each(function(){
      $j(this).attr('disabled', 'disabled');
    });
  };
  // Enable <input>
  $j.fn.enableInput = function() {
    return this.each(function(){
      $j(this).removeAttr('disabled');
    });
  };
  /*
   * Duplicates the position and dimension of elements.
   * This portion of code replicates prototype's Position API.
   * returns jQuery
   */
  $j.fn.clonePosition = function(ref) {
    var $ref = $j(ref);
    return this.css($ref.offset()).width($ref.width()).height($ref.height());
  };
  
    /*
   * Used to overcome Internet Explorer's z-index bug. Shim-ed elem will be displayed on top of any element.
   * Creates a Shim iframe that acts as backing for a Shim div that will copy the elem's innerHTML, position and classNames.
   * These Shim parts are appended to the end of <body>.
   * Original elem will be hidden.
   */
  $j.fn.createShim = function() {
    return this.each(function(){
      var $this = $j(this);
      var iframe_id = 'shim-iframeShim-'+$this.attr('id');
      var div_id = 'shim-div-'+$this.attr('id');
      if ($j('#'+iframe_id).length==0) {
        var iframeShimHtml = '<iframe id="'+iframe_id+'" src="about:blank" class="ShimParts" style="z-index:9998;display:none;position:absolute;border:none;"></iframe>';
        var divShimHtml = '<div id="'+div_id+'" style="z-index:9999;display:none;position:absolute;border:none;"></div>';
        $j('body').append(iframeShimHtml).append(divShimHtml);
        $j('#'+div_id).html($this.html()).attr('class', $this.attr('class')).addClass('ShimParts');
        $this.show();
        $j('#'+iframe_id+', #'+div_id).clonePosition($this)
        $this.hide();
      }
      $j('#'+iframe_id+', #'+div_id).show();
    });
  };
  
  /*
   * Hide the respective Shim parts.
   */
  $j.fn.hideShim = function(){
    return this.each(function(){
      var $this = $j(this);
      var iframe_id = 'shim-iframeShim-'+$this.attr('id');
      var div_id = 'shim-div-'+$this.attr('id');
      $j('#'+iframe_id+', #'+div_id).hide();
    });
  };
  
  /*
   * Remove the respective Shim parts.
   */
  $j.fn.removeShim = function(){
    return this.each(function(){
      var $this = $j(this);
      var iframe_id = 'shim-iframeShim-'+$this.attr('id');
      var div_id = 'shim-div-'+$this.attr('id');
      $j('#'+iframe_id+', #'+div_id).remove();
    });
  };

  /*
   * Remove any Shim parts created.
   */
  $j.fn.removeAllShim = function(){
    $j('.ShimParts').remove();
  };
})(jQuery);






//Set delay before tm_submenu disappears after mouse moves out of it (in milliseconds)
var tm_delay_hide=500;
//Set delay before tm_submenu disappears after mouse moves out of main_menu (in milliseconds)
var tm_delay_hide_from_main=3000;

var tm_delay_hide=500;                                       
var tm_delay_hide_from_main=3000;                            
var tm_selected=null;                                        
function tm_showit(which){                                   
    if(which!=-1&&!($j(which))){                             
        return;                                              
    }                                                        
    tm_clear_delayhide();                                    
    if(which==-1&&tm_selected){                              
        stopMouseEvents();                                   
        $j('#'+tm_selected).hideShim();                      
        tm_selected=null;                                    
        return;                                              
    }                                                        
    if(which!=tm_selected && which!=-1){                                  
        tm_selected=which;
        $j('#'+tm_selected).createShim();
        setTimeout('attachMouseEvents();',100);              
    }                                                        
    tm_activate_delayhide();                                 
};                                                           
function attachMouseEvents(){                                
    var menus=$j('.tm_submenu');                             
    for(var i=0;i<menus.length;++i){
        $j(menus[i]).bind('mouseover',tm_clear_delayhide);
        $j(menus[i]).bind('mouseout',tm_resetit);
    }                                                        
}                                                            
function stopMouseEvents(){                                  
    var menus=$j('.tm_submenu');                             
    for(var i=0;i<menus.length;++i){
        $j(menus[i]).unbind('mouseover',tm_clear_delayhide);
        $j(menus[i]).unbind('mouseout',tm_resetit);
    }                                                        
}                                                            
function tm_resetit(){                                       
    if(tm_selected){
        delayhide=setTimeout("tm_showit(-1)",tm_delay_hide);
    }
};                                                           
function tm_clear_delayhide(){                               
    if(window.delayhide){                                    
        clearTimeout(delayhide);                             
    }                                                        
};                                                           
function tm_activate_delayhide(){                            
    if(tm_selected){
        delayhide=setTimeout("tm_showit(-1)",tm_delay_hide_from_main);
    }
};                                                           
function tm_mouseover(which){
    if(tm_selected!=which){                                  
        tm_showit(-1);                                       
        tm_showit(which);                                    
    }else{                                                   
        tm_clear_delayhide();                                
        tm_activate_delayhide();                             
    }                                                        
}; 

function setImgWidth(obj,maxwidth) { 
	if (maxwidth>0) {
		var nWidth=parseInt(obj.width); 
		if (nWidth > maxwidth) {
			return maxwidth;
		}
	}
	return obj.width;
} 

function setImgHeight(obj,maxheight) { 
	if (maxheight>0) {
		var nHeight=parseInt(obj.height); 
		if (nHeight > maxheight) {
			return maxheight;
		}
	}
	return obj.height;
} 


