$(document).ready(function() {


    //Display tumblr feed
    /*getTumblr();*/


    //Initialise local scroll
    $.localScroll();

    //Bind scrolling function
    window.onscroll = function () {
        checkSection();
    }
    

    //Initialise homepage news slider
    $(".slider_home").dualSlider({
        auto:true,
        autoDelay: 8000,
        easingCarousel: "easeOutBack",
        durationCarousel: 800,
        durationDetails: 500
    });


    //Initialise portolio and labs slider
    $(".slider_portfolio, .slider_labs").dualSlider({
        auto:true,
        autoDelay: 8000,
        easingCarousel: "easeOutBack",
        durationCarousel: 800
    });






    //Bind dropdown menus
	$(".header .nav_dd").mouseover(function(){
		$(this).addClass("current_li").children().next().show();
		$(this).children().addClass("current");
		$(".mask").show();
	});
	$(".header .nav_dd").mouseleave(function(){
		$(this).removeClass("current_li").children().next().hide();
		$(this).children().removeClass("current");
        $(".mask").hide();
	});
	

	
	
	//Bind switchInput to form fields
	$(".input_field").focus(function(){
		switchInput($(this),$(this).attr("rel"));
	});
	$(".input_field").blur(function(){
		switchInput($(this),$(this).attr("rel"));
	});
    
    
    //Bind skills 
	$("ul.skills a.more").click(function(){
		($(this).html()=="Show more") ? $(this).html("Show less") : $(this).html("Show more");
		$(this).toggleClass("more_less");
		$("ul.skills .hidden").toggleClass("shown");
	});

   
    
   
}); 



//Clear field text, or replace guide text
function switchInput(x,y){
    if($(x).val()==y)
    {
        $(x).val("");
    }
    else if($(x).val()=="")
    {
        $(x).val(y);
    }
}




var Browser = {
  Version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}





//Check area and set navigation position accordingly
function checkSection(){
    



    var offset;

    switch(navigator.appName)
    {
      case 'Microsoft Internet Explorer':

        if (Browser.Version() >=7) {
            offset = document.documentElement.scrollTop;
        }
        else
        {
            offset = document.body.scrollTop;
        }
        break;

      default:
        offset = window.pageYOffset;
    }



    $(".position").html("..." + offset);



    if(offset<999){
        clearSection()
		$(".nav_home").addClass("selected");    
    
    }
    else if(offset>1500 && offset<3299){
        clearSection();
		$(".nav_portfolio").addClass("selected");        
    }
    /* else if(offset>3300 && offset<4999){
        clearSection();
		$(".nav_journal").addClass("selected");        
    }*/
    else if(offset>3300 && offset<4999){
        clearSection();
		$(".nav_labs").addClass("selected");        
    }
    else if(offset>5000 && offset<6999){
        clearSection();
		$(".nav_about").addClass("selected");        
    }
    else if(offset>7000){
        clearSection();
		$(".nav_contact").addClass("selected");        
    }
    
}

function clearSection(){
    $(".header .nav").removeClass("selected");
}


//User email registration
function userRegister()
{
    $.post("/signup.php", {email: $(".input_field_newsletter").val()},

        function(data){

            if(data.status==true)
            {
                $("#registerForm").hide();
                $(".messageRegister").addClass("messageSuccess").html("Thanks, <strong>" + data.email + "</strong> has been added to the list.").show();
            }
            else {
                $(".messageRegister").html(data.message).slideDown();
            }

    }, "json");
    return false;
}

//User feedback
function userFeedback()
{
    $.post("/feedback.php", {email: $(".field_feedback_email").val(), comment: $(".field_feedback_comment").val()},

        function(data){

            if(data.status==true)
            {
                $("#feedbackForm").hide();
                $(".messageFeedback").addClass("messageSuccess").html("Thanks, <strong>" + data.email + "</strong> your feedback has been sent.").show();
            }
            else {
                $(".messageFeedback").html(data.message).slideDown();
            }

    }, "json");
    return false;
}


//User contact
function userContact()
{
    $.post("/contact.php", {name: $(".field_contact_name").val(), email: $(".field_contact_email").val(), comment: $(".field_contact_comment").val()},

        function(data){

            if(data.status==true)
            {
                $("#contactForm").hide();
                $(".messageContact").addClass("messageSuccess").html("Thanks " + data.name + " your message has been sent.").show();
            }
            else {
                $(".messageContact").html(data.message).slideDown();
            }

    }, "json");
    return false;
}


function getTumblr(){

    

    $.getJSON("http://hellraiserrob.tumblr.com/api/read/json?callback=?", 

        function(data) {

        var html="";


            $.each(data.posts, function(i,posts){


            if(i==0 || i==5){
                 html += "<ul>";
            }

            var className="";

            if(i%2 == 0)
            {
                className="alt clearfix";

            }

            var title = "";




            switch(this.type){

                case "link":
                    title = this["link-text"];
                    break;
                case "video":
                    title = this["video-caption"];
                    break;
                case "regular":
                    title = this["regular-title"];
                    break;
                case "photo":
                    title = this["photo-caption"];
                    break;
                case "quote":
                    title = this["quote-text"];
                    break;


                default:
                    title= "Title not set";

            }

            var type = this.type;
            var date = this.date;
            var url = this["url-with-slug"];


            html += '<li class="'+className+'"><a class="'+type+'" href=' +url +'>' + title + '</a></li>';


            if(i==9 || i==4){
                 html += "</ul>";
            }


        });

          


            $(".tumblr").html(html);

     });

    

}















