$(document).ready( function() {
	YouFoot.init();
	var fav = $('.testFav').html();
	if(fav == 13){
		$("#match-favorite").parent().remove();
		$("#player-favorite").parent().remove();
		$("#team-favorite").parent().remove();
		$("#competition-favorite").parent().remove();
		$("#venue-favorite").parent().remove();
	}
});

/**
 * Collapsing elements found throughout the site.
 */
YouFoot.collapsers = {
	init: function() {
		$('.collapser').each(function(){
			var $this = $(this);
			var content = $this.find('.content');
			$this.find('.title-bar a').click(function() {
				content.slideToggle();
				$this.toggleClass('collapsed');
				return false;
			});
			if (!$this.hasClass('heavy-bar')) {
				content.hide();
				$this.toggleClass('collapsed');
			}
			if ($this.hasClass('notcollapse')) {
				content.show();
				$this.toggleClass('collapsed');
			}
			if ($this.hasClass('additional-object-tags')){
				content.show();
			}
		});
	}
};
/**
	Collapser working for competition page
**/
YouFoot.collapsers2 = {
		init: function() {
			$('.collapser2').each(function(){
				var $this = $(this);
				var content = $this.find('.content2');
				var url = $(location).attr('href');
				var id = url.split('#');
				$this.find('.title-bar2 a').click(function() {
					content.slideToggle();
					$this.toggleClass('collapsed2');
					return false;
				});
				if ($this.hasClass('heavy-bar2')) {
                                    if(url.match('competitions/') && url.match('/edit')){
                                        $(".collapser2").removeClass('collapsed2');
                                        $(".content2").show();
                                    }
					if(url.match('#[0-9+$]|[A-Z+$]')){
						if(url.match('competitions/')){
							$("#"+id[1]).parent().addClass('notcollapse2');
							$("#"+id[1]).parent().removeClass('collapsed2');
						}else{
							$("#"+id[1]).parent().parent().parent().addClass('notcollapse2');
						}
					}
					content.hide();
					$this.toggleClass('collapsed2');
					if(url.match('players/edit') || url.match('teams/edit') || url.match('venues/edit')){
						$("#"+id[1]).parent().parent().parent().removeClass('collapsed2');
					}
				}
				if ($this.hasClass('notcollapse2')) {
					content.show();
					if(url.match('competitions/')){
						$this.toggleClass('collapsed2');
					}
				}
			});
		}
	};

$(document).ready(function(){

    $(".full_champ:last").find(".content2:first").show();
    $(".full_champ:last").removeClass('collapsed2');
    $(".full_champ:last").next().find('.content2:first').show();
    $(".full_champ:last").next().removeClass('collapsed2');

    $(".full_group").find(".content2").show();
    $(".full_group").removeClass('collapsed2');

    $(".full_nobrak:last").next().find(".content2:first").show();

	$(".search-categories .news").remove();
	$(".search_index_right .news").remove();
	
	//$("#other-title-bar").parent().show();

    //$("table").find('tr:last').mouseover(function(){
      //alert("succes");
   // });

 });
/** 
 * Behaviors attached to the navigation menu.
 */
YouFoot.navigationMenu = {
	menuFavoritesCollection : null,
	
	//attach events to all the favorites menus.
	init: function() {
		var self = this;
		var menuItemVisible = false;
		this.menuFavoriteCollection = $('.header-bottom div.favorites-menu');
		
		var handleClick = function (event) {
			event.stopPropagation();
			event.preventDefault();
			self.hideAll();
			var menuBlock = $(this).next('div.favorites-menu');
			menuBlock.show();
			menuItemVisible = true;
		}
		$('#profile_menu_img, .namefirstname').click(function(){
			if($('.bubble-profile').css('display') == 'block' ||  $('.bubble-profile').css('display') == 'inline' ){
				$('.bubble-profile').hide();
			}else{
				$('.bubble-profile').show();
			}
		});
		$("#header, .white-grass").live('mouseup',function(){
			$('.bubble-profile').hide();
		});
		
		$('.displayoptions').click(function(){
			if($('#match-timeline-visibility').css('display') == 'block' ||  $('#match-timeline-visibility').css('display') == 'inline' ){
				$('#match-timeline-visibility').hide();
			}else{
				$('#match-timeline-visibility').show();
			}
		});
		$('.header-bottom a.arrow').each(function() {
			var openButton = $(this);
			if (openButton.next('div.favorites-menu')) {
				openButton.click(handleClick);
			}
		});
		$('.more').click(function(){
			var openButton = $(this);
			if (openButton.next().next().next('div.favorites-menu')) {
				var handleClickMore = function (event) {
					event.stopPropagation();
					event.preventDefault();
					self.hideAll();
					var menuBlockMore = $(this).next().next().next('div.favorites-menu');
					menuBlockMore.show();
					menuItemVisible = true;
				}
				openButton.click(handleClickMore);
			}
		})

		$(document).click(function() {
			if (menuItemVisible) {
				self.hideAll();
			}
		});
		self.hideAll();
	},
	hideAll: function() {
		this.menuFavoriteCollection.hide();
	}
	
}

