$(document).ready(function(){
    ////////////////////////
    // NAVIGATION
    $("ul#nav li a").click(function(){
       	var href = $(this).attr("href");
       	if(href != "#") {
       		$(".subnav").hide();
       		$(href).show();
       		return false;
       	}
       });
    $("#nav-search ul li").click(function(){    	
    	var href = $(this).find("a").attr("href");
		$("#nav-search ul li").removeClass("active");
		$(this).addClass("active");
		$("#nav-search .tab").hide();
		$(href).show();
		return false;
    });
    $("ul#nav li a").click(function(){
		$("#nav-search").css({"background-color":"#0099FF"});
		$(".subnav ul").css({"background-color":"#0099FF"});
    	$(".subnav").css({"z-index":"4"});
	});
	$("ul#nav li a.register").click(function(){
		$("#nav-search").css({"background-color":"#4EAE00"});
		$(".subnav ul").css({"background-color":"#4EAE00"});
		$(".subnav").css({"z-index":"5"});
	});    
    ///////////////////////////////	
	// LOGIN TABS
	$("#user-login-tab .login-content input.submit").click(function(){
		var correct = false;
		if(!correct) {
			$("#user-login-tab .login-content").hide();
			$("#user-login-tab .incorrect-data").show();
			$("#user-login-tab .incorrect-data a.try-again").click(function(){
				$("#user-login-tab .incorrect-data").hide();
				$("#user-login-tab .login-content").show();
			});
			$("#user-login-tab .incorrect-data a.forgot").click(function(){
				$("#user-login-tab .incorrect-data").hide();
				$("#user-login-tab .forgot-password").show();
			});
		}
	});
	//////////////////////////
	//ADD TO FAVOURITES BUTTON
	$("a.add-to-favourites").click(function(){
		//$(this).html("Favorite Game");
	});
	////////
	//INPUTS
	$('.blink').focus(function () {
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
			}
		})
	$('.blink').blur(function () {
			if ($(this).val() == '') {
				$(this).val($(this).attr('title'));
			}
		})  
    ////////////////////////////
    // GAME TABS ON PROFILE.HTML
    $("#game-tabs ul.tabs li").click(function(){
    	$("#game-tabs ul.tabs li").removeClass("active");
    	$(this).addClass("active");
    	var href = $(this).find("a").attr("href");
    	$("#game-tabs .tab").hide();
    	$(href).show();
    	return false;
    });
    //BADGES TAB - CLICK AN ELEMENT
    $(".all-badges .badge-holder a").click(function(){
    	$(".all-badges .badge-holder").removeClass("badge-holder-active");
    	$(this).parent().addClass("badge-holder-active");
    	$(".badge-info .badge").hide();
    	var href = $(this).attr("href");
    	$(href).fadeIn();
    	return false;
    });

    
    $("#profile-sidebar .side-tabs ul.tab li a").click(function(){
    	$(this).parent().find("div:eq(0)").toggle();
    	return false;
    });

	//////////////////////////////////
	// POPUPS
    $("#top-rated-games .games-holder .game-box, #recommended-games .cnt a , #more-games-tab .games a").hover(
    	function(){
			var t = $(this).position().top + 50;
			var l = $(this).position().left - 30;
			
			$('.rating-tooltip').css({ 'top' : t + 'px', 'left' : l + 'px' }).show();
		},
		function(e){
			dialog_object = $(this);
			$(document).bind('mouseout', monitor_close_balloon);
		}
	);
	function monitor_close_balloon(e) {
		if ($(e.relatedTarget).hasClass('rating-tooltip')) {
			return;
		}
		if ($(e.relatedTarget).parents('.rating-tooltip').length !== 0) {
			return;
		}
		$('.rating-tooltip').hide();
	}
	//STARS CLICK
	$("a.star").click(function(){
			if($(this).hasClass("first")){
				$(this).addClass("active");
				return false;
			}
			else if($(this).hasClass("second")) {
				$(".rating-tooltip a.first").addClass("active");
				$(this).addClass("active");
				return false;
			}
			else if($(this).hasClass("third")) {
				$(".rating-tooltip a.first").addClass("active");
				$(".rating-tooltip a.second").addClass("active");
				$(this).addClass("active");
				return false;
			}
			else if($(this).hasClass("fourth")) {
				$(".rating-tooltip a.first").addClass("active");
				$(".rating-tooltip a.second").addClass("active");
				$(".rating-tooltip a.third").addClass("active");
				$(this).addClass("active");
				return false;
			}
			else {
				$(".rating-tooltip a").addClass("active");
				return false;
			}		
		return false;
	});
	//STARS HOVER
	$("a.star").hover(
		function(){
			$(".rating-tooltip a").removeClass("active");
			
			if($(this).hasClass("first")){
				$(this).addClass("active");
			}
			else if($(this).hasClass("second")) {
				$(".rating-tooltip a.first").addClass("active");
				$(this).addClass("active");
			}
			else if($(this).hasClass("third")) {
				$(".rating-tooltip a.first").addClass("active");
				$(".rating-tooltip a.second").addClass("active");
				$(this).addClass(active);
			}
			else if($(this).hasClass("fourth")) {
				$(".rating-tooltip a.first").addClass("active");
				$(".rating-tooltip a.second").addClass("active");
				$(".rating-tooltip a.third").addClass("active");
				$(this).addClass("active");
			}
			else {
				$(".rating-tooltip a").addClass("active");
			}
		},
		function() {
			$(".rating-tooltip a").removeClass("active");
		}
	);
	
	//FULL GAMES LIST ELEMENTS HOVER
	$(".full-games-list ul li a").hover(
		function(){
			var t = $(this).position().top-32;
			var l = $(this).position().left + $(this).width();
			
			$('.games-list-popup').css({ 'top' : t + 'px', 'left' : l + 'px' }).show();
		},
		function(e){
			dialog_object = $(this);
			$(document).bind('mouseout', monitor_close_box);
		}
	);
	function monitor_close_box(e) {
		if ($(e.relatedTarget).hasClass('games-list-popup')) {
			return;
		}
		if ($(e.relatedTarget).parents('.games-list-popup').length !== 0) {
			return;
		}
		$('.games-list-popup').hide();
	}
	//////
	//CAROUSEL INITIALISING
	// HOME PAGE
	$("#featured-games .slide-holder ul").jcarousel({
		scroll: 1,
		wrap: 'both',
		initCallback: mycarousel_initCallback1,
		buttonNextHTML: null,
		buttonPrevHTML: null
	}); 
	$("#new-games .slide-holder ul").jcarousel({
		scroll: 1,
		wrap: 'both',
		initCallback: mycarousel_initCallback2,
		buttonNextHTML: null,
		buttonPrevHTML: null
	}); 
	$("#cat-popular-games #tabs-content .slide-holder ul").jcarousel({
		scroll: 1,
		wrap: 'both',
		initCallback: mycarousel_initCallback3,
		buttonNextHTML: null,
		buttonPrevHTML: null
	}); 	
	//
	$("#activity-tab .slide-holder ul").jcarousel({
		scroll: 1,
		wrap: 'both',
		initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null
	});        
    $(".content-tabs ul li").click(function(){
    	$(".content-tabs ul li").removeClass("active");
    	$(this).addClass("active");
    	var href = $(this).find("a").attr("href");
    	$(".content-tabs .tab").hide();
    	$(href).show();
		 $(".slide-holder ul").jcarousel({
				scroll: 1,
				wrap: 'both',
				initCallback: mycarousel_initCallbacknew,
				buttonNextHTML: null,
				buttonPrevHTML: null
		 });    	
    	return false;
    });
});


 

function mycarousel_initCallback(carousel) {
	$('.mycarousel-next').bind('click', function() {
		carousel.next();
		return false;
	});
	$('.mycarousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};
function mycarousel_initCallback1(carousel) {
	$('#featured-games-next').bind('click', function() {
		carousel.next();
		return false;
	});
	$('#featured-games-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};
function mycarousel_initCallback2(carousel) {
	$('#new-games-next').bind('click', function() {
		carousel.next();
		return false;
	});
	$('#new-games-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};
function mycarousel_initCallback3(carousel) {
	$('#popular-games-next').bind('click', function() {
		carousel.next();
		return false;
	});
	$('#popular-games-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
};

