var shakeIt = true;
var shakeNext = 0;
//var debugBox = null; // DEBUG

function initIntroPage() {
	var moveTimeStart = "";
	var moveTimeEnd = "";
	
	jQuery(".navigation").css("left",-800);
	
	/*
	jQuery("a.navigationimg").bind("mouseover",showNavigation);
	jQuery("a.navigationimg").bind("mouseout",hideNavigation);
	*/
	
	jQuery(".icon").bind("click",iconClicked);
	
	jQuery(".icon").bind("mouseover",showNavigation);
  jQuery(".navigation").bind("mouseover",keepNavigation);
  
	jQuery(".navigationcontainer").bind("mouseleave",hideNavigation);
	
	/*
	jQuery(".icon").bind("mouseover",showNavigation);
	jQuery(".icon").bind("mouseout",hideNavigation);
	
    jQuery(".navigation").bind("mouseover",keepNavigation);
    jQuery(".navigation").bind("mouseout",hideNavigation);
    */
    
    jQuery(this).everyTime(1500,'checkmovement',shakeMe,0);
    //jQuery("ul#ticker").liScroll({travelocity: 0.05});
    //jQuery("#playground").css("height","15px");
    
    //jQuery("#playgroundoptions").css("height","0");
    //jQuery("#playgroundtoggle").css("top","0");
 	jQuery("#introcloud").fadeIn(1000);
}

function iconClicked() {
	href = jQuery(this).siblings('.link').find('a').attr('href');
	window.location = href;
	return false;
}

function initMain() {
	/*
	// DEBUG
	jQuery('body').append('<div style="position: absolute; top: 10px; right: 10px; width: 500px; height: 600px; background-color: black; color: white; overflow: auto; z-index: 1000; text-align: left;" id="debugbox">DEBUG BOX</div>');
	debugBox = jQuery('#debugbox');
	*/

	initIntroPage();
	
	// jQuery(document).pngFix();
}

function debugOut(msg, elem) {
	var jElem = jQuery(elem);
	var time = '';
	var path = '#'+elem.id+'.'+elem.className;
	debugBox.prepend('<span style="display: block; width: 250px; float:left;">'+time+' ['+path+']</span>'+msg+'<br />');
}

function shakeMe() {
	//debugOut('shakeMe', this); // DEBUG
	
	if (shakeIt) {
		shakeElement = (shakeNext++) % 3;
		//var shakeElement = Math.floor(Math.random()*3);
		//debug: var shakeElement = 2;
	    jQuery(".navigationcontainer").eq(shakeElement).effect("shake",{times:10, distance:4},25);
	}
}

function showNavigation() {
	jQuery(this).parents('.navigationcontainer').addClass('navigationOpen');
	
	//debugOut('showNavigation - open: '+String(jQuery('.navigationOpen').length), this); // DEBUG
	
	//jQuery('#introcloud').clearQueue();
  dimIntrocloud();
	navDiv = jQuery(this).siblings('.navigation');
	shakeIt = false;
    navDiv.stop(true,false);
    navDiv.animate({
		left: '0'
	},{"duration":800,"easing":"swing"});
}

function hideNavigation() {
	var container = jQuery(this);
	if (!container.hasClass('navigationcontainer')) {
		container = container.parents('.navigationcontainer');
	}
	container.removeClass('navigationOpen');
	
	//debugOut('hideNavigation - open: '+String(jQuery('.navigationOpen').length), this); // DEBUG
	
	//alert(jQuery(this).attr("class"));
	//shakeIt = true;
	if (jQuery(this).hasClass("navigation")) {
		navDiv = jQuery(this);
	} else {
		navDiv = jQuery(this).find('.navigation');
	}
    navDiv.stop(true,false);
    navDiv.animate({
    	left: '-800'
     },{"duration":1200,"easing":"swing","complete":showIntrocloud})
}

function keepNavigation() {
	//debugOut('keepNavigation', this); // DEBUG
	
	jQuery(this).stop(true,false);
	jQuery(this).animate({
		left: '0'});
	//jQuery('#introcloud').hide();
	dimIntrocloud();
}

function dimIntrocloud() {
	//debugOut('dimIntrocloud', this); // DEBUG
	
    var introcloud = jQuery('#introcloud');
    introcloud.clearQueue();
    introcloud.fadeTo(500, 0.0);
}

function showIntrocloud() {
	//debugOut('showIntrocloud - open: '+String(jQuery('.navigationOpen').length), this); // DEBUG
	
	if (jQuery('.navigationOpen').length == 0) {
    var introcloud = jQuery('#introcloud');
    introcloud.clearQueue();
    introcloud.fadeTo(500, 1.0);
  }
}

function transformBackground() {
    jQuery(this).css("color","red");
   jQuery(this).animate({
    color: "red",
        backgroundColor: "#006dB1"
   },"fast");
}

jQuery(document).ready(initMain);