/***********************************************
***  BUTTON ROLLOVER & PRELOAD  ****************
************************************************/

$(function(){
	// button rollover
	if ($('.imgBtn') != null) {
		$('.imgBtn').hover(function(){
				if(!$(this).is('.disabled'))
					$(this).attr("src", $(this).attr("src").replace(/^(.*?)(?:-hover)?(\..*?)$/i,'$1-hover$2'));
			},
			function(){
					if(!$(this).is('.disabled'))
						$(this).attr("src", $(this).attr("src").replace(/^(.*)-hover(\..*?)$/i, '$1$2'));
				});
	}
	// preload image rollovers
	if ($("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']") != null) {
	    $("img.imgBtn, img[src^='images/buttons/btn'], input[src^='images/buttons/btn']").each(function(){
			    $("<img>").attr("src", $(this).attr("src").replace(/(btn-.*)(\..*?)/i, '$1-hover$2'));
		    });
	}
});

/***********************************************
***  EXTERNAL LINKS  ***************************
************************************************/

$(document).ready(function() {
	$("a[rel=external]").each(function() {
		$(this).addClass('external');
		$(this).attr({
			title: "Open this page in a new window",
			target: "_blank"
		});
	})
});

/***********************************************
***  CUFON  ************************************
************************************************/

$(function() {
	Cufon.replace('#menu a', { hover: true });
	Cufon.replace('#rules h2');
	Cufon.replace('#about h2');
	Cufon.replace('#leaderboard h2', { hover: true });
	Cufon.replace('#entryForm h1');
	Cufon.replace('#wallLayer h2');
	Cufon.replace('#wallLayer h3');
	Cufon.replace('#sidebar .feature h2');
	Cufon.replace('#prizes h2');
	Cufon.replace('.termsConditions h1');
    	Cufon.now();
});

/***********************************************
***  MENU STATES  ******************************
************************************************/

$(document).ready(function() {
	$("#menu a").click(function(){
			$(this).parent().parent().children('li').removeClass('selected');
			$(this).parent().addClass('selected');
			Cufon.refresh('#menu a');
		});
});

/***********************************************
***  GEE CAROUSEL  *****************************
************************************************/
/*
$(function(){
	$('.feature').geeCarousel({
		wrapperID: '.feature',
		prevBtnID: '.featureArrowLeft',
		nextBtnID: '.featureArrowRight',
		animateDuration: 200
	});
});
*/

$(function(){
		$('.featureFrames > li:not(:first)').addClass('hidden').css({
				'display': 'none'
			});
		$('.feature a').click(function(){
				if($(this).is('.current'))
						return false;
				$('.current').removeClass('current');
				$(this).addClass('current');
				var n = $(this).text()
				$('.featureFrames > li:not(.hidden)').fadeOut('medium', function(){
						$(this).addClass('hidden');
						$('.featureFrames > li:nth-child(' + n + ')').removeClass('hidden').fadeIn('medium');
					});
				return false;				
			});
	});


/***********************************************
***  VIDEO WALL  *******************************
************************************************/

$(document).ready(function() {
		$('#wallLayer .closeBtn').click(function() {
				$('#wallLayer').hide('slow');
				$("#menu .selected").removeClass('selected');
				Cufon.refresh('#menu a');
				return false;
			});
	});


/***********************************************
***  MENU  *************************************
************************************************/

