// For replacing images
$.fn.image = function(src, f){
    return this.each(function(){ 
        var i = new Image();
        i.src = src;
        i.onload = f;
        this.appendChild(i);
    });
}

$(document).ready(function()
{
	TopListInfoBox.init();
	ExclOffers.init();
	Spotlight.init();
	ToolTip.init();
	Screenshots.init();
	ReviewCurrencies.init();
	ReviewPayments.init();
	FakeLogoLink.init();
	ConvTest.init();
});

var TopListInfoBox = {
	init: function() {
		$('div.slots-tournament td.c-info').each(function() {
			info = $('span', this).text();
			$(this).append('<img src="/images/info.gif" class="tooltip" title="' +info +'" alt="Info" />');
		});
	}
};

var FakeLogoLink =
{
	init:function()
	{
		var $mod_prog_jackpot = $('#mod-prog-jackpot');

		if (!$mod_prog_jackpot.size())
		{
			return false;
		}
		
		$logo_href = $mod_prog_jackpot.find('div.jackpot a').attr('href');
		
		$mod_prog_jackpot.append('<a href="' + $logo_href + '" class="logo-link"  target="_blank"></a>').css({'position':'relative'});		

		$mod_prog_jackpot.find('a.logo-link')
		.css(
		{
			'display':'block',
			'position':'absolute',
			'top':'50px',
			'left':'15px',
			'width':'150px',
			'height':'110px'
		});
	}
};

var ExclOffers =
{	
	init:function()
	{
		if (!$('#pt-index').size()) {return false;}
		
		var $container 	= $('#mod-excl-offer');
		var $offers		= $container.find('h4');
		
		if ($offers.size() < 2) {return false;}
		
		$offers.not(':first').hide(); // Hide all offers except first
		
		var list_items = "";
		
		// Loop offers to fetch text and link from it to build a <ul>
		$offers.each(function(i) {
			
			$this = $(this);
			link = $this.find('a').attr('href');
			
			// add sel class to the first one
			class_sel = "";
			if (i == 0) {class_sel = ' class="sel"';}
			
			list_items+= '<li' + class_sel + '><a href="' + link + '" target="_blank">' + $this.text() + '</a></li>';
		});
		
		list_items = '<ul class="bullets">' + list_items + '</ul>';
		
		$container.append(list_items);
	},
	
	start:function()
	{
		if ($('#pt-index').size())
		{
			setTimeout("ExclOffers.switch_offer(0)",2000); // Start switching
		}
	},
	
	// Switch between offers from a given interval by fading in/out
	// Also move sel class in <ul> below to show which one is currently displayed
	switch_offer:function(num)
	{	
		var $container 	= $('#mod-excl-offer');
		var $offers 	= $('#mod-excl-offer h4');
		var $list_items	= $('#mod-excl-offer').find('li');

        if($list_items.size() > 1)
        {
            $offers.eq(num).fadeOut("normal",function()
            {
                next = num +1;
                if (num == $list_items.size() -1)
                {
                    next = 0;
                }
                $list_items.removeClass("sel");
                $list_items.eq(next).addClass("sel");
                $offers.eq(next).fadeIn("normal",function() {
                    setTimeout("ExclOffers.switch_offer(" + next + ")",3500);
                });
            });
        }
	}
};

