/* O.A.R. */
/* Javascript
-------------------------------------------------------------------------------- */


	$(document).ready(function() {
				
		hideWhileLoading();		

	});

	$(window).load(function() {
		
		navigation(); // Sliding navigation
		links(); // Link behaviors
		flash(); // Flash embeds
		prep(); // Misc stuff
		
	}); 
	
	
/* FUNCTION: HIDE WHILE LOADING
----------------------------------------------------------------------------- */


	function hideWhileLoading() {
	
		// PHOTO PAGE
		if (page == "photo") $('#content').css('visibility','hidden');
		
		// ALERTS
		if ($('#layout > .widget-alert').length > 0) $('#promo').hide();
		
		// COMMENT
		$('#comments .widget-comment.modify').hide();
		
		// DATES
		$('.widget-event.program ul.events li > strong, .widget-editorial li > strong, .widget-blog li .post > strong, .widget-blog li > strong').hide();
	
	}


/* FUNCTION: NAVIGATION
----------------------------------------------------------------------------- */


	function navigation() {
	
		// BROWSER DETECT
		var ie = (navigator.userAgent.match('MSIE') ? true : false);
		
		if (!ie) {


/* Section Positions */


			var home = $('div.section-home').position();
			var band = $('div.section-band').position();
			var music = $('div.section-music').position();
			var video = $('div.section-video').position();
			var photos = $('div.section-photos').position();
			var tour = $('div.section-tour').position();
			var community = $('div.section-community').position();
			var widget = $('div.section-widget').position();
			var links = $('div.section-links').position();
			var account = $('div.section-account').position();
			var contact = $('div.section-contact').position();
			var legal = $('div.section-legal').position();
			var tv = $('div.section-tv').position();
			

/* Build Content Area */

		
			// GET CONTENT
			var pageContent = $('#holder').html();
			$('#holder').remove();
			
			// PLACE CONTENT IN CORRECT SECTION
			$('div.section-' + section).html(pageContent);
			
			// MOVE CONTENT AREA TO SHOW CURRENT SECTION
			var top = eval(section).top * -1;
			var left = eval(section).left * -1;
			$('#content').css('margin-top',top + 'px');
			$('#content').css('margin-left',left + 'px');
		
		
/* Navigation */


			// ONLOAD SELECTED SECTION INDICATOR
			$('body.section-home #navigation .home a, body.section-band #navigation .band a, body.section-video #navigation .video a, body.section-photos #navigation .photos a, body.section-tour #navigation .tour a, body.section-community #navigation .community a, body.section-contact #navigation .contact a, body.section-links #navigation .links a, body.section-music #navigation .music a, body.section-tv #navigation .tv a').addClass('selected');
			

/* Animation */


			if (subdomain != true) {
			
				$('a[rel=slide]').click(function() {
		
					// CHANGE SECTION INDICATOR
					$('#navigation a').removeClass('selected');
					$(this).addClass('selected');
						
					// LOADING NOTIFICATION
					$('em.loading').remove(); 
					$(this).parent().append('<em class="loading">Loading...</em>'); 
					$(this).ajaxStop(function() { $('em.loading').remove(); }); 
				
					// GET SELECTED SECTION
					var selectedSection = $(this).parent().attr('class');
					var selectedPage = $(this).attr('href').replace(site_host, '') + '?ajax=true';
					
					// RESET SECTION VARIABLE
					section = $(this).parent().attr('class');
					
					$('div.section-' + selectedSection).load(selectedPage, function(data) {
				
						// PREP NEWLY-LOADED PAGE BEFORE ANIMATION STARTS
						preSlide();
						
						// KILL LOADING INDICATOR
						$('em.loading').remove();
						$(this).css('cursor', 'pointer');
						
						// GET NEW CONTENT POSITION
						var top = eval(selectedSection).top * -1;
						var left = eval(selectedSection).left * -1;
						
						// ANIMATE
						$('#content').animate({
						marginTop: top,
						marginLeft: left
						}, 500, function() {
						
							postSlide(selectedSection);
		
						});
										
					});
					
					return false;
					
				});
			
			}
		
		
/* Normal page loads for IE */


		} else {
		
			// TURN TEMPORARY 'HOLDER' DIV INTO PERMANENT CONTENT AREA AND REMOVE SECTIONS
			$('#content div.section').remove();
			$('#holder').addClass('section');
			$('#holder').addClass('section-' + section);
			
			// ONLOAD SELECTED SECTION INDICATOR
			$('body.section-home #navigation .home a, body.section-band #navigation .band a, body.section-video #navigation .video a, body.section-photos #navigation .photos a, body.section-tour #navigation .tour a, body.section-community #navigation .community a, body.section-contact #navigation .contact a, body.section-links #navigation .links a, body.section-music #navigation .music a, body.section-tv #navigation .tv a').addClass('selected');
		
		}
	
	}
	
	
