/*
 * Author : Cvetozar Ninov
 * Email  : cninov@gmail.com
 * */
var form_params = {};
var rotate_by = 5000; //miliseconds
var index_news = 0;
var index_leagues_to_show=0;
var popup_content = "";
var stop_image_rotation = 0;
var t=0;

function add_flash_message(message)
{
	 $('html,body').animate({scrollTop: 0}, 500, function(){
		 $('#flash_messages').remove();
		 var flash_message = $('<div style="display: none;" id="flash_messages"><ul><li class="close">Hide</li><li>'+message+'</li></ul></div>');

		 $('#content').before(flash_message);
		 $(flash_message).show('normal');

		$('#flash_messages li.close').click( function() {
			$(this).parent().parent().hide('normal',function(){});
		});
	 });
}

$(document).ready(function() 
{
	$(".description").click(function(){
		url = $('.latest_news_items a:eq('+index_news+')').attr("href");
		window.location = url;
	});

		$('#flash_messages').each(function(){
			$('html,body').animate({scrollTop: 0}, 0, function(){});
			$(this).css('display', 'none');
			$(this).show('slow');
			$('.close', $(this)).click(function(){ $('#flash_messages').fadeOut(function(){ $(this).remove();}); });
		});
		
		$('.latest_news_items a').live("mouseover",function(event){
			index_news = $('.latest_news_items a').index(this);
			$('.news_accent').css('display','none');
			$('.news_accent:eq('+index_news+')').css('display','block');
		});
		
		$('#news_accent_caroucel').bind('mouseover',function(event){
			stop_image_rotation = 1;
		}).bind('mouseleave',function(event){
			stop_image_rotation = 0;
			__triger_mouse_enter();
		});
		
		__triger_mouse_enter();
			

	$("#date a#login , #date a#logout").click(function(){
		if(this.id=="login")
		{
			$.get(_root+'xhr?method=popup_form',{},function(data){
				if(data) {
					$(data).appendTo($('body'));
					$(data).mask();	
					popup_content = data;		
					
					$.each(form_params, function(i,item){
					   if(item.name != "password" && item.name !="password_confirm" 
					   	   && item.name!='email_confirm')
			           $(".register input[name='"+item.name+"']").val(item.value);
			         });
				}
			});
		}
		else if(this.id=="logout") 
		{
			$.get(_root+'xhr?method=user_logout',{},function(data){
			add_flash_message(data);
			$("#date a#logout").html("Login");
			$("#date a#logout").attr("id","login");
			$("form.game_matches p").html("Login to see your points.");
			$("form.game_matches .correct, form.game_matches .incorrect").removeClass("correct").removeClass("incorrect");
			$("form.game_matches div em").removeClass("selected");
			$("#edit_profile").remove();
			
		});
		}
	});
	
	$("a#login_button").live("click",function(){
		var username = $('#username',$(this).parent()).val();
		var password = $('#password',$(this).parent()).val();
		$.get(_root+'xhr?method=user_login',{username:username,password:password},function(data){
			
			if(data.status == 1) {
				hide_mask();
				add_flash_message(data.message);
				$("#date a#login").html("Logout "+username);
				$("#date a#login").attr("id","logout");
				$("#date a#logout").after('<a id="edit_profile" href="'+_site_url+'profile/"> ( Edit )</a>')
				
				if(data.in_game==1) 
				{	
					is_game_disabled = data.game_info.game_disabled;
					if(is_game_disabled) {
						window.location.reload();		
					}
				
					
					if(is_game_disabled) {
						$("form.game_matches p").html("Points : "+data.game_info.week_points+" (This week) / "+data.game_info.total_points+" (Total)");
					}
					
				}
			}
			else {
				$("#popup_forms p").remove();
				$("form.login").after("<p>"+data.message+"</p>");
				setTimeout(function(){
					$("#popup_forms p").fadeTo("slow",0,function(){$("#popup_forms p").remove();});
				},4000);
			}
			
			
		},'json');
	});
	
	$(".register a.submit").live("click",function(){
		form_params = $(this).parents("form").serializeArray();
		$.post(_root+'xhr?method=user_register',form_params,function(data){
			
			if(data["message"]) {
				
				if(data.status == 1) {
					form_params = {};
					hide_mask();
					add_flash_message(data["message"].join("</li><li>"));
				}
				else {
					$("#popup_forms p").remove();
					$("form.login").after("<p>"+data["message"].join("</p><p>")+"</p>");
					setTimeout(function(){
						$("#popup_forms p").fadeTo("slow",0,function(){$("#popup_forms p").remove();});
					},4000);
					
				}
			}
		},'json');
		return false;
	});
	
	$(".retrieve_password a.submit").live("click",function(){
		form_params = $(this).parents("form").serializeArray();
		$.get(_root+'xhr?method=user_lost_password',form_params,function(data){
			if(data["message"]) {
				if(data.status == 1) {
					form_params = {};
					add_flash_message(data["message"]);
					hide_mask();
				}
				else {
					$("form.login + p").remove();
					$("form.login").after("<p>"+data.message+"</p>");
					setTimeout(function(){
						$("form.login + p").fadeTo("slow",0);
					},4000);
				}
			}
		},'json');
		return false;
	});
	
	$("#lost_password").live("click",function(){
		$("form.register").fadeTo("slow",0,function(){
			$(this).remove();
			$("#lost_password").html("Create new account");
			$("#lost_password").attr("id","create_account")
			$(".retrieve_password").remove();
			$("h3.new_account").html("Retrieve lost password");
			$("h3.new_account").after('<form method="post" action="" class="retrieve_password"><div class="input"><label>Email :</label><input type="text" name="email"/></div><div class="input"><label>Username :</label><input type="text" name="username"/></div><a href="javascript:;" class="submit">Retrieve</a><a onclick="hide_mask()" href="javascript:;">cancel</a></form>');

		})
	});
	
	$("#create_account").live("click",function(){	
		$("#popup_forms").remove();
		$(popup_content).appendTo($('body'));
		$(popup_content).mask();	
	});
	
	//Refresh those pages at some interval
	$("body.application,body.livescores,body.preview").each(function(){
		setTimeout(function(){
			window.location.reload();
		},4*60*1000); // 4 minutes delay refresh
	});
});


function __triger_mouse_enter() {
	var len = $('.news_accent').length;
	if(index_news==len) {
		stop_image_rotation=1;
		if($('body.application').length){
		//clearTimeout(t);
		//__triger_mouse_enter_leagues();	
      index_news=-1;
			stop_image_rotation=0;	
		}
		else {
			index_news=-1;
			stop_image_rotation=0;
		}
	}
	if(stop_image_rotation==0) {
		t = setTimeout(function(){
			if(stop_image_rotation==0) {
				index_news++;
				if(/*$('body.application').length==0 && */index_news>=len) {index_news=0;}
				$('.news_accent').css('display','none');
				$('.news_accent:eq('+index_news+')').css('display','block');
				__triger_mouse_enter();
			}
		},rotate_by);
	}
}

