/*
 * decorator.js
 *
 * @name        decorator
 * @package     www.foto-de-sign.de
 * @category    script
 * @author      Marcel Jänicke Software <mail@mj-software.net>
 * @copyright   © Marcel Jänicke Software, 2012 - Alle Rechte vorbehalten.
 * @since       30.09.2011
 * @modified    14.01.2011
 */
// global mousepositions
var mTop;
var mTopTmp;
var mLeft;
var mLeftTmp;
// after 5 seconds the arrow will be hidden
var fiveTimes = 0;
// page is in startmode
var isStart = true;
//list of images
var images = new Array();
// current image index
var index;
// contact or links clicked
var isContactLinksClicked = false;
var isReferencesClicked = false;

$(window).load(function() {
    // check after 1 second in interval if mouseposition the same
    setInterval(function() {
    	checkMousePosition();
    }, 1000);
    
    // check clickarea an do function
    $(window).click(function(event) {
		if (checkArea(event) == "top") {
		    showCursor();
		    hideArrow();
		    showNavigation();
		}
		
		if (checkArea(event) == "right" && !isContactLinksClicked) {
		    slide(true);
		}
	
		if (checkArea(event) == "left" && !isContactLinksClicked) {
		    slide(false);
		}
	
		if (checkArea(event) == "bottom" && !isContactLinksClicked && !isReferencesClicked && $(".draggable.text.center").length == 0) {
			$src = "?page=download&source=" + $(".background img").attr("src");
		    $("iframe").attr("src", $src);
		} else if (checkArea(event) == "bottom" && isReferencesClicked) {
		    slide(event.pageX < $(window).width() / 2 ? false : true);
		}
    });
    
    $(window).trigger("resize");

    showNavigation();
    hideCursor();
    
    makeDraggable(true);
    
    $(".draggable").mouseleave(function(event) {
    	hideCursor();
    });

    $("#navigation li").each(function() {
		if ($(this).hasClass("padd")) {
		    $(this).click(function() {
				if ($(this).attr("id") == "liContact" || $(this).attr("id") == "liImprint") {
				    hideContent();
				    hideGradient();
				    isContactLinksClicked = true;
				    isReferencesClicked = false;
				    $(".background img").hide();
				    var image = new Image();
				    image.src = "resource/picture/" + $(this).attr("id") + "_tatjana_kunath.jpg";
				    $(image).load(function() {
				    	$(".background img").attr("src", image.src).fadeIn();
				    });
				    loadContent($(this).attr("id"));
				    isStart = false;
				} else if ($(this).attr("id") != "liBlog") {
				    hideContent();
				    isContactLinksClicked = false;
				    // if a categories name is REFERENZEN, neccessary
				    if ($(this).text() == "REFERENZEN") {
				    	isReferencesClicked = true;
				    } else {
				    	isReferencesClicked = false;
				    }
				    loadContent($(this).attr("id"));
				    isStart = false;
				    $.post("?page=home", {
				    	ajax : true,
				    	rel : $(this).find("a").text()
				    }, function(data) {
				    	prepareImages(data);
				    });
				}
		    });
		}
    });
});

$(window).mousemove(function(event) {
    // set global mousepositions
    mTop = event.pageY;
    mLeft = event.pageX;
    // show arrow im mousemove
    fiveTimes = 0;
    // if no animation active, show arrow on cursor position
    if ($("#outer").queue().length == 0 && $(".draggable:hover").length == 0) {
    	hideCursor();
    	// wenn Maus oben
    	if (checkArea(event) == "top") {
		    setArrowDetails(event, 92, 85, "arrow-top");
		    if (event.pageY <= 100) {
		    	showCursor();
		    	hideArrow();
		    	showNavigation();
		    } else if (event.pageY > 100 && isNavigationVisible() && !isArrowVisible()) {
		    	showCursor();
		    }
		}
		// wenn Maus rechts, nicht der erste Seitenaufruf vorliegt und nicht auf "Contact und Links" geklickt wurde
		else if (checkArea(event) == "right" && !isStart && !isContactLinksClicked) {
		    setArrowDetails(event, 70, 61, "arrow-right");
		    showArrow();
		    checkNavigationHiding();
		}
		// wenn Maus unten, nicht auf "Contact und Links" geklickt wurde und nicht auf "Referenzen" geklickt wurde
		else if (checkArea(event) == "bottom" && !isContactLinksClicked && !isReferencesClicked) {
		    setArrowDetails(event, 151, 88, "arrow-bottom");
		    showArrow();
		    checkNavigationHiding();
		}
		// wenn Maus links, nicht der erste Seitenaufruf vorliegt und nicht auf "Contact und Links" geklickt wurde
		else if (checkArea(event) == "left" && !isStart && !isContactLinksClicked) {
		    setArrowDetails(event, 70, 61, "arrow-left");
		    showArrow();
		    checkNavigationHiding();
		}

		else {
		    showArrow();
		    checkNavigationHiding();
		    // wenn der erste Seitenaufruf vorliegt
		    if (isStart) {
		    	setArrowDetails(event, 92, 85, "arrow-top");
		    }
		    else {
		    	// wenn auf "Contact und Links" geklickt wurde
				if (isContactLinksClicked) {
				    setArrowDetails(event, 92, 85, "arrow-top");
				} else {
					// alle anderen Fälle
				    setArrowDetails(event, 70, 61, event.pageX < $(window).width() / 2 ? "arrow-left" : "arrow-right");
				}
		    }
		}
    }
});

