/*
 * Lookit! Javascripty Action
 *
 */
 
 
$(document).ready(function() {

	$('ul#hnav li span').click(function(){
		$(this).activate_panel();
	});
	
	$('span.openmobile').click(function() {
		if($('ul#hnav li span.mobileapps.active').length) {
			$('html, body').animate({scrollTop: '250'}, 400);
		} else {
			$('div#contentwrapper div').fadeOut(200);
			$('div#mobileapps').fadeToggle(200);
			$('div#choices').fadeIn(200);
			$('ul#hnav li span.active').removeClass('active');
			$('ul#hnav li span.mobileapps').addClass('active');
		}
	});
	
	$('img.submit').click(function(){
		$(this).parent().submit();
	});
	
	$.fn.fadeToggle = function(speed, easing, callback) { 
    	return this.animate({opacity: 'toggle'}, speed, easing, callback); 
	};

//Help search box: hide default text on focus
	var swap_text_boxes = [];

	function init_swap_text_boxes(){
	  //Store the default value for each box
	  $('input[type=text][value].swaptextbox').each(function() {
	    swap_text_boxes[$(this).attr('id')] = $(this).attr('value');
	  });
	  //Add focus and blur events to set or clear the value
	  $('input[type=text][value].swaptextbox').bind('focus', function() {
	    if($(this).val() == swap_text_boxes[$(this).attr('id')]) {
	      $(this).val('');
	      $(this).removeClass('swaptextbox'); // so that the text input is styled normally
	    }
	  });
	  $('input[type=text][value].swaptextbox').bind('blur', function() {
	    if($(this).val() == '') {
	      $(this).addClass('swaptextbox'); // so that the placeholder text is styled correctly
	      $(this).val(swap_text_boxes[$(this).attr('id')]);
	    }
	  });
	 }
	 

	function get_url_anchors() {
		var myFile = document.location.toString();
		if (myFile.match('#')) { // the URL contains an anchor
		  var jumpTo = myFile.split('#')[1]; // get the entire anchor
		  var realAnchor = jumpTo.split('-')[1]; // get the real div you want to use, after the dash
		  var myAnchor = 'span.' + realAnchor; // click that div (no page jump!)
		  $(myAnchor).click();
		} else {
		  // no anchor here, bro
		  return false;
		}	
	}
	
	
	$.fn.activate_panel = function() {

		var selection = $(this).attr('class');
		if($(this).hasClass('active')){
			return false;
		} else {
			$('div#contentwrapper div').fadeOut(200);
			var activatingDiv = $('div#'+selection);
			$(activatingDiv).fadeToggle(200);
			if($(activatingDiv).attr('id') == 'mobileapps') {
				$('div#choices').fadeIn(200);
			}
			$('ul#hnav li span.active').removeClass('active');
			$(this).addClass('active');
		}	
	
	}
	init_swap_text_boxes();
	
	if($('ul#hnav').length) {
		get_url_anchors(); // always make this last. not really sure why though.
	}
	
	if($('div#trophybox img.status').length) {
		// need to figure out how to make a popup based on the class and ID of a status/award when user hovers over it
		$('div#trophybox img.status').each(function() {

			var status = $(this).attr('id');
			
			var statusDescription = new Array();
				statusDescription['flag'] = 'Flagged. Read the <a href="/the-virgin-guide">Virgin Guide</a>!';
				statusDescription['coffee'] = 'Obligatory Coffee Shot';
				statusDescription['perfect'] = 'Perfect Score!';
				statusDescription['creeper'] = 'Creeper Pic';
				statusDescription['clever'] = 'Ooooh, clever!';
				statusDescription['eye'] = 'I see what you did there...';
				statusDescription['first'] = 'First pic of the day';
				statusDescription['warm'] = 'Getting popular...';
				statusDescription['hot'] = 'HOT!';
				statusDescription['self'] = 'You probably think this game is about you';
				
			var newAlt = statusDescription[status];
			
			$(this).parent().find('span').html(newAlt);
						
				
			});
						
		
		
	}

});