$(document).ready(function() {
	var arrPage = [{
			'id': '',
			'btn': '#menuHome'
		},{
			'id': '#about',
			'btn': '#menuAbout'
		},{
			'id': '#rules',
			'btn': '#menuRules'
		},{
			'id': '#prizes',
			'btn': '#menuPrizes'
		}];

	var current = 0;
	$('#menu #menuHome a').click(function() {
			if(current !== 0){
				scrollOut(arrPage[current].id, 'down');
				current = 0
			}
			return false;
		});
	$('#menu #menuAbout a').click(function() {
			if(current !== 1){
				scrollOut(arrPage[current].id, (current < 1)?'up':'down');
				$('#about').scrollTop(0);
				scrollIn('#about', (current < 1)?'up':'down');
				current = 1;
			}
			return false;
		});
	$('#menu #menuRules a').click(function() {
			if(current !== 2){
				scrollOut(arrPage[current].id, (current < 2)?'up':'down');
				$('#rules').scrollTop(0);
				scrollIn('#rules', (current < 2)?'up':'down');
				current = 2;
			}
			return false;
		});
	$('#menu #menuPrizes a').click(function() {
			if(current !== 3){
				scrollOut(arrPage[current].id, (current < 3)?'up':'down');
				$('#prizes').scrollTop(0);
				scrollIn('#prizes', (current < 3)?'up':'down');
				current = 3;
			}
			return false;
		});
	function scrollOut(id, direction){
		$(id).stop().animate({
				top: (direction === 'up')?'-400px':'500px'
			}, 400, "swing")
	}
	function scrollIn(id, direction){
		$(id).stop().css({
				top: (direction === 'up')?'500px':'-400px'
			}).animate({
					top: '74px'
				}, 400, "swing")
	}
	$('#leaderboard .viewAll, #menu #menuGallery a').click(function() {
			if(current !== 0){
				scrollOut(arrPage[current].id, 'down');
				current = 0
			}
			$('.videoWall').addClass('loading');

			var currentWallPage = 0;

			function wallLoaded(data, textStatus, XMLHttpRequest){
				$('.videoWall .videos, .videoWall .paging').remove();
				$('.videoWall').append(data).removeClass('loading');
				$('.videos a').click(function(){
						if($.browser.msie && jQuery.browser.version === '6.0'){
							$.fn.colorbox({
									href: 'obsolete-browser.asp',
									innerWidth:"300px",
									innerHeight:"300px",
									initialWidth: "1px",
									initialHeight: "1px",
									iframe: true
								});
						}
						else{
							$.fn.colorbox({
									href:$(this).attr('href').replace(/watch\?v=/, 'v/'), 
									iframe:true, 
									innerWidth:425, 
									innerHeight:344,
									onComplete: function(){
											$('#colorbox').addClass('videoIbox');
										},
									onClosed: function(){
											$('#colorbox').removeClass('videoIbox');
										}
									});
						}
						return false;
					});
				$('.videoWall .paging a').click(function(){
						if($(this).is('.prev')){
							currentWallPage--;
						}
						else if($(this).is('.next')){
							currentWallPage++;
						}
						else{
							currentWallPage = parseInt($(this).text()) -1;
						}
						$.ajax({
								'url': 'show-wall.asp?p=' + currentWallPage,
								'success': wallLoaded
							});
						$('.videoWall').addClass('loading');
						return false;
					});
				$(".videos li:not(.preLoad) img[alt]").each(function(){
						var trigger = $(this);
						trigger.tooltip({
								tip: '.leaderboardPopup', 
								offset: [20, -45],
								effect: 'slide',
								predelay:200,  
								delay:200,
								lazy: false,
								onBeforeShow: function(){
										var a = trigger.attr('alt').match(/\((.*)\) Name: (.*?) Time: (\d+ Seconds)$/);
										if(!a)
											return;
										$('.leaderboardPopup .name').text(a[2]);
										$('.leaderboardPopup .time').text(a[3]);
										$('.leaderboardPopup .position').text(a[1]);
									}
							});
					});
				$(".videos li.preLoad img[alt]").each(function(){
						var trigger = $(this);
						trigger.tooltip({
								tip: '.leaderboardPreLoadPopup', 
								offset: [20, -45],
								effect: 'slide',
								predelay:200,  
								delay:200,
								lazy: false,
								onBeforeShow: function(){
										$('.leaderboardPreLoadPopup .content').text(trigger.attr('alt'));
									}
							});
					});
			}
			$.ajax({
					'url': 'show-wall.asp',
					'success': wallLoaded

				});
			$('#wallLayer').show('slow');
			return false;
		});


		if($.browser.msie && jQuery.browser.version === '6.0'){
			$("#menuCompete a").click(function(){
					$.fn.colorbox({
							href: 'obsolete-browser.asp',
							innerWidth:"300px",
							innerHeight:"300px",
							initialWidth: "1px",
							initialHeight: "1px",
							iframe: true
						});
					return false;
				});
		}
		else{
			$("#menuCompete a").colorbox({
					href: 'terms-conditions-accept.asp',
					innerWidth:"600px",
					innerHeight:"425px",
					iframe:true,
					initialWidth: "1px",
					initialHeight: "1px",
					onComplete: function(){
							if(current !== 0){
								scrollOut(arrPage[current].id, 'down');
								current = 0
							}
							$('#colorbox').addClass('showingTerms');
						},
					onClosed: function(){
							$('#colorbox').removeClass('showingTerms');
						}
				});
		}

		$(".release a").colorbox({
				href: 'terms-conditions.asp',
				innerWidth:"600px",
				innerHeight:"425px",
				iframe:true,
				initialWidth: "1px",
				initialHeight: "1px",
				onComplete: function(){
						$('#colorbox').addClass('showingTerms');
					},
				onClosed: function(){
						$('#colorbox').removeClass('showingTerms');
					}
			});
		$(".uploadTile").colorbox({
				innerWidth:"498px",
				innerHeight:"658px",
				iframe:false,
				scalePhotos: false,
				initialWidth: "1px",
				initialHeight: "1px",
				onComplete: function(){
						$('#colorbox').addClass('showingImage');
						$('#cboxLoadedContent').append('<a href="http://www.ifoce.com/news.php?action=detail&amp;sn=779" target="_blank" style="position: absolute; top: 55px; left: 76px; width: 140px; height: 61px; display: block; background: url(images/icons/spacer.gif)"></a>').css({
								'position': 'relative'
							});
					},
				onClosed: function(){
						$('#colorbox').removeClass('showingImage');
					}
			});
		
		$(document).bind('cbox_load', function(){
				$('#cboxClose').hide();
			});
		$(document).bind('cbox_complete', function(){
				$('#cboxClose').show();
			});
		$(document).bind('cbox_closed', function(){
				$('#cboxClose').show();
				$("#menu .selected").removeClass('selected');
				Cufon.refresh('#menu a');
			});
});

