/**
 * @author Paul
 */
// JavaScript Document

$(document).ready(function(){
						   
	$('.1').flash({
						src: 'flash/1.swf',
						width : 982,
						height: 400,
						wmode: 'opaque'
						});
	
	$("slideTag").css("position", "absolute");
	
	function changeWidth (id) {
		var str = id + " > *"
		var numba = $(str).length;
		var b = numba*982;
		var nWidth = b.toString() + 'px';
		$(id).css('width',nWidth);
		return [b,numba];
	};
	
	changeWidth("#slides");
	var a = changeWidth("#slides");
	var maxMoves = a[1] - 1;
	var moves = 0;
	var curMargin = 0;
	var tagMargin = maxMoves * -113;
	$("#tags .inner").css("margin-top", tagMargin);
	var strNumber = '<span class="accent">0' + (moves + 1) + '</span> of 0' + a[1];
	$("#number").html(strNumber);
	
	function moveLeft () {
		
		if(moves < maxMoves) {
			curMargin = curMargin - 982;
			tagMargin += 113;
			$("#tags .inner").animate({marginTop:tagMargin},{ queue:false, duration:600 });
			$("#slides").animate({marginLeft:curMargin}, 600);
			moves += 1
			flashClass = '.' + (moves + 1);
			flashSrc = 'flash/' + (moves + 1) + '.swf';

			$(flashClass).flash({
								src: flashSrc,
								width : 982,
								height: 400,
								wmode: 'opaque'
								});
			strNumber = '<span class="accent">0' + (moves + 1) + '</span> of 0' + a[1];
			$("#number").html(strNumber);
			
			var learnMore = $(flashClass).attr("rel");
			$('#slideTag .more').attr({ href:learnMore });

		} else {
				
				tagMargin = maxMoves * -113;
				$("#tags .inner").animate({marginTop:tagMargin},{ queue:false, duration:1000 });
				$("#slides").animate({marginLeft:0}, 1000);
				
				curMargin = 0;
				moves = 0;
				flashClass = '.' + (moves + 1);
				flashSrc = 'flash/' + (moves + 1) + '.swf';
	
				$(flashClass).flash({
									src: flashSrc,
									width : 982,
									height: 400,
									wmode: 'opaque'
									});				
				strNumber = '<span class="accent">0' + (moves + 1) + '</span> of 0' + a[1];
				$("#number").html(strNumber);
				
				var learnMore = $(flashClass).attr("rel");
				$('#slideTag .more').attr({ href:learnMore });
				
			}
		
	}
	
	function moveRight () {
		
		if(moves > 0) {
			curMargin = curMargin + 982;
			tagMargin -= 113;
			$("#tags .inner").animate({marginTop:tagMargin},{ queue:false, duration:600 });
			$("#slides").animate({marginLeft:curMargin}, 600);
			moves -= 1
			flashClass = '.' + (moves + 1);
			flashSrc = 'flash/' + (moves + 1) + '.swf';

			$(flashClass).flash({
								src: flashSrc,
								width : 982,
								height: 400,
								wmode: 'opaque'
								});			
			strNumber = '<span class="accent">0' + (moves + 1) + '</span> of 0' + a[1];
			$("#number").html(strNumber);
			
			var learnMore = $(flashClass).attr("rel");
			$('#slideTag .more').attr({ href:learnMore });			
			
		}
		
	}
	
	function timer() {
		interval = setInterval(function () {
		
			if(moves < maxMoves) {
				curMargin = curMargin - 982;
				tagMargin += 113;
				$("#tags .inner").animate({marginTop:tagMargin},{ queue:false, duration:600 });
				$("#slides").animate({marginLeft:curMargin}, 600);
				moves += 1
				flashClass = '.' + (moves + 1);
				flashSrc = 'flash/' + (moves + 1) + '.swf';
	
				$(flashClass).flash({
									src: flashSrc,
									width : 982,
									height: 400,
									wmode: 'opaque'
									});				
				strNumber = '<span class="accent">0' + (moves + 1) + '</span> of 0' + a[1];
				$("#number").html(strNumber);
				
				var learnMore = $(flashClass).attr("rel");
				$('#slideTag .more').attr({ href:learnMore });
			
			} else {
				
				tagMargin = maxMoves * -113;
				$("#tags .inner").animate({marginTop:tagMargin},{ queue:false, duration:1000 });
				$("#slides").animate({marginLeft:0}, 1000);
				
				curMargin = 0;
				moves = 0;
				flashClass = '.' + (moves + 1);
				flashSrc = 'flash/' + (moves + 1) + '.swf';
	
				$(flashClass).flash({
									src: flashSrc,
									width : 982,
									height: 400,
									wmode: 'opaque'
									});				
				strNumber = '<span class="accent">0' + (moves + 1) + '</span> of 0' + a[1];
				$("#number").html(strNumber);
				
				var learnMore = $(flashClass).attr("rel");
				$('#slideTag .more').attr({ href:learnMore });
				
			}
		
		}, 12000);
		
	}
	
	timer();

	$("#rightArrow").click(function() {moveLeft(); clearInterval(interval); timer(); });
	$("#leftArrow").click(function() {moveRight(); clearInterval(interval); timer(); });

	
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

	$('.slide').click(function() {
		var app = $(this).attr('rel');
		var url = 'http://joimedia.com/' + app;
		window.location = url;
	});
	
// XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

	function changeTweet(target,numba) {
		
		$('#overlay').fadeIn(600,function() {
			$('.tweetList li').animate( {"opacity": "hide"}, 0 );
			$(target).animate( {"opacity": "show"}, 0 );
			$('.dot').removeClass("active");
			$('#dot_' + numba).addClass("active");
			$('#overlay').fadeOut(600);
		});
	};
	
	i = 0;
	function tweetTimer() {
		tweet = setInterval(function () {
		
			count = $('.tweetList > li').length;	
			if(i < (count-1) ) {
				i ++;
				} else {
				i = 0;
			}	
				  
			tweetLI = '.tweet_content_' + i;
			changeTweet(tweetLI,i);		
			
		},6500);
	}
	
	tweetTimer();
	
	function twitterInit() {
	
		var c = 0;
		var leftSpace = 0;
		while (c < tweetNumber) {
		
			$('#dots').append('<div id="dot_' + c + '" class="dot"></div>');
			
			c ++
		
		}
		var dotsWidth = $('#dots').width();
		leftSpace = (300 - dotsWidth)/2;
		$('#dots').css('left',leftSpace);
		$('#dot_0').addClass("active");

	}
	
	twitterInit();
	
	$('.dot').click(function() {

		var thisID = $(this).attr("id");
		thisID = thisID.substring(4);
		$('.dot').removeClass("active");
		$('#dot_' + thisID).addClass("active");
		i = thisID;
		tweetLI = '.tweet_content_' + thisID;
		changeTweet(tweetLI,thisID);
		clearInterval(tweet);
		tweetTimer();
		
	});
									 
});