var settings = {
    textFadeSpeed: 400
};

$(document).ready(function () {
    page.init();
    jobs.init();
    
});

var mainCarousel = new Carousel({currentIndex:0, animationTime:800});
mainCarousel
	.bind("beforeChange", function (currentIndex, newIndex) {
	    $(".next, .prev").hide();
	    $(".show").hide();
	    // animate elements out
	    //animateSlideOut(currentIndex);
	})
	.bind("afterChange", function (newIndex, oldIndex) {
	    if (newIndex > 0) setTimeout(function () { $(".prev").fadeTo(1000, .4); }, 1000);
	    setTimeout(function () { $($(".slide-text")[newIndex]).fadeIn(settings.textFadeSpeed); }, 1000);
	    if (newIndex >= 0 && newIndex < mainCarousel.getChildren().length - 1) {

	        
	        setTimeout(function () {
	            $(".next").fadeTo(1000, 1, function () {

	            });
	        }, 1000);
	    }
	})
	.bind("afterElementChange", function (currentIndex, animationIndex, newIndex) {
	    if (animationIndex == newIndex) {
	        // animate elements in
	        animateSlideIn(animationIndex);
	    }
	});


function animateSlideIn(index){
	switch (index){
		case 0:
			jQuery.easing.def = "easeOutQuad";
			var width = parseInt($(".element1 .main").css("width"));
			$(".element1 .main").css("width", width / 2 + "px");

			jQuery.easing.def = "easeOutBounce";
			$(".element1 .main").animate({ width: width, opacity: 1 }, 700, "easeOutBounce", function(){
				jQuery.easing.def = "linear";
				if ($(window).width() > 769) {
					$(".show1").fadeIn(2000, function () {
							$(".show1").fadeOut(3000, function () {
									$(".show2").fadeIn(2000, function () {
											$(".show2").fadeOut(3000, function () {
													$(".show3").fadeIn(2000, function () {
															$(".show3").fadeOut(3000);
													});
											});
									});
							});
					});
				}
			});

			break;
		case 1:
			$(".element2").find(".illus1,.illus2,.illus3").addClass("center").removeClass("right left");
			break;
		case 2:
			$(".element3").find(".illus1,.illus2,.illus3").addClass("center").removeClass("right left");
			break;
		case 3:
			$(".element4").find(".illus1").addClass("center").removeClass("right left");
			break;
		case 4:
			$(".element5").find(".illus1").addClass("center").removeClass("right left");
			break;
		case 5:
			$(".element6").find(".illus1").addClass("center").removeClass("right left"); //$(".element6").find(".illus1").animate({ marginTop: 0}, 800, "easeOutBounce");
			break;
		case 6:
			$(".element7").find(".illus1").animate({opacity:1},1000); //$(".element7").find(".illus1").addClass("center").removeClass("right left");
			break;
	}
}

function animateSlideLeft(index){
	switch (index){
		case 0:
			$(".element1").find(".main").animate({opacity:0},300).end().find(".show").fadeOut(300);
			break;
		case 1:
			$(".element2").find(".illus1,.illus2,.illus3").addClass("left").removeClass("center");
			break;
		case 2:
			$(".element3").find(".illus1,.illus2,.illus3").addClass("left").removeClass("center");
			break;
		case 3:
			$(".element4").find(".illus1").addClass("left").removeClass("center");
			break;
		case 4:
			$(".element5").find(".illus1").addClass("left").removeClass("center");
			break;
		case 5:
			$(".element6").find(".illus1").addClass("left").removeClass("center"); //$(".element6").find(".illus1").animate({ marginTop: "-80%"}, 600, "easeOutBounce");
			break;
		case 6:
			break;
	}
}

function animateSlideRight(index){
	switch (index){
		case 0:
			break;
		case 1:
			$(".element2").find(".illus1,.illus2,.illus3").addClass("right").removeClass("center");
			break;
		case 2:
			$(".element3").find(".illus1,.illus2,.illus3").addClass("right").removeClass("center");
			break;
		case 3:
			$(".element4").find(".illus1").addClass("right").removeClass("center");
			break;
		case 4:
			$(".element5").find(".illus1").addClass("right").removeClass("center");
			break;
		case 5:
			$(".element6").find(".illus1").addClass("right").removeClass("center"); //$(".element6").find(".illus1").animate({ marginTop: "-80%"}, 600, "easeOutBounce"); //
			break;
		case 6:
			$(".element7").find(".illus1").animate({opacity:0},1000); //$(".element7").find(".illus1").addClass("right").removeClass("center");
			break;
	}
}

