/*
    Document   : site-wide jQuery
    Created on : 28-Jun-2010, 16:53:38
    Author     : Sarah Walsh, http://www.developbydesign.co.uk
    Description:
        2010 Quickening Design for Activideo - http://www.activideo.co.uk
*/
$(function () {

  if(document.forms[0]) {
    document.forms[0].onsubmit = function () {
      return validate()
    }
  }

  jQuery("input[type=text],textarea").focus(function() {
    $(this).css({
      'background-color': '#cccccc'
    });
  }).blur(function () {
    $(this).css({
      'background-color': '#ffffff'
    });
  });

  // apply to all <a> tags that have an href that starts with "http"
  jQuery("a[href^='http']").each(function(){
    jQuery(this).click(function() {
      // remove http:// and https://
      link = jQuery(this).attr('href');
      if (link.substring(0,7)=='http://') {
        link = link.substring(7);
      } else if (link.substring(0,8)=='https://') {
        link = link.substring(8);
      } else {
        return;
      }
      
      // spilt on '/'
      split1 = link.split('/');

      // compare href hostname to site hostname
      if (split1[0]!=location.hostname && split1[0] != 'ma.activideo.co.uk') {
        var myPopup = window.open( jQuery(this).attr('href'), "myPopup", "scrollbars=yes, toolbar=yes, directories=no, status=yes, menubar=yes, width=800,height=600");
        // if popups are disabled
        if (myPopup==null || typeof(myPopup)=="undefined") {
          jQuery(this).attr('target','_blank');
          return false;
        }
        if (window.focus) myPopup.focus();
        return false;
      }
    });
  });

  // apply to all <a> tags that have an href that starts with "http"
  jQuery("a[href*=.pdf]").each(function(){
    jQuery(this).click( function() {
  
      var myPopup = window.open( jQuery(this).attr('href'), "myPopup", "scrollbars=yes, toolbar=yes, directories=no, status=yes, menubar=yes, width=800,height=600");
      if (window.focus) myPopup.focus();
      // if popups are disabled
      if (myPopup==null || typeof(myPopup)=="undefined") {
        jQuery(this).attr('target','_blank');
        return false;
      }
      return false;

    });
  });

  jQuery('#case-studies').find('#content-padding').addClass('scroll-pane').jScrollPane({
    showArrows:true
  });

  if (jQuery("input[name='link_only']:checked").val()) jQuery("#all-content").hide('slow');
  jQuery("input[name='link_only']").click(function(){
    if (jQuery("input[name='link_only']:checked").val()) {
      jQuery("#all-content").hide('slow');
    } else {
      jQuery("#all-content").fadeIn("slow");
    }
  });


  if ( jQuery("body").attr("id") == 'frontpage') {
    var options = {
      newsList: "#news",
      startDelay: 6,
      placeHolder1: " []"
    }

    jQuery().newsTicker(options);
  }

  jQuery( "#textsizer a" ).textresizer({
    target: "#content-padding",
    type: "fontSize",
    sizes: [ "11px", "13px", "15px", "17px" ],
    selectedIndex: 1
  });
});

function validate() {
  validForm = false;
  firstError = null;
  errorstring = '';

  var x = document.forms[0].elements;

  for (var i=0;i<x.length;i++) {
    var j = x[i].className.split(' ');
    for (var k=0;k<j.length; k++) {
      if (j[k] == 'required' && !x[i].value) writeError(x[i],'This field is required');
    }
    if (x[i].value.match('http')) {
      alert("No URLs please");
      x[i].value="";
      return false;
    }
    if (!x[i].value && i==5) return false; //end of input fields
  }


  if (firstError) {
    firstError.focus();
    return false;
  }
  validForm = true;
  return validForm;
}

// change createElement to span to be on same line
function writeError(obj,message) {
  validForm = false;
  if (obj.hasError) return;
  obj.className += ' error';
  obj.onchange = removeError;
  var sp = document.createElement('p');
  sp.className = 'error';
  sp.appendChild(document.createTextNode(message));
  obj.parentNode.appendChild(sp);
  obj.hasError = sp;
	
  if (!firstError) firstError = obj;
}

function removeError()
{
  this.className = this.className.substring(0,this.className.lastIndexOf(' '));
  this.parentNode.removeChild(this.hasError);
  this.hasError = null;
  this.onchange = null;
}
