$(document).ready(function() {	
	
	var FIS = 125;
	var FOS = 175;
	var DS = 4500;

	$.fadeInNext = function(next) {
		
		if (next.find('img').attr('src') == null)
			fadeInAgain();
		
		next.fadeIn(FIS).delay(DS).fadeOut(FOS, function() {
			$.fadeInNext($(this).next());
		});
		
		/*
		next.fadeIn(FIS, function() {
			$(this).fadeOut(FOS);

			$.fadeInNext($(this).next());
		});
		*/
	}
	
	
	function fadeInAgain() {
		$('#easel_container div').hide().fadeInEach();
	}
	
	
	$.fn.fadeInEach = function() {
		
		this.eq(0).fadeIn(FIS).delay(DS).fadeOut(FOS, function() {
			$.fadeInNext($(this).next());
		});
		/*
		this.eq(0).fadeIn(FIS, function() {
			$(this).fadeOut(FOS);
			$.fadeInNext($(this).next());
		});
		*/
	};
	$('#easel_container div').hide().fadeInEach();


	//Hovers ---------------------------------------------------------
	$(".wallitem").live('mouseover', function() {
		var img = $(this).find('img'); 		
		img.attr("src", img.attr("src").replace("_off", "_on")); 
	});
   
	$(".wallitem").live('mouseout', function() {
		var img = $(this).find('img'); 		
		img.attr("src", img.attr("src").replace("_on", "_off")); 
	});   
   	
	
	

	//Start actions ----------------------------------------------------
	$('#poll').click(function() {
		loadModalContent('/modal/poll.php');
	});
	
	$('#fb').click(function() {
		loadModalContent('/modal/timeline.php');
	});
	
	$('#twooffpie').click(function() {
		loadModalContent('/modal/coupon.php?np=t');
	});


	function loadModalContent(url) {
		$('#modal-body').load(url, function() {
		  $('#modal-window').modal({
				keyboard: true,
				backdrop: true,
				show: true	
			})
		});
	}

	//Start animation ----------------------------------------------------
	var scrolling = false;
	var RIGHTBOUND = -630;
	var LEFTBOUND = 300;
	
	jQuery(function($){
		$("#btnleft").mousedown(function(){
			scrolling = true;
			startScrolling($("#bg"), "bg", "+=60px", "left");
			startScrolling($("#footer"), "footer", "+=60px", "left");
			startScrolling($("#wall"), "wall", "+=60px", "left");
		}).mouseup(function(){
			scrolling = false;
		});
		
		$("#btnright").mousedown(function(){
			scrolling = true;
			startScrolling($("#bg"), "bg", "-=60px", "right");
			startScrolling($("#footer"), "footer", "-=60px", "right");
			startScrolling($("#wall"), "wall", "-=60px", "right");
		}).mouseup(function(){
			scrolling = false;
			//$(this).stop(true, true);
		});
	});
	
	function startScrolling(obj, ele, param, dir) {
		var stopall = false;
		var position = obj.position();
		console.log( "obj is " + ele + " left: " + position.left + ", top: " + position.top );
		
		
		/*
		if ( (ele == "bg" ) && (position.left > LEFTBOUND)) {
			scrolling = false;
			stopall = true;
			//console.log("stopping");
		}
		
		if ( (ele == "bg" ) && (position.left < RIGHTBOUND)) {
			scrolling = false;
			stopall = true;
			//console.log("stopping");
		}
		*/

		if (!stopall) {
			obj.animate({"left": param}, 20, function(){
				if (scrolling) {
					startScrolling(obj, ele, param, dir);
				}
			});
		}
	}

});