var Spotlight =
{
	init:function()
	{
		if (!$('#mod-spotlight').length) return false;
		
		// Set up initial state and config
		
		this.s = $('#mod-spotlight');
		
		this.heading = $(this.s).find('.heading');
		
		this.heading_current = $(document.createElement('h4'))
			.appendTo(this.heading);
		
		this.bodies = $(this.s).find('.body');
		
		this.bodies_current = 0;
		
		this.thumbs_width = 100; // Including margin, padding and borders
		
		this.thumbs_visible = 5;
		
		this.thumbs_nav_current = 0;
		
		this.thumbs_nav_disabled_opacity = 0.25;
		
		this.thumbs_nav_disabled_animation_speed = 500;
		
		this.thumbs_nav_animation_speed = 100;
		
		this.thumbs = $(document.createElement('ul'))
			.addClass('thumbs clearfix')
			.css('width',this.thumbs_width * (this.bodies.length + 1));
		
		this.thumbs_nav = $(document.createElement('ul'))
			.addClass('nav');
		
		this.thumbs_nav_viewport = $(document.createElement('div'))
			.addClass('viewport')
			.append(this.thumbs);
		
		this.thumbs_nav_prev = $(document.createElement('li'))
			.addClass('prev disabled')
			.text('Previous')
			.click(function() { Spotlight.switch_thumbs('prev'); })
			.css('opacity',this.thumbs_nav_disabled_opacity)
			.appendTo(this.thumbs_nav);
		
		this.thumbs_nav_next = $(document.createElement('li'))
			.addClass('next')
			.text('Next')
			.click(function() { Spotlight.switch_thumbs('next'); })
			.appendTo(this.thumbs_nav);
		
		this.footer = $(document.createElement('div'))
			.addClass('footer')
			.append(this.thumbs_nav_viewport)
			.append(this.thumbs_nav)
			.appendTo(this.s);
		
		$(spotlight_data).each(function(i)
		{
			$(document.createElement('li'))
				.html('<img src="' + this.image_url + '" title="' + this.name + '" alt="' + this.name + '"/>')
				.click(function() { Spotlight.switch_casino(i); })
				.appendTo(Spotlight.thumbs);
		});
		
		$(this.heading_current).html(spotlight_data[this.bodies_current].name);
		$(this.bodies[this.bodies_current]).show();
	},
	
	switch_casino:function(bodies_next)
	{
		if (bodies_next === this.bodies_current) return false;
		
		$(this.heading_current).html(spotlight_data[bodies_next].name);
		
		$(this.bodies[bodies_next]).show();
		$(this.bodies[this.bodies_current]).hide();
		
		this.bodies_current = bodies_next;
	},
	
	switch_thumbs:function(direction)
	{
		if (direction === 'prev')
		{
			if (this.thumbs_nav_current === 0) return false;
			
			this.thumbs_nav_current -= 1;
			
			if (this.thumbs_nav_current === 0)
			{
				$(this.thumbs_nav_prev)
					.addClass('disabled')
					.animate({opacity: this.thumbs_nav_disabled_opacity}, this.thumbs_nav_disabled_animation_speed);
			}
			
			if ($(this.thumbs_nav_next).hasClass('disabled'))
			{
				$(this.thumbs_nav_next)
					.removeClass('disabled')
					.animate({opacity: 1}, this.thumbs_nav_disabled_animation_speed);
			}
		}
		else if (direction === 'next')
		{
			if (this.thumbs_nav_current === this.bodies.length - this.thumbs_visible) return false;
			
			this.thumbs_nav_current += 1;
			
			if (this.thumbs_nav_current === this.bodies.length - this.thumbs_visible)
			{
				$(this.thumbs_nav_next)
					.addClass('disabled')
					.animate({opacity: this.thumbs_nav_disabled_opacity}, this.thumbs_nav_disabled_animation_speed);
			}
			
			if ($(this.thumbs_nav_prev).hasClass('disabled'))
			{
				$(this.thumbs_nav_prev)
					.removeClass('disabled')
					.animate({opacity: 1}, this.thumbs_nav_disabled_animation_speed);
			}
		}
		else return false;
		
		$(this.thumbs).animate({left: (this.thumbs_width * this.thumbs_nav_current) * -1}, this.thumbs_nav_animation_speed); // '* -1' makes the number negative
	}
}

