// starting the script on page load
$(document).ready(function() {
  // load navigation for IE 6
  if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {

    $(".box").html(document.getElementById('navigation').innerHTML);
    $("#navigation").empty();
  }

  //The following code is used to retrieve the querystring. The current pagemasters site receieves "a=LF" when incorrect username or password is entered fails.

  var querystring = location.search.replace('?', '').split('&'); // get querystring as an array split on "&"
  var queryObj = {}; // declare object

  // loop through each name-value pair and populate object
  for (var i = 0; i < querystring.length; i++) {
    var name = querystring[i].split('=')[0]; // get name 
    var value = querystring[i].split('=')[1]; // get value          
    queryObj[name] = value; // populate object
  }

  if (queryObj["a"] == "LF") {
    $.fn.colorbox({ inline: true, href: "#errorbox", width: "200", height: "100", transition: 'elastic', speed: 300, opacity: .3 });
  } else if (queryObj["a"] == "RE") {
    $("#errorbox").html("<p><strong>Please logon again.</strong></p>" +
        "<p><small>Invalid session or session expired.</small></p>");
    $.fn.colorbox({ inline: true, href: "#errorbox", width: "200", height: "100", transition: 'elastic', speed: 300, opacity: .3 });
  }


  // The services page receieves an anchor which is the name of the html page to load. i.e #editorial-production uses ajax to load editorial-production.html
  function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return decodeURIComponent(results[1].replace(/\+/g, " "));
  }

  var myFile = document.location.toString();

  if (myFile.match('#')) { // the URL contains an anchor
    // click the navigation item corresponding to the anchor
    thisPage = myFile.split('#')[1];

    if (thisPage.match('\\?'))
      thisPage = thisPage.split('?')[0];
    // THIS LINE IS JUST TO DEMONSTRATE HOW THE LOGIN ERROR WORKS.
    // WILL PROBABLY HAVE TO DELETE THIS LINE.
    // $("#login").attr({"action": $("#login").attr("action") + "#" + thisPage });
  }

  $.localScroll({ hash: true, easing: 'easeOutQuint', duration: 1000 });

  $("form#login").validate();

  $(".bttn_more").mouseover(function() {
    $(this).stop().animate({ "padding-right": "15px" }, { "duration": 200, "easing": "easeOutCirc" });
    $(this).css({ "background": "#262626 url(Images/arrow-right.gif) 40px center  no-repeat" });
  }).mouseout(function() {
    $(this).stop().animate({ "padding-right": "5px" }, { "duration": 200, "easing": "easeOutCirc" });
    $(this).css({ "background": "#00aeef" });
  });

  $('#cboxOverlay').click(function() {

    $(this).stop().fadeTo(200, 0).delay(200).hide();
    $('.marker div.shadow').fadeOut({ easing: 'easeOutQuint', duration: 500 });
    $('.link').css({ "color": "#404040" });

    //$('#employment_description').slideUp(200);
    //$('#employment_tab').slideDown(200);
    //$('#employment').css("border-bottom","1px solid #c4c4c4");

    $('#special-features').slideUp(200);
  });

  $('a.close').click(function() {
    $('#cboxOverlay').stop().fadeTo(200, 0).delay(200).hide();
    //$('#employment_description').slideUp(200);
    //$('#employment_tab').slideDown(200);
    //$('#employment').css("border-bottom","1px solid #c4c4c4");
    $('#special-features').slideUp(200);
  });

  $("#nav li a").click(function() {
    $("#nav li a").attr("class", "");
    $("#cboxOverlay").stop().fadeTo(200, 0).delay(200).hide();
    $(this).attr("class", "active");
  }).mouseover(function() {
    $(this).stop().animate({ "padding-top": "15px" }, { "duration": 200, "easing": "easeOutCirc" });
  }).mouseout(function() {
    $(this).stop().animate({ "padding-top": "5px" }, { "duration": 200, "easing": "easeOutCirc" });
  });

  $("#logo img").mouseover(function() {
    $(this).stop().animate({ "padding-left": "10px" }, { "duration": 200, "easing": "easeOutCirc" });
  }).mouseout(function() {
    $(this).stop().animate({ "padding-left": "0px" }, { "duration": 200, "easing": "easeOutCirc" });
  });

  $("input.bttn").mouseover(function() {
    $(this).stop().animate({ "padding-left": "6px" }, { "duration": 200, "easing": "easeOutCirc" });
  }).mouseout(function() {
    $(this).stop().animate({ "padding-left": "0px" }, { "duration": 200, "easing": "easeOutCirc" });
  }).focus(function() {
    $(this).stop().animate({ "padding-left": "6px" }, { "duration": 200, "easing": "easeOutCirc" });
  }).blur(function() {
    $(this).stop().animate({ "padding-left": "0px" }, { "duration": 200, "easing": "easeOutCirc" });
  })

  $(".img_animate").mouseover(function() {
    $(this).stop().animate({ "bottom": "-10px" }, { "duration": 200, "easing": "easeOutCirc" });
  }).mouseout(function() {
    $(this).stop().animate({ "bottom": "-20px" }, { "duration": 200, "easing": "easeOutCirc" });
  }).click(function() {
    $("#cboxOverlay").stop().fadeTo(200, 0).delay(200).hide();
  });

  /* FORM FUNCTIONAILITY */
  $('input.text').css('color', '#b8b8b8');
  $('textarea').css('color', '#b8b8b8');

  function inputblur() {
    if ($(this).attr('value') == '') {
      $(this).attr('value', $(this).attr('title')).css('color', '#b8b8b8');
    }
  }

  function inputfocus() {
    if ($(this).attr('value') == ($(this).attr('title'))) {
      $(this).attr('value', '').css('color', '#404040');
    }
  }

  $('input.text').blur(inputblur).focus(inputfocus);

  $('input#password').focus(function() {
    if ($(this).attr('value') == 'password') {
      $(this).attr('value', '').css('color', '#404040');
      //document.getElementById('password').type = 'password';

      document.getElementById("container-password").innerHTML = "<input id=\"password\" name=\"password\" type=\"password\" class=\"text required password\" value=\"\" />";      
      $("#password").focus();      
    }
  }).blur(function() {
    //if($(this).attr('value') == ''){	
    //$(this).css('color','#b8b8b8').attr('value','password');
    //document.getElementById('password').type = 'text';
    //}
  });

  $('input#username').focus(function() {
    if ($(this).attr('value') == 'client login') {
      $(this).attr('value', '').css('color', '#404040');
    }
  }).blur(function() {
    if ($(this).attr('value') == '') {
      $(this).css('color', '#b8b8b8').attr('value', 'client login');
    }
  });

  $('textarea').focus(function() {
    if ($(this).html() == 'Enquiry...') {
      $(this).html("").attr('value', '').css('color', '#404040');
    }
  }).blur(function() {
    if ($(this).attr('value') == '') {
      $(this).css('color', '#b8b8b8').html("Enquiry...").attr('value', 'Enquiry...');
    }
  });
});