var subpageCarousel = new Carousel({currentIndex:0, animationTime:500,container:$("#subpages"),child:$("#subpages article")});


$("#slider-navigation .next").click(function(){
	if (mainCarousel.isRunning() || $(this).hasClass("disabled")) return false;
	var slideIndex = mainCarousel.getCurrentIndex();

	$($(".slide-text")[slideIndex]).fadeOut(settings.textFadeSpeed, function () {
	    animateSlideLeft(slideIndex);
        setTimeout(function () {
	        mainCarousel.next();
	    }, 200);
	});
	
//	animateSlideLeft(slideIndex);
//	setTimeout(function(){
//			$($(".slide-text")[slideIndex]).fadeOut(200,function(){
//				mainCarousel.next();
//			});
//	},200);

});

$("#slider-navigation .prev").click(function(){
	if (mainCarousel.isRunning() || $(this).hasClass("disabled")) return false;
	var slideIndex = mainCarousel.getCurrentIndex();

	$($(".slide-text")[slideIndex]).fadeOut(settings.textFadeSpeed, function () {
	    animateSlideRight(slideIndex);
	    setTimeout(function () {
			mainCarousel.prev();
        }, 200);
	});

//    animateSlideRight(slideIndex);
//    setTimeout(function () {
//        $($(".slide-text")[slideIndex]).fadeOut(200, function () {
//            mainCarousel.prev();
//        });
//    }, 200);

});

$("#main-menu a").click(function (e) {

    e.preventDefault();
    if (!page.obtainSlideAccess())
        return;

    // Index is defined by the position in the menu
    var index = $(this).index();
    page.toggleSubpage(true);

    var loaded = function () {

        $("#subpages article").removeClass("active-page");
        $(".sub-element" + index).addClass("active-page");

        page.setSubpageHeight();

        // Hide main menu
        $("#main-menu").removeClass("open");

        page.toggleSubpageSliding(false);
    }

    if ($("#subpages, #main-menu").hasClass("active")) {
        subpageCarousel.goTo(index - 1, false, loaded);
    } else {
        $("#subpages, #main-menu").addClass("active");
        subpageCarousel.goTo(index - 1, true, loaded);
    }
});

$("#backToHomeLink").click(function () {
    page.toggleSubpage(false);
    $("#subpages,#main-menu").removeClass("active");
    if (page.isMobile()) {
        $("#subpages").height(0);
        $("#page").height("100%");
    }
});

$("#main-menu .mobile-menu-element").unbind().click(function(e){
		e.stopPropagation();
		$("#main-menu").toggleClass("open");
});


