/*
 * NEWSFEED JABBER CLIENT	
 * Version: 0.1a
 * Strophe Library
 * Author : Amine MOKEDDEM
 * "May the Force be with you"
 * 
 */

function bindMouseOverPublishButton(){
	$('.post-right').mouseover(function(){
		$('.post-top').addClass('post-topOver').removeClass('post-top');
		$('.publish-bottom').addClass('publish-bottomOver').removeClass('publish-bottom');
		$('.post-center').addClass('post-centerOver').removeClass('post-center');
		$('.publish-ball').addClass('publish-ballOver').removeClass('publish-ball');
		$('.publish-button').addClass('publish-buttonOver').removeClass('publish-button');
	});
	$('.post-right').mouseout(function(){
		$('.post-topOver').addClass('post-top').removeClass('post-topOver');
		$('.publish-bottomOver').addClass('publish-bottom').removeClass('publish-bottomOver');
		$('.post-centerOver').addClass('post-center').removeClass('post-centerOver');
		$('.publish-ballOver').addClass('publish-ball').removeClass('publish-ballOver');
		$('.publish-buttonOver').addClass('publish-button').removeClass('publish-buttonOver');
	});
}
function initClips(){
	$(".clip").toggle(function(){
			var background = $("#"+this.id).css("background-position");
			var type = this.id.substring(0,this.id.length-5);
			if(type == 'players'){
				type = 'friends';
			}
			$("#" + this.id).attr({
				"old-back": background
			});
			$("#" + this.id).removeClass().addClass("clipOff").css("background-position", "0px -645px");
			$("#" + this.id).css("background-repeat", "repeat-x");
			$("#" + this.id).css("font-size", "8pt");
			$(".marker-"+type).hide();
		},
		function(){
			var background = $("#" + this.id).attr("old-back");
			var type = this.id.substring(0,this.id.length-5);
			if(type == 'players'){
				type = 'friends';
			}
			$("#" + this.id).removeClass().addClass("clip").css("background-position", background);
			$("#" + this.id).css("font-size",null);
			$(".marker-"+type).show();
		}
	);
}

function pop_login(){
    if ($.cookie("userName-userID") == null) {
		$("#loginContent").modal();
		$("a.modalCloseImg").remove();
		$("#modalContainer").css({
			"width": "300px",
			"margin-left": "-30px",
			"margin-top": "220px",
			"height": "100px"
		});
		$('#loginSubmit').click(function(){
			var username = $('#loginField').val();
			newsFeedLogin(username);
		});
		$("#loginField").keypress(function(e){
			if (e.which == 13) {
				var username = $('#loginField').val();
				newsFeedLogin(username);
			}
		})
	}
}
function close_pop_login(){
	 $.modal.close();
}

function initFriendsAcceptence(){
	
	$('.links-bar > span:contains("Accept")').click(function(){
		
		var href = $(this).parent().parent().children('a:first').attr('href');
		var userId= href.split('/')[3];
		var requestURL=$(this).attr('href');
		var userName=$(this).parent().parent().children('.title-bar').children('span:first').text();
		YouFoot.alert('Friend accepted', 'Please wait until reload...');
		becomeFriendWith(userId,1,requestURL,userName);
		return false;
	});
	$('div.sidebox > ul > li > span.accept-friend').click(function(){
		var href = $(this).attr('href');
		var userId= href.split('/')[3];
		var requestURL=href;
		var userName=$('.info-head > h2').text();
		YouFoot.alert('Friend accepted', 'Please wait until reload...');
		becomeFriendWith(userId,1,requestURL,userName);
		return false;
	});
}

function bindSubmitButton(model,NoRedirect){

	switch(model){
		case 'teams':
			$("#AddTeam").click(function(){
				$(this).unbind('click');
				submitForm(model,'TeamAddForm','team',NoRedirect);
				YouFoot.alert('Creating...', 'Please wait until reload...');
			});
			break;
		case 'competition_names':
			$("#AddCompetition").click(function(){
				$(this).unbind('click');
				submitForm(model,'CompetitionNameAddForm','competition',NoRedirect);
				YouFoot.alert('Creating...', 'Please wait until reload...');
			});
			break;
		case 'Competition_names':
			$("#AddCompetition").click(function(){
				$(this).unbind('click');
				submitForm(model,'CompetitionNameAddForm','competition',NoRedirect);
				YouFoot.alert('Creating...', 'Please wait until reload...');
			});
			break;
		case 'players':
			$("#AddPlayer").click(function(){
				$(this).unbind('click');
				submitForm(model,'PlayerAddForm','player',NoRedirect);
				YouFoot.alert('Creating...', 'Please wait until reload...');
			});
			break;
		case 'matches':
			$("#AddMatch").click(function(){
				if ($(this).val()=="create1stLeg") {
					$("#MatchAddForm")[0].submit();
				}else {
					$(this).unbind('click');
					submitForm(model, 'MatchAddForm', 'match',NoRedirect);
					YouFoot.alert('Creating...', 'Please wait until reload...');
				}
			});
			break;
		case 'venues':
		$("#AddVenue").click(function(){
			$(this).unbind('click');
			submitForm(model, 'VenueAddForm', 'venue',NoRedirect);
			YouFoot.alert('Creating...', 'Please wait until reload...');
		});
		break;
	}
	
}

