var CP={
	init:function(){
		$("body").addClass("js");
		CP.Features.init();
		CP.Util.equalize("#meta>div");
	},
	Features:{
		init:function(){
			Tabs.init($(".panels"));
		}
	},
	Util:{
		equalize:function(selector){
			var maxHeight=1;
			$(selector).each(function(){
				if (this.offsetHeight>maxHeight) maxHeight=this.offsetHeight;
			});
			$(selector).css("height",maxHeight+"px");
		}
	}
}

var Tabs={
	container:null,
	panels:Array(),
	init:function(container){
		Tabs.container=$(container);
		$('.panel',Tabs.container).each(
			function(i){
				var newPanel={};
				newPanel.element=this;
				newPanel.id=this.id;
				newPanel.index=i;
				newPanel.title=$('h2',this)[0];
				newPanel.tab=Tabs.createTab(newPanel);
				Tabs.panels[i]=newPanel;
			}
		)
		Tabs.createTabsBar();
		Tabs.showPanel(Tabs.panels[0]);
	},
	createTab:function(panel){
		var tab=document.createElement("li");
		tab.className="inactive";
		var tabLink=document.createElement("a");
		tabLink.innerHTML=panel.title.innerHTML;
		tabLink.href="#"+panel.id;
		$(tabLink).click(function() {Tabs.showPanel(panel); return false;})
		tab.appendChild(tabLink);
		return tab;
	},
	createTabsBar:function(){
		var tabsParent=$(".tab-container")[0];
		var ul=document.createElement("ul");
		$(Tabs.panels).each(function(){ ul.appendChild(this.tab); });
		var tabBar=document.createElement("div");
		tabBar.className="tabs";
		tabBar.appendChild(ul);
		tabsParent.appendChild(tabBar);

		next=document.createElement("a");
		next.className="next";
		next.innerHTML="&raquo;";
		next.href="#";
		$(next).click(function() {Tabs.showNextPanel(); return false;})

		previous=document.createElement("a");
		previous.className="previous";
		previous.innerHTML="&laquo;";
		previous.href="#";
		$(previous).click(function() {Tabs.showPreviousPanel(); return false;})

	},
	showNextPanel:function(){
		if (Tabs.activePanelIndex<(Tabs.panels.length-1)){
			nextIndex=Tabs.activePanelIndex+1;
		}else{
			nextIndex=0;
		}
		Tabs.showPanel(Tabs.panels[nextIndex]);
	},
	showPreviousPanel:function(){
		if (Tabs.activePanelIndex>(0)){
			nextIndex=Tabs.activePanelIndex-1;
		}else{
			nextIndex=Tabs.panels.length-1;
		}
		Tabs.showPanel(Tabs.panels[nextIndex]);
	},
	showPanel:function(panel){
		var l = ($(".tab-container")[0].clientWidth)*panel.index;		
		Tabs.container.animate({left:"-"+l+"px"},"slow","easeboth");
		Tabs.activeTab(panel.tab);
		Tabs.activePanelIndex=panel.index;
	},
	activeTab:function(tab){
		$("#features li.active").each(function(){$(this).removeClass("active");$(this).addClass("inactive")});
		tab.className="active";
		$(tab).DropInUp(250);
	}
}

/** 
 * Loads in a URL into a specified divName, and applies the function to 
 * all the links inside the pagination div of that page (to preserve the ajax-request) 
 * @param string href The URL of the page to load 
 * @param string divName The name of the DOM-element to load the data into 
 * @return boolean False To prevent the links from doing anything on their own. 
 */ 
function loadPiece(href,divName) {     
    $(divName).load(href, {}, function(){ 
        var divPaginationLinks = divName+" #pagination a"; 
        $(divPaginationLinks).click(function() {      
            var thisHref = $(this).attr("href"); 
            loadPiece(thisHref,divName); 
            return false; 
        });
    }); 
}

function acceptTeam(tournamentteam_id) {
	$("#tournament_teams").load('/tournaments/accept_team/'+tournamentteam_id);
	return false;
}

function pendTeam(tournamentteam_id) {
	$("#tournament_teams").load('/tournaments/move_to_pending/'+tournamentteam_id);
	return false;
}
function declineTeam(tournamentteam_id) {
	$("#tournament_teams").load('/tournaments/remove_team/'+tournamentteam_id);
	return false;
}
function doSearch(string) {
	$("#searchResults").load('/search/doSearch/'+string);
}
function checkUser(string,status) {
	document.getElementById("team_id").value = jQuery.get('/users/exists/'+string).data;
}

$(document).ready(function(){
	
	$('a.facebox').facebox();
	
	if(window.location.pathname == "/" || window.location.pathname == "/")
		loadPiece("/news/paginator_list","#newsList");
	
	if(window.location.pathname == "/media/all" || window.location.pathname == "/media/all/")
		loadPiece("/media/paginator_list","#mediaList");

	if(window.location.pathname == "/galleries" || window.location.pathname == "/galleries/")
		loadPiece("/galleries/paginator_list","#galleryList");
		
	if(window.location.pathname == "/teams" || window.location.pathname == "/teams/")
		loadPiece("/teams/paginator_list","#teamsList");
	
	if(window.location.pathname == "/iol/tournaments" || window.location.pathname == "/tournaments" || window.location.pathname == "/tournaments/")
		loadPiece("/tournaments/paginator_list","#oldTournaments");
	
	if(window.location.pathname.substr(0,11) == "/news/view/" || window.location.pathname.substr(0,12) == "/news/view/") {
		news_id = window.location.pathname.split("/");
		news_id = news_id[news_id.length-1];
		loadPiece("/comments/comments_list/"+news_id,"#commentsList");
	}
    
	if(window.location.pathname.substr(0,16) == "/galleries/view/" || window.location.pathname.substr(0,16) == "/galleries/view/") {
		gallery_id = window.location.pathname.split("/");
		gallery_id = gallery_id[gallery_id.length-1];
		loadPiece("/images/images_list/"+gallery_id,"#imagesList");
	}
	
	$("#user_autocomplete").keyup(function() {
		checkUser(this.value);
	});

	if(window.location.pathname == "/search" || window.location.pathname == "/iol/search") {
		$("#search").keyup(function() {
			if(this.value.length >= 2) {
				document.getElementById("s_message").innerHTML = "";
				doSearch(this.value.replace("%",""));
			} else {
				document.getElementById("s_message").innerHTML = "You need to at least specify minimum two characters for a result";
			}
		});
	}

	if(window.location.pathname.substr(0,17) == "/tournaments/view" || window.location.pathname.substr(0,18) == "/tournaments/view/") {
		CP.init();
	}
});