/* SLIPKNOT */
/* Javascript Functions
----------------------------------------------------------------------------- */


	$(document).ready(function() {

		markup();	
		browser();
		ie();
		
	});
	
	$(window).load(function() {

	//	if (page == "index") cufon();
		if (page == "index") videoPlayer();
		chat();
		chatPopup();
		navigation();
		tabs();
		actions();
		if (page == "index") subscribe();
		slideshows();
		exclusive();
		lastTab();
		noContests();		
		
		if (window.ie6) ie6();
		
	});
	
	
/* FUNCTION: BROWSER DETECT
----------------------------------------------------------------------------- */


	function browser() {
	
		if (navigator.userAgent.match('MSIE 6')) {
		
			browser = "ie6";
			
		} else if (navigator.userAgent.match('MSIE')) {
		
			browser = "ie";
			
		} else if (navigator.userAgent.match('Firefox')) {
		
			browser = "gecko";
			
		} else if (navigator.userAgent.match('AppleWebKit')) {
		
			browser = "webkit";
		
		}
		
		$('body').addClass(browser);

	}
	

	
/* FUNCTION: CUFON
----------------------------------------------------------------------------- */


	function cufon() {

		// Franklin Gothic
		Cufon.replace('#page-index ul.actions a, #referral p', { hover: 'true', fontFamily: 'ITC Franklin Gothic' });
		
		// Reveal Text
		$('#page-index ul.actions a, #referral p').css('visibility', 'visible');
		
	}
	
	
/* FUNCTION: SLIDESHOWS
----------------------------------------------------------------------------- */


	function slideshows() {
	
	
/* News Ticker */

	
		if ($('#ticker').length > 0) {
		
			$('#ticker > ul.headlines').cycle({ 
			fx:      'fade', 
			speed:    400, 
			timeout:  8000
			});
			
			$('#ticker').fadeIn();
		
		}
		

/* Index Page */


		if (page == "index") {
				
			if ($('div.benefits div').length > 0) {

				$('div.benefits div').cycle({
		
					fade: true,
					speed: 400,
					timeout: 6000
				
				});

			}
		
			if ($('#featured_merch #merch > ul.items > li').length > 1) {
			
				$('#featured_merch #merch').append('<a href="#prev" id="prev" class="paging">Previous</a><a href="#next" id="next" class="paging">Next</a>');
				$('#featured_merch #merch > ul.items').cycle({
			
					fade: true,
					speed: 400,
					timeout: 8000000,
					next: '#next',
					prev: '#prev' 
				
				});
				
			}
			
		}
		
		
/* Login Page */

		
		if ($('#page-login div.benefits div').length > 0) {

			$('div.benefits div').cycle({
		
				fade: true,
				speed: 400,
				timeout: 6000
			
			});

		}
		
		
/* Gallery Page */


		if ($('.slideshow').length > 0 && $('.slideshow').find('a.paging').length === 0) {
		
			$('.slideshow').append('<a href="#prev" id="prev" class="paging">Previous</a><a href="#next" id="next" class="paging">Next</a>');
			$('.slideshow > ul.photos').cycle({
		
				fade: true,
				speed: 400,
				timeout: 8000000,
				next: '#next',
				prev: '#prev' 
			
			});
		
		}
	

/* Join Page */


		if (page == "join") {
	
			$('#slides').each(function() {		
				
				// START SLIDESHOW
				$(this).before('<div id="paging">').cycle({
					fx: 'fade',
					speed: 2000,
					timeout: 6500,
					pager: '#paging',
					
				before: function() { if (window.console) console.log(this.src); }
				});
			
			
				// CUSTOMIZE PAGING LINKS
				$('#paging a:nth-child(1)').livequery(function() { $(this).addClass('presales'); });
				$('#paging a:nth-child(1)').livequery(function() { $(this).text('Pre-Sales'); });
	
				$('#paging a:nth-child(2)').livequery(function() { $(this).addClass('meetandgreet'); });
				$('#paging a:nth-child(2)').livequery(function() { $(this).text('Meet & Greets'); });
	
				$('#paging a:nth-child(3)').livequery(function() { $(this).addClass('merch'); });
				$('#paging a:nth-child(3)').livequery(function() { $(this).text('Merch Package'); });
	
				$('#paging a:nth-child(4)').livequery(function() { $(this).addClass('exclusive'); });
				$('#paging a:nth-child(4)').livequery(function() { $(this).text('Exclusive Content'); });
	
				$('#paging a:nth-child(5)').livequery(function() { $(this).addClass('network'); });
				$('#paging a:nth-child(5)').livequery(function() { $(this).text('Network'); });
	
				$('#paging a:nth-child(6)').livequery(function() { $(this).addClass('comments'); });
				$('#paging a:nth-child(6)').livequery(function() { $(this).text('Comments'); });
	
				// UNHIDE SLIDESHOW
				$(this).css('visibility','visible');
				$('#upsell #slides li.slide').css('visibility','visible');
				
			});
			
			$('a#watch').click(function() {

				$('#slides').hide();
				$('div.video').show();
		
			});
			
			$('#paging a').click(function() {

				$('div.video').hide();
				$('#slides').show();
		
			});
		
		};
		
	}
	
	