/* FUNCTION: FLASH
----------------------------------------------------------------------------- */
	
	
	function flash() {
	
		
/* Top Banner */


		if ($('#promo').length > 0) {
			
			var so = new SWFObject(asset_host + "/flash/banner.swf?placement=top&xml=http://www.ofarevolution.com/feeds/banners", "top", "100%", "100%", "9", "#000000");
			so.addParam("wmode", "transparent");
			so.write("promo");
		
		}
		
	}


	
/* FUNCTION: LINK BEHAVIORS
----------------------------------------------------------------------------- */
		
	
	function links() {


/* External Links */


		$('a[rel=external]').click(function() {
		
			link = $(this).attr('href');
			window.open(link);
			
			return false;
	
		});
		
		
/* Audio Player Popup */

		
		$('a#audio').click(function() {

			var link = $(this).attr('href');
			window.open(link, 'audio', 'location=0, status=0, scrollbars=0, width=400, height=207');
			return false;

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


	function player_home(clip) {

		var clip = clip;

		$('div#player_home').siblings('a.prompt').remove();
	
		var _so = new SWFObject( site_host + "/flash/player.swf", "instance_home", "420", "206", "#000000" );
		_so.addParam( "allowScriptAccess", "always" );
		_so.addParam( "allowFullScreen" , "true" );
		_so.addVariable( "playerID", "instance_home" );
		_so.addVariable( "autoPlay", "false" );
		_so.addVariable( "playMode", "continuous" );
		_so.addVariable( "smoothing", "true" );
		_so.addVariable( "playlistXML", "/playlists/home.xml?version=1467" );

		if (clip) {
		
			_so.addVariable( "clipName", clip );

		}

		_so.addVariable( "mediaMode", "video" );
		_so.write( "player_home" );
		
		controls();
		
		return false;

	}
	

/* FUNCTION: PREP NEW PAGE
----------------------------------------------------------------------------- */


	function prep() {


/* Video Page Player */


		if ($('#video-browse').length > 0) {
		
			// ENCODE PLAYLIST URL
          	var playlist = site_host + '/widget/media/browse?tag=' + tag + '&options[items]=99';
          	var playlist = playlist.replace('?','%3F').replace('&','%26').replace('=','%3D').replace('[','%5B').replace(']','%5D');

			// EMBED VIDEO PLAYER          	
          	var _so = new SWFObject( asset_host + "/flash/OARPlayer.swf", "videoplayer", "420", "345", "#000000" );
          	_so.addParam( "allowScriptAccess", "always" );
          	_so.addParam( "allowFullScreen" , "true" );
          	_so.addVariable( "playerID", "videoplayer" );
          	_so.addVariable( "playMode", "single" );
          	_so.addVariable( "autoPlay", "true" );
          	_so.addVariable( "playlistXML", playlist);     	
          	_so.write( "player" );
          	          	
			// VIDEO SELECTION
			$('ul.clips li a').click(function() {

				var clip = this.pathname.split('/').pop();
				document['videoplayer'].playClip(clip);
								
				return false;
	
			});

		}
		

/* Video Player */


		$('.widget-media.video div.details a').attr('href','#');
		$('div#player_home').siblings('a.prompt').click(player_home);

	
/* Initiate Tabs */


		$('.tabbed > ul.tabs').tabs({ fxSlide: false });

		
/* Toggle Comments */


		$('a[href=#postcomment]').click(function() {
		
			$(this).addClass('selected');

			$('.widget-comment.modify').slideToggle('fast', function() {
							
				if ($(this).css('display') == "none") {
				
					$('a.post').removeClass('selected');
					
				} else {
				
					$(this).children('textarea').focus();
				
				}
				
			});
						
			return false;
		
		});
		
		
/* Comment Limit Counter */


		var max = 700;
		$('#comment-modify small').prepend('<strong><var>' + max + '</var> characters left<span> / </span></strong>');
		$('#comment-modify textarea').keypress(function() { characterCount($(this),max) });
		$('#comment-modify textarea').change(function() { characterCount($(this),max) });
		
		
/* Change Member Status */


		$('#member > a.change').click(function() {
		
			$(this).siblings('form').children('#status-modify').show();
			$(this).hide(); 
			
			return false;
		
		});
		
		
/* Photo Page */


		if (page == "photo") $('#content').css('visibility', 'visible');
		
		
/* Hover Effects */


		// VIDEO BROWSER
		$('#video-browse ul > li').hover(function() {
		
			$(this).children('div').show();
		
		}, function() {
		
			$(this).children('div').hide();
		
		});
		
		
		// CURRENT ALBUM INDICATOR
		if ($('ul.releases').length > 0) {
		
			$('ul.releases').find('a[href=/music/all/' + topic + ']').parent().parent().addClass('selected');
		
			// INITIAL INDICATOR
			if (!topic) $('ul.releases > li:first-child').addClass('selected');
			
		}

		// RELEASE BROWSER
		$('ul.releases > li:not(.selected)').hover(function() {
		
			$(this).children('strong').children('a').hide();
		
		}, function() {
		
			$(this).children('strong').children('a').show();
		
		});
	
	
/* Form Placeholders */


		if ($('#mailinglist').length > 0) {
		
			// EMAIL
			var mailinglist = "Enter your email address";
			$('#mailing_list_entry_email').attr('value', mailinglist);
			
			$('#mailing_list_entry_email').focus(function() {
			
				if ($(this).attr('value') == mailinglist) $(this).attr('value', '');
			
			});
			
			$('#mailing_list_entry_email').blur(function() {
			
				if (!$(this).attr('value')) $(this).attr('value', mailinglist);
				
			});
			
			// TEXT MESSAGE
			var text = "Enter your cell number";
			$('#mailing_list_entry_mobile').attr('value', text);
			
			$('#mailing_list_entry_mobile').focus(function() {
			
				if ($(this).attr('value') == text) $(this).attr('value', '');
			
			});
			
			$('#mailing_list_entry_mobile').blur(function() {
			
				if (!$(this).attr('value')) $(this).attr('value', text);
				
			});

		}

		
/* Canned Functions */


		markup();
		slideshows();
		flagger();
		widgets();
	
	}
	

/* FUNCTION: PRE SLIDE
----------------------------------------------------------------------------- */


	function preSlide() {

		// PREP NEWLY LOADED CONTENT
		prep();
		
		// KILL INTERNAL PAGE BODY ID AND CLASS
		$('body').attr('id', '');
		$('body').attr('class', '');

		// REMOVE OLD ALERT IF PRESENT
		if ($('.widget-alert').length > 0) {
		
			$('.widget-alert').hide();
			$('#promo').show();
			
		}
		
		// HIDE FLASH ELEMENTS FOR SLIDE
		$('#content').find('embed').css('visibility','hidden');
		
		// STOP VIDEO FROM PLAYING
		$('.widget-media.video embed').each(function() {
		
			$(this).remove();
		
		});
		
	}
		
		
/* FUNCTION: POST SLIDE
----------------------------------------------------------------------------- */


	function postSlide(selectedSection) {
	
		
/* Unhide Flash */


		$('#content').find('embed').css('visibility','visible');


/* Remove Previous Section's Content */


		$('div.section').each(function() {
		
			if ($(this).attr('class').search(selectedSection) == -1) {
			
				$(this).html('');
			
			}
		
		});		
	
	}
	
	
/* FUNCTION: CHARACTER LIMIT COUNTER
----------------------------------------------------------------------------- */


	function characterCount(editor,max) {
		
		var count = max - editor.val().length - 1;
		
		if (count > 0) {
		
			$(editor).parent().children('small').children('strong').children('var').html(count);
			$(editor).parent().children('small').children('strong').children('var.error').removeAttr('class');
			
		} else {
		
			$(editor).parent().children('small').children('strong').children('var').html('0');
			$(editor).parent().children('small').children('strong').children('var').attr('class','error');
			
		}
		
	}