/*
 *  Galaxy4Gamers
 *
*/

Cufon.replace('#hd_nav a.swap', { fontFamily: 'Flama', hover : true });
Cufon.replace('#news_nav a', { fontFamily: 'Flama', hover : true });
Cufon.replace('h3', { fontFamily: 'Flama' });
Cufon.replace('#bd_cta span', { fontFamily: 'Flama' });

var G4G = { };

G4G.News = Class.$extend({

  __init__ : function($_container, $_item, $_links) {
  
    this.container = $_container;
    this.item = $_item;
    this.links = $_links;
    this.rotateID = false;
    this.current = false;
    
    this.setContainerWidth();
    this.buildLinks();
    this.startCarousel();    
      
  },
  
  setContainerWidth : function() {
  
    var tmp = 0, $self = this;
    
    this.getItems().each(function(i, $_el){
      if(i == 0) { 
        $self.current = $($_el).attr('id');
      }
      tmp += $($_el).width();
    });

    $(this.container).width(tmp+'px');
    this.setActiveLink();
  
  },
  
  getItems : function() {
  
    return $(this.container+' '+this.item);
  
  },
  
  slideRight : function() {
  
    var items = this.getItems(),
        $self = this;
        
    /* Move Last to First */
    
    if(items.length > 1) {
    
      var last = items.last().css('margin-left', '-700px');
      $($self.container).prepend(last);    
    
      last.animate({
        marginLeft : '+=700'
      }, 500, function(){
      
        $self.current = last.attr('id');
        $self.setActiveLink();
      
      });
    
    
    }
  
  },
  
  slideLeft : function() {
  
    var items = this.getItems(),
        $self = this;
    
    if(items.length > 1) {
    
      items.first().animate({
        marginLeft : '-=700'
      }, 500, function(){
      
        $(this).css('margin-left', '0px');
        $($self.container).append(this);
        $self.current = $self.getItems().first().attr('id');
        $self.setActiveLink();
   
      })
    
    }
  
  },
  
  startCarousel : function() {
  
    var $self = this;
  
    this.rotateID = setInterval(function(){
    
      $self.slideLeft();
    
    }, 10000);
  
  },
  
  stopCarousel : function() {
  
    clearInterval(this.rotateID);
  
  },
  
  goToItem : function($_id) {
  
    var start = 0, stop = 0, $self = this;
  
    this.getItems().each(function(i, $_el){
    
      if(('#'+$($_el).attr('id')) == $_id) {
        stop = i;
      } else if($($_el).attr('id') == $self.current) {
        start = i;
      }
    
    });
        
    if(start > stop) {
    
      this.multiSlide('right', start - stop);
    
    
    } else { 
    
      this.multiSlide('left', stop - start);
    
    }
  
  },
  
  multiSlide : function($_direction, $_times) {
  
    var $self = this, c = 0;
  
    var tmpId = setInterval(function(){
    
      if(c < $_times) {
    
        if($_direction == 'left') {
          $self.slideLeft();
        } else {
          $self.slideRight();
        }
        
        c++;
        
      } else {
      
        clearInterval(tmpId);
        $self.startCarousel();
      
      }
    
    }, 600); //extra 1/10th of a second
  
  
  },
  
  buildLinks : function() {
  
    var $self = this;
  
    $(this.links).each(function(i, $_el){
    
      $($_el).click(function(e){
      
        e.preventDefault(); 
        $self.stopCarousel();
        $self.goToItem($(this).attr('href'));
              
      })
    
    
    });
  
  },
  
  setActiveLink : function() {
  
    var $self = this;
  
    $(this.links).each(function(i, $_el){
    
      if($($_el).attr('href') == '#'+$self.current) {
        $($_el).parent().addClass('selected');
      } else {
        $($_el).parent().removeClass('selected');
      }
    
    });
    
    Cufon.refresh('#news_nav a');
  
  }

});

$(document).ready(function(){
   
  var news = new G4G.News('#news_container', '.news_item', '#news_nav a');
  
  	var useH = jQuery("#dropNav li").size()*31+8
	jQuery("#dropNav").height('0px')
  	jQuery("#dropNavP").mouseenter(function(){
		jQuery("#dropNavP a:first").removeClass("aro1").addClass("aro2")								
		jQuery("#dropNav").clearQueue().stop().height(useH)							 
	}).mouseleave(function(){
		jQuery("#dropNav").clearQueue().stop().delay(200).animate({'height': 0}, 200, function(){
			jQuery("#dropNavP a:first").removeClass("aro2").addClass("aro1")																					   
		})					 
	})  
  
  	var useH2 = jQuery("#dropNav2 li").size()*31+8
	jQuery("#dropNav2").height('0px')
  	jQuery("#dropNavP2").mouseenter(function(){
		jQuery("#dropNavP2 a:first").removeClass("aro1b").addClass("aro2b")								
		jQuery("#dropNav2").clearQueue().stop().height(useH2)							 
	}).mouseleave(function(){
		jQuery("#dropNav2").clearQueue().stop().delay(200).animate({'height': 0}, 200, function(){
			jQuery("#dropNavP2 a:first").removeClass("aro2b").addClass("aro1b")																					   
		})					 
	})  
});





// mikes js ================================================