/**
 * Mostly Static collection of utility methods.
 */
YouFoot.utils = {
	init: function() {
		$(".jsHide").hide();
		$(".jsShow").show();
	},
//
// Change a named parameter's value in the `url`
// Will append the param if it doesn't exist in the URL
// and will swap the value if it does. 
// @return string.
	changeNamedParam: function (url, name, value) {
		var search = new RegExp(name + ':([^/]+)(/)?');
		var matches = url.match(search);
		if (matches) {
			var endSlash = (matches[2] === undefined) ? '' : matches[2];
			return url.replace(search, name + ':' + value + endSlash);
		} else {
			return url + '/' + name + ':' + value;
		}
	}
};

YouFoot.resultsFixtureUtils = {
	makeLegends: function (context) {
		context = context || document
		$('.show-legend', context).bind('click', function (event) {
			event.preventDefault();
			var target = $(this).attr('href');
			$(target).toggle();
		});
		$('.legend').hide();
	},

	toggleDetails: function () {
		$('.results-details .toggle-details').live('click', function (event) {
			event.preventDefault();
			var $this = $(this);
			$this.prev().toggle();
		});
		$('.results-details .results-details-contents').hide();
	}

};

/**
 * 
 */
YouFoot.loginSidebarValidation = {
	init: function() {
		if (!$('#SidebarLogin').length) {
			return;
		}
		$("#SidebarLogin").validate();
		$("#SidebarLoginEmail").rules("add", {
			minlength: 2
		});
	}
};

YouFoot.expandingText = {
	init: function () {
		$('a.expanding-more').bind('click', function (event) {
			$(this).prev().toggle();
			return false;
		});
	}
};

YouFoot.pagination = {
	init: function () {
		var form = $('#pagination-page-form');
		if (!form.length) {
			return;
		}
		form.bind('submit', this.submitPageChange.bind(this));
		form.find('input[type=text]')
			.bind('blur', this.blurPageChange.bind(this))
			.bind('focus', function (event) {
				$(this).val('');
			});
	},
	
	submitPageChange: function (event) {
		event.preventDefault();
		var pageValue = $(event.target).find('input[type=text]').val();
		this.changePage(pageValue);
	},
	
	blurPageChange: function (event) {
		var pageValue = $(event.target).val();
		this.changePage(pageValue);
	},
	
	changePage: function (pageNumber) {
		var location = window.location.href;
		if (!location.match(/find/)) {
			location += '/find';
		}
		var newUrl = YouFoot.utils.changeNamedParam(location, 'page', pageNumber);
		window.location = newUrl; 
	}
};

/*
* Add to Favorite without reloading the whole page
* by Djamy
*/

	function addToFav(id,page,elem){
	    if (page == 1){
	        url = "/favorites/add/match/"+id;
	    }else if (page == 2){
	        url = "/favorites/add/player/"+id;
	    }else if (page == 3){
	        url = "/favorites/add/team/"+id;
	    }else if (page == 4){
	        url = "/favorites/add/competition/"+id;
	    }else if (page == 5){
	        url = "/favorites/add/venue/"+id;  
	    }
	    $.ajax( {
	        type: "GET",
	   beforeSend: function(){
	        $("#match-favorite").removeAttr('onClick');
	        $("#player-favorite").removeAttr('onClick');
	        $("#team-favorite").removeAttr('onClick');
	        $("#competition-favorite").removeAttr('onClick');
	        $("#venue-favorite").removeAttr('onClick');
	        $(elem).removeAttr('onClick');
	    },
	    url: url,
	    success: function(){
	        $('#match-favorite').hide();
	        $('#player-favorite').hide();
	        $('#team-favorite').hide();
	        $('#competition-favorite').hide();
	        $('#venue-favorite').hide();
	        $(elem).append(YouFoot.alert(YouFoot.i18n.get('Added in your favorite !')));
	        $(elem).empty();
	        
	        if (page == 1){
	        	$(elem).append(YouFoot.i18n.get('Favorited match'));
		    }else if (page == 2){
		    	$(elem).append(YouFoot.i18n.get('Favorited player'));
		    }else if (page == 3){
		    	$(elem).append(YouFoot.i18n.get('Favorited team'));
		    }else if (page == 4){
		    	$(elem).append(YouFoot.i18n.get('Favorited competition'));
		    }else if (page == 5){
		    	$(elem).append(YouFoot.i18n.get('Favorited venue'));
		    }
	        $(elem).removeClass();
	    }
	    });
	}
