var OTM = {
	preload : function() {
		var imgs = [
			'images/show-less.gif',
			'images/minus.gif'	
		];
		for(i = 0; i < imgs.length; i++) {
			x = new Image();
			x.src = imgs[i];
		}
	},

	nudge : function(obj,x,y) {
		$(obj).css('background-position',x + 'px ' + y + 'px');
	},

	frmFocus : function(obj) {
		var el = $(obj);

		if(el.hasClass('field-state-blank')) {
			el.val('');
			el.removeClass('gray');
			el.addClass('dark-gray');
		}
	},

	frmBlur : function(obj,blank) {
		var el = $(obj);
	
		if(el.val().trim() == '') {
			el.val(blank);
			el.removeClass('dark-gray');
			el.removeClass('field-state-typed');
			el.addClass('field-state-blank');
			el.addClass('gray');
		} else {		
			el.removeClass('field-state-blank');
			el.removeClass('field-state-typed');
			el.addClass('field-state-typed');
		}
	},

	pwdFocus : function(obj) {
		var el = $(obj);
		var pwd = el.next();
	
		pwd.val('');
		pwd.removeClass('hidden');
		el.addClass('hidden');
		pwd.focus();
	},

	pwdBlur : function(obj) {
		var pwd = $(obj);
		var el = pwd.prev();
	
		if(pwd.val() == '') {
			el.removeClass('hidden');
			pwd.addClass('hidden');
		}
	},

	reveal : function(section,src,callback) {
		var el = $(src);
		$('#' + section).show('blind',500,function() {
			var smImg = el.find('img[src$=show-more.gif]');
			var pImg = el.find('img[src$=plus.gif]');

			el.attr('title','Show Less');
			el.attr('onClick',"OTM.hide('" + section + "',this" + (callback != undefined ? ",'" + callback + "'" : '') + ")");
			smImg.attr('alt','Show Less');
			smImg.attr('src','images/show-less.gif');
			pImg.attr('alt','Show Less');
			pImg.attr('src','images/minus.gif');	
		});			
			
		if(callback != undefined) eval(callback);
	},

	hide : function(section,src,callback) {
		var el = $(src);
		$('#' + section).hide('blind',500,function() {
			var smImg = el.find('img[src$=show-less.gif]');
			var pImg = el.find('img[src$=minus.gif]');
	
			el.attr('title','Show More');
			el.attr('onClick',"OTM.reveal('" + section + "',this" + (callback != undefined ? ",'" + callback + "'" : '') + ")");
			smImg.attr('alt','Show More');
			smImg.attr('src','images/show-more.gif');
			pImg.attr('alt','Show More');
			pImg.attr('src','images/plus.gif');

			if(callback != undefined) eval(callback);
		});
	},

	pricingCallback : function() {
		var cell = $('#pricing-show-hide');
		if(cell.attr('valign') == 'top') {
			cell.attr('valign','bottom');
		} else {
			cell.attr('valign','top');
		}
	},

	twitter : {
		tweets : null,
		visibleTweet : 0,
		init : function() {
			$.getJSON('ajax/getLatestTweets.php', function(data){
				OTM.twitter.tweets = data;
				if(OTM.twitter.tweets.length > 0) {
					for(i = 0; i < OTM.twitter.tweets.length; i++) {
						OTM.twitter.tweets[i]['tweet'] = OTM.twitter.urlize(OTM.twitter.tweets[i]['tweet']);
					}

					OTM.twitter.updateTweet();
				}
			});
		},
		updateTweet : function() {
			var tweet = $("#tweet");
			tweet.hide();
				
			$("#tweettext").html('"' + OTM.twitter.tweets[OTM.twitter.visibleTweet]['tweet'] + '"');
			$("#tweettimestamp").html(OTM.twitter.tweets[OTM.twitter.visibleTweet]['updated']);
			tweet.fadeIn();
				
			if(OTM.twitter.tweets.length > 1) {
				OTM.twitter.visibleTweet = OTM.twitter.visibleTweet + 1;
				if(OTM.twitter.visibleTweet >= OTM.twitter.tweets.length) {
					OTM.twitter.visibleTweet = 0;
				}
				
				setTimeout(function() {
					OTM.twitter.updateTweet();
				},8000); 
			}
		},
		urlize : function(text) {
			text = text.replace(/(https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)/g,'<a href="$1" target="_blank" class="light-gray">$1</a>');
			text = text.replace(/(@([0-9A-Za-z]+))/g,'<a href="http://www.twitter.com/$2" target="_blank" class="light-gray" title="$2 twitter feed">$1</a>');
			text = text.replace(/(#([0-9A-Za-z]+))/g,'<a href="http://twitter.com/search?q=%23$2" target="_blank" class="light-gray" title="$1 twitter hash">$1</a>');
			return text;
		}
	},

	hero : {
		carousel : null,
		nextBtn : null,
		prevBtn : null,
		manual : false,
		currentTimeout : null,
		timeout : 9000,
		slides : 4,
		cycles : 2,
		makeManual : function() {
			OTM.hero.manual = true;
			if(OTM.hero.currentTimeout != null) {
				clearTimeout(OTM.hero.currentTimeout);
			}
		},
		advance : function(remain) {
			if(!this.manual && remain > 0 && this.carousel != null) {
				this.carousel.jcarousel('next');
				this.currentTimeout = setTimeout('OTM.hero.advance(' + --remain + ')',this.timeout);
			}
		}
	},

	demo : function() {
		$('#demo-reel').jwplayer({
			autostart : false,
			flashplayer : 'mediaplayerhw.swf',
			skin : 'jwplayer5/five/five.xml'
		});	
	}
};

$(document).ready(function() { 
	OTM.preload(); 
	OTM.hero.carousel = $('#promo-slider > ul').jcarousel({
		scroll : 1,
		visible : 1,
		animation: 1000,
		wrap: 'circular',
		easing: "easeInOutCubic",
		buttonNextHTML : '<div onmouseover="OTM.nudge(this,-426,0)" onmouseout="OTM.nudge(this,-388,0)" class="point absolute button-sprite hero-next-btn" style="right: 0; top: 93px" id="tab-right-btn"></div>',
		buttonPrevHTML : '<div onmouseover="OTM.nudge(this,-350,0)" onmouseout="OTM.nudge(this,-312,0)" class="point absolute button-sprite hero-prev-btn" style="left: 0; top: 93px" id="tab-left-btn"></div>'
	});
	
	OTM.hero.nextBtn = $('#promo-slider .hero-next-btn');
	OTM.hero.prevBtn = $('#promo-slider .hero-prev-btn');

	OTM.hero.nextBtn.click(OTM.hero.makeManual);
	OTM.hero.prevBtn.click(OTM.hero.makeManual);

	OTM.hero.currentTimeout = setTimeout('OTM.hero.advance(' + (OTM.hero.slides * OTM.hero.cycles) + ')',OTM.hero.timeout);
	OTM.twitter.init();

	$('a[name=modal]').click(function(e) {
		$('.window').hide();
		e.preventDefault();
		var id = $(this).attr('href');
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		$('#mask').fadeIn(500);
		$('#mask').fadeTo(500,0.8);
		var winH = $(window).height();
		var winW = $(window).width();
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
		$(id).fadeIn(500);
	});

	$('.window .close, .window .close2, .login-close, .window-close').click(function (e) {
		e.preventDefault();
		$('#mask, .window').hide();
    });

	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});

	OTM.demo();
});

