var contactCount = 0;
var FAVOURITE_TYPE_MEDIA		= 1;
var FAVOURITE_TYPE_CONTENT		= 2;
var FAVOURITE_TYPE_BLOG			= 3;
var FAVOURITE_TYPE_LEAGUE		= 4;
var FAVOURITE_TYPE_CLUB			= 5;
var FAVOURITE_TYPE_PLAYER		= 6;
var FAVOURITE_TYPE_USER			= 7;
var FAVOURITE_TYPE_TEAM			= 8;
var	FAVOURITE_TYPE_DIVISION		= 9;

function loadClubPage(_url, _target){
	
	location.href = _url;
	/*
		$.ajax({
			type: 	'POST',
			url: 	_url,
			success: function(html){
				$(_target).html(html);
			}
		});
	*/
}


function Viewseason(url){
    
	location.href = url;
    
}

function changeTeam(_club,_view, _tid){
	var stabid = $("#tab_selected").val();
	
	if(_tid == "#")
		return;
	if(stabid)
		location.href = '/clubs?'+_view+'&cid='+_club+'&tid='+_tid+'&stabid='+stabid;
	else
		location.href = '/clubs?'+_view+'&cid='+_club+'&tid='+_tid;
	//loadClubPage('/clubs?'+_view+'&cid='+_cid+'&mcid='+_mcid+'&ajax=1&fbml=1', '#club_page_container');
}
function changeComp(_view, _tid,_sdiv){
	var stabid = $("#tab_selected").val();
	var season_params = $("#current_season").val();
	var showall = $("#showall").val();
	var allCompetitions = _tid.indexOf('#');
	var selection = "&sel=";
	if(allCompetitions > 0)
	{
		_tid = _tid.substring(0,allCompetitions);
		selection += "0";
		//location.href = '/clubs?'+_view+'&tid='+_tid+'&sel=0&stabid='+stabid+season_params;
	}
	else
		selection += "1";
	
	if(showall == 1)
		selection += "&showall=" + showall;
		//location.href = '/clubs?'+_view+'&tid='+_tid+'&sel=1&stabid='+stabid+season_params;
	location.href = '/clubs?'+_view+'&tid='+_tid+selection+'&stabid='+stabid+season_params;
}

function changeGsmComp(_view, _lid,_sdiv){
	
	var stabid = $("#tab_selected").val();
	var season_params = $("#current_season").val();
	var showall = $("#showall").val();
	
    var allCompetitions = _lid.indexOf('#');
	
    var selection = "&sel=";
	if(allCompetitions > 0)
	{
		_lid = _lid.substring(0,allCompetitions);
		selection += "0";
        if(showall == 1)
		  selection += "&showall=" + showall;
        location.href = '/clubs?'+_view+'&tid='+_lid+selection+'&stabid='+stabid+season_params;
        return;
        
	}
	else
		selection += "1";
	
	if(showall == 1)
		selection += "&showall=" + showall;
		//location.href = '/clubs?'+_view+'&tid='+_tid+'&sel=1&stabid='+stabid+season_params;
	location.href = '/clubs?'+_view+'&tid='+_sdiv+'&lid='+_lid+selection+'&stabid='+stabid+season_params;
}

function changeSeason(url,single){
    var stabid = $("#tab_selected").val();	
	location.href = url+'&stabid='+stabid+'&sel='+single;
	//loadClubPage('/clubs?'+_view+'&cid='+_cid+'&mcid='+_mcid+'&ajax=1&fbml=1', '#club_page_container');
}
function ShowMatchesTab(tab)
{
    //Disabled for now(SEO)
    return;
	if(tab == 1)
	{
		$("#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");
		$("#fixtures_dropdown").hide();
		$("#seasons_dropdown").hide();
		$("#subnav_league_name").show();
	}
	if(tab == 2)
	{
		$("#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");
		$("#fixtures_dropdown").show();
		$("#seasons_dropdown").show();
		$("#subnav_league_name").hide();
	}
	if(tab == 3)
	{							
		$("#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");
		$("#fixtures_dropdown").show();
		$("#seasons_dropdown").show();
		$("#subnav_league_name").hide();
	}
}

function addClubTeamToFav(_cid, _tid, _data){
	/* do the modal stuff and wait for a response */
	//$('#notify_content').html("Click 'Confirm' to add this team to your 'I Support' section. <br/><br/> You may support as many teams as you like and Goalrun will automatically supply all relevant news and information to your personal profile page.");
	$('#notify_content').html("<div class='modalInfoText'>Com'on!!<br/><br/>All relevant news and great footy content about this team will now be shown on your personal Goalrun Profile, and in your 'I Follow' section. <br/> <br/>There is no limit to the number of teams you can follow. (Why not keep an eye on your biggest rivals by 'following' them too?).<br/> <br/> Thank you!</div>");
    modal_base.init("error", "Follow this team!", "#notification", "Confirm", addTeamNow, arguments );
}

function addTeamNow(){
	var _cid = arguments[0][0];
	var _tid = arguments[0][1];
	var _data = arguments[0][2];
	addFavourite(_cid, FAVOURITE_TYPE_CLUB, null, null);
	addFavourite(_tid, FAVOURITE_TYPE_TEAM, addFavCallback, _data);
	
}

function addClubTeamToFavURL(_cid, _tid, _data){
	/* do the modal stuff and wait for a response */
	$('#notify_content').html("You can add as many teams to your 'I Follow' section as you like. We realise that everyone has a second team, or perhaps a club who are a local rival who you'd like to keep an eye on, so we've made it possible for you to add as many teams as you like.<br/><br/> If you only have one favourite team, that teams's club page will be shown, but if you have more than one, the 'I Follow' homepage will display all your favourites.");
	modal_base.init("error", "Add this team to my 'I Follow' section ", "#notification", "Confirm", addTeamNowURL, arguments );
}