// Function for showing customized tooltip.
// Make sure there's a nicely styled #tooltip in your css
// .tooltip may also be styled if you for example want a pointer cursor..
var ToolTip =
{
	init:function()
	{
		var $targets = $('.tooltip'); // Store all elements that wants a tooltip
		
		if (!$($targets).size()) {return false;}	// If none - bye bye
		
		$targets.each(function() // Loop targets
		{
			var $this 			= $(this);
			var display_text 	= $this.attr('title');
			
			if (!$("#tooltip").size()) // if tooltip div doesnt exist
			{
				$this.mouseover(function(e)
				{	
					$this.attr('title',''); // Remove title - we dont want both´the browser tooltip and our customized one to show one.
					
					var mouse_y = e.pageY + 10 + 'px'; // Cordinates (10 px offset on both directions)
					var mouse_x = e.pageX + 10 + 'px';

					$('body').append('<div id="tooltip" style="visibilty:none">' + display_text + '<div>'); // Append tooltip div to body

					$("#tooltip").css({top: mouse_y, left:mouse_x }); // Set x & y based on mouse's position 
				});
			}
			
			// Remove tooltip on mouseout and reset title
			$this.mouseout(function(display_text)
			{
				$("#tooltip").remove();
				$this.attr('title',display_text);
			});
			
			// make tooltip move when you move your mouse over target
			$this.mousemove(function(e)
			{
				var mouse_y = e.pageY + 5 + 'px';
				var mouse_x = e.pageX + 10 + 'px';
				
				$("#tooltip").css({top: mouse_y, left:mouse_x });
			});
		});		
	}
};

var Screenshots =
{
	init:function()
	{
		if ($('#pt-review').size())
		{
			Screenshots.setup('#mod-screenshots div.imgs',true,255,'p',2);
		}
		else
		{
			return false;
		}
	},
	
	setup:function(target_div,thickbox,img_width,img_container,min_num_imgs)
	{
		var $target = $(target_div);
		
		if (thickbox == true)
		{
			$target.find('a').addClass('thickbox');
		}
		
		var num_imgs = $target.find(img_container).size();
		
		if (num_imgs < min_num_imgs+1) {return false;}
		
		$('.viewport').after('<ul class="nav"><li class="prev"><a href="#"><span>Previous</span></a></li><li class="next"><a href="#"><span>Next</span></a></li></ul>');
		
		var $prev = $('.prev');
		var $next = $('.next');
		
		Screenshots.set_opacity($prev,0.25);
		
		var total_width 	= img_width * num_imgs;
		var end_left_pos 	= total_width - img_width * min_num_imgs;
		end_left_pos 		= parseInt("-" + end_left_pos);
		
		var speed = 250;
		//alert(total_width);
		$target.css("width",total_width + 100 + "px");
		
		$prev.find('a').click(function()
		{
			$(this).blur();
			
			if (parseInt($target.css('left')) == 0) {return false;}
			
			Screenshots.reset_opacity($prev,$next);
			
			$target.animate(
				{left: "-=-" + img_width + "px"},
				speed,
				function() {
					if (parseInt($target.css('left')) == 0) {Screenshots.set_opacity($prev,0.25);}
				}
			);
			return false;
		});
		
		$next.find('a').click(function()
		{	
			$(this).blur();
			
			if (parseInt($target.css('left')) == end_left_pos) {return false;}
			
			Screenshots.reset_opacity($prev,$next);
			
			$target.animate(
				{left: "+=-" + img_width + "px"},speed,
				function()
				{
					if (parseInt($target.css('left')) == end_left_pos) {Screenshots.set_opacity($next,0.25);}
				}
			);
			return false;
		});
	},
	
	set_opacity:function(target,opacity)
	{
		target.css('opacity',opacity);
	},
	
	reset_opacity:function(prev,next)
	{
		Screenshots.set_opacity(prev,1);
		Screenshots.set_opacity(next,1);
	}
};

window.onload = ExclOffers.start;