/***********************************************
***  COMPETE IBOX  *****************************
************************************************/

$(function(){
	$("#uploadVideo").prettyfile({
			html: "<span class='pf_ph'>&nbsp;</span><span class='uploadBtn'>Browse</span>"
		});
});


/***********************************************
***  EXPOSE  ***********************************
************************************************/


$(function() { 
		var vcApi = $('#videoContainer').expose({api: true, color: '#000', onClose: function(){
					$('#videoContainer').hide();
			}});
		$("#videoContainer .closeBtn").click(function() { 
				$(this).parent().hide();
					vcApi.close();
				return false;
			}); 

		$(".leaderboardThumb a, a.loadVid").click(function() {
				if($.browser.msie && jQuery.browser.version === '6.0'){
					$.fn.colorbox({
							href: 'obsolete-browser.asp',
							innerWidth:"300px",
							innerHeight:"300px",
							initialWidth: "1px",
							initialHeight: "1px",
							iframe: true
						});
					return false;
				}
				else{
					pageTracker._trackPageview($(this).attr('href').replace(/watch\?v=/, 'v/'));
					$('#videoPlayer').html(
							'<iframe style="height: 381px; width: 581px" frameborder="0" src="' + $(this).attr('href').replace(/watch\?v=/, 'v/') + '?version=3&autoplay=1&color1=0xb1b1b1&color2=0xcfcfcf&feature=player_embedded"/>'
						);
				}
				$('#videoContainer').show();
				vcApi.load();
				return false;
			});
	});


/***********************************************
***  TOOLTIP  **********************************
************************************************/

$(document).ready(function() {
	$(".leaderboardScroll .leaderboardThumb img[alt]").each(function(){
			var trigger = $(this);
			trigger.tooltip({
					tip: '.leaderboardPopup', 
					offset: [20, -45],
					effect: 'slide',
					predelay:200,  
					delay:200,
					lazy: false,
					onBeforeShow: function(){
							var a = trigger.attr('alt').match(/\((.*)\) Name: (.*?) Time: (\d+ Seconds)$/);
							$('.leaderboardPopup .name').text(a[2]);
							$('.leaderboardPopup .time').text(a[3]);
							$('.leaderboardPopup .position').text(a[1]);
						}
				});
		});
});

/***********************************************
***  COMPETE  **********************************
************************************************/

function acceptClicked(){
	$.fn.colorbox({
			href: 'upload-form.asp',
			innerWidth:"300px",
			innerHeight:"425px",
			iframe:true,
			initialWidth: "1px",
			initialHeight: "1px",
			onComplete: function(){
					$('#colorbox').removeClass('showingTerms');
				}
		});
}

/***********************************************
***  SWF OBJECT  *******************************
************************************************/

swfobject.embedSWF("swf/7ES0899_Sports_Slurping_611x81_v02.swf", "headerBannerFlash", "611", "81", "8.0.0", "", {}, {wmode : "transparent"}, {});
