var animationCheck = 0;
var dogaja = 1;
var reachedOver = false;
var defixStoreHeaderTop = 0;

var clickDisabled = false;

var animationTime = 1000;
var easingType = 'easeOutCubic'; //easeOutQuint

jQuery.easing.def = easingType;

function openLink(object) {
	//alert("openLink" + $(object).attr("href"));
	if (isStore == true) {
		toggleHeaders($(object).attr("href"));
		//$(object).animate({}, animationTime*3, function() { redirectFromObjectTo($(this)); });
	} else {
		redirectTo($(object).attr("href"));
	}
}

function fondaStoreLink(object) {
	toggleHeaders($(object).attr("href"));
}

function redirectTo(url) {
	window.location.replace(url);
}

function redirectFromObjectTo(object) {
	redirectTo($(object).attr("href"));
}

/*
$("#xxxxxxx").slideUp({
	duration:animationTime,
	easing:easingType,
	complete:function(){
		//animation complete
}
});
*/

function goToStoreWithoutAnimation() {
	$("#page-content").css("display", "none");
	
	$("#store-header").css("bottom", "auto");
	$("#store-header").css("position", "fixed");
	$("#store-header").css("top", "60px");
	$("#store-header").css("z-index", "11000");
	
	$("#store-content").css("display", "block");
	
	$("#store-content-store-header-mimic").css("display", "block");
	
	isStore = true;
}

function setStoreMode() {
	isStore = true;
}

/*
//changed css
#page-content {
    display: none;
}


//changed css
#store-header {
    bottom: auto;
    position: fixed;
    top: 60px;
    z-index: 11000;
}

#store-content {
    display: block;
}
*/


//opening and closing the store
function animateStoreController(url) {
	clickDisabled = true;
	if (!isStore) {
		//squeezing page-content
		//$('#page-content').slideUp(5656); 
		$("#page-content").slideUp({
		duration:animationTime,
		easing:easingType,
		complete:function(){
			//animation complete
			redirectTo(url);
		}
		}); 
		
		//expanding store-content
		$("#store-content").slideDown({
		duration:animationTime,
		easing:easingType,
		complete:function(){
			//animation is complete (both animations)
			/*
			fixStoreHeader();
			clearInterval(animationCheck);
			clickDisabled = false;
			*/
		}
		}); 
		  
		  
	} else {
		defixStoreHeader();
		$("#page-content").slideDown({
		duration:animationTime,
		easing: "easeOutSine",
		complete:function(){
			//animation is complete (both animations)
			clickDisabled = false;
		}
		}); 
		  
		//$('#store-content').slideUp(5656);
		$("#store-content").slideUp({
		duration:animationTime,
		easing:"easeOutSine",
		complete:function(){
			//animation complete
			redirectTo(url);
		}
		}); 
		
		
		
	}
	isStore = !isStore;
}

function animateScrollToTop() {
	if (!isStore) {
		$('html, body').animate({scrollTop:0}, animationTime);
	} else {
		$('html, body').animate({scrollTop:0}, 0); //:TODO:this is a workaround !!!
	}
}

//this function calls store header and pushes it up when it reaches the store header
function checkAndPushStoreHeader() {
	if (!isStore) {
		animationCheck = setInterval("makeHeaderFollowContentUp()", 13);
	} else {
		animationCheck = setInterval("makeHeaderFollowContentDown()", 13);
	}
}

function makeHeaderFollowContentUp() {
	var contentPosition = $("#store-content").position();
	var headerPosition =$("#store-header").position();
	
	//$("#debugger-window").html("content : " + Math.ceil(contentPosition.top) + " header : " + Math.ceil(headerPosition.top)  + "dogaja: " + dogaja++);
	//$("#debugger-window").html("store-content.top : " + Math.ceil(contentPosition.top) + "<br>store-header.top : " + Math.ceil(headerPosition.top)  + "<br>display: " + $("#store-header").css("display"));
	//updateDebugInfo();
	
	if (contentPosition.top < headerPosition.top) { //drags the header with it
		$("#store-header").css("top", contentPosition.top + "px");
		//$(window).height();
		// if (headerPosition.top < 65) { fixStoreHeader(); } else { staticStoreHeader(); }	
		
		//:WARNING:workaround - if you do like below (reachedOver == false), if you scroll page-content and then go back, it sets its property to fixed (?!) {maybe because you set the top position here?!}
		 staticStoreHeader(); 
		
		//do this only the first time it flips
		if (reachedOver == false) {
			reachedOver = true;
		 	//alert('reached_over'); 
			
			staticStoreHeader(); //when it reaches over, put it into static mode (so it follows the content)
		}
	}
}

