// Main JS for Fry
jQuery( document ).ready( fry_ready );

function fry_ready()
{
	fry_profile_init();
	fry_rate_init();
	fry_record_view();
	fry_forum_quote_init();
	fry_font_resize_init();
	fry_email_friend_init();
	fry_forum_latest_init();
	fry_features_external();
	// Give the last elements on the nav a class
	jQuery( '#nav li:last-child' ).addClass( 'last-child' );
	// Give active A elements in the nav a class of current (ideally would be done serverside)
	jQuery( '#nav li.current_page_item > a' ).add( '#nav li.current_page_ancestor > a' ).addClass( 'current_item' );
}

function fry_profile_init()
{
	// Add a hide/reveal control for the profile editor
	jQuery( '#profile-wrapper .profile h3.my-profile' ).append( ' (<a href="#" class="show-profile-editor">edit</a>)' ).find( '.show-profile-editor' ).click( function () {
		// Hide/show the description and slide up/down the profile editor
		jQuery( '#profile-wrapper .profile-info' ).add( '#profile-wrapper .profile-edit' ).toggle();
		return false;
	} );
}

function fry_rate_init()
{
	jQuery( '#main_column .rating-active img' ).each( function () {	
		// Store the relevant on and off states
		var src = jQuery( this ).attr( 'src' );
		var on = src;
		var off = src.replace( "orange", "off", "gi" );
		if ( src.indexOf( 'off' ) != -1 ) {
			var on = src.replace( "off", "orange", "gi" );
			var off = src;
		}
		jQuery( this ).mouseover( function() { // -- MOUSEOVER --
			// Already rated on this page view?
			if ( jQuery( this ).parents( '.rate' ).hasClass( 'rated' ) ) return;
			// What star number are we (check using the alt, won't work if we ever translate)
			var alt = jQuery( this ).attr( 'alt' );
			var current = Number( alt.charAt( 0 ) );
			// Loop over all the stars
			for ( var i = 1; i <= 5; i++ ) {
				if ( i >= current + 1 ) {
					// Turn stars above the currently moused star off...
					jQuery( this ).siblings( '.star-' + i ).attr( 'src', off );
				} else {
					// ...turn other stars on.
					jQuery( this ).siblings( '.star-' + i ).attr( 'src', on );
				}
			}
			// Cheat: ensure that this star, currently moused, is definitely on
			jQuery( this ).attr( 'src', on );
		} ).click( function () { // -- CLICK --
			// What star number are we (check using the alt, won't work if we ever translate)
			var alt = jQuery( this ).attr( 'alt' );
			var rating = Number( alt.charAt( 0 ) );
			// Loop over all the stars and turn them on as appropriate
			for ( var i = 1; i <= 5; i++ ) {
				if ( i >= rating + 1 ) {
					// Turn stars above the currently moused star off...
					jQuery( this ).siblings( '.star-' + i ).attr( 'src', off );
				} else {
					// ...turn other stars on.
					jQuery( this ).siblings( '.star-' + i ).attr( 'src', on );
				}
			}
			// Cheat: ensure that this star, currently moused, is definitely on
			jQuery( this ).attr( 'src', on );
			jQuery( this ).parents( '.rate' ).addClass( 'rated' );
			// The post ID is the same as the ID attribute on the rate wrapper
			var post_id = jQuery( this ).parents( '.rate' ).attr( 'id' );
			// Now send the rating
			var data = { 
				action: 'fry_rate', 
				rating: rating, 
				post_id: post_id 
			};
			jQuery.post( fry_data.ajax_url, data, fry_rating_reply, 'json' );
		} );
		jQuery( this ).parents( '.rate' ).mouseout( function() {
			// Already rated on this page view?
			if ( jQuery( this ).hasClass( 'rated' ) ) return;
			for ( var i = 1; i <= 5; i++ ) {
				if ( jQuery( this ).find( '.star-' + i ).hasClass( 'off' ) ) {
					jQuery( this ).find( '.star-' + i ).attr( 'src', off );
				} else {
					jQuery( this ).find( '.star-' + i ).attr( 'src', on );
				}
			}
		} );
	} );
}

function fry_record_view()
{
	// Currently we're only recording views for media
	if ( ! fry_data.is_media_single ) return;
	// Now send the rating
	var data = { 
		post_id: fry_data.post_id 
	};
	// Not expecting a reply. 
	jQuery.post( fry_data.quick_view_url, data );
}

function fry_forum_quote_init()
{
	if ( ! jQuery( 'body' ).hasClass( 'logged-in' ) ) {
		jQuery( 'body.forum #latest .poststuff' ).append( '<p>(You must be <a href="' + fry_data.login_url + '">logged in</a> to reply)</p>' );
		return; // Nothing further
	}
	// So do we have a variable specifying a post ID to get quote data for?
	if ( fry_data.quote_post_id ) {
		// Do the AJAX query
		var data = { 
			action : 'fry_quote_post', 
			post_id_str: 'post-' + fry_data.quote_post_id
		};
		jQuery.post( fry_data.ajax_url, data, fry_post_quote_reply, 'html' );
	}
	// Setup the AJAX calls and associated links for the quote functionality
	jQuery( 'body.forum #latest .poststuff' ).each( function() {
		jQuery( this ).append( '<p class="fry-quote"><a href="#">Quote and reply</a></p>' );
		jQuery( this ).find( '.fry-quote a' ).click( function() {
			// Some generic prep
			var post_id_str = jQuery( this ).parents( '.post' ).attr( 'id' );
			// If the post form is on this page then populate it with AJAX
			if ( jQuery( '#postform' ).length ) {
				// Do the AJAX query
				var data = { 
					action : 'fry_quote_post', 
					post_id_str: post_id_str
				};
				jQuery.post( fry_data.ajax_url, data, fry_post_quote_reply, 'html' );
				// Move us down to the reply form (and hope the quote comes quickly)
				return false;
			}
			// Otherwise we're sending them to another page, and adding a URL param
			// indicate that a quote is needed in the post form.
			// Assume there's no other URL params (i.e. we ARE using pretty URLs)
			var redirect_to = fry_data.topic_last_page_link + '?quote_post_id_str=' + escape( post_id_str ) + '#postform';
			window.location = redirect_to;
			return false;
		} );
	} );
}

