$(document).ready(function(){
	
	$('#featured .buttons li a:first').addClass("selected");
	$('#content div.box:odd').addClass("alt");
	
	$('#roomsList tbody tr:even').addClass("alternate");
	
	if($('#userAlert').length){
		
		$('#userAlert').css('display', 'none');
		$('#userAlert').slideToggle();
		
		setTimeout(function(){
			$('#userAlert').slideToggle();
		}, 2500)
	}
	
	/* FRONT FADE IN ANIMATION */
	
	$('#featured .sliding img').hide().filter(':first').show();
	
	function fade(currentPhoto) {
	  
		$('#featured .sliding').find('.selected').stop(true, true).fadeOut("normal").removeClass('selected');
		$('#featured .sliding img').eq(currentPhoto).stop(true, true).fadeIn("normal").addClass('selected')
	
	}
	
	$('#featured .buttons a').click(function(){
		fade($('#featured .buttons a').index(this));
		
		$('#featured .buttons a').removeClass('selected');
		
		$(this).addClass('selected');
		
		
		return false;
	})
	
	var c = 0;
	setInterval(function(){
		var len = $('#featured .buttons a').length;
		c++;
		
		if(c == len){
			$('#featured .buttons li:first a').trigger("click");
			c = 0;
		}else{
			$('#featured .buttons a.selected').parent().next().find('a').trigger("click");
		}
		
	},5000)
	
	/* END FRONT FADE IN ANIMATION */
	
	//hide review button
	$('.thumbnail a.review').hide();
	
	$('div.page .box').hover(function(){
		$(this).find('.thumbnail a.review').stop(true, true).fadeIn("normal");
	}, function(){
		$(this).find('.thumbnail a.review').stop(true, true).fadeOut("normal");
	})
	
	$('.calendar td.next a, .calendar td.prev a').live("click", function(){
		
		var year = $(this).parent().find('.year').text(),
			month = $(this).parent().find('.month').text();
		
		$.ajax({
			type: "POST", 
			url: "ipokerrace/calendar",
			data: "year=" + year + "&month=" + month,
			beforeSend: function(){
				$('#boxCalendar table').remove();
				$('#boxCalendar').prepend("<div class='ajaxIcon' />");
			},
			success: function(response){
				$('#boxCalendar').prepend(response);
				$('#boxCalendar .ajaxIcon').remove();
			}
		})
		
		return false;
	})
	
	function user_alert(message){
		
		if($('#userAlert').length)
			$('#userAlert').remove();
		
		$('body').prepend(
							"<div id='userAlert'>"
							+message
							+"</div>"
						);
						
		$('#userAlert').hide().slideToggle("fast");
		
		setTimeout(function(){
			$('#userAlert').slideToggle();
		}, 2500)
			
	}
	
	$('#roomsList a.create').click(function(){
		
		$(this).modalbox();
		
		return false;
	})
	
	function add_user_room(elem){
		
		var post = elem.serialize();
		
		$.ajax({
			type: "POST",
			url: elem.attr("action"),
			data: post,
			dataType: "json",
			beforeSend: function(){
				old_value = $(elem).find('button').text();
				$(elem).find('button').text('').append('<div class="btnImage" />');
				$(elem).find('.user_msg').fadeOut(0, function(){
					$(this).remove();
				});
			}, 
			success: function(response){
				$(elem).find('.btnImage').remove();
				$(elem).find('button').text(old_value);
				if(response.ok == "false"){
					$(elem).prepend("<div class='user_msg false'>" + response.message + "</div>").hide().fadeIn('slow');
				}else{
					window.location = "";
				}
			}
		})	
	}
	
	$('#frmUserRoom').live("submit", function(){
		
		add_user_room($(this));
		
		return false;
	})
	
	$('#frmUserRoom button').live("click", function(){
		
		add_user_room($('#frmUserRoom'));
		
		return false;
	})
	
	$('#gmail').click(function(){
		
		$('.page.hidden').stop(true, true).slideToggle();
		
		return false;
		
	})
	
	function verify_credentials(elem, location){
		
		var data = elem.serialize();
		$.ajax({
			type: "POST",
			url: location,
			data: data,
			dataType: "json",
			beforeSend: function(){
				old_value = $(elem).find('button').text();
				$(elem).find('button').text('').append('<div class="btnImage" />');
			},
			success: function(result){
				
				if(result.result == "success"){
					$(elem).find('.btnImage').remove();
					$(elem).find('button').text(old_value);
					$('#ajaxResponse').html(result.message);
					$('.page.hidden').hide();
				}else{
					$(elem).find('.btnImage').remove();
					$(elem).find('button').text(old_value);
					user_alert(result.message);
				}
			}
		})
		
	}
	
	/* START HOTMAIL SCRIPT */
	
	
	
	$('div.page a#hotmail').click(function(){
		
			var url = window.location.href.toString(),
			returnURI = url + "/live_delegation",
			privacy = url + "/privacy",
			PermissionRequestURI = "https://consent.live.com/Delegation.aspx?RU=" + (returnURI) + "&ps=" + "Contacts.View" + "&pl=" + (privacy);
			
			window.open(PermissionRequestURI, "", "width=760px,height=560,left=100,top=100,scrollbars=yes, location=yes");
			
			return false;
		})

	
	/* END HOTMAIL SCRIPT */
	
	$('#ajaxResponse span.check').find('.checkAll').live('click', function(){
		
		$('#ajaxResponse').find('input[type=checkbox]').attr("checked", true);
		
		return false;
	})
	
	$('#ajaxResponse span.check').find('.checkNone').live('click', function(){
		
		$('#ajaxResponse').find('input[type=checkbox]').attr("checked", false);
		
		return false;
	})
	
	$('#frmEmailLogin').live('submit', function(){
		
		var elem = $(this), 
			location = $(this).attr('action');

		verify_credentials(elem, location);
		
		return false;
	})
	
	$('#frmSubmit a.add').live("click", function(){
		
		var el = '<ul class="sideFields field">';
					el += '<li class="first">';
						el += '<label for="name">Friend Name</label>';
						el += '<div class="input">';
							el += '<input type="text" name="name[]" id="name">';
						el += '</div>';
					el += '</li>';		
					el += '<li>';
						el += '<label for="email">Friend Email</label>';
						el += '<div class="input">';
							el += '<input type="text" name="email[]" id="email">';
						el += '</div>';
					el += '</li>';	
				el += '</ul>';
				
		$(this).parent().parent().find('.button').before(el);
		
		
		
		return false;
	})
	
	/* forgot username/password */
	
	$('#reset').click(function(){
		
		$(this).modalbox();
		
		return false;
	})
	
	
	
	function recovery(elem){
		
		var post = elem.serialize();
		
		$.ajax({
			type: "POST",
			url: elem.attr("action"),
			data: post,
			dataType: "json",
			beforeSend: function(){
				old_value = $(elem).find('button').text();
				$(elem).find('button').text('').append('<div class="btnImage" />');
				$(elem).find('.user_msg').fadeOut(0, function(){
					$(this).remove();
				});
			}, 
			success: function(response){
				$(elem).find('.btnImage').remove();
				$(elem).find('button').text(old_value);
				if(response.ok == "false"){
					$(elem).prepend("<div class='user_msg false'>" + response.message + "</div>").hide().fadeIn('slow');
				}else{
					window.location = "";
				}
			}
		})	
	}
	
	$('#frmForgot').live("submit", function(){
		
		recovery($(this));
		
		return false;
	})
	
	$('#frmForgot button').live("click", function(){
		
		recovery($('#frmForgot'));
		
		return false;
	})


    /* RANKINGS SLIDER  - feature added later */
	var hash = window.location.hash;

    var tabContainers = $('.tabs > div');
    tabContainers.hide().filter(':first').show();

    $('div.tabs ul.nav a').click(function(){

        $('div.tabs ul.nav a').removeClass("selected");
        $(this).addClass("selected");

        tabContainers.hide();
        tabContainers.filter(this.hash).fadeIn("slow");

        return false;
    })
    
    if(hash == ""){
    	$('div.tabs ul.nav a').filter('a:first').click();
    }else{
    	$.each($('div.tabs ul.nav a'), function(i,v){
    		if(this.hash == hash){
    			$(this).trigger('click');
    			return;
    		}
    		return;
    	})
    }

    /* END RANKINGS SLIDER */
    
    /* SOCIAL ANIMATION */
    
    
  var $element   = $("#social"),
        $window    = $(window),
        offset     = $element.offset(),
        topPadding = 15;
    
	if($('#social').length > 0){
		
		$(window).scroll(function(){
	    	
	    	$window.scroll(function() {
	        if ($window.scrollTop() > offset.top) {
	            $element.stop().animate({
	                marginTop: $window.scrollTop() - offset.top + topPadding
	            });
	        } else {
	            $element.stop().animate({
	                marginTop: 0
	            });
	        }
	    });
	    	
	    })
	}
    
    
    /* END SOCIAL ANIMATION */
	
})
