var leagueModal = null;
var contactCount = 0;
var linksCount = 0;

jQuery().ready(function(){
	

	
	
});

function loadLeaguePage(_url, _target){
	location.href = _url;
	/*
		$.ajax({
			type: 	'POST',
			url: 	_url,
			success: function(html){
				$(_target).html(html);
			}
		});
	*/
}

function changeDivision(_view, _lid, _did){
	var stabid = $("#tab_selected").val();
	if(stabid){
		location.href = '/leagues?'+_view+'&lid='+_lid+'&did='+_did+'&stabid='+stabid+'&CupComp=1';
}else{
	location.href = '/leagues?'+_view+'&lid='+_lid+'&did='+_did+'&CupComp=1';
}
	//loadLeaguePage('/leagues?'+_view+'&lid='+_lid+'&did='+_did+'&ajax=1&fbml=1', '#league_page_container');
}
function changeComp(_view,_lid,_sdiv,_comp){
	var stabid = $("#tab_selected").val();
	var season_params = $("#current_season").val();
	var showall = $("#showall").val();
	var allCompetitions = _lid.indexOf('#');
	var selection = "&sel=";
	if(!_sdiv || _comp < 1)
	{
		selection += "0";
	}else{
		selection += "1";
}

if(showall > 0){
    var xtra = "&showall=1";
}else{
    var xtra = "";
}
		//location.href = '/clubs?'+_view+'&tid='+_tid+'&sel=1&stabid='+stabid+season_params;
	location.href = '/leagues?'+_view+'&lid='+_lid+'&did='+_sdiv+'&comp='+_comp+selection+'&stabid='+stabid+season_params+xtra;
}
function changeSeason(url,single){
	var stabid = $("#tab_selected").val();	
	/*var month = $("#team_selector").attr("selectedIndex");*/
	location.href = url;
}


function Viewseason(url){
    
	location.href = url;
    
}

function ShowMatchesTab(tab)
{
	if(tab == 1)
	{

		$("#subnav_dropdown").hide();
		$("#sub_nav_tab_1").show();
		$("#sub_nav_header_1").attr('class','sub_nav_tab sub_nav_selected_tab');
		$("#sub_nav_tab_2").hide();
		$("#sub_nav_header_2").attr('class','sub_nav_tab sub_nav_item_bottom');
		$("#sub_nav_tab_3").hide();
		$("#sub_nav_header_3").attr('class','sub_nav_tab sub_nav_item_bottom');
		$("#tab_selected").val("1");
	}
	if(tab == 2)
	{
		$("#subnav_dropdown").show();
		$("#sub_nav_tab_1").hide();
		$("#sub_nav_header_1").attr('class','sub_nav_tab sub_nav_item_bottom');
		$("#sub_nav_tab_2").show();
		$("#sub_nav_header_2").attr('class','sub_nav_tab sub_nav_selected_tab');							
		$("#sub_nav_tab_3").hide();
		$("#sub_nav_header_3").attr('class','sub_nav_tab sub_nav_item_bottom');
		$("#tab_selected").val("2");
	}
	if(tab == 3)
	{
		$("#subnav_dropdown").show();							
		$("#sub_nav_tab_1").hide();
		$("#sub_nav_header_1").attr('class','sub_nav_tab sub_nav_item_bottom');
		$("#sub_nav_tab_2").hide();
		$("#sub_nav_header_2").attr('class','sub_nav_tab sub_nav_item_bottom');
		$("#sub_nav_tab_3").show();
		$("#sub_nav_header_3").attr('class','sub_nav_tab sub_nav_selected_tab');
		$("#tab_selected").val("3");
	}
}


function addLeagueDivisionToFav(_lid, _did, _data){
	addFavourite(_lid, FAVOURITE_TYPE_LEAGUE, addFavCallback, _data);
	addFavourite(_did, FAVOURITE_TYPE_DIVISION, null, null);
}

function removeLeagueDivisionFromFav(_lid, _did, _data){
	removeFavourite(_did, FAVOURITE_TYPE_DIVISION, delFavCallback, null);
	//removeFavourite(_lid, FAVOURITE_TYPE_LEAGUE, delFavCallback, _data);
}

