// JavaScript Document

var twitteraccount;
var settwitteraccount;
var touts;
var lang;
var theLocation;
var scrolldelay;

jQuery.extend( jQuery.easing,
{
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	}
	
});

$(document).ready(function(){
	loadTwitter();
	
	if(jQuery.easing.easeOutQuad != undefined) {
		$(window).scroll(function(){ 
			window.clearTimeout(scrolldelay);
			scrolldelay = window.setTimeout(scrollclouds, 200); 
		});
	}
	
	if(touts != undefined) displaytouts();
	
});

function scrollclouds() {
	$('#cloud1, #cloud2, #cloud3, #cloud4').stop();
	$('#cloud1').animate({top:-($(window).scrollTop() * .4)}, 2550, 'easeOutQuad');	
	$('#cloud2').animate({top:-($(window).scrollTop() * .5)}, 2600, 'easeOutQuad');	
	$('#cloud3').animate({top:-($(window).scrollTop() * .35)}, 2400, 'easeOutQuad');	
	$('#cloud4').animate({top:-($(window).scrollTop() * .6)}, 2650, 'easeOutQuad');	
}

function displaytouts() {
	var t = touts;
	var totals = [];
	var tnum = 0;
	var count = [];
	
	var locations = [];
	locations['default'] = [];
		
	for(tn in t) {
		tt = t[tn];	
		if(tt.location == undefined) {
			tt.location = 'default';
			locations['default'].push(tt);
			locations['default'].name = 'default';
		} else {
			if(locations[tt.location] == undefined) {
				locations[tt.location] = [];
				locations[tt.location].name = tt.location;
			}
			locations[tt.location].push(tt);
		}
	}
	
	var gen = 0;
	var spec = 1;
	for(c in locations) {
		
		for (d in locations[c]) {
			if(d.toString() != 'name') {
				totals[locations[c].name] = spec;
				spec++;
			}
		}
		
		spec = 1;
		totals['general'] = ++gen;
	}
		
	if(totals['general'] == 1) {
		$('.tout').addClass('default');
	}
		
	for(ln in locations) {
		tnum = 0;
				
		if(locations[ln].length > 0) {
					
			if(totals[locations[ln].name] >= 1) {
				
				if(totals[locations[ln].name] > 1) {			
					
					// SET INTIAL TOUT
					settout(locations[ln][tnum]);		
									
					// LOOP THROUGH FEATURES				
					count[locations[ln][tnum].location] = 1;
					
					$('.tout.' + locations[ln][tnum].location).everyTime(5000, function(i) {
						var pos = $(this).attr('class').toString().replace('tout ', '');
						var num = count[pos];
						if(count[pos] + 1 > totals[pos]) count[pos] = 0;
						settout(locations[pos][count[pos]]);
						count[pos] = count[pos] + 1;
						
					});
					
				} else {
					settout(locations[ln][tnum]);
				} 
			}
		}	
	}
}

function settout(t) {
		
	var target = (t.target != undefined) ? t.target : '_blank';
	var side = (t.location != undefined) ? t.location : 'default';
		
	var theLink = $('.tout.' + side + ' a');
	var theImage = $('.tout.' + side + ' img');
	
	if(t.url != undefined) theLink.attr('href', t.url).attr('target', target);
	else theLink.removeAttr('href');
		
	theImage.attr('src', t.file);
	
	if(t.title != undefined) theImage.attr('alt', t.title).attr('title', t.title);
	else theImage.removeAttr('alt').removeAttr('title');
}

function loadTwitter() {
	if(twitteraccount == undefined) twitteraccount = 'virginmobilecan';
	var q = twitteraccount == 'virginmobilecan' ? '%23vfest' : '';
	$("#news").tweet({
		username: twitteraccount,
		join_text: "&nbsp;|&nbsp;",
		count: 5,
		auto_join_text_default: "we said,", 
		auto_join_text_ed: "we",
		auto_join_text_ing: "we were",
		auto_join_text_reply: "we replied to",
		auto_join_text_url: "we were checking out",
		loading_text: "<div class=\"loading\">LOADING NEWS...</div>",
		query:q
	});
	
}