var page = {
    isSubpageSliding: false,
    isSubpage: false,
    init: function () {
        page.setup();

        // splash main window
        animateSlideIn(0);
        $($(".slide-text")[0]).fadeIn(200, function () {
            $(".next").fadeIn(1000);
        });

        $('INPUT.inactive, TEXTAREA.inactive').focus(function () {
            $(this).removeClass('inactive');
            $(this).val('');
        });

        $('.next, .prev').click(function () {
            $(this).hide();
        });

        $('.banner').click(function () {
            $('.touch').fadeIn(300);
        });
        $('.touch').click(function () {
            $('.touch').fadeOut(300);
        });

        $('#subject').focus(function () {
            $('.error1').fadeOut(200);
        });
        $('#email').focus(function () {
            $('.error2').fadeOut(200);
        });
        $('#message').focus(function () {
            $('.error3').fadeOut(200);
        });

        $('.logo').click(function () {
            page.toggleSubpage(false);
            $("#subpages,#main-menu").removeClass("active");
            if (page.isMobile()) {
                $("#subpages").height(0);
                $("#page").height("100%");
            }
        });

        $('.sendbutton').click(function () {
            var error = false;
            if ($('#subject').val().length > 0 && $('#subject').hasClass('inactive') == false) {
                $('.error1').fadeOut(300);
            } else {
                error = true;
                $('.error1').fadeIn(300);
            }
            if ($('#email').val().length > 3 && $('#email').hasClass('inactive') == false && validateEmail($('#email').val())) {
                $('.error2').fadeOut(300);
            } else {
                error = true;
                $('.error2').fadeIn(300);
            }

            if ($('#message').val().length > 2 && $('#message').hasClass('inactive') == false) {
                $('.error3').fadeOut(300);
            } else {
                error = true;
                $('.error3').fadeIn(300);
            }

            if (!error) {

                $('.sendbutton').fadeOut(200, function () {
                    $('.wait').fadeIn(200);
                });
                $.getJSON("/Mail/Send", { title: $('#subject').val(), subject: $('#subject').val(), message: $('#message').val(), email: $('#email').val() }, function (data) {
                    $('.wait').fadeOut(300);
                    $('.contact').fadeOut(300, function () {
                        $('.success').fadeIn(300);
                        setTimeout("$('.success').hide();$('.sendbutton').show(); $('.contact').show();", 10000);
                    });
                });
            }

        });

        // while resizing
        $(window).resize(function () {
            page.setup();
        });
    },

    setup: function () {
        if (!page.isSubpage) {
            $("#page").height($(window).height());
            $(".show").fadeOut(0);
        }
        else {
            page.setSubpageHeight();
        }

        if ($(window).width() > $(window).height()) {
            $('.subpageBg').css('height', $(window).height() + ' !Important');
        }


    },

    obtainSlideAccess: function () {
        if (page.isSubpageSliding)
            return false;

        page.isSubpageSliding = true;
        return true;
    },

    toggleSubpageSliding: function (explicit) {
        page.isSubpageSliding = explicit != null ? explicit : !page.isSubpageSliding;
    },

    toggleSubpage: function (explicit) {
        page.isSubpage = explicit != null ? explicit : !page.isSubpage;
    },

    isMobile: function () {
        return $(window).width() < 600;
    },

    isTablet: function () {
        var w = $(window);
        return w.width >= 600 && w.width() < 1024;
    },

    isSmallDesktop: function () {
        var w = $(window);
        return w.width() >= 1024 && w.height < 900;
    },

    isLargeDesktop: function () {
        var w = $(window);
        return w.width() >= 1024 && w.height >= 900;
    },

    setSubpageHeight: function () {
        if (page.isMobile()) {

            // Calculate the height of the content
            var activePage = $("#subpages .active-page .subpageContent");

            // The height of the content + 100px to adjust for the menu and the padding at the bottom
            var h = activePage.height() + 100;

            // Set page height
            $("#subpages").removeClass("subpage-height");
            $("#subpages").height(h);
            $("#page").height(h);

        }
        else if (page.isTablet()) {

            $("#subpages").addClass("subpage-height");
            $("#page").height($(window).height());

        }
        else if (page.isSmallDesktop()) {

            $("#subpages").addClass("subpage-height");
            $("#page").height($(window).height());

        }
        else {

            $("#subpages").addClass("subpage-height");
            $("#page").height($(window).height());

        }
    }
};

var webBrowser = {
    shit: false,
    getInternetExplorerVersion: function ()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    {
        var rv = -1; // Return value assumes failure.
        if (navigator.appName == 'Microsoft Internet Explorer') {
            var ua = navigator.userAgent;
            var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat(RegExp.$1);
        }
        return rv;
    },

    checkForShittyBrowser: function () {
        var msg = "You're not using Internet Explorer.";
        var ver = webBrowser.getInternetExplorerVersion();

        if (ver < 9 && ver > 0) {
            webBrowser.shit = true;
            webBrowser.showGetOut();
        }

    },

    showGetOut: function () {
        $(document).ready(function () {
            $('#page').empty();

            $('#page').html('<div class="ie"><img src="/content/images/ielogo.png" class="sicklogo" /><h1>You are using a sick web browser :(</h1></div>');
            $('.ie').append("We're sorry, but you are using an old and really sucky browser, so we just can't let you into our super cool web site.<br/><br/>The good news is that we have the balls to tell you, so you can finally get a shiny new web browser like everybody else, and see how the web SHOULD look like. <br/><br/> ");
            $('.ie').append('<div class="break"><br/><br/><b>Get an up to date browser:</b> <br /><br /> <img src="/content/images/firefox.png" /><a href="http://www.mozilla.org/nb-NO/firefox/fx/" target="_blank">Firefox</a>                <img src="/content/images/chrome.png" /><a href="http://www.google.com/chrome"  target="_blank">Google Chrome</a>                <img src="/content/images/opera.png" /><a href="http://www.opera.com"  target="_blank">Opera</a>                <img src="/content/images/safari.png" /><a href="http://www.apple.com/safari/" target="_blank">Safari</a></div>');
        });
    }
};

