
/**
 * Blog read page for adding comments
 */
function addComment(blogID) {
	var form = document.forms.commentForm;
	
	Ext.Ajax.request({
		url: '/blog/comment/id/'+blogID,
		success: function(response, options) {
			var res = response.responseText;
			Ext.fly('comment-add').insertHtml('afterBegin', res);
			Ext.fly('comment-add').show(true);
			Ext.fly('comment-form-container').hide(true);
			form.body.value = '';
		},
		failure: this.ajaxError,
		params: {
			'name': (form.name)?form.name.value:'',
			'body': (form.body)?form.body.value:''
		},
		scope: this
	});
}

function hideCommentBox() {
	Ext.fly('comment-form-container').hide(true);
}

function openYear(e) {
	Ext.select("div.year-container").removeClass('year-open');
	var target = e.getTarget(null, null, true);
	if (target.hasClass("year-container")) {
		target.addClass("year-open");
	} else {
		target.findParent(".year-container", null, true).addClass("year-open");
	}
}

/**
 * Products page
 */
function reloadB2bPricing(product) {
	Ext.get('pricing-table').load({
		url: '/products/pricing/product/'+action+'/currency/'+Ext.fly('currency').getValue(),
		method: 'get',
		text: 'Please wait...'
	});
}

function reloadCertPricing() {
	Ext.get('pricing-table').load({
		url: '/certification/pricing/currency/'+Ext.fly('currency').getValue(),
		method: 'get',
		text: 'Please wait...'
	});
}

function switchFaq(faq) {
	if (faq == "schoolscompanies") {
		Ext.fly('schoolscompanies').setStyle({display: 'block'});
		Ext.fly('freetypingtutor').setStyle({display: 'none'});
		Ext.fly('schoolscompanies-link').addClass('active-faq');
		Ext.fly('freetypingtutor-link').removeClass('active-faq');
	} else {
		Ext.fly('schoolscompanies').setStyle({display: 'none'});
		Ext.fly('freetypingtutor').setStyle({display: 'block'});
		Ext.fly('schoolscompanies-link').removeClass('active-faq');
		Ext.fly('freetypingtutor-link').addClass('active-faq');
	}
}

/**
 * Typing tutor page
 */
function calcTimeSaved() {
	var hourstyping = parseInt(Ext.fly('hourstyping').dom.value, 10);
	var currentwpm = parseInt(Ext.fly('currentwpm').dom.value, 10);
	var goalwpm = parseInt(Ext.fly('goalwpm').dom.value, 10);
	
	if (isNaN(hourstyping) || isNaN(currentwpm) || isNaN(goalwpm)) {
		return;
	}

	Ext.fly('displaywpm').update(currentwpm);
	Ext.fly('displayhours').update(hourstyping);
	Ext.fly('displaygoal').update(goalwpm);

	var diff = 1-currentwpm/goalwpm;
	Ext.fly('resultsday').update(Math.round(diff*hourstyping/7*10)/10);
	Ext.fly('resultsweek').update(Math.round(diff*hourstyping*10)/10);
	Ext.fly('resultsmonth').update(Math.round(diff*hourstyping*4.2*10)/10);
	Ext.fly('resultsyear').update(Math.round(diff*hourstyping*52));

	Ext.fly('typingcalc').frame();
}

function goToTabIndex(tabIndex) {
	if (parseInt(tabIndex, 10) && Ext.fly('copy-tabs')) {
		Ext.select(".active-tab").removeClass("active-tab");
		Ext.select(".hidden-content").setStyle("display", "none");
		tabIndex = parseInt(tabIndex, 10)-1;
		var tabs = Ext.select("#copy-tabs li");
		tabs.each(function(tab, tabs, index){
			if (index == tabIndex) {
				tab.addClass("active-tab");
				Ext.fly(tab.dom.title.toLowerCase().trim().replace(/\s/, '-')).setStyle("display", "block");
			}
		}, this);
	}
}

