// jquery - qTips/simple tooltip - eurocopter world
function simple_tooltip(target_items, name) {
    $(target_items).each(function(i) {
        $(this).tipsy({gravity: $.fn.tipsy.autoNS});
    });
}

function scroll_benefits()
{
	if ($('#developers-benefits p').filter(':visible').next().length > 0) {
		$('#developers-benefits h4, #developer-benefits p').filter(':visible').hide().next().filter(':visible').hide().next().fadeIn('slow').next().fadeIn('slow');
	}
	else {
		$('#developers-benefits h4, #developers-benefits p').hide().filter(':first').fadeIn().next().fadeIn();
	}
}

$(document).ready(function() {
	simple_tooltip('.call-to-action-button', 'tooltip');

	//	form validation - enforcing HTML5 behaviour
	$('form').submit(function() {
        var valid = true;
        $(this).find('[required]').not(':hidden').not('type=[submit]').each(function() {
            $(this).removeClass('form-error');
            if (!$(this).val()) {
                if (valid) {
                    // scroll to the first control that fails
                    $('body').scrollTo($(this), 1000);
                    $(this).focus();
                }
                $(this).addClass('form-error');
                valid = false;
            }
        });
        return valid;
    });

	//	merge into one function based on classes, shame on you Martin
	if ($('#tabber').length) {
		$('#tabs li:first-child a').addClass('active');
		$('#value-proposition > li').hide();
		$('#value-proposition > li:first-child').show();
		
		$('#tabs a').click(function() {
			$('#value-proposition > li').hide().filter(this.rel).show();
			$('#tabs a').removeClass('active');
			$(this).addClass('active');
			return false;
		});
	}

	if ($('#tabber-vertical').length) {
		$('#tabs-vertical li:first-child a').addClass('active');
		$('#explained > li').hide();
		$('#explained > li:first-child').show();

		$('#tabs-vertical a').click(function() {
			$('#explained > li').hide().filter(this.rel).show();
			$('#tabs-vertical a').removeClass('active');
			$(this).addClass('active');
			return false;
		});
	}

	//	please clean up
	if ($('#tabber-vertical3').length) {
			if (!document.domain.match('.*gametreelinux.com$')) {
				$('#tabs-vertical3 li a:first').addClass('active');
			}
			else {
				$('#tabs-vertical4 li a:first').addClass('active');
			}
		$('#explained3 > li').hide();
		if (!document.domain.match('.*gametreelinux.com$')) {
			$('#explained3 > li:first-child').show();
		}
		else {
			$('#engine').show();
		}

		$('#tabs-vertical3 a, #tabs-vertical4 a').click(function() {
			$('#explained3 > li').hide().filter(this.rel).show();
			$('#tabs-vertical3 a, #tabs-vertical4 a').removeClass('active');
			$(this).addClass('active');
			return false;
		});
	}

	if ($('#login-header #username').val() == '') {
		$('#login-header #username').focus();
	}
	else {
		$('#login-header #password').focus();
	}
	
	if ($('#developers-benefits').length) {
		$('#developers-benefits h4, #developers-benefits p').hide().filter(':first').fadeIn().next().fadeIn();		
		setInterval("scroll_benefits()", 7000);
	}

	$('span.required').hide();
	
	$('#flash').hide().fadeIn('slow');
	
	if ($('#datepicker').size()) {
	    $('#datepicker').datepick({dateFormat: 'MM d, yy'}); 
	}
	
	if ($('textarea.tinymce').size()) {
	    $('textarea.tinymce').tinymce({
    		// Location of TinyMCE script
    		script_url: '/js/tiny_mce/tiny_mce.js',
    		plugins : "safari,spellchecker,fullscreen",
    		// General options
    		theme: "advanced",		
    		theme_advanced_buttons1: "bold,italic,strikethrough,|,bullist,numlist,blockquote,|,justifyleft,justifycenter,justifyright,|,link,unlink,|spellchecker,|,fullscreen,code",
    		theme_advanced_buttons2: "formatselect,underline,justifyfull,forecolor,|,pastetext,pasteword,removeformat,|,media,charmap,|,outdent,indent,|,undo,redo,wp_help",
    		theme_advanced_buttons3: '',
    		theme_advanced_buttons4: '',

    		theme_advanced_toolbar_location:"top",
    		theme_advanced_toolbar_align:"left",
    		theme_advanced_statusbar_location:"bottom",
    		theme_advanced_resizing : true
    	});
    }

	if ($('textarea.tinymceforums').size()) {
	    $('textarea.tinymceforums').tinymce({
    		// Location of TinyMCE script
    		script_url: '/js/tiny_mce/tiny_mce.js',
    		plugins : "safari,spellchecker,fullscreen",
    		// General options
    		theme: "simple",		
    		theme_advanced_buttons1: "bold,italic,strikethrough,|,bullist,numlist,blockquote,|,justifyleft,justifycenter,justifyright,|,link,unlink,|spellchecker,|,fullscreen,code",
    		theme_advanced_buttons2: "formatselect,underline,justifyfull,forecolor,|,pastetext,pasteword,removeformat,|,media,charmap,|,outdent,indent,|,undo,redo,wp_help",
    		theme_advanced_buttons3: '',
    		theme_advanced_buttons4: '',

    		theme_advanced_toolbar_location:"top",
    		theme_advanced_toolbar_align:"left",
    		theme_advanced_statusbar_location:"bottom",
    		theme_advanced_resizing : true
    	});
    }
});

