// Functions used outside of Jquery
// 	-  called from within Flash
// 		Need to pass in video_path as well
//		Switch image_viewer to use this same method, to avoid hard coding vars in flash
function video_player(id){
	GB_show('Video Player', 'popup/videoplayer/?video_id='+ id, 645, 946, 'video_player');
}

// Loads a div with ajax content, and makes pagination links call this function
function loadPiece(href,divName) {    
    $(divName).load(href, {}, function(){
        var divPaginationLinks = divName+" .pagination a";
        $(divPaginationLinks).click(function() {
            var thisHref = $(this).attr("href");
            loadPiece(thisHref,divName);
            return false;
        });
    });
} 

// Jquery based JS used on document.ready
$(document).ready(function(){
	 
	/********************************
	Greybox Popups
	********************************/
	$("a.confirm_leave").click(function(){
	  var t = this.title || $(this).text() || this.href;
	  GB_show(t,this.href,406,710,'confirm_leave');
	  return false;
	});
	
	// greybox for links that are entered into the news admin, where HTML is NOT permitted
	// ignore links that contain wolfenstein.com, so as to not invoke greybox on internal links.
	// precede .com w/ a slash, makes wolfenstein.com in the URL be required to avoid the popup
	$("a.postlink").not("a.postlink[href*=wolfenstein\.com],a.postlink[href*=2advanced\.com]").click(function(){
	  var t = this.title || $(this).text() || this.href;
	  var post_link_href = BASE_URL+"popup/confirmleave/?url="+encodeURIComponent(this.href);
	  GB_show(t,post_link_href,406,710,'confirm_leave');
	  return false;
	});

	// close buttons or links for ALL popups
	$(".popup_close").click(function(){
		parent.parent.GB_hide();
	});

	$("a.missions_popup").click(function(){
	  var t = this.title || $(this).text() || this.href;
	  GB_show(t,this.href,556,710,'missions_instructions');
	  return false;
	});

	$("a.inline_art_popup").click(function(){
	  var t = this.title || $(this).text() || this.href;
	  GB_show(t,this.href,805,1200,'inline_art');
	  return false;
	});

	/*****************************************************************
	These functions are primarily run from within Flash
	these are HTML versions, used for testing
		- add <a href="#" class="foo_bar"> for simulation
	*****************************************************************/
	/*
	$("a.video_player").click(function(){
		video_player();
		return false;
	});
	$("a.video_pause").click(function(){
		videoPause(); //videoPause() is defined in jquery.greybox.js
		return false;
	});
	$("a.video_kill").click(function(){
		killVideo(); //videoPause() is defined in jquery.greybox.js
		return false;
	});
	*/
	/* HTML links to image popups are used on /media/ */
	$("a.image_viewer_link").click(function(){
	  var t = this.title || $(this).text() || this.href;
	  GB_show(t,this.href,490,810,'image_viewer');
	  return false;
	 // sample URL to send to image_viewer:
	 // popup/imageviewer/?img=http://foo.com/bar.jpg&amp;dl_path=http://foo.com/bar.zip"
	});

	// replace target="_blank"
	$("[rel='external']").click(function(){
		this.target = "_blank";
	});	

	// hide gamertag inputs until platform is checked
	$("label.gamer_tag, label.gamer_tag + input.small").hide();
	
	$(".platforms input[type='checkbox']").click(function() {
		$(this).siblings("label.gamer_tag,input.small").toggle();
	});

	// backup of CSS version
	$("input[type='hidden']").css({'display' : 'none'});
	
	
	// fancy scroll to top on #top link!
	$("p.top a").click(function(){
		$("html").animate({scrollTop: 0}, "slow");
		return false;
	});

	/*****************************************************************
	BLOG / NEWS list posts
	fancy animation when user selects article link (or click on LI).
		- Do not apply to .permalinks, we use those for direct links
			to a news/ blog post
	*****************************************************************/
	// hover on P tags in news / blog postings
    /*
    $(".posts li p,.posts li").hover(function () {
		$(this).css({'cursor' : 'pointer'});
      }, function () {
		$(this).css({'cursor' : 'default'});
     });
     */
	// Click on LI, to make it GOOOO to it's child's HREF
   /*
   $(".news .posts li").click(function () {
    	alert ("gang bustas");
     	window.location(media);
     });
     */

	/*****************************************************************
	age_gate
	*****************************************************************/	
	$("#month").focus(function(){
		if(this.value == '00'){
			this.value = '';
		} 
	});
	
	$("#month").blur(function(){
		if(this.value == ''){
			this.value = '00';
		} 
	});
	$("#month").keyup(function(){
		if(this.value.length == 2){
			$("#day").focus();
		}
	});
	
	$("#day").focus(function(){
		if(this.value == '00'){
			this.value = '';
		} 
	});	
	$("#day").blur(function(){
		if(this.value == ''){
			this.value = '00';
		} 
	});	
	$("#day").keyup(function(){
		if(this.value.length == 2){
			$("#year").focus();
		}
	});
	
	$("#year").focus(function(){
		if(this.value == '0000'){
			this.value = '';
		} 
	});	
	$("#year").blur(function(){
		if(this.value == ''){
			this.value = '0000';
		} 
	});	
	
	/*****************************************************************
	PAGINATION
	*****************************************************************/	
	var page_jump_lang = $(".page_jump").val();
	$(".page_jump").focus(function(){
		if ($(this).val() == page_jump_lang) {
			$(this).val("");
		} 
	});	
	$(".page_jump").blur(function(){
		if ($(this).val() == '') {
			$(this).val(page_jump_lang);
		}
	});	

	/*****************************************************************
	SEND TO FRIEND
	*****************************************************************/
	/*
	$("#send_friend_button").click(function(){
		$.get(BASE_URL+"home/share", { friend_email: $("#send_friend_email").val(), user_email: $("#send_user_email").val() }, function(data){
			if (data == 'success')
			{
				// Do successful stuff
				alert('success');
			}	
			else
			{
				// Do error stuff
			}
		});
		return false;
	});
	*/
});


var selected_avatar;

function set_selected_avatar(id) {
	selected_avatar = id;
}

function highlight_avatar(id) {
	if (selected_avatar != id) {
		if (selected_avatar != undefined) {
			$('#'+selected_avatar).parents('.avatar_item').removeClass('avatar_item_selected');
		}
		$('#'+id).parents('.avatar_item').addClass('avatar_item_selected');
		set_selected_avatar(id);
	}
}
	
$(document).ready(function(){
	$('.toggle_radio:radio').change(function() {  
		highlight_avatar($(this).attr('id'));
	});
});