$(document).ready(function () {
    $isIE6 = false;
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
        $isIE6 = true;
    }

    $('body').removeClass('noJS');

    //footer size 
    $footerHeight = $(window).height() - $('#footer').position().top;
    if (!$.browser.msie) {
        $('#footer').css({ 'minHeight': $footerHeight });
    }

    var $scrollerWindow = $('#content');
    var $speed = 1500;
    var $homeSpeed = 10000;

    //header & menu navigation fade effect
    $('#nav li a').hover(function () {
        $(this).fadeTo('fast', 0.5);
    },
	function () {
	    $(this).fadeTo('fast', 1);
	});

    $('#logo').hover(function () {
        $(this).stop().animate({ backgroundPosition: '-210px 0px' }, $speed / 3);
    },
	function () {
	    $(this).stop().animate({ backgroundPosition: '0px 0px' }, $speed / 3);
	});


    //home switching content
    var homeSlides = $('#home .homeSlide');
    var currentHomeSlide = 0;
    if (homeSlides.length > 1) {
        var homeSwitcher = setInterval(switchHomeContent, $homeSpeed);
    }

    function switchHomeContent() {
        $(homeSlides[currentHomeSlide]).hide('normal');

        currentHomeSlide++;
        if (currentHomeSlide >= homeSlides.length)
            currentHomeSlide = 0;

        $(homeSlides[currentHomeSlide]).show('normal');
    }



    //music info/covers switching
    $("#music .info").click(function () {
        $(this).hide();
        $("#music .close").show(); 
        $('#music #credits').fadeIn();
    });
    $("#music .close").click(function () {
        $(this).hide();
        $("#music .info").show(); 
        $('#music #credits').fadeOut();
    });

    $("#music .info").show();
    $("#music .close").hide(); 
    $('#music #credits').hide();


    function clearNextPrev() {
        $('#nextControl').removeClass("noScroll goto_home goto_music goto_biography goto_contact");
        $('#prevControl').removeClass("noScroll goto_home goto_music goto_biography goto_contact");
    }

    function gotoHome() {
        $scrollerWindow.stop().scrollTo($('#home'), $speed, { axis: 'x', offset: { left: -20, top: 0} });
        $('h1').stop().animate({ backgroundPosition: '0px 0px' }, $speed);
        $('#title2').stop().animate({ backgroundPosition: '20px 0px' }, $speed + 300);
        $('#title3').stop().animate({ backgroundPosition: '20px 0px' }, $speed + 600);
        clearNextPrev();
        $('#nextControl').addClass("goto_music");
        $('#prevControl').addClass("noScroll");
        $('h1').text('Roy Edri');
        _gaq.push(['_trackEvent', 'Nav', 'Home']);
        return false;
    }

    function gotoMusic() {
        $scrollerWindow.stop().scrollTo($('#music'), $speed, { axis: 'x', offset: { left: -20, top: 0} });
        $('h1').stop().animate({ backgroundPosition: '-980px 0px' }, $speed);
        $('#title2').stop().animate({ backgroundPosition: '-1160px 0px' }, $speed + 300);
        $('#title3').stop().animate({ backgroundPosition: '-1360px 0px' }, $speed + 600);
        clearNextPrev();
        $('#nextControl').addClass("goto_biography");
        $('#prevControl').addClass("goto_home");
        $('h1').text('Music');
        _gaq.push(['_trackEvent', 'Nav', 'Music']);
        return false;
    }

    function gotoBiography() {
        $scrollerWindow.stop().scrollTo($('#biography'), $speed, { axis: 'x', offset: { left: -20, top: 0} });
        $('h1').stop().animate({ backgroundPosition: '-1960px 0px' }, $speed);
        $('#title2').stop().animate({ backgroundPosition: '-2340px 0px' }, $speed + 300);
        $('#title3').stop().animate({ backgroundPosition: '-2740px 0px' }, $speed + 600);
        clearNextPrev();
        $('#nextControl').addClass("goto_contact");
        $('#prevControl').addClass("goto_music");
        $('h1').text('Biography');
        _gaq.push(['_trackEvent', 'Nav', 'Biography']);
        return false;
    }

    function gotoContact() {
        $scrollerWindow.stop().scrollTo($('#contact'), $speed, { axis: 'x', offset: { left: -20, top: 0} });
        $('h1').stop().animate({ backgroundPosition: '-2940px 0px' }, $speed);
        $('#title2').stop().animate({ backgroundPosition: '-3520px 0px' }, $speed + 300);
        $('#title3').stop().animate({ backgroundPosition: '-4120px 0px' }, $speed + 600);
        clearNextPrev();
        $('#nextControl').addClass("noScroll");
        $('#prevControl').addClass("goto_biography");
        $('h1').text('Contact');
        _gaq.push(['_trackEvent', 'Nav', 'Contact']);
        return false;
    }

    function noScroll() {
        return false;
    }


    //navigation 
    $('.goto_home').live("click", gotoHome);
    $('.goto_music').live("click", gotoMusic);
    $('.goto_biography').live("click", gotoBiography);
    $('.goto_contact').live("click", gotoContact);
    $('.noScroll').live("click", noScroll);

    gotoHome();

    $("#submit").click(function () {
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

        var emailFromVal = $("#emailFrom").val();
        if (emailFromVal == '') {
            $("#emailFrom").addClass("error");
            hasError = true;
        } else if (!emailReg.test(emailFromVal)) {
            $("#emailFrom").addClass("error");
            hasError = true;
        }
        else {
            $("#emailFrom").removeClass("error");
        }

        var nameVal = $("#emailName").val();
        if (nameVal == '') {
            $("#emailName").addClass("error");
            hasError = true;
        }
        else {
            $("#emailName").removeClass("error");
        }

        var messageVal = $("#emailMessage").val();
        if (messageVal == '') {
            $("#emailMessage").addClass("error");
            hasError = true;
        }
        else {
            $("#emailMessage").removeClass("error");
        }


        if (hasError == false) {
            $(this).hide();
            $("#myForm").fadeOut("fast", function () {
                $("#myForm").before('<img src="images/loading.gif" alt="Loading" id="loadingImage" />');
            });

            $.post("example.php", function () {
                alert("success");
            })


            var jqxhr = $.ajax({
                url: "http://www.quickode.com/apps/basics/email/send",
                type: "get",
                data: { From: emailFromVal, Name: nameVal, Message: messageVal, To: 'info@royedri.com',Subject:"message from website" }

            })

                .complete(function () {
                    $("#loadingImage").fadeOut("fast", function () {
                        $("#loadingImage").before('<p>Your message has been sent. Cheers!</p>');
                    });
                });

        }

        return false;
    });
});