//set size of navigation and the navigation outer
$(window).resize(function() {
	if ($(window).width() < $(".background img").width()) {
		$("body").css("overflow-x", "auto");
		$("#outer").width($(".background img").width() + 50);
	} else {
		$("body").css("overflow-x", "hidden");
		$("#outer").width($(window).width());
	}
	
    $("#navigation").width($("#size img").width());
    var leftPosition = $(window).width() / 2 - $(".background img").width() / 2;
    $(".gradientLeft").css("left", leftPosition < 0 ? 0 : leftPosition - 1);
    $(".gradientRight").css("left", leftPosition < 0 ? $(".gradientLeft").width() + 3 : leftPosition + $(".gradientLeft").width() + 3);
    $(".gradientLine").css("left", leftPosition < 0 ? 0 : leftPosition);
});

function loadContent(site) {
	$("body").css("overflow-y", site == "liImprint" ? "auto" : "hidden");
	$("#outer, .background").css("position", site == "liImprint" ? "fixed" : "absolute");
    $.post("?page=home", {
	ajax : true,
	site : site
    }, function(data) {
    	$("body").append(data);
		makeDraggable(false);
		checkContent();
    });
}

function hideContent() {
    $(".draggable").remove();
}

function makeDraggable(fullType) {
    if (fullType) {
    	$(".draggable").draggable();
    }
    $(".draggable").mouseover(function(event) {
		if (isArrowVisible()) {
		    hideArrow();
		}
    });
}

function slide(rightClicked) {
    if (rightClicked) {
    	if (index < images.length - 1) {
    		index++;
    	} else {
    		index = 0;
    	}
    } else {
    	if (index > 0) {
    		index--;
    	} else {
    		index = images.length - 1;
    	}
    }
    slideImages(rightClicked);
    preloadImage();
}

function slideImages(rightClicked) {
    var slideWidth = rightClicked ? "-=" + $(window).width() : "+=" + $(window).width();
    var leftPosition = rightClicked ? $(window).width() : -$(window).width();
    var insert = "<div style='left: " + leftPosition + "px;' class='background'><img class='stretch' src='" + images[index]["name"] + "' />";
    insert += includeGradient();
    insert += "</div>";
    $(".background").addClass("remove");
    if (rightClicked) {
    	$(".background:last").after(insert);
    } else {
    	$(".background:last").before(insert);
    }
    hideGradient();

    $(".background").animate({
	left: slideWidth
    }, "slow", function() {
    	$(".remove").remove();
    	$(window).trigger("resize");
    	showGradient();
    });
}

function prepareImages(data) {
    $(".background img").hide();
    hideGradient();
    json = jQuery.parseJSON(data);
    images = json.images;
    index = 0;

    // load image in javascript image
    var image = new Image();
    image.src = images[index]["name"];
    $(image).load(function() {
	$(".background img").attr("src", images[index]["name"]).fadeIn().load(function() {
	    showGradient();
	    $(window).trigger("resize");
	    $(".background img").unbind("load");
	});
    });
    preloadImage();
}

function preloadImage() {
    if (index >= 0 && index < images.length - 1) {
        // load image in javascript image
        var image = new Image();
        image.src = images[index + 1]["name"];
    }
}

// set cursor alias pictures details
function setArrowDetails(event, width, height, image) {
    $("#arrow").width(width);
    $("#arrow").height(height);
    $("#arrow").css("left", event.pageX - $("#arrow").width() / 2);
    $("#arrow").css("top", event.pageY - 80);
    var isFirst = isReferencesClicked && $(".gradientLeft").text() == "" ? true : false;
    $("#arrow").css("background", "url(resource/picture/" + (isFirst ? "grey-" : "") + image + ".png)");
}