//TODO TODO TODO
function makeHeaderFollowContentDown() {
	var windowHeight = $(window).height();
	var headerPosition = $("#store-header").position();
	var contentPosition = $("#page-content").position();
	
	//$("#debugger-window").html(Math.ceil(headerPosition.top)  + " > " + windowHeight + " " + dogaja++ + $("#store-header").css("display"));
	
	//when it reaches the bottom of viewport, it switches to fix
	if (parseFloat((headerPosition.top+70)) > parseFloat(windowHeight)) { //drags the header with it
		fixStoreHeaderBottom(); //self-explanatory
		clearInterval(animationCheck); //stop with this function
		//$("#store-content").hide(); //since we are finished with animation - hide the store-content
		
		$("#store-content").stop(true, true);
		$("#page-content").stop(true, true);
		
	}
}

//UP - when it reaches over the viewport
function staticStoreHeader() {
	$("#store-header").css({
        'position': 'static',
        'bottom': 'auto'
		/*,
		'z-index': 'auto', // added from here
		'top': 'auto'
		*/
    });
}

//UP - when it reaches the top, we fix it
function fixStoreHeader() {
	//alert('wait for it');
	defixStoreHeaderTop = $("#store-header").css('top'); //store top value of defixed value
	
	$("#store-header").css({
        'position': 'fixed',
		'z-index': '11000',
		'bottom': 'auto',
		'top': '60px'
    });
	
	//to push the #store-content down when #store-header flips to fixed
	$("#store-content-store-header-mimic").css('display', 'block');
}

//DOWN - on the start
function defixStoreHeader() {
	$("#store-header").css({
        'position': 'static',
		'z-index': 'auto',
		'bottom': 'auto',
		'top': defixStoreHeaderTop + 'px'
    });
	
	//to push the #store-content up when #store-header flips to static
	$("#store-content-store-header-mimic").css({
        'display': 'none'
    });
}

//DOWN - when it reaches the bottom of viewport, it switches to fix
function fixStoreHeaderBottom() {
	$("#store-header").css({
        'position': 'fixed',
		'z-index': '11000',
		'bottom': '0px',
		'top': 'auto'
    });
}

function updateDebugInfo() {
	var contentPosition = $("#store-content").position();
	var headerPosition =$("#store-header").position();
	
	$("#debugger-window").html("store-content.top : " + Math.ceil(contentPosition.top) + "<br>store-header.top : " + Math.ceil(headerPosition.top)  + "<br>display: " + $("#store-header").css("position"));	
}

function toggleCartTop() {
		var cartDisplay = $("#store-cart-top").css('display');
		if (cartDisplay == "none") {
			showCartTop();
		} else {
			hideCartTop();
		}
		return false;
}

function showCartTop() {
	$("#store-cart-top").slideDown({
		duration:500,
		easing:easingType,
		complete:function(){
			//animation complete
		}
	}); 
}

function hideCartTop() {
		$("#store-cart-top").slideUp({
			duration:500,
			easing:easingType,
			complete:function(){
				//animation complete
			}
		}); 
}

function animateSubmenuBadge() {
	/*
	alert("animate");
	*/
	$('#submenu-badge').delay(500).animate({
		top: '0px',
		easing:easingType
	}, 500, function() {
	});
}

function toggleHeaders(url) {
	$('#store-header-shadow-top').hide();
	if (clickDisabled == false) {
		animateScrollToTop();
		checkAndPushStoreHeader();
		animateStoreController(url);  //this order is neccessary <- isStore gets inverted here
	}
	$('#store-header-shadow-top').hide();	
}



$(document).ready(function() {
	
	animateSubmenuBadge();
	
	$("#debugger-window").click(function() {
		updateDebugInfo();
	});
	
	
	
	
	/*
	$('#store-header').click(function() {
		toggleHeaders();
	});
	*/
	
	
	
	
	$('#store-cart-top-button-close').click(function() {
		hideCartTop();
	});
	
	$("#store-cart-top-button").hover(
	  function () {
		showCartTop();
	  },
	  function () {
		//$(this).removeClass("hover");
	  }
	);
	
	$("#store-cart-top").hover(
	  function () {
		//showCartTop();
	  },
	  function () {
		//hideCartTop();
		//because it is a tool now!
	  }
	);
	
	//if there is hash in the address
	if (window.location.hash) {
		if  (($(window).scrollTop() != $(document).height() - $(window).height())){ //if we are not at the end of page
			$('body').delay(1000).scrollTo( '-=100px', 800 );
		}
	}
	
	/*****************************************/
	
	/*
	animating store badge
	$("#store-badge").hover(
	  function () {
		$('#store-badge').animate({
			top: '-37px',
		}, 500, function() {
		});
	  },
	  function () {
		$('#store-badge').animate({
			top: '-61px',
		}, 500, function() {
		});
	  }
	);	
	*/
	
	/*
	top: -61px;
	*/

    // call it onload
    /*
	fixToBottom("#store-header");

    // assure that it gets called when the page resizes
    $(window).resize(function(){
        fixToBottom('#store-header');
    });
	
	 $(window).scroll(function () { 
		fixToBottom('#store-header');
	 });
	 */
});