// JQuery tipsy plugin
(function(b){function a(d,c){this.$element=b(d);this.options=c;this.enabled=true;this.fixTitle()}a.prototype={show:function(){var f=this.getTitle();if(f&&this.enabled){var e=this.tip();e.find(".tipsy-inner")[this.options.html?"html":"text"](f);e[0].className="tipsy";e.remove().css({top:0,left:0,visibility:"hidden",display:"block"}).appendTo(document.body);var i=b.extend({},this.$element.offset(),{width:this.$element[0].offsetWidth,height:this.$element[0].offsetHeight});var c=e[0].offsetWidth,h=e[0].offsetHeight;var g=(typeof this.options.gravity=="function")?this.options.gravity.call(this.$element[0]):this.options.gravity;var d;switch(g.charAt(0)){case"n":d={top:i.top+i.height+this.options.offset,left:i.left+i.width/2-c/2};break;case"s":d={top:i.top-h-this.options.offset,left:i.left+i.width/2-c/2};break;case"e":d={top:i.top+i.height/2-h/2,left:i.left-c-this.options.offset};break;case"w":d={top:i.top+i.height/2-h/2,left:i.left+i.width+this.options.offset};break}if(g.length==2){if(g.charAt(1)=="w"){d.left=i.left+i.width/2-15}else{d.left=i.left+i.width/2-c+15}}e.css(d).addClass("tipsy-"+g);if(this.options.fade){e.stop().css({opacity:0,display:"block",visibility:"visible"}).animate({opacity:this.options.opacity})}else{e.css({visibility:"visible",opacity:this.options.opacity})}}},hide:function(){if(this.options.fade){this.tip().stop().fadeOut(function(){b(this).remove()})}else{this.tip().remove()}},fixTitle:function(){var c=this.$element;if(c.attr("title")||typeof(c.attr("original-title"))!="string"){c.attr("original-title",c.attr("title")||"").removeAttr("title")}},getTitle:function(){var e,c=this.$element,d=this.options;this.fixTitle();var e,d=this.options;if(typeof d.title=="string"){e=c.attr(d.title=="title"?"original-title":d.title)}else{if(typeof d.title=="function"){e=d.title.call(c[0])}}e=(""+e).replace(/(^\s*|\s*$)/,"");return e||d.fallback},tip:function(){if(!this.$tip){this.$tip=b('<div class="tipsy"></div>').html('<div class="tipsy-arrow"></div><div class="tipsy-inner"></div>')}return this.$tip},validate:function(){if(!this.$element[0].parentNode){this.hide();this.$element=null;this.options=null}},enable:function(){this.enabled=true},disable:function(){this.enabled=false},toggleEnabled:function(){this.enabled=!this.enabled}};b.fn.tipsy=function(g){if(g===true){return this.data("tipsy")}else{if(typeof g=="string"){var i=this.data("tipsy");if(i){i[g]()}return this}}g=b.extend({},b.fn.tipsy.defaults,g);function f(k){var l=b.data(k,"tipsy");if(!l){l=new a(k,b.fn.tipsy.elementOptions(k,g));b.data(k,"tipsy",l)}return l}function j(){var k=f(this);k.hoverState="in";if(g.delayIn==0){k.show()}else{k.fixTitle();setTimeout(function(){if(k.hoverState=="in"){k.show()}},g.delayIn)}}function e(){var k=f(this);k.hoverState="out";if(g.delayOut==0){k.hide()}else{setTimeout(function(){if(k.hoverState=="out"){k.hide()}},g.delayOut)}}if(!g.live){this.each(function(){f(this)})}if(g.trigger!="manual"){var c=g.live?"live":"bind",h=g.trigger=="hover"?"mouseenter":"focus",d=g.trigger=="hover"?"mouseleave":"blur";this[c](h,j)[c](d,e)}return this};b.fn.tipsy.defaults={delayIn:0,delayOut:0,fade:false,fallback:"",gravity:"n",html:false,live:false,offset:0,opacity:0.8,title:"title",trigger:"hover"};b.fn.tipsy.elementOptions=function(d,c){return b.metadata?b.extend({},c,b(d).metadata()):c};b.fn.tipsy.autoNS=function(){return b(this).offset().top>(b(document).scrollTop()+b(window).height()/2)?"s":"n"};b.fn.tipsy.autoWE=function(){return b(this).offset().left>(b(document).scrollLeft()+b(window).width()/2)?"e":"w"}})(jQuery);

// JQuery scrollTo plugin
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

