$(document).ready(function() {

	$("body").removeClass("no-js").addClass("has-js");

	var $cycle = $("#intro-area").find(".carousel").first();
	var pedalCyclist;
	function stopCycle() {
		clearInterval(pedalCyclist);
	};
	function startCycle() {
		pedalCyclist = setInterval(cyclist, 6500);
	};
	function cyclist() {
		if ( $cycle.find(".slide-list").first().data("hover") == false ) {
			spinRound($cycle,"forward");
		}
	};

	$cycle.data("activeLI", 0).delegate(".back", "click", function(){
		spinRound($(this),"back");
		return false;
	}).delegate(".forward", "click", function(){
		spinRound($(this),"forward");
		return false;
	}).find(".slide").each(function(i){
		var $t = $(this);
		//make jQ display as block when .show()
		$t.not(":first-child").css("display","block").hide();
		var $c = $(this).closest(".carousel");
		var i2 = i + 1;
		$c.find(".control:last-child").clone().removeClass("back").addClass("item-control").html("<a href='#ITEMlink#' title='News item "+ i2 +"'>"+ i2 +"</a>")
			.insertAfter(".forward").bind("click",function(){
				spinRound($(this),"direct");
				return false;
			});
	}).end().find(".item-control a").last().addClass("active").end().end().find(".slide-list").first().hover(function() {
			$.data(this, 'hover', true);
		}, function() {
			$.data(this, 'hover', false);
		}
	).data('hover', false);

	function spinRound($this,direction) {
		if ( !direction ) {
			var direction = "forward";
		}
		var $c = $this.closest(".carousel");
		if ( $this.is($cycle) ) {
			$c = $this;
		}
		var len = $c.find(".slide").length;
		var act = $c.data("activeLI");
		var newAct;
		var newCon;

		if ( direction == "forward") {
			newAct = act + 1;

			if ( newAct == len ) {
				newAct = 0;
			}
			newCon = -(newAct+1);
		} else if ( direction == "back" ) {
			newAct = act - 1;

			if ( newAct == (len * -1) - 1 ) {
				newAct = -1;
			}
			newCon = -(newAct+1);
		} else if ( direction == "direct" ) {
			var base = $c.find(".item-control").index($this);
			newCon = (base - len);
			newAct = -1 * (newCon + 1);
		}

		$c.data("activeLI", newAct)
		.find(".slide").stop(true).css("opacity",1).fadeOut(800, function(){
			$c.find(".item-control a").removeClass("active").eq(newCon).addClass("active");
		}).eq(newAct).fadeIn(820, function(){
			stopCycle();
			startCycle();
		});
	};

	if ( $cycle.length > 0 ) {
		startCycle();
	}

	$("#main-menu").fadeIn(2000);

	function share(targetSite) {
		var d=document,
			e=encodeURIComponent,
			l=d.location,
			h=l.href,
			t=d.title;
		
		if ( targetSite == "facebook" ) {
			var f='https://www.facebook.com/share',
				p='.php?src=bm&v=4&i=1316463083&u='+e(h)+'&t='+e(t);
				1;
				try{if (!/^(.*\.)?facebook\.[^.]*$/.test(l.host))throw(0);share_internal_bookmarklet(p)}catch(z) {a=function() {if (!window.open(f+'r'+p,'sharer','toolbar=0,status=0,resizable=1,width=626,height=436'))l.href=f+p};if (/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}}void(0)
		} else if ( targetSite == "twitter" ) {
			window.open("http://twitter.com/home?status=MultiEmployer.com:%20"+e(l), "tweet", "height=450,width=550,resizable=1");
		} else if ( targetSite == "email" ) {
			//do nothing in here
		} else if ( targetSite == "print" ) {
			window.print();
		} else {
			alert("Error: No social media site defined.")
		}
	};
	$(".share-me li").click(function(){
		share(""+$(this).attr("class"));
	}).css({"cursor":"pointer"}).filter(".email").find("a").attr("href", function(i,val){
		return val+"?subject=MultiEmployer.com&body=Link:%20"+encodeURIComponent(document.location);
	});

});