function fry_font_resize_init()
{
	// Increase Font Size
	jQuery( '#mnui_larger' ).click( function() {
		var size = fry_current_font_size();
		if ( size < 3 ) fry_font_size( size, 1 );
		return false;
	} );
	// Decrease Font Size
	jQuery( '#mnui_smaller' ).click( function() {
		var size = fry_current_font_size();
		if ( size > 0 ) fry_font_size( size, -1 );
		return false;
	} );
}

function fry_current_font_size()
{
	// Does any body class begin with 'font'
	var class_attr = jQuery( 'body' ).attr( 'class' );
	var classes = class_attr.split( ' ' );
	var size = 0;
	var i;
	for ( i in classes ) {
		if ( classes[ i ].substring( 0, 4 ) == 'font' ) {
			return parseFloat( classes[ i ].substring( 5, 6 ), 10);
		}
	}
	return size;
}

function fry_font_size( size, change )
{
	// Add the body classes to trigger the font changes
	jQuery( 'body' ).removeClass( 'font-' + size );
	size += change;
	jQuery( 'body' ).addClass( 'font-' + size );
	// Now set a cookie, so we can persist the size
	jQuery.cookie( 'fry_font', 'font-' + size, { expires: 7, path: '/' } );
}

function fry_post_quote_reply( html )
{
	jQuery( '#post_content').val( html + "\n\n" );
	window.location.hash = 'postform';
}

function fry_rating_reply( data )
{
	jQuery( '#rate-' + data.post_id ).append( "<span class='msg'>" + data.msg + "</span>" );
}

function fry_do_share( url, panel_selector ) {
	jQuery( panel_selector ).hide();
	var myWindow = window.open( url, 'mywindow', '' );
	return false;
}

function fry_toggle_panel( id )
{
	jQuery( '#' + id ).slideToggle().removeClass( 'error' );
	return false;
}

function fry_email_friend_init( id )
{
	jQuery( '#main_column .popup_email_friend form' ).submit( function() {
		// Grab the data
		var name = jQuery( this ).find( '.name' ).val();
		var email = jQuery( this ).find( '.email' ).val();
		var friend_name = jQuery( this ).find( '.friend-name' ).val();
		var friend_email = jQuery( this ).find( '.friend-email' ).val();
		var post_id = jQuery( this ).find( '.post-id' ).val();
		var panel_id = jQuery( this ).parents( '.popup_email_friend' ).attr( 'id' );
		// Validate that all user entered fields are present
		if ( ! name || ! email || ! friend_name || ! friend_email ) {
			var msg = "You need to fill in all the fields below, please, before we can email your friend.";
			// Add the message
			jQuery( this ).parents( '.popup_email_friend' ).addClass( 'error' ).find( '.msg' ).text( msg );
			return false;
		}
		// Do the AJAX query
		var data = { 
			action : 'fry_email_friend',
			panel_id : panel_id,
			post_id : post_id,
			name : name,
			email : email,
			friend_name : friend_name, 
			friend_email: friend_email
		};
		jQuery.post( fry_data.ajax_url, data, fry_email_friend_reply, 'json' );
		// Return false so the form doesn't submit
		return false;
	} );
}

function fry_email_friend_reply( data )
{
	jQuery( '#'+data.panel_id ).removeClass( 'error' ).addClass( data.status ).find( '.msg' ).text( data.msg );
	if ( data.status != 'error' ) jQuery( '#' + data.panel_id ).find( 'form' ).hide();
}

function fry_forum_latest_init()
{
	jQuery( '#content_container ul.forum_list' ).each( function () {
		jQuery( this ).hide().before( '<div class="rollup_forum"><a href="#">Latest 3 posts</a></div>' );
		jQuery( this ).parent().find( '.rollup_forum a' ).click( function() {
			jQuery( this ).parents( '.widget_fry_latest_forum' ).find( '.forum_list' ).slideToggle();
			return false;
		} );
	} );	
}

function fry_features_external() {
	jQuery( '#features a.launch_external' ).each( function() {
		var href = jQuery( this ).attr( 'href' );
		var internal = jQuery.url.setUrl( href ).param( "fry_internal" );
		jQuery( this ).click( function() {
			// Open a new window with the HREF in it
			var fry_external = window.open( href, 'fry_external' );
			// Open the internal URL in the current window
			window.location.href = internal;
			// Focus the external window
			if (window.focus) 
				fry_external.focus()
			return false;
		} );
	} );
}