/* FUNCTION: NAVIGATION DROP DOWN MENUS
----------------------------------------------------------------------------- */


	function navigation() {
	
		$('#navigation > li > div').each(function() {
		
			$(this).parent().hover(function() {
			
				$(this).addClass('selected');
				$(this).children('div').children('ul').stop();
				$(this).children('div').children('ul').animate({ top: '0' }, 200);
				$('#ticker').fadeOut('fast');
			
			}, function() {
			
				$(this).children('div').children('ul').stop();
				$(this).children('div').children('ul').animate({ top: '-39px' }, 200, function() {
				
					$(this).parent().parent().removeClass('selected');
				
				});
				$('#ticker').fadeIn();

			});
		
		});
		
		$('#navigation li.shop a').click(function() {

			window.open('http://www.slipknotmerch.com');
			window.location('/store');
			return false;

		});
	
	}
	
	
/* FUNCTION: TABS
----------------------------------------------------------------------------- */


	function tabs() {
	

/* Javascript */


		$('.tabbed:not(.ajax):not(.static) > ul.tabs').tabs({
		
			fxSlide: false,
			click: function() {
			
				/*
				// STOP VIDEO FROM PLAYING IN IE WHEN TABS ARE CLICKED
				$('#page-index #features .widget-media div.flash > div').each(function() {
					
					var instance = $(this).attr('class').replace('flash ', '');
					document[instance].pauseClip();
				
				});
				*/
			
			}, load: function(e, ui) {
			
				if ($('.slideshow').length > 0 && $('.slideshow').find('a.paging').length === 0) {
		
					$('.slideshow').append('<a href="#prev" id="prev" class="paging">Previous</a><a href="#next" id="next" class="paging">Next</a>');
					$('.slideshow > ul.photos').cycle({
				
						fade: true,
						speed: 400,
						timeout: 8000000,
						next: '#next',
						prev: '#prev' 
					
					});
				
				}
		
				$('.tabbed .tabbed > ul.tabs').tabs({
		
					fxSlide: false
			
				});

			}
			
		});
		
		
/* AJAX */

		
		$('.tabbed.ajax > ul.tabs').tabs({
			
			fxSlide: false,
			
			click: function(e, ui) {	
			
				loading('start', e);
				
			}, load: function(e, ui) {

				subTabs();
			//	markup(); Removed until markup function can be refined enough to not hit EVERYTHING 2x
				exclusive();
				if (window.ie6) { ie6() }
				loading('stop');

			}, show: function(e, ui) {
			
				subTabs();
			//	markup();
				if (window.ie6) { ie6() }
				exclusive();
				loading('stop');

			}
	
		});
		
		
/* Static */


		$('.tabbed.static').children('ul.tabs').children('li').children('a').click(function() {
		
			$(this).addClass('loading');
			
		});
		
	
/* Fix Static Paging */
	
	
		if (tab) $('.tabbed.static ul.paging a').each(function() { $(this).attr('href', $(this).attr('href') + '&tab=' + tab) });
		
		
/* Fix Javascript Paging */
	

		$('.tabbed:not(.ajax):not(.static) ul.paging a').each(function() { 
			
			var pane = $(this).parents('div.pane').attr('id'); 
			$(this).attr('href', $(this).attr('href') + '#' + pane); 
		
		});
	
		
/* Fix AJAX Paging */
	

		$('.tabbed.ajax ul.paging a').livequery('click', function(event) {
		
			$(this).closest('div.tabbed.ajax > div.pane').load($(this).attr('href'));	
			return false;

		});

		
/* Markup Changes */


		// IDENTIFY SINGLE-SECTIONED PANES
		$('.pane > div:only-child').addClass('loner');
		
		
/* Center Tabs */


		$('.tabbed:not(.pane .tabbed) > ul.tabs').each(function() {
		
			var containerWidth = $(this).width();
			var tabsWidth = 0;
		
			$(this).children().each(function() {
			
				tabsWidth += $(this).width();
			
			});
			
			var paddingLeft = (containerWidth - tabsWidth) / 2;
			
			$(this).css('padding-left', paddingLeft + 'px');
			$(this).css('width', containerWidth - paddingLeft + 'px');
		
		});
	
		
/* Unhide Tabs */


		$('ul.tabs').css('visibility', 'visible');

	
	}

	