function submitForm(model,form,singular,noRedirect){
	$('#'+form).ajaxSubmit(function(data){
		if (data != 'false') {
			var Objectid = data.split(';')[0];
			var name = data.split(';')[1];
			var options = {
				model: model,
				singular: singular,
				id: data.split(';')[0],
				name: data.split(';')[1],
				subscribe:$('.isFavorite').is(':checked'),
				noRedirect:noRedirect
			};
			//alert(options.id);
			createPageNodes(model+'-'+Objectid,null,options);
			//publishContribution(options);
		}else{
			$('#alert').jqmHide();
			YouFoot.error('The '+singular+' could not be saved.',' Please, try again.');
		}
	})
}

var favoriteOptions = null;
function bindFavoritesButtons(){
	$('#venue-favorite').click(function(){
		var model = $(this).attr('id').split('-')[0];
		var options= {
			id:$.url.segment(2),
			link:$.url.attr('relative'),
			add_url:$(this).attr('href'),
			page:model+'-'+$.url.segment(2),
			singular:model,
			name:getPageName(model)
		};
		favoriteOptions=options;
		//alert(options.name);
		putInFavorite(options.page);
		$(this).attr('href','javascript:void(0);return false;');
	});
	$('#match-favorite').click(function(){
		var model = $(this).attr('id').split('-')[0];
		var options= {
			id:$.url.segment(2),
			link:$.url.attr('relative'),
			add_url:$(this).attr('href'),
			page:model+'-'+$.url.segment(2),
			singular:model,
			name:getPageName(model)
		};
		favoriteOptions=options;
		//alert(options.name);
		putInFavorite(options.page);
		$(this).attr('href','javascript:void(0);return false;');
	});
	$('#competition-favorite').click(function(){
		var model = $(this).attr('id').split('-')[0];
		var options= {
			id:$.url.segment(2),
			link:$.url.attr('relative'),
			add_url:$(this).attr('href'),
			page:model+'-'+$.url.segment(2),
			singular:model,
			name:getPageName(model)
		};
		favoriteOptions=options;
		//alert(options.name);
		putInFavorite(options.page);
		$(this).attr('href','javascript:void(0);return false;');
	});
	$('#player-favorite').click(function(){
		var model = $(this).attr('id').split('-')[0];
		var options= {
			id:$.url.segment(2),
			link:$.url.attr('relative'),
			add_url:$(this).attr('href'),
			page:model+'-'+$.url.segment(2),
			singular:model,
			name:getPageName(model)
		};
		favoriteOptions=options;
		//alert(options.name);
		putInFavorite(options.page);
		$(this).attr('href','javascript:void(0);return false;');
	});
	$('#team-favorite').click(function(){
		var model = $(this).attr('id').split('-')[0];
		var options= {
			id:$.url.segment(2),
			link:$.url.attr('relative'),
			add_url:$(this).attr('href'),
			page:model+'-'+$.url.segment(2),
			singular:model,
			name:getPageName(model)
		};
		favoriteOptions=options;
		//alert(options.name);
		putInFavorite(options.page);
		$(this).attr('href','javascript:void(0);return false;');
	});
}

function bindUnfavoriteButtons(){
	$('.favorite-list .remove').each(function(){
		var options = {
			reload_url : $(this).attr('href')
		};
		$(this).click(function(){
			alert(options.reload_url);
		});
		$(this).attr('href','javascript:void(0);return false;');
	});
}

function getPageName(model){
	switch(model){
		case 'team':
			return $('.team-title:first').text();
			break;
		case 'player':
			return $('.team-title:first').text();
			break;
		case 'match':
			return $('.home-team > h3').text()+ ' vs ' +$('.away-team > h3').text();
			break;
		case 'competition':
			return $('.title:first').text();
			break;
		case 'venue':
			return $('.info-title:first').text();
			break;
	}
	
}