var ReviewCurrencies = 
{
	init:function()
	{
		if (!$('#pt-review').size()) {return false;}
				
		var $buttons = $( 'dl.currency-selector' );
		var $select = $( '#type-b select#set-currency' );
		
		if ($buttons.size())
		{
			$buttons.find( 'a' ).click(function()
			{
				$this = $( this );
				var currency = $this.attr( 'class' );

				$( 'table.bonus-info' ).addClass( 'bonus-info-hidden' ).removeClass( 'bonus-info' );
				$( '.l-c table#'+currency ).addClass( 'bonus-info' ).removeClass( 'bonus-info-hidden' );

				//$( '#mod-screenshots ul' ).addClass( 'bonus-info-hidden' ).removeClass( 'bonus-info' );
				$( '#mod-screenshots ul' ).each(function (i){
					//Don't include the flags
			        if(!$(this).hasClass('non-interactive'))
			        {
			        	//Change the buttons
			        	$(this).addClass( 'bonus-info-hidden' ).removeClass( 'bonus-info' );
			        }
			      });
				
				$( '#mod-screenshots ul#'+currency ).addClass( 'bonus-info' ).removeClass( 'bonus-info-hidden' );

				$( '#mod-review ul' ).addClass( 'bonus-info-hidden' ).removeClass( 'bonus-info' );
				$( '#mod-review  ul#'+currency ).addClass( 'bonus-info' ).removeClass( 'bonus-info-hidden' );

                $( '#mod-review div' ).addClass( 'review-text-hidden' ).removeClass( 'review-text' );
                $( '#mod-review div#review-'+currency ).addClass( 'review-text' ).removeClass( 'review-text-hidden' );

				$( '.currency-selector > .selected' ).removeClass( 'selected' );
				$this.parent().addClass( 'selected' );

				$( '#mod-screenshots ul.nav' ).removeClass( 'bonus-info-hidden' );

				return false;
			});		
		}
		
		if ($select.size())
		{
			$select.change(function()
			{
				$this = $( this );
				var currency = $this.val();
				
				$( '#type-b div.bonus-info' ).addClass( 'bonus-info-hidden' ).removeClass( 'bonus-info' );
				$( '#type-b div#'+currency ).addClass( 'bonus-info' ).removeClass( 'bonus-info-hidden' );
			
				switchCurrency(currency);
				
				return false;
				
			});
		}
	}
}

var ReviewPayments = 
{
	init:function()
	{
		if (!$('#pt-review').size()) {return false;}

        // Check if elapsed div is higher than collapsed div. If so, show a more link.
        var withdrawalStartHeight = $("#withdrawal-methods").height();
        $("div.withdrawal-methods-show").addClass( 'withdrawal-methods-hide' );
        $("div.withdrawal-methods-show").removeClass( 'withdrawal-methods-show' );
        var withdrawalHiddenHeight = $("#withdrawal-methods").height();

        if( withdrawalStartHeight > withdrawalHiddenHeight )
        {
            var link = '<a class="more-follow-withdrawal" href="">' + $( "#more-withdrawal-text" ).attr( "value" ) + '</a>';
            $( "#withdrawal-methods" ).parent().append( link );
        }
		
		$('a.more-follow').click( function()
		{	
			if( $("div.payment-methods-show").length > 0 )
			{
				$("div.payment-methods-show").addClass( 'payment-methods-hide' );				
				$("div.payment-methods-show").removeClass( 'payment-methods-show' );
				return false;
			}
			else
			{
				$("div.payment-methods-hide").addClass( 'payment-methods-show' );
				$("div.payment-methods-hide").removeClass( 'payment-methods-hide' );
				return false;
			}
						
		});
		
		$('a.more-follow-withdrawal').click( function()
		{	
			if( $("div.withdrawal-methods-show").length > 0 )
			{
				$("div.withdrawal-methods-show").addClass( 'withdrawal-methods-hide' );
				$("div.withdrawal-methods-show").removeClass( 'withdrawal-methods-show' );
				return false;
			}
			else
			{
				$("div.withdrawal-methods-hide").addClass( 'withdrawal-methods-show' );
				$("div.withdrawal-methods-hide").removeClass( 'withdrawal-methods-hide' );
				return false;
			}
						
		});

	}
}

function switchCurrency( currency )
{
    // TODO: The currency should be set in the session instead
    var today = new Date();
    var expires_date = new Date( today.getTime() + (1000 * 60 * 60) );
    
    document.cookie = 'currency=' + currency + '; expires=' + expires_date.toGMTString() + '; path=/';
    
    window.location.reload(true);
}