/* FUNCTION: SUB-TABS
----------------------------------------------------------------------------- */


	function subTabs(where) {
	
	
/* Javascript */


		$('.tabbed.ajax .tabbed:not(.ajax):not(.static) > ul.tabs').tabs({
		
			fxSlide: false
			
		});
		
		
/* Markup Changes */


		$('.pane > div:only-child').addClass('loner');
		
		
	}
	
	
/* FUNCTION: LOADING INDICATOR
----------------------------------------------------------------------------- */


	function loading(event, e) {
	
		if (event == "start") {
		
			// SHOW LOADING INDICATOR
			var tab = e.toString().split('#').pop();
			$('ul.tabs a[href=#' + tab).addClass('loading');
	
		}
	
	}
	

/* CHAT PROMPT
--------------------------------------------------------------------------- */

	
	function chat() {
				
		$('#dim').click(function() {
			
			$('#chat .prompt.chat').show();
			$(this).addClass("dim");
			
		});
				
	}


/* FUNCTION: VIDEO PLAYER
----------------------------------------------------------------------------- */


	function videoPlayer() {
	
		$('div.widget-media.player.video').each(function() {
		
			var $this = $(this);
		
			$('#features').hover(function() {
		
				$this.children().stop();
				$this.children('ul.controls').animate({ bottom: 0 });
				$this.children('div.details').animate({ top: 0 });
				
				// Internet Explorer
				if (navigator.userAgent.match('MSIE')) {
				
					$this.find('div.flash').children('div').animate({
					
						height: '206px',
						marginTop: '50px'
					
					});
				
				}

			}, function() {
			
				$this.children().stop();
				$this.children('ul.controls').animate({ bottom: '-38px' });
				$this.children('div.details').animate({ top: '-29px' });

				// Internet Explorer
				if (navigator.userAgent.match('MSIE')) {

					$this.find('div.flash').children('div').animate({
					
						height: '247px',
						marginTop: '0'
					
					});
				
				}

			});
			
		});
	
	}
	