function addTeamNowURL(){
	var _cid = arguments[0][0];
	var _tid = arguments[0][1];
	var _data = arguments[0][2];
	addFavourite(_cid, FAVOURITE_TYPE_CLUB, null, null);
	addFavourite(_tid, FAVOURITE_TYPE_TEAM, addFavCallback, _data);
	
}


function removeClubTeamFromFav(_cid, _tid, _data){
	//$('#notify_content').html("Are you sure you want to remove this team from your favourites? You can add it back again later, but any content you have contributed to the club profile may be lost.");
    $('#notify_content').html("Are you sure you wish to stop following this team? You can add it back at any time, but any content you have contributed to this team may be removed.<br/><br/>");
	arguments[0][0] = _cid;
    arguments[0][1] = _tid;
    arguments[0][2] = _data;
	
    modal_base.init("error", "Remove this team from my 'I Follow' section", "#notification", "Confirm", removeTeamNow, arguments );
	
}

/*Problem with a club? Here's the facebox presented to the user*/
function clubProblem(prb){
	$('#notify_content').html($('#club_problem_modal').html());
	modal_base.init("error", "There is an issue with this club", "#notification", "Send", sendClubProblem, arguments);
	
    $('#facebox').find('#club_problem').val(prb);
} 
function validateClubProblemFields()
{
	var _problem 		= $('#facebox').find('#club_problem').val();
	 var _club_problem_email	= $('#facebox').find('#club_problem_email').attr("value");
    
   // alert(_club_problem_email);
     
	if (_problem == ""){
			alert("Please let us know what the problem may be!");
			return false;
	}
	if (_club_problem_email.length < 1){
			alert("Please enter your email");
			return false;
	}
	if(!validEmail(_club_problem_email))
	{
		alert('Please provide a valid email address');
		return false;
	}
	
	return true;
}

function setValue(element,email)
{
	var leave_text = $("#remove_club_problem_email").val();
	
	if(email.length < 1 && leave_text == 0)
	{
		$(element).html("");	
		$("#remove_club_problem_email").val("1");
	}
}
/*Once the user has submitted their problem....*/
function sendClubProblem(){

		//getting required info on user uid and club cid 
		var _problem 		= $('#facebox').find('#club_problem').val();
		var _gr_club_id 	= $('#gr_club_id').val();
		var _gr_club_name 	= $('#gr_club_name').val();
		var _club_problem_email	= $('#facebox').find('#club_problem_email').attr("value");
		var _url 			= escape(location.href);
		
		//_problem = "User Submitted problem:\n\n"+_problem+"\n\nUser ID is: "+_uid+"\n\nClub ID is: "+_cid+"\n\nURL of user: "+_url+"\n\n";
		if (!validateClubProblemFields()) {
			setTimeout("clubProblem('" + _problem + "')",500);
			return false; 
		
		} else {
			$.ajax({
				type: 	'POST',
				url: 	'scripts.php?script=sendClubProblem',
				data:	'problem='+_problem+'&gr_club_id='+_gr_club_id+'&gr_club_name='+_gr_club_name+"&club_problem_email="+_club_problem_email,
				success: function(html){
					$('#notify_content').html("Thanks for your comments. We&lsquo;ll do our best to check your correction/suggestion and make the appropriate changes as quickly as we can.");
					modal_base.init("error", "Notification", "#notification", "OK");
				}
			});
		}
	}
/*End of wrong club facebox*/


function removeTeamNow(){
    var _cid 	= arguments[0][0];
	var _tid 	= arguments[0][1];
	var _data 	= arguments[0][2];
	removeFavourite(_tid, FAVOURITE_TYPE_TEAM, delFavCallback, _data);
}

function addFavCallback(_html, _data){
    $('.controlPanelButton').css("display","none");
	//location.href = location.href+"&fav=true";

}


function delFavCallback(_html, _data){
	var _url = location.href.replace(/&fav=[a-z]+/,"");
	location.href = _url;
}

function addPlayerToFav(_pid, _data){
	addFavourite(_pid, FAVOURITE_TYPE_PLAYER, addFavCallback, _data);
};

function removePlayerFromFav(_pid, _data){
	removeFavourite(_pid, FAVOURITE_TYPE_PLAYER, delFavCallback, _data);
};

function showModeratorModal(_club_id){
	$('#notify_content').html($('#club_mod_modal').html());
	modal_base.init("error", "Club Moderation", "#notification", "Accept", becomeMod, _club_id);
};

function showActivateModal(_){
	$('#notify_content').html($('#pre_act').html());
	modal_base.init("error", "Activation Required", "#notification", "OK");
};

function becomeMod(_id){
	$.ajax({
		type: 	'POST',
		url: 	'scripts.php?script=clubMod',
		data:	'cid='+_id,
		success: function(html){
			setTimeout(becameMod, 500);
		}
	});
}

function becameMod(){
	$('#notify_content').html("You are now a club moderator");
	modal_base.init("error", "Congratulations", "#notification", "OK", delFavCallback);
};

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){
	loadClubPage(_url+"&pg="+_page, '#club_page_container');
};

/* remove news */

function removeNews(_id, _tid) {

	/* 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 _tid = arguments[0][1];
	location.href = "?club_news&action=delete&id="+_id+"&tid="+_tid;
};

/* 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);
			$('#ntitle').val(_obj.gr_rss_content_title);
			$('#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);
			
			
		}
	});
};

function clearForm(){
	$('#nid').val("");
	$('#ntitle').val("");
	$('#nimage').val("");
	$('#ncontent').html("");
	$('#nlink').val("");
	$('#nemail').val("");
	
};


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 flickrClone(){
	var _clone = $('#flickr_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_flickr');
};





