(function($) {
	$.fn.fadeIn = function(speed, callback) {
		return this.animate({opacity: 'show'}, speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute("filter");
			if(jQuery.isFunction(callback))
				callback();
		});
	};
	$.fn.fadeOut = function(speed, callback) {
		return this.animate({opacity: 'hide'}, speed, function() { 
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute("filter");
			if(jQuery.isFunction(callback))
				callback();
		});
	};
	$.fn.sSlideDown = function(speed, callback) {
		return this.slideDown(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(jQuery.isFunction(callback))
				callback();
		});
	};
})(jQuery);

$(document).ready(function(){
	
	$(document).pngFix();
	$("a, button").click(function(){$(this).blur();});
	$("button").bt({
		width: '300px',
		padding: 10,
		spikeLength: 20,
		shrinkToFit: true,
		strokeStyle: "#aaa",
		cornerRadius: 0,
		fill: 'rgba(127, 127, 127, .7)',
		cssStyles: {color: '#fff'}
	});
	$("div#mainHeader div a").bt({
		padding: 10,
		positions: ['bottom', 'top'],
		spikeLength: 30,
		shrinkToFit: true,
		strokeStyle: "#aaa",
		cornerRadius: 0,
		fill: 'rgba(127, 127, 127, .7)',
		cssStyles: {color: '#fff'}
	});
	$("a.external").attr("target", "_blank");
//	alert($.cookie("lights"));
	$("a#lightswitch").text("off");
	if (!$.cookie("lights")) {
		$.cookie("lights", "light", { expires: 90 });
	} else if ($.cookie("lights")=="dark") {
		$("body").addClass($.cookie("lights"));
		$("a#lightswitch").text("on");
	}
	$("#lightswitch").click(function() {
		if ($.cookie("lights")=="light") {
			$.cookie("lights", "dark", { expires: 90 });
			$("body").addClass("dark");
			$("a#lightswitch").text("on").blur();
		} else if ($.cookie("lights")=="dark") {
			$.cookie("lights", "light", { expires: 90 });
			$("body").removeClass("dark");
			$("a#lightswitch").text("off").blur();
		}
//		alert ($.cookie("lights"));
		return false;
	});
	
	$("div.coListing").hover(
		function() { 
			$(this).addClass("coListingHover").children("h4").animate({fontSize:"150%"},333);
		},
		function() {
			$(this).removeClass("coListingHover").children("h4").animate({fontSize:"120%"},333);
		}
		);
	
});


function popUp(url, width, height) {
	window.open(url,'irishpackaging_popup','scrollbars=yes,resizable=yes,width='+width+',height='+height);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function df_validateForm() {
  var i,p,q,nm,nid,test,num,min,max,errors='',args=df_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) {
    test=args[i+2]; val=MM_findObj(args[i]);
    if (val) {
      nm=val.name; nid=val.id; nid=nid.replace(/_/g,' ');
      if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) {
	  p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nid+' must contain an e-mail address.\n';
        } else if (test!='R') {
	  num = parseFloat(val);
          if (test.indexOf('isSel')!=-1) {
	    if (num==-1) {
	      errors += '- '+nid+' must be chosen.\n';
	    }
          }
          if (test.indexOf('isRad')!=-1) {
	    if (!val.checked) {
	      errors += '- '+nid+' must be chosen.\n';
	    }
          }
	  else if (isNaN(val)) {
	    errors+='- '+nid+' must contain a number.\n';
	  }
          if (test.indexOf('inRange') != -1) {
	    p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nid+' must contain a number between '+min+' and '+max+'.\n';
          }
	}
      } else if (test.charAt(0) == 'R') errors += '- '+nid+' is required.\n';
    }
  }
  if (errors) alert('The following error(s) occurred:\n\n'+errors);
  document.df_returnValue = (errors == '');
}

function validateEmailInput(elem) {
	if (!validateEmail(elem.value)) elem.select();
}
	
function validateEmail(src) {
   var emailReg = "^[\\w-\.]*[\\w-_\.]\@[\\w-\.]*[\\w][\\w]+\\.+[\\w]+[\\w]$";
   var regex = new RegExp(emailReg);
   if (!(regex.test(src))) {
     alert('You have entered an invalid email address!\nPlease provide an email address like:\n\n- xx@yy.zz\n- joe@bloggs.com\n\nOr\n\n- aa@bb.cc.dd\n- john@joe.bloggs.com');
     return false;
   } else return true;
}