/* CHAT PROMPT
--------------------------------------------------------------------------- */
	
	
	function chatPopup() {
	
		var embed = $('#chat table');
		
		$('a.launchchat').click(function(){
		
			$('div.widget-chat').addClass('dim');
			$('#features #chat').append('<a class="reembed" href="#">Re-embed Chat Here</a>');
			$('#chat table').remove();
			$('a.launchchat').hide();

			window.open( site_host + '/chat-popup','Chat','width=750,height=550,resizable=0,scrollbars=0,toolbar=0,location=0,directories=0,status=0,menubar=0');
			
			$('a.reembed').click(function(){

				$('#features #chat').prepend(embed);
				$('a.reembed').remove();
				$('a.launchchat').show();

			});
			
			return false;
			
		});
		
		$('#chat-popup a.closechat').click(function(){
	
			window.close();
			
		});
		
	}
	
	
/* FUNCTION: MARKUP CHANGES
----------------------------------------------------------------------------- */


	function markup() {


/* Add Container to Display Widget */


		$('.widget-editorial.display, .widget-event.display, .widget-blog.display, .widget-blog.modify, .widget-photo.display, #page-friend .widget-member.display').each(function() {
		
			if ($(this).find('div.content').length == 0) $(this).children('*:not(h3, .actions, a:not(.picture))').wrapAll('<div class="content"></div>');
		
		});
		

/* Tag Browser */

		
		$('.widget-tag.browse > ul').children('li:first-child').each(function() {
		
			if (tag == "all" || !tag) $(this).children('a').wrap('<strong></strong>');
			
		});
		

/* Network Header */


		$('#networkheader').each(function() {
		
			$('#networkheader > #site > .display').append('<div class="mask"></div>');
			
		});
	

/* Remove Empty Tabs */

		
		// SUBTABS FIRST
		$('.tabbed .tabbed > ul.tabs').children('li').each(function() {
		
			var pane = $(this).find('a').attr('href');
			if ($(this).parent().siblings(pane).children('*').length == 0) {
			
				$(this).parent().siblings(pane).remove();
				$(this).remove();

			}
	
		});
		
		// REMOVE ENTIRE TABBED PANEL IF EMPTY
		$('.tabbed').children('ul.tabs').each(function() {
		
			if ($(this).children('li').length == 0) $(this).parent().remove();
	
		});
		

/* Fix upload artwork button */


		$('#page-page #artwork a.upload').each(function() {
		
			if (!$(this).attr('href').match('rtwork')) $(this).attr('href', $(this).attr('href') + '/artwork');
			
		});
		

/* Rank Badges */


		$('ul.leads > li > div.member').each(function() {
		
			var groups = $(this).children('em').text();
			
			if (groups.match('Draftees')) {
				
				$(this).addClass('draftees');
				
			} else if (groups.match('Year 3')) {
	
				$(this).addClass('year_3');
			
			} else if (groups.match('Year 2')) {
			
				$(this).addClass('year_2');

			} else if (groups.match('Year 1')) {
				
				$(this).addClass('year_1');

			}
		
		});


/* Home Page */


		if (page === "index") {
		
			$('#online a.action.browse').text('Meet More Maggots');
			$('#latest_news a.action.browse').text('See More News');
			$('#news a.action.browse').text('Browse News Archive');
			$('#network #photos a.action.browse').text('View All Submissions');

		}

	}
	

/* MAILING LIST
----------------------------------------------------------------------------- */

	
	function subscribe() {

		$("#subscribe #mailing_list_entry_email").val("E-mail Address").attr("title", "E-mail Address");
		$("#subscribe input[type=text]").click(function() {
			
			var label = $(this).attr("title");
			if (label == $(this).val()) {
				
				$(this).val("");
				
			}
				
		});
	
		$("#subscribe #mailing_list_entry_mobile").val("Mobile Number").attr("title", "Mobile Number");
		$("#subscribe input[type=text]").click(function() {
			
			var label = $(this).attr("title");
			if (label == $(this).val()) {
				
				$(this).val("");
				
			}
				
		});

		$("#subscribe input[type=text]").blur(function() {
		
			var label = $(this).attr("title");
			if ($(this).val() == "") {
			
				$(this).val(label);
			
			}
			
		});
		
	}
	
	