function addFavCallback(_html, _data){
	var _elem = _data['element'];
	$(_elem).html("Added to Favourites");
	
	if (fbinfo){
		load_feed();
	}
	 
//	alert("Added to Favourites");
}

function delFavCallback(_html, _data){
	var _elem = _data['element'];
	$(_elem).html("Removed from Favourites");
	//alert("Removed from Favourites");
}

var mediaContainer = "#media_view_window";

function rotateMedia(_mid){
	$.ajax({
		type: 	'POST',
		url: 	'scripts.php?script=rotateMedia',
		data:	'mid='+_mid,
		success: function(html){
			$(mediaContainer).html(html);
		}
	});
}

function reload(_aid, _page, _url){
	loadLeaguePage(_url+"&pg="+_page, '#league_page_container');
 	//document.location.href=_url+"&pg="+_page;
}

/* remove news */

function removeNews(_id, _lid, _did) {

	/* do the modal stuff and wait for a response */
	$('#notify_content').html("Are you sure you want to remove this item of news?");
	modal_base.init("error", "Delete News", "#notification", "Confirm",
			removeNewsNow, arguments);
}

function removeNewsNow() {
	var _id 	= arguments[0][0];
	var _lid 	= arguments[0][1];
	var _did 	= arguments[0][2];
	location.href = "?league_news_item&action=delete&lid="+_lid+"&did="+_did+"&id="+_id;
}

/* use some ajax to get the content and populate the form */
function editNews(_id){
	$.ajax({
		type: 	"POST",
		url: 	"/scripts.php?script=get_news",
		data: 	"id="		+ _id ,
		success: function(html){
			
			$('#rss_content_form_container').show();
		
			var _obj = eval('('+html+')');
	//		console.log(_obj);
	
			$('#nid').val(_obj.gr_rss_content_id);
			$('#nimage').val(_obj.gr_rss_content_image);
			$('#ncontent').html(_obj.gr_rss_content_desc);
			$('#nlink').val(_obj.gr_rss_content_link);
			$('#nemail').val(_obj.gr_rss_content_contact);
			$('#ntitle').val(_obj.gr_rss_content_title);
			
			
		}
	});
}

function clearForm(){
	$('#nid').val("");
	$('#ntitle').val("");
	$('#nimage').val("");
	$('#ncontent').html("");
	$('#nlink').val("");
	$('#nemail').val("");
	
}


function featurenews(str){
    
var featurediv = "news_"+str;

document.getElementById(featurediv).innerHTML  = "<a class='feature_news_wait'>Please wait</a>";
   
    	$.ajax({
		type: 	"POST",
		url: 	"/scripts.php?script=get_news&action=feature",
		data: 	"id="		+ str ,
		success: function(html){
		  if(html == 1){
		      
		      document.getElementById(featurediv).innerHTML  =  "<a href=\"javascript:unfeaturenews("+str+");\" class=\"unfeature_news\">Unfeature</a>";
		      
              }
		  }
		  
	});

}

function unfeaturenews(str){
    
var featurediv = "news_"+str;


document.getElementById(featurediv).innerHTML  = "<a class='feature_news_wait'>Please wait</a>";
   
    	$.ajax({
		type: 	"POST",
		url: 	"/scripts.php?script=get_news&action=unfeature",
		data: 	"id="		+ str ,
		success: function(html){
		  if(html == 1){
		      document.getElementById(featurediv).innerHTML  =  "<a href=\"javascript:featurenews("+str+");\" class=\"feature_news\">Feature</a>";
		      
              }
		  }
		  
	});

}



function infoClone(){
	var _clone = $('#contact_clone').clone();
	_clone.attr("id", "null");
	_clone.css("display", "block");
	
	var _elements = _clone.find(".cloned").each(function(){
		$(this).attr("name", $(this).attr("name").replace(/ID/, contactCount));
	
	});
	contactCount++;
	_clone.appendTo('#clone_target');
}

function linksClone(){
	var _clone = $('#links_clone').clone();
	_clone.attr("id", "null");
	_clone.css("display", "block");
	
	var _elements = _clone.find(".cloned").each(function(){
		$(this).attr("name", $(this).attr("name").replace(/ID/, linksCount));
	
	});
	linksCount++;
	_clone.appendTo('#links_clone_target');
}

function removeFile(_id){
	location.href = location.href+"&remove=true&id="+_id;
}

