/*
 * World of Slots
 * jQuery-basied site functions
 */

// Initialize elements
$(document).ready(function() {	
	
	//remove top border for the first Top online slots table
	$('#homeTop5Online table tr:first td').css("border-top", "0px");
	
	// Sub-page featured slider
	if ($('#header_content_smaller')) {
		$('#header_content_smaller').slides({
			container: $('#header_content_smaller'),
			slidesWrapper: $('#header_content_smaller_wrapper'),
			slides: $('.header_content_smaller_slide'),
			slideWidth: 780
		});
	}

	// Homepage popular slider
	if ($('#homewidget')) {
		$('#homewidget').slides({
			container: $('#homewidget'),
			slidesWrapper: $('#homewidget_wrapper'),
			slides: $('.homewidget_slide'),
			slideWidth: 655,
			insertButtons: false,
			changeInterval: 4000
		});
	}

	// Scroll to top of page
	$('a[href=#top]').click(function() {
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});

	if ($('#flash-game')) {
		setTimeout(function() {
			$('#flash-game').css('background-image', 'none');
		}, 4000);
	}
});


$(window).load(function () {

	//vertical center icon for Top 5 Free Slots
	$('#sidelist-highlighted ul li').each(function(index) {
    	var $liH = $(this).height();
    	var $imgH = $(this).children('a').children('img').height();
    	
    	if($imgH < $liH){
    		var topMrg = parseInt(($liH - $imgH)/2 - 1); 
    		$(this).children('a').children('img').css("margin-top", topMrg + "px");
    	}
  	});

});

	
  	
  	

// Slider widget
jQuery.fn.slides = function() {
	var config = arguments[0] || {};

	this.each(function() {
		var container = config.container;
		var slidesWrapper = config.slidesWrapper;
		var slides = config.slides;
		var insertButtons = (config.insertButtons != null) ? config.insertButtons : true;
		var slideWidth = config.slideWidth;
		var numberOfSlides = slides.length;
		var changeInterval = (config.changeInterval != null) ? config.changeInterval : 0;
		var animateSpeed = (config.animateSpeed != null) ? config.animateSpeed : 400;
		var currentPosition = 0;

		// Remove scrollbar in JS
		config.container.css('overflow', 'hidden');

		// Wrap all slides with the inner slide div
		slides.wrapAll('<div id="' + slidesWrapper.attr('id') + '"></div>').css({
			'float' : 'left',
			'width' : slideWidth
		});

		// Set #slideInner width equal to total width of all slides
		slidesWrapper.css('width', slideWidth * numberOfSlides);

		var containerId = container.attr('id');
		var rotate = null;

		// Insert controls in the DOM
		if (insertButtons) {
			container
			.prepend('<span class="' + containerId + '_control control_prev" id="' + containerId + '_prev">Previous</span>')
			.append('<span class="' + containerId + '_control control_next" id="' + containerId + '_next">Next</span>');
		}

		// Function for rewinding the slides
		var rotateSlidesRewind = function() {
			currentPosition = (currentPosition <= 0) ? numberOfSlides-1 : currentPosition-1;
			slidesWrapper.animate({
				'marginLeft': slideWidth*(-currentPosition)
			}, animateSpeed);
		}

		// Function for advancing the slides
		var rotateSlidesForward = function() {
			currentPosition = (currentPosition >= numberOfSlides-1) ? 0 : currentPosition+1;
			slidesWrapper.animate({
				'marginLeft': slideWidth*(-currentPosition)
			}, animateSpeed);
		}

		// Create event listeners for .controls clicks
		$('.' + containerId + '_control').bind('click', function() {
			if (rotate != null) {
				clearInterval(rotate);
				rotate = setInterval(rotateSlidesForward, changeInterval);
			}
			if ($(this).hasClass('control_prev')) {
				rotateSlidesRewind();
			} else {
				rotateSlidesForward();
			}
		});

		if (changeInterval > 0) {
			rotate = setInterval(rotateSlidesForward, changeInterval);
		}
	});
};