/* FUNCTION: ACTION BEHAVIOR
----------------------------------------------------------------------------- */


	function actions() {


/* Go Back */


		$('a[href=#back]').click(function() {
		
			history.go(-1);
			return false;
		
		});


/* Change Member Status */


		$('#member a.change').click(function() {
		
			$('#member').find('#status-modify').fadeIn();
			$(this).fadeOut(); 
			
			return false;
		
		});
		
		
/* Change Network Header */


		$('#site a.action.change, #site a.action.image').click(function() {
		
			$('#site div.picture p').text('Replace image:');			
			$('#site').find('#network-modify').fadeIn();
			$(this).parent().parent().hide(); 
			$('#site > div.display > *:not(img, .mask)').fadeOut();
			$('div.mask').addClass('modify');
			
			// TITLE PLACEHOLDER TEXT
			var title = "Title";
			if ($('input#title').attr('value') == "") $('input#title').attr('value', title);
			$('input#title').focus(function() { if ($(this).attr('value') == title) $(this).attr('value', '') });
			$('input#title').blur(function() { if (!$(this).attr('value')) $(this).attr('value', title) });
			
			// TAGLINE PLACEHOLDER TEXT
			var tagline = "Tagline";
			if ($('input#description').attr('value') == "") $('input#description').attr('value', tagline);
			$('input#description').focus(function() { if ($(this).attr('value') == tagline) $(this).attr('value', '') });
			$('input#description').blur(function() { if (!$(this).attr('value')) $(this).attr('value', tagline) });
			
			return false;
		
		});
		
		$('#site input[type=file]').change(function() {
		
			$('div.widget-network').parent().submit();
			
		});

	
/* Comment Form Toggle */


		$('a.prompt.post').toggle(function() {

			$(this).parent().find('div.modify').slideDown(function() {
			
				$(this).find('textarea').focus();		
			
			});
		
		}, function() {
		
			$(this).parent().find('textarea').blur();
			$(this).parent().find('div.modify').slideUp();
		
		});
		
		
/* Close Alerts */

	
		$("#alert a.close").click(function () {
		
			$("#alert").fadeOut("slow");
			return false;
			
		});
    
	
	}


/* FUNCTION: NAVIGATION DROP DOWN MENUS
----------------------------------------------------------------------------- */


	function exclusive() {
	
		if (page == "index") {
			
			$('#news').find('h4').children('a[href*="OT9_Exclusive"]').each(function() {
				
				$(this).parent().parent().addClass('exclusive');
				
			});
			
		}

	}
	

/* FUNCTION: IE RANK MEDAL TRANSPARENCY
----------------------------------------------------------------------------- */


	function ie() {
	
		$('body.ie6 img.badge').each(function() {

			$(this).attr("style", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + $(this).attr('src') + "',sizingMethod=\'scale\');");
			$(this).attr('src', asset_host + '/images/badges/badge-transparent-lg.gif');	
		
		});

	}
	
	
/* FUNCTION: ADD CLASS TO LAST TAB
--------------------------------------------------------------------------- */


	// Since IE doesn't use :last-child

	function lastTab() {
		
		$('div.tabbed ul.tabs li:last-child').addClass('last');
		
	}
	
	
/* FUNCTION: NO CONTESTS MESSAGE
--------------------------------------------------------------------------- */


	function noContests() {
		
		if (page == "network" && $('#contest').length == 0 && (!window.location.href.match('tab=') || window.location.href.match('tab=stories'))) {
		
			$('#main').prepend('<div id="contest" class="none"><h3>There are No Contests Running at this Time</h3></div>');
		
		}
		
	}