// check cursor area
function checkArea(event) {
    if (event.pageY <= $(window).height() * 0.25) {
    	return "top";
    }
    
    if (event.pageX >= $(window).width() / 2 && event.pageY > $(window).height() * 0.25 && event.pageY < $(window).height() - $(window).height() * 0.25) {
    	return "right";
    }
    
    if (event.pageY >= $(window).height() - $(window).height() * 0.25) {
    	return "bottom";
    }
    
    if (event.pageX <= $(window).width() / 2 && event.pageY > $(window).height() * 0.25 && event.pageY < $(window).height() - $(window).height() * 0.25) {
    	return "left";
    }
}

function checkMousePosition() {
    if (mTopTmp == mTop && mLeftTmp == mLeft && fiveTimes != 5) {
    	fiveTimes++;
    }
    // after 5 seconds hide arrow
    if (fiveTimes == 5) {
    	hideArrow();
    }
    mTopTmp = mTop;
    mLeftTmp = mLeft;
}

function showNavigation() {
    $("#outer").animate({
    	top : 0
    }, 500);
}

function checkNavigationHiding() {
	// wenn Navigation sichtbar und nicht der erste Seitenaufruf vorliegt
	if (isNavigationVisible() && !isStart) {
    	hideNavigation();
    }
}

function hideNavigation() {
    $("#outer").animate({
    	top : "-33px"
    }, 250);
}

function showArrow() {
    if (!isArrowVisible()) {
    	$("#arrow").fadeIn(250);
    }
}

function hideArrow() {
    $("#arrow").fadeOut(250);
}

function showCursor() {
    $("body").css("cursor", "pointer");
}

function hideCursor() {
    $("body").css("cursor", "url(resource/picture/none.png), url(none.ico), pointer");
}

function isArrowVisible() {
    return $("#arrow").css("display") != "none";
}

function isNavigationVisible() {
    return $("#outer").css("top") != "-33px";
}

function includeGradient() {
    var insert = "<div class='gradientLine'></div>";
    return insert + "<div class='gradientLeft'></div><div class='gradientRight'>";
}

function showGradient() {
    if ($(".gradientLeft").length == 0) {
    	$(".background").append(includeGradient());
    }
    var text = images[index]["text"];
    $(".gradientLeft").html(text.replace(/ /g, "&nbsp;"));
    
    if ($(".gradientLeft").text() != "") {
    	$(".gradientLeft").fadeIn(100);
    	$(".gradientRight").fadeIn(100);
    	if (images[index]["line"] == 1) {
    		$(".gradientLine").fadeIn(100);
    	}
    	if ($(".gradientLeft").width() > 200) {
    		$(".gradientRight").width($(".gradientLeft").width());
    	} else {
    		$(".gradientRight").width($(".gradientLeft").width() * 2);
    	}

		$(".gradientRight").css("background", "-moz-linear-gradient(left, rgba(0, 0, 0, 1) 0%, transparent 100%)"); // FF 3.6+
		$(".gradientRight").css("background", "-webkit-gradient(linear, left top, right top, color-stop(0%, rgba(0, 0, 0, 1)), color-stop(100%, transparent))"); // Chrome10+, Safari4+
		$(".gradientRight").css("background", "-o-linear-gradient(left, rgba(0, 0, 0, 1) 0%, transparent 100%)"); // OP 11.10+
		$(".gradientRight").css("background", "-ms-linear-gradient(left, rgba(0, 0, 0, 1 0%, transparent 100%)"); // IE10
		$(".gradientRight").css("background", "linear-gradient(left, rgba(0, 0, 0, 1) 0%, transparent 100%)"); // W3C
		
		$(".gradientLine").width($(".gradientLeft").width() + $(".gradientRight").width());
		$(".gradientLine").css("background", "-moz-linear-gradient(left, rgba(255, 255, 255, 0.7) 0%, transparent 100%)"); // FF 3.6+
		$(".gradientLine").css("background", "-webkit-gradient(linear, left top, right top, color-stop(0%, rgba(255, 255, 255, 0.7)), color-stop(100%, transparent))"); // Chrome, Safari4+
		$(".gradientLine").css("background", "-o-linear-gradient(left, rgba(255, 255, 255, 0.7) 0%, transparent 100%)"); // OP 11.10+
		$(".gradientLine").css("background", "-ms-linear-gradient(left, rgba(255, 255, 255, 0.7) 0%, transparent 100%))"); // IE10
		$(".gradientLine").css("background", "linear-gradient(left, rgba(255, 255, 255, 0.7) 0%, transparent 100%)"); // W3C
    }
}

function hideGradient() {
    $(".gradientLeft").fadeOut(100);
    $(".gradientRight").fadeOut(100);
    $(".gradientLine").fadeOut(100);
}