function AddToFavorites(){
	title = "Galaxy4Gamers | Xbox360, PS3 and Wii Online Video Game Tournaments for Cash Money";
	url = "http://www.galaxy4gamers.com";
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
}
function alerts(type){
	//if(type == 'Tournament Invites')document.getElementById('aInvites').style.display = 'block'
	//else document.getElementById('aInvites').style.display = 'none'
	if(type == 'Friend Requests')document.getElementById('aFriends').style.display = 'block'
	else document.getElementById('aFriends').style.display = 'none'
	if(type == 'Tournament Reminders')document.getElementById('aReminders').style.display = 'block'
	else document.getElementById('aReminders').style.display = 'none'
	if(type == 'Email Messages')document.getElementById('aMail').style.display = 'block'
	else document.getElementById('aMail').style.display = 'none'
	if(type == 'Friends')document.getElementById('aFriendsShow').style.display = 'block'
	else document.getElementById('aFriendsShow').style.display = 'none'
	
	document.getElementById('sign_in_back').style.height = '100%'
	document.getElementById('aTitle').innerHTML = type
}


function SignIn(){
	document.getElementById('sign_in_back').style.height = '100%'
}
function NotYet(){
	document.getElementById('sign_in_back').style.height = '0px'
}
function theClose(t){
	ajt = t
	document.getElementById('sign_in_back').style.height = '0px'
	document.getElementById("theQue").style.display = 'block'
	document.getElementById("theChat").style.display = 'none'
	document.getElementById("messageArrayCheck3").innerHTML = 'back'
	document.getElementById("chatFriend").innerHTML = "menu"
}
// tourney block sort ====================================
function Sort(x, c, o){
	if(!o) var o='output=user'
	jQuery("#tabhold").load( '/ajax/sort-tourneys.php?System='+x+'&coll='+c+'&limit=all&'+o);
}
function deleteMail(m){
	jQuery.ajax({
		type: 'GET',
		url: '/ajax/deleteMail.php',
		data: 'mail='+m,
		cache:false,
		beforeSend: function() {
			jQuery("#deleteMail"+m).addClass('delete');
		},
		success: function() {
			jQuery("#deleteMail"+m).remove()
		}
	});   
}
function deleteInvite(m){
	jQuery.ajax({
		type: 'GET',
		url: '/ajax/deleteInvite.php',
		data: 'invite='+m,
		cache:false,
		beforeSend: function() {
			jQuery("#deleteInvite"+m).addClass('delete');
		},
		success: function() {
			jQuery("#deleteInvite"+m).remove()
		}
	});   
}
function addFriend(f, a){
	if(a=="accept") var DATA =  'accept=1&friendCode='+f
	else var DATA = 'decline=1&friendCode='+f
	jQuery.ajax({
		type: 'GET',
		url: '/ajax/addFriend.php',
		data: DATA,
		cache:false,
		beforeSend: function() {
			if(a=="accept")jQuery("#friendReq"+f).addClass('accept');
			else jQuery("#friendReq"+f).addClass('delete');
		},
		success: function() {
			jQuery("#friendReq"+f).remove()
		}
	});   
}

function select_innerHTML(objeto,innerHTML){
	/******
	* select_innerHTML - corrige o bug do InnerHTML em selects no IE
	* Veja o problema em: http://support.microsoft.com/default.aspx?scid=kb;en-us;276228
	* Versão: 2.1 - 04/09/2007
	* Autor: Micox - Náiron José C. Guimarães - micoxjcg@yahoo.com.br
	* @objeto(tipo HTMLobject): o select a ser alterado
	* @innerHTML(tipo string): o novo valor do innerHTML
	*******/
	objeto.innerHTML = "";
	var selTemp = document.createElement("micoxselect");
	var opt;
	selTemp.id="micoxselect1";
	document.body.appendChild(selTemp);
    selTemp = document.getElementById("micoxselect1");
    selTemp.style.display="none";
    
    	if(innerHTML.toLowerCase().indexOf("<option")<0){
        innerHTML = "<option>" + innerHTML + "</option>";
    }
	
    innerHTML = innerHTML.replace(/<option/g,"<span").replace(/<\/option/g,"</span");
    selTemp.innerHTML = innerHTML;
    
    for(var i = 0; i < selTemp.childNodes.length; i++){
    	var spantemp = selTemp.childNodes[i];
        if(spantemp.tagName){
            opt = document.createElement("OPTION");
    
            if(document.all){
            	objeto.add(opt);
            }
            else {
            	objeto.appendChild(opt);
            }       
    
            //getting attributes
            for(var j = 0; j < spantemp.attributes.length; j++){
            	var attrName = spantemp.attributes[j].nodeName;
            	var attrVal = spantemp.attributes[j].nodeValue;
            	if(attrVal){
            		try {
            			opt.setAttribute(attrName,attrVal);
            			opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
            		}
            		catch(e){}
            	}
            }
            //getting styles
            if(spantemp.style){
            	for(var y in spantemp.style){
            		try {
            			opt.style[y] = spantemp.style[y];
            		}
            		catch(e){}
            	}
            }
            //value and text
            opt.value = spantemp.getAttribute("value");
            opt.text = spantemp.innerHTML;
            //IE
            opt.selected = spantemp.getAttribute('selected');
            opt.className = spantemp.className;
        } 
    }    
 
    document.body.removeChild(selTemp);
    selTemp = null;
}
