/* CasinoSpy global JS */

// --------- vars and functions used for tab rotation
var currentTab = 0; 
// var rotateSpeed = 3000;
var numTabs;
// var autoRotate;
// var tabUserInteraction = false; 

function openTab(clickedTab) {
	var thisTab = $("#homeTabs .tabs a").index(clickedTab);
	$('#homeTabs .tabs a').removeClass('selected');
	$tabLink = $("#homeTabs .tabs li a:eq("+thisTab+")");
	$tabLink.addClass("selected");
	$('#homeTabs .tabsBody div').hide();
	$elem = $el = $($tabLink.attr('href'));
	$elem.show();
	currentTab = thisTab;
}

/*
function rotateTabs() {
	var nextTab;
	if (currentTab == (numTabs - 1)) {
		nextTab = 0;
		clearInterval(autoRotate);
	}
	else {
		nextTab = currentTab + 1;
	}
	$tabLink = $("#homeTabs .tabs li a:eq("+nextTab+")");
	openTab($tabLink);
}
*/

/* ------------------------------------------------------------------------
 * Tooltip script by Alen Grakalic (http://cssglobe.com)
 * more info: http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 */
 this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$(".tooltip").hover(function(e){
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$(".tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

// --------------------------

$(function() {
	
	// large boxes containing listst - remove border from last item
	// boxlarge2 fix - 
	$('.boxLarge2 ol li:last').css({'border-bottom':'none', 'padding-bottom':'14px'});

	// print
	$('#articleHeader a.print').click(function() {
		window.print();
		return false;
	});
	
	// tell a friend
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);  
	};

	$('#tellfriend').hide();
	$('#articleHeader a.email, #tellfriend a.close').click(function() {
		$("#tellfriend").fadeToggle('slow');
	}); 


	// fake hover for IE6
	$('#nav li').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});

	// login menu
	var ulWidth = $('#loginBar ul').width();

	var $loginForm = $('#loginBar form');
	var formWidth = $loginForm.width();
	var $loginMenu = $('#loginBar #menuLogin');

	var $loginForm = $('#loginBar form');
	var formWidth = $loginForm.width();

	$loginForm.hide();

	$('#close').click(function() { 
		$('#loginBar').animate({
			width: '150px'
		}, 500, 'swing', function() { $loginForm.fadeOut(); });
		$loginMenu.fadeIn();
		return false;
	})

	$('#loginBar .showForm').click(function() {
		$loginMenu.fadeOut();
		$('#loginBar').animate({
			width: '570px'
		}, 500, 'swing', function() { $loginForm.fadeIn(); });

		return false;
	});

// -------------------------------------------------
// login if user hits enter in a text field in one of the login forms


	$("form#frmLogin input").keypress(function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) 
		{
			if (document.frmLogin != null && document.frmLogin != 'undefined')
			{
				document.frmLogin.submit();
			}
		} 
		return true;
	});

	$("form.inlineLogin input").keypress(function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) 
		{
			document.frmLogin2.submit();
		} 
		return true;
	});

		function callbackSuccess(data) { // post-submit callback
			alert(data)
		}


/* --------------------------------------------
	// homepage tabs - preload CLAIM button

	var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
	$.preLoadImages("../../theme/img/global/claim_now.png");
*/


	if($('#homeTabs').length) {
		numTabs = $("#homeTabs .tabs a").length;
 
		$("#homeTabs .tabs a").click(function() { 
			// tabUserInteraction = true;
			// clearInterval(autoRotate)
			openTab($(this)); return false; 
		});
		/*
		$("#homeTabs").mouseover(function(){
						clearInterval(autoRotate)
					  })
					  .mouseout(function(){
							// console.log('tabUserInteraction = ' + tabUserInteraction);
							if (!tabUserInteraction) // continue the loop if user didn't clicked on a tab
							{
								autoRotate = setInterval("rotateTabs()", rotateSpeed)
							}
						});
		*/
	 	openTab( $("#homeTabs .tabs a:eq("+currentTab+")") );
		//$("#homeTabs").mouseout();

	}

	// add caption animation (slide up, then down - once) 
	 $.fn.delay = function(time, callback){ 
		// Empty function:
		jQuery.fx.step.delay = function(){};
	    // Return meaningless animation, (will be added to queue)
		return this.animate({delay:1}, time, callback);
	}
	if($('#adBox').length) {
		$('#adBox .caption').slideToggle('slow', function() {
			 $('#adBox .caption').delay(4000).slideToggle('slow');
		});
	}

	$("form#frmSearch input").keypress(function (e) {
			if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
				document.frmSearch.submit();
				e.preventDefault();
				return false;
			} else {
				return true;
			}
	});

	// starting the tooltip script on page load
	tooltip();

});