webBrowser.checkForShittyBrowser();

var jobs = {
    closable: true,
    init: function () {
        jQuery.easing.def = "easeOutBounce";
        //setTimeout("$('.work-with-us').animate({ left: -250 }, 500);", 3000);
        //setTimeout("$('.work-with-us').animate({ left: -310 }, 500, function() {jQuery.easing.def = 'easeOutQuad'; });", 5000);
        if ($(window).width() > 600) {
            $('.work-with-us-blur').fadeTo(0, 0.01);
            if ($(window).width() < 1100) {
                $('.work-with-us-blur').css('left', 500);
            }
            setTimeout("$('.work-with-us-blur').animate({ opacity:1, left: -300 }, 600, function() { $('.work-with-us').css('left',-310); });", 5000);
        }
        $('.work-with-us').click(function () {
            jobs.closable = false;
            $('.work-with-us').animate({ left: 430 }, 500);
            $('.work-with-us-2').animate({ left: 0 }, 500, function () {
                jQuery.easing.def = "easeOutQuad";
                $('.jobphoto3').animate({ left: 400, top: 330 }, 500, function () {
                    $('.jobphoto2').animate({ left: 400, top: 195 }, 500, function () {
                        $('.jobphoto1').animate({ left: 530, top: 210 }, 500);
                        $('.jobphoto1').rotate({ animateTo: 10 }, 500);
                    });
                    $('.jobphoto2').rotate({ animateTo: -10 }, 500);
                });
                $('.jobphoto3').rotate({ animateTo: 10 }, 500);
            });
        });

        $('.job-finish').click(function () {
            $('.work-with-us').animate({ left: -310 }, 400);
            $('.work-with-us-2').animate({ left: -480 }, 500);
            $('.work-with-us-3').animate({ left: -480 }, 500);
            $('.jobphoto1').animate({ left: -500, top: 210 }, 500);
            $('.jobphoto2').animate({ left: -500, top: 200 }, 500);
            $('.jobphoto3').animate({ left: -500, top: 330 }, 500);
            $('.job-option').show();
            jobs.closable = true;
        });

        $('.job-option-yes').click(function () {
            jQuery.easing.def = "easeInQuad";
            $('.job-option').fadeOut(200);
            $('.jobphoto').each(function () {
                $(this).animate({ left: parseInt($(this).css('left')) + 430 }, 500);
            });
            $('.work-with-us').animate({ left: 860 }, 500);
            $('.work-with-us-2').animate({ left: 430 }, 500);
            $('.work-with-us-3').animate({ left: 0 }, 500);
        });

        $('.job-option-no').click(function () {
            $('.work-with-us').animate({ left: -310 }, 400);
            $('.work-with-us-2').animate({ left: -480 }, 500);
            $('.jobphoto1').animate({ left: -500, top: 210 }, 500);
            $('.jobphoto2').animate({ left: -500, top: 200 }, 500);
            $('.jobphoto3').animate({ left: -500, top: 330 }, 500);
            jobs.closable = true;
        });

        $('.work-with-us').mouseenter(function () {
            jQuery.easing.def = "easeOutBounce";
            if (jobs.closable && parseInt($('.work-with-us').css('left'))==-310) {
                $(this).animate({ left: 0 }, 1000, function () {
                    jQuery.easing.def = "easeOutQuad";
                });
            }

        });
        $('.work-with-us').mouseleave(function () {
            jQuery.easing.def = "easeOutExpo";
            if (jobs.closable && parseInt($('.work-with-us').css('left'))==0) {
                $('.work-with-us').animate({ left: -310 }, 400, function () {
                    jQuery.easing.def = "easeOutQuad";
                });
            }
        });
    }
};



function validateEmail(id) {
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(id);

}  

