// CCC Global Object
 if (typeof CCC == "undefined" || !CCC) {
    var CCC = {};
}

// Legacy Browser Rejection
CCC.LegacyBrowsers = {
	init: function() {	
		
		$.reject({
			display: ['firefox','chrome','msie','safari'], // What browsers to display and their order  
			browserInfo: { // Settings for which browsers to display  
				firefox: {  
					text: 'Firefox 6+', // Text below the icon  
					url: 'http://www.mozilla.com/firefox/' // URL For icon/text link  
				},  
				safari: {  
					text: 'Safari 5+',  
					url: 'http://www.apple.com/safari/download/'  
				},  
				chrome: {  
					text: 'Chrome',  
					url: 'http://www.google.com/chrome/'  
				},  
				msie: {  
					text: 'Internet Explorer 8+',  
					url: 'http://www.microsoft.com/windows/Internet-explorer/'  
				}  
			},  
			header: 'Your Internet Explorer Browser is out of date', // Header of pop-up window  
			paragraph1: 'Your browser is out of date, and is not supported by our website. A list of the most popular web browsers can be found below.', // Paragraph 1  
			close: true, // Allow closing of window  
			closeMessage: 'By closing this window you acknowledge that your experience on this website will be degraded' // Message displayed below closing link  
		});
	}
}

// CCC Main Navigation
CCC.MainNavigation = {
    state: null,
    init: function() {
		// Activate all second-level menus
		$("#mainNav li.firstCMSListMenuLI, #mainNav li.firstCMSListMenuLIfirst, #mainNav li.firstCMSListMenuLIlast, #mainNav li.firstCMSListMenuHighlightedLI, #mainNav li.firstCMSListMenuHighlightedLIfirst, #mainNav li.firstCMSListMenuHighlightedLIlast").mouseenter(function() {
			$(this).children("ul").show();
			$(this).children("a").addClass("selected");
		}).mouseleave(function() {
			$(this).children("ul").hide();
			$(this).children("a").removeClass("selected");
		});
		
		// Adjust positioning for menus which extend beyond right side of container
		var navRightOffset = $("ul#menuElem").offset().left + $("ul#menuElem").width();
		$("#mainNav ul.secondCMSListMenuUL").each(function() {
			var relativeLeftPos = $(this).parent().offset().left - $(this).parent().parent().offset().left;
			if((relativeLeftPos + $(this).width()) > 960) {
				$(this).css('margin-left', navRightOffset+5 - $(this).parent().offset().left - $(this).width());
			}
		});
		
		/*
		// Add right-arrow pointers to all second-level menus with child menus
		$.each($("#mainNav ul.secondCMSListMenuUL li"), function() {
			if($(this).children("ul").length > 0) {
				$(this).children("a").addClass('submenu');
			}
		});
		
		// Adjust 'top' positions of third-level menus
		$("#mainNav ul.secondCMSListMenuUL li").mouseenter(function() {
			$(this).children("ul").css('top', $(this).position().top);
		});
		
		// Adjust 'left' positions of third-level menus
		$.each($("#mainNav ul.thirdCMSListMenuUL"), function() {
			$(this).css('left', $(this).parent().parent().width()-10);
		});
		*/
	}
};

CCC.AthletePhotoGallery = {
	init: function(athlete_tag) {
		// Declare variables
		var apiKey = '704e6866ae352c0c3314e76493d8621d';
		var userId = '64117730@N05';
		
		// Make AJAX call to flickr
		$('ul#AthletePhotoGallery').jflickrfeed({
			limit: 15,
			feedapi: 'photos_public.gne',
			qstrings: {
				id: '64117730@N05',
				tags: athlete_tag,
				tagmode: 'any'
			},
			itemTemplate: '<li><a class="fancybox" rel="group" title="{{title}}" href="{{image}}"><img alt="{{title}}" src="{{image_s}}" /></a></li>'
		}, function(data) {
			if(data.items.length == 0) {
				$('div.AthletePhotoGallery').html("No photos found. Check out our full Flickr photo gallery at <a href=\"http://www.flickr.com/photos/cccski\">www.flickr.com/photos/cccski</a>.");
			}
			else {
				$('a.fancybox').fancybox();
				$('div.flickrLink').html('<a target="_blank" class="btn_flickr" href="'+data.link+'">View full collection on athlete photos on Flickr</a>');
				//console.log(data);
			}
		});
	}
};


CCC.HomeFlickrWidget = {
	init: function(athlete_tag) {
		// Declare variables
		var apiKey = '704e6866ae352c0c3314e76493d8621d';
		var userId = '64117730@N05';
		
		// Make AJAX call to flickr
		$('ul.HomeFlickrPhotos').jflickrfeed({
			limit: 8,
			feedapi: 'photos_public.gne',
			qstrings: {
				id: '64117730@N05'
			},
			itemTemplate: '<li><a class="fancybox" rel="group" title="{{title}}" href="{{image}}"><img alt="{{title}}" src="{{image_s}}" /></a></li>'
		}, function(data) {
			$('a.fancybox').fancybox();
			$('div.flickrLink').html('<a target="_blank" class="btn_flickr" href="'+data.link+'">View full collection on athlete photos on Flickr</a>');
			
		});
	}
};

CCC.RacingNewsFlickrGallery = {
	init: function(athlete_tag) {
		// Declare variables
		var apiKey = '704e6866ae352c0c3314e76493d8621d';
		var userId = '64117730@N05';
		
		// Make AJAX call to flickr
		$('ul.flickrGallery').jflickrfeed({
			limit: 8,
			feedapi: 'photos_public.gne',
			qstrings: {
				id: '64117730@N05'
			},
			itemTemplate: '<li><a class="fancybox" rel="group" title="{{title}}" href="{{image}}"><img alt="{{title}}" src="{{image_s}}" /></a></li>'
		}, function(data) {
			if(data.items.length == 0) {
				$('div.RacingNewsFlickrGallery').html("No photos found. Check out our full Flickr photo gallery at <a href=\"http://www.flickr.com/photos/cccski\">www.flickr.com/photos/cccski</a>.");
			}
			else {
				$('a.fancybox').fancybox();
				//console.log(data);
			}
		});
	}
};

CCC.Badges = {
	init: function() {
	}
}

CCC.InTouchMediaPlayer = {
	open: function() {
		
		// Add a link which will trigger the media player
		$('body').append('<a class="inmedia iframe" href="http://fisplayer.infrontams.tv/cross-country/">InTouch Media Player</a>');
		$('a.inmedia').fancybox({
			height: 400,
			width: 1005
		});
		
		// Trigger the link and hence the fancybox
		$('a.inmedia').trigger('click');
		
		// Finally remove the link
		$('a.inmedia').remove();
	}
}


// Start!
$(document).ready(function() {
	// Legacy browser rejection
	CCC.LegacyBrowsers.init();
	
	CCC.MainNavigation.init();
});