var langMenuTimeout;
Ext.onReady(function() {
	MakeDropShadow();
	
	Shadowbox.init({
		 skipSetup: false,
		 animate: false,
		 animateFade: true,
		 animSequence: false,
		 modal: false,
		 overlayOpacity: 0.5,
		 displayNav: true,
		 enableKeys: true
	});
	
	var suggest = Ext.select('.suggest-translation');
	if (suggest) {
		suggest.on('click', function(e) {
			e.preventDefault();
				Shadowbox.open({
			        player: 'iframe',
			        content: "/suggest/index/c/"+controller+"/a/"+action,
			        width: 900,
			        height: 600,
			        title: suggestTitle
			    });
		}, this);
	}
	
	
	var bug = Ext.select('.bug-report');
	if (bug) {
		bug.on('click', function(e) {
			e.preventDefault();
				Shadowbox.open({
			        player: 'iframe',
			        content: "/suggest/bug/",
			        width: 660,
			        height: 510,
			        title: "Report Error or Bug"
			    });
		}, this);
	}
	
	
	var years = Ext.select("div.year-container");
	if (years) {
		years.on('click', openYear);
	}
	
	var commentLink = Ext.fly('add-comment-link');
	if (commentLink) {
		commentLink.on('click', function(e){
			e.preventDefault();
			var commentBox = Ext.get('comment-form-container');
			if (!commentBox) { return; }
			if (commentBox.isVisible()) {
				commentBox.hide(true);
			} else {
				coords = Ext.get('add-comment-link').getXY();
				commentBox.setLocation(coords[0]-5, coords[1]+15);
				commentBox.show(true);
			}
		});
	}
	
	/**
	 * Global Stuff
	 */
	var tabs = Ext.select("#copy-tabs li");
	if (tabs) {
		tabs.on('click', function(e) {
			var ele = e.getTarget("li", null, true);
			var id = ele.dom.title.toLowerCase().trim().replace(/\s/, '-');
			Ext.select('.active-tab').removeClass('active-tab');
			Ext.select('.hidden-content').setStyle('display', 'none');
			ele.addClass('active-tab');
			var content = Ext.fly(id);
			if (content) {
				content.setStyle('display', 'block');
			}
			Ext.select('.hover-tab').removeClass('hover-tab');
		});
	}
	
	var langs = Ext.fly('languages-link');
	if (langs) {
		langs.on('mouseover', function(e){
			if (langMenuTimeout) { langMenuTimeout.cancel(); }
			coords = Ext.fly('languages-link').getXY();
			var langsBox = Ext.fly('select-language');
			if (!langsBox) { return; }
			langsBox.setLocation(coords[0]-5, coords[1]+15);
			langsBox.show(true);
		});
		langs.on('click', function(e){
			e.preventDefault();
		});
		langs.on('mouseout', function(e){
			if (langMenuTimeout) { langMenuTimeout.cancel(); }
			langMenuTimeout = new Ext.util.DelayedTask(function(){
				Ext.fly('select-language').hide(true);
			});
			langMenuTimeout.delay(500);
		});
	}
	
	var langsBox = Ext.fly('select-language');
	if(langsBox) {
		langsBox.on('mouseover', function(){
			langMenuTimeout.cancel();
		});
		langsBox.on('mouseout', function() {
			langMenuTimeout = new Ext.util.DelayedTask(function(){
				Ext.fly('select-language').hide(true);
			});
			langMenuTimeout.delay(500);
		});
	}
	
	
	/**
	 * typingtutor page
	 */
	if (Ext.select(".featured-testimonial").getCount() > 0) {
		var task = {
			run: function() {
				var featured = Ext.select(".featured-testimonial");
				var rand = Math.floor(Math.random()*featured.getCount());
				featured.setDisplayed(false);
				featured.item(rand).show(true);
			},
			interval: 10000
		};
		var wait = new Ext.util.DelayedTask(function() {
			Ext.TaskMgr.start(task);
		}, this);
		wait.delay(10000);
	}
	
	if (location.href.indexOf("#") != -1) {
		var tabIndex = location.href.substr(location.href.indexOf("#")+1);
		goToTabIndex(tabIndex);
	}
	
	var sideMouseOut = new Ext.util.DelayedTask(function() {
		Ext.select("#content-left li").each(function(ele){
			if (!ele.hasClass('force-active')) {
				ele.removeClass('active-choice');
			}
		}, this);
	}, this);
	if (Ext.fly("content-left")) {
		Ext.select("#content-left li").on('mouseover', function(e) {
			sideMouseOut.cancel();
			var target = Ext.get(e.target);
			if (target.dom.tagName != "LI") {
				target = target.parent("LI");
			}
			
			Ext.select("#content-left li").each(function(ele){
				if (ele != target && !ele.hasClass('force-active')) {
					ele.removeClass('active-choice');
				}
			}, this);
			if (target.dom.tagName == "LI" && !target.hasClass("force-active") && !target.hasClass("active-choice")) {
				target.addClass("active-choice");
			}
		}, this);
		Ext.select("#content-left li").on('mouseout', function(e) {
			if (sideMouseOut) sideMouseOut.delay(300);
		}, this);
	}
	Ext.QuickTips.init();
});