jQuery(document).ready(function(){
	jQuery('#search input.form-submit').val('SEARCH');
	//Main menu hover
	jQuery('#menu li').hover(
		function(){jQuery(this).addClass('hover')},
		function(){jQuery(this).removeClass('hover')}
	);
	//Popup (eggs)
	jQuery("a.popup-btn").click(function(){
		if(!jQuery(this).hasClass('open')) {
			//jQuery('#pop_up ul').show();
			//jQuery(this).addClass('open');
		} else {
			//jQuery('#pop_up ul').hide();
			//jQuery(this).removeClass('open');
		};
		return false;
	});
	jQuery('ul.eggs a').bind("click", function(){
		jQuery('a.popup-btn img').attr({src: jQuery(this).attr('href')});
		jQuery('#pop_up ul').hide();
		jQuery('a.popup-btn').removeClass('open');
		return false;
	});
	//Popup (registration)
	jQuery('#user-login-form ul li a').click(function(){
		if(!jQuery(this).hasClass('open')) {
			jQuery('#pop_up_form').show();
			jQuery(this).addClass('open');
		} else {
			jQuery('#pop_up_form').hide();
			jQuery(this).removeClass('open');
		};
		return false;
	});
	jQuery('#pop_up_form .form-submit').val('SUBMIT');
	//Flash hide button
	jQuery('.flash a').click(function(){
		if(!jQuery(this).hasClass('open')) {
			jQuery('.flash_bg').hide();
			jQuery(this).addClass('open');
			jQuery(this).html('Show');
			jQuery(this).css({"background-position":"-9999px"});
		} else {
			jQuery('.flash_bg').show();
			jQuery(this).removeClass('open');
			jQuery(this).html('Close');
			jQuery(this).css({"background-position":"0"});
		};
		return false;
	});
	//Content image resize
	jQuery('img.image-preview').attr({width: "258"});
	jQuery('img.image-preview').attr({height: "186"});
	jQuery('img.image-preview').parent().parent().width(258);
	jQuery('div.big img').attr({width: "258"});
	jQuery('div.big img').attr({height: "186"});
	//Show/hide comments button
	jQuery('a.comments_btn').click(function(){
		if (jQuery(this).hasClass('open')) {
			jQuery(this).removeClass('open');
			jQuery('#comments').hide('slow');
		} else {
			jQuery(this).addClass('open');
			jQuery('#comments').show('slow');
		}
		return false;
	});
	jQuery('a.comments').click(function(){
		if (jQuery(this).hasClass('open')) {
			jQuery(this).removeClass('open');
			jQuery(this).parent().parent().parent().parent().children('div.comments').hide('slow');
		} else {
			jQuery(this).addClass('open');
			jQuery(this).parent().parent().parent().parent().children('div.comments').show('slow');
		}
		return false;
	});
	//Profile page accordion
	jQuery(".tabs ul li.active ul").show();
	jQuery(".tabs ul li a.nav").click(function(){
		jQuery(".tabs ul li").removeClass('active');
		jQuery(this).parent().addClass('active');
		jQuery(".tabs ul li ul:visible").slideUp("slow");
		jQuery(this).parent().children('ul').slideDown("slow");
		jQuery(".tabs ul li a").removeClass('open');
		return false;
	});
	//Link of the correction
	jQuery("#gg_url_link").click(function(){
		window.open(jQuery("#gg_url_link").html(), "_blank");
	});
	//Link of explanations (Grammar)
	jQuery("#gg_grammar_category").click(function(){
		var refURL;
		switch(jQuery("#gg_grammar_category").text()){
			case "Adverb/adjective confusion":
				refURL = "http://goosegrade.com/node/920";
			break;
			case "Count vs Mass nouns and their qualifiers":
				refURL = "http://goosegrade.com/node/921";
			break;
			case "Apostrophe errors":
				refURL = "http://goosegrade.com/node/922";
			break;
			case "Subject/verb Agreement":
				refURL = "http://goosegrade.com/node/923";
			break;
			case "The false future perfect":
				refURL = "http://goosegrade.com/node/925";
			break;
			case "Punctuation":
				refURL = "http://goosegrade.com/node/920";
			break;
			default:
				refURL = "";
			break;
		}
		window.open(refURL, "_blank");
	});
	//Link of factual URL
	jQuery("#gg_factual_url").click(function(){
		var url = jQuery("#gg_factual_url").text();	
		var finalURL;
		if(url.indexOf("http://") == -1){
			finalURL = "http://" + url;   
		}else{
			finalURL = url;
		}
		window.open(finalURL, "_blank");
	});
	//Copy Buttons
	jQuery("#gg_copy_ori").click(function(){
		var original = jQuery("#gg_original").text();
		var copy_original = jQuery.copy(original.substring(11, original.length - 1));
	});
	jQuery("#gg_copy_cor").click(function(){
		var correct = jQuery("#gg_correction").text();
		var copy_correct = jQuery.copy(correct.substring(13, correct.length - 2));
	});
	jQuery("#gg_copy_embed").click(function(){
		var embed = jQuery("#gg_txt_embed").text();
		var copy_correct = jQuery.copy(embed);
	});
	//Profile page (central_content) accordion

	//jQuery(".pink_box ul li.active div.collapsible").show();
	/*jQuery(".pink_box ul li").livequery('click', function(){
		jQuery(".pink_box ul li").removeClass('active');
		jQuery(".pink_box ul li div.collapsible:visible").slideUp("slow");
		//jQuery(this).attr("id", 'gg_title');
		jQuery(this).addClass('active');
		jQuery(this).children('div.collapsible').slideDown("slow");
		return false;
	});*/

	jQuery(".pink_box ul li.show p.active").livequery('click', function(){
		var li = jQuery(this).parent();
		li.removeClass('active');
		jQuery(this).removeClass('inactive');
		jQuery(this).removeClass('active');
		jQuery(this).nextAll("div.collapsible").slideUp("slow");
		jQuery(this).addClass('inactive');
		return false;
	});

	jQuery(".pink_box ul li.show p.inactive").livequery('click', function(){
		jQuery(this).addClass('active');
		jQuery(this).removeClass('inactive');
		jQuery(this).nextAll("div.collapsible").slideDown("slow");
		return false;
	});

	jQuery(".pink_box ul li.show p.title").livequery('click', function(){
		var li = jQuery(this).parent();
		li.addClass('active');
		jQuery(this).addClass('active');
		jQuery(this).removeClass('inactive');
		jQuery(this).nextAll("div.collapsible").slideDown("slow");
		return false;
	});

	//Profile page tabs
	jQuery('#tabs_b a').click(function(){
		jQuery('#tabs_b li').removeClass('active');
		jQuery(this).parent().addClass('active');
		jQuery('div.central_content').html('');
		jQuery(this).parent().children('div').clone().appendTo('div.central_content');
		return false;
	});
	//Profile page prepare
	jQuery('#edit-name-wrapper').appendTo('#tabs div.profile');
	jQuery('#edit-profile-date-wrapper').appendTo('#tabs div.profile');
	jQuery('#edit-profile-about-wrapper').appendTo('#tabs div.profile');
	jQuery('#edit-profile-profession-wrapper').appendTo('#tabs div.profile');
	jQuery('#edit-profile-website-wrapper').appendTo('#tabs div.profile');

	jQuery('div.central_content fieldset:not(:first)').appendTo('#tabs div.picture');

	jQuery('#edit-mail-wrapper').appendTo('#tabs div.account');
	jQuery('#edit-pass-wrapper').appendTo('#tabs div.account');
	jQuery('#edit-profile-state-wrapper').appendTo('#tabs div.account');
	jQuery('#edit-profile-country-wrapper').appendTo('#tabs div.account');

	//jQuery('#tabs div.profile').clone().appendTo('div.central_content fieldset');
	jQuery('#tabs div.profile').appendTo('div.central_content fieldset');
	jQuery('#tabs div.account').appendTo('div.central_content fieldset');
	jQuery('#tabs div.picture:first').appendTo('div.central_content fieldset');

	//Profile page tabs
	jQuery('div.central_content fieldset div.picture a').click(function(){return false;});
	jQuery('div.central_content fieldset div.picture').hide();
	jQuery('div.central_content fieldset div.account').hide();

	function first(){
		jQuery('div.central_content fieldset div.profile').show();
		jQuery('div.central_content fieldset div.picture').hide();
		jQuery('div.central_content fieldset div.account').hide();
		jQuery('div.central_content input#edit-submit').css({'margin-left':'94px'});
		jQuery('div.buttons p.current').html('1 of 3')
		jQuery('div.buttons a.next').show();
		jQuery('div.buttons a.back').hide();
		jQuery('#tabs li.profile').addClass('active');
		jQuery('div.buttons a.next').attr({'rel':'second'});
	};
	function second(){
		jQuery('div.central_content fieldset div.profile').hide();
		jQuery('div.central_content fieldset div.picture').show();
		jQuery('div.central_content fieldset div.account').hide();
		jQuery('div.central_content input#edit-submit').css({'margin-left':'16px'});
		jQuery('div.buttons p.current').html('2 of 3')
		jQuery('div.buttons a').show();
		jQuery('#tabs li.picture').addClass('active');
		jQuery('div.buttons a.next').attr({'rel':'third'});
		jQuery('div.buttons a.back').attr({'rel':'first'});
	};
	function third(){
		jQuery('div.central_content fieldset div.profile').hide();
		jQuery('div.central_content fieldset div.picture').hide();
		jQuery('div.central_content fieldset div.account').show();
		jQuery('div.central_content input#edit-submit').css({'margin-left':'130px'});
		jQuery('div.buttons p.current').html('3 of 3');
		jQuery('div.buttons a.next').hide();
		jQuery('#tabs li.account').addClass('active');
		jQuery('div.buttons a.back').attr({'rel':'second'});
		jQuery('div.buttons a.back').show();
	};

	jQuery('#tabs a').click(function(){
		jQuery('#tabs li').removeClass('active');
		switch (jQuery(this).parent().attr('class')) {
			case 'profile' : first(); break;
			case 'picture' : second();	break;
			case 'account' : third(); break;
		};
		jQuery(this).parent().addClass('active');
		return false;
	});

	//back/next buttons prepare
	jQuery('div.buttons a.back').hide();
	jQuery('div.buttons a.next').attr({'rel':'second'});
	//Next button
	jQuery('div.buttons a.next').click(function(){
		jQuery('#tabs li').removeClass('active');
		switch (jQuery(this).attr('rel')) {
			case 'second' :
				second();
				jQuery(this).attr({'rel':'third'});
				jQuery('div.buttons a.back').attr({'rel':'first'});
				break;
			case 'third' :
				third();
				jQuery('div.buttons a.back').attr({'rel':'second'});
				break;
		};
		return false;
	});
	//Back button
	jQuery('div.buttons a.back').click(function(){
		jQuery('#tabs li').removeClass('active');
		switch (jQuery(this).attr('rel')) {
			case 'first' :
				first();
				break;
			case 'second' :
				second();
				jQuery(this).attr({'rel':'first'});
				jQuery('div.buttons a.back').attr({'rel':'first'});
				break;
		};
		return false;
	});

});

