jQuery(function($){	
	$.easy.navigation();
	$.easy.tooltip();
	$.easy.popup();
	$.easy.external();
	$.easy.rotate();
	$.easy.cycle();
	$.easy.forms();
	$.easy.showhide();
	$.easy.jump();
	$.easy.tabs();
	$.easy.accordion();	
	
	var fra = $('body').hasClass('fra');
	
	// homepage intro 
	$('#intro').each(function(){
							  
		var obj = this; 
		var li = $('.news li', obj);
		var content = $('.body', obj);
		var related = $('.related', obj);
		var timeout;
		var pause = 4000;
		var current = 0;
		var className = 'current';
	
		function next(){
			current = ( current < (li.length -1) ) ? current+1 : 0;
			show(current);
		};
		
		function show(i){
			clearTimeout(timeout);
			current = i;
			$.each(content,function(index,value){
				$(value).hide();
			})
			$(related).hide();
			$(li[i]).fadeIn();			
			$(content[i]).fadeIn();
			$(related[i]).fadeIn();
			$(li).removeClass(className);
			selected = $(li).get(i);
			$(selected).addClass(className);
			if(li.length > 1) timeout = setTimeout(function(){ next(); }, pause);
		};
		
		$(li).each(function(i){								
			$(this).css('cursor','pointer').click(function(){
				show(i);
			});
		});
		
		show(current);
	
	});
	
	//twitter box
	$('#twitter').each(function(){
		var obj = this;		
		var feed = (fra) ? 'http://twitter.com/status/user_timeline/CanadaSoccerFR.json?count=4&callback=?' : 'http://twitter.com/status/user_timeline/CanadaSoccerEN.json?count=6&callback=?';
		$('<ul class="list1"></ul>').insertAfter('#twitter h3');
		$.getJSON(feed, function(json) {
			$.each(json, function(i, item) {   
				var text = item.text;
				$('ul',obj).append('<li>'+text+'</li>');		  
		   });		   
		});	
	});
	
	
/*
 * 	Easy Paginate 1.0 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/
 *
 *	Copyright (c) 2011 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

(function($) {
		  
	$.fn.easyPaginate = function(options){

		var defaults = {				
			step: 4,
			delay: 100,
			numeric: true,
			nextprev: true,
			auto:false,
			loop:false,
			pause:4000,
			clickstop:true,
			controls: 'pagination',
			current: 'current' 
		}; 
		
		var options = $.extend(defaults, options); 
		var step = options.step;
		var lower, upper;
		var children = $(this).children();
		var count = children.length;
		var obj, next, prev;		
		var page = 1;
		var timeout;
		var clicked = false;
		
		function show(){
			clearTimeout(timeout);
			lower = ((page-1) * step);
			upper = lower+step;
			$(children).each(function(i){
				var child = $(this);
				child.hide();
				if(i>=lower && i<upper){ setTimeout(function(){ child.fadeIn('fast') }, ( i-( Math.floor(i/step) * step) )*options.delay ); }
				if(options.nextprev){
					if(upper >= count) { next.fadeOut('fast'); } else { next.fadeIn('fast'); };
					if(lower >= 1) { prev.fadeIn('fast'); } else { prev.fadeOut('fast'); };
				};
			});	
			$('li','#'+ options.controls).removeClass(options.current);
			$('li[data-index="'+page+'"]','#'+ options.controls).addClass(options.current);
			
			if(options.auto){
				if(options.clickstop && clicked){}else{ timeout = setTimeout(auto,options.pause); };
			};
		};
		
		function auto(){
			if(options.loop) if(upper >= count){ page=0; show(); }
			if(upper < count){ page++; show(); }				
		};
		
		this.each(function(){ 
			
			obj = this;
			
			if(count>step){
				
				var pages = Math.floor(count/step);
				if((count/step) > pages) pages++;
				
				var ol = $('<ol class="'+ options.controls +'"></ol>').insertAfter(obj);
				
				if(options.nextprev){
					prev = $('<li class="prev">Previous</li>')
						.hide()
						.appendTo(ol)
						.click(function(){
							clicked = true;
							page--;
							show();
						});
				};
				
				if(options.numeric){
					for(var i=1;i<=pages;i++){
					$('<li data-index="'+ i +'">'+ i +'</li>')
						.appendTo(ol)
						.click(function(){	
							clicked = true;
							page = $(this).attr('data-index');
							show();
						});					
					};				
				};
				
				if(options.nextprev){
					next = $('<li class="next">Next</li>')
						.hide()
						.appendTo(ol)
						.click(function(){
							clicked = true;			
							page++;
							show();
						});
				};
			
				show();
			};
		});	
		
	};	

})(jQuery);


$('#tab1').each(function(){
	var obj = this;
	var ul = $('<ul class="thumbs video"></ul>').appendTo(obj);
	var feed = 'http://gdata.youtube.com/feeds/api/users/CanadaSoccerTV/uploads?orderby=updated&alt=json-in-script&callback=?&max-results=24&start-index=1';
	//var feed = 'http://gdata.youtube.com/feeds/api/users/CanadaSoccerTV/favorites?alt=json-in-script&callback=?&max-results=13';
	$.getJSON(feed, 
		function(data){ 
			$.each(data.feed.entry, function(i, item) {
				var title = item['title']['$t'];
				var video = item['id']['$t'];
				video = video.replace('http://gdata.youtube.com/feeds/api/videos/','http://www.youtube.com/watch?v=');  //replacement of link
				videoID = video.replace('http://www.youtube.com/watch?v=',''); // removing link and getting the video ID
				$(ul).append('<li><a rel="" href="http://www.youtube.com/v/'+ videoID +'&hl=en_US&fs=1&" title="'+ title +'" class="popup flash"><img width="90" height="80" src="http://img.youtube.com/vi/'+ videoID +'/1.jpg" />'+ title +'</a></li>');	
			});
			$('ul',obj).easyPaginate({ step:3 });
			$.easy.popup();
	});	
});	

//
$('#tab2').each(function(){
var obj = this;
var ul = $('<ul class="thumbs"></ul>').appendTo(obj);
var feed = 'http://api.flickr.com/services/feeds/photos_public.gne?id=46765827@N08&lang=en-us&format=json&jsoncallback=?';
$.getJSON(feed, 
		function(data){
			$.each(data.items, function(i, item) {
				var source = (item.media.m).replace("_m.jpg", "_s.jpg");
				var link = item.link;
				$(ul).append('<li><a href="' + item.link + '" target="_blank"><img src="' + source + '" width="90" height="80" /></a></li>');
		});	
		$('ul',obj).easyPaginate({ step:3 });
		$.easy.popup();
	});
});	

// newsletter form
$('#newsletterForm').each(function(){
	var form = this;
	var field = $('.field',this);						   
	var fieldset = $('fieldset',this);
	var divs = $('fieldset div',this);
	var ad = $('#umbro');
	$(field).focus(function(){ $(fieldset).slideDown('fast',function(){ $(divs).css('visibility','visible');$(ad).hide(); });})
	$('html').click(function(){ $(fieldset).slideUp('fast');$(divs).css('visibility','hidden');$(ad).show();})
	$(this).click(function(e){ e.stopPropagation(); })
});

// fetch recent results data
$('#tables').each(function(){

	var obj = this;
	$('table',this).remove();
	var url = '/recent-results.php';
	var html;
	$.get(url,function(data){
	  html = data;
	  $('table:eq(0)',html).insertAfter('#tables .equal:eq(0) h2');
	  $('table:eq(1)',html).insertAfter('#tables .equal:eq(1) h2');
	  $('#tables tr').hover(function(){ $(this).addClass('over') },function(){ $(this).removeClass('over') })
	});

});


// language change
$('.lang').each(function(){		
	en = 'English';
	fr = 'Français';
	enurl = 'language=en';
	frurl = 'language=fr';
	url = window.location.href;
	host = window.location.hostname;
	url = url.replace(host,'');
	url = url.replace('http://','');
	url = url.replace('https://','');
	thisurl = (fra) ? frurl : enurl;
	thaturl = (fra) ? enurl : frurl;
	text = (fra) ? en : fr; 		
		
	if (url == '/' || url.indexOf('/?language') != -1){
		href = '/?'+ thaturl;
	} else if(url.indexOf('?language') != -1){
		url = url.replace(enurl,'');
		url = url.replace(frurl,'');
		href = url + thaturl;
	}else {
		url = url.replace('&'+ thisurl,''); 
		parts = url.split('#');			
		href = parts[0] + '&'+ thaturl;
		if(parts.length > 1) href = href +'#'+ parts[1];
	};	
	
	$('<a href="'+ href +'">'+ text +'</a>').prependTo(this);
});


$('input.calendar').datepicker( { dateFormat: 'yy-mm-dd', yearRange:'1990:2021' } );
$('#gallery ul').easyPaginate({ step:6 });
$('#profile').each(function(){
	if(document.URL.indexOf('opponent_team') != -1){
		$('a[href="#stats"]').trigger('click');
	};
});

});
