var popupTimer;
var currentPopup;

$(document).ready(function(){

	$(".page-template-page-shop-php .category a.product-image img").each(function(){
		var link = $(this).attr('src');
		var pos = link.indexOf('?');
		var realLink = link.substr(0, pos);
		$(this).parent().attr('href', realLink);
	});
	
	$(".page-template-page-shop-php .product a.product-image img").each(function(){
		var link = $(this).attr('src');
		var pos = link.indexOf('?');
		var realLink = link.substr(0, pos);
		$(this).parent().attr('href', realLink);
	});
	
	$(".page-template-page-shop-php .product a.product-image").fancybox();
	$(".page-template-page-shop-php .category a.product-image").fancybox();

	//Gets the facebook status updates
	$.ajax({
		url: "/wp-content/themes/gifsundsvall/includes/get_facebook.php",
		dataType: "html",
		cache: true,
		success: function(data, status) {
			$('#tabs-3 .tab-content-inner').html('');
			$('<h4></h4>').text('Facebook').addClass('facebook-header').appendTo('#tabs-3 .tab-content-inner');
			$(data).appendTo('#tabs-3 .tab-content-inner');
		}
	});
	
	//Gets the YouTube RSS Feed
	$.jGFeed(
		"http://gdata.youtube.com/feeds/api/users/gifsundsvall/uploads",
		function(feeds){
			if(!feeds){
				return false;
			}
			$('<h4></h4>').text('YouTube').addClass('youtube-header').appendTo('#tabs-2 .tab-content-inner');
			for(var i = 0; i < feeds.entries.length; i++) {
				$('#tabs-2 .tab-content-inner').append('<p><a href="'+feeds.entries[i].link+'" title="'+feeds.entries[i].contentSnippet+'">' + 
				feeds.entries[i].title + '</a></p>');
			}
		},
		6
	);
	
	//Runs the tooltip plugin
	$(".post a").easyTooltip();
	$("#game-host img").easyTooltip();
	
	//Runs the jQuery UI accordion	
	$( ".panel-wrapper" ).accordion({
		'autoHeight' : false
	});
	
	//Runs the jQuery UI tabs
	$( "#next-game-tabs" ).tabs( { fx: { opacity: 'toggle', duration: 'fast' } });	
	
	//Instantiates the calendar	
	$('#calendar').calendar();

	//Shows the youtube iframe on play click
	$('.next-game-picture-wrapper .play-button').click(function(){
		$(this).hide();
		$(this).parent().hide();
		$(this).parent().parent().find('.youtube-wrapper').show();
	});
	
	//Animates the textwidget
	var newsString = $('.news .wrapper .textwidget').text();
	if (newsString.length > 115) {
		$('.news .wrapper div').SetScroller({	
			velocity: 60,
			cursor: 'text'
		});
	}
	
	//Add an odd class on the team list
	$('#team-wrapper ul li:nth-child(odd)').addClass('odd');
	$('.player-holder ul li:nth-child(odd)').addClass('odd');
	
	//Shows the player thumbnail in the team list
	$('#team-wrapper ul span.title a').hover(function(){
		$(this).parents('.player-list').find('.popup-wrapper').fadeIn(100);
	}, function(){
		$(this).parents('.player-list').find('.popup-wrapper').fadeOut(100);
	});
	
	//Removes the text in the search form
	$('.search').focus(function(){
		if($(this).val() == 'Sök...'){
			$(this).val('');
		}
	});
	$('.search').blur(function(){
		if($(this).val() == ''){
			$(this).val('Sök...');
		}
	});
	
	//Main menu
	$('.main-navigation').superfish({
		autoArrows: false,
		speed: 'fast',
		delay: 300
	});
	
	$('.main-navigation li').hover(function() {
		
		var ul = $(this).find('.menu-items');
		
		// container offset + width
		var c_offset = $('#container').offset();
		var document_width = parseInt(c_offset.left) + 980;
		
		// menu item offset + submenu width
		var parent_position = $(this).offset();
		var submenu_position = parent_position.left + $(ul).width() + 27;
		
		// check position
		if(document_width <= submenu_position) {
			var move = 0 - (submenu_position - document_width);
			$(ul).css('margin-left', move);
		}

		
	});
	
	//$('.no-click').click(function(e){
	//	e.preventDefault();
	//});
	
	$('.widget-float-left').each(function() {
		$(this).addClass('background-left');
	});
	
	$(".widget-float-left:first-child").addClass("no-background");
	
	//Splash
	$('#splash-img').click(function() {
		$(this).hide();
		$('#splash-wrap').hide();
	});
	
	$('#splash-wrap').click(function() {
		$('#splash-img').hide();
		$(this).hide();
	});
	
});
