// JavaScript Document
//  gets a year
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
// makes an array for use in printDay and PrintDate
function dateArray0() 
{
     for (i = 0; i<makeArray0.arguments.length; i++)
     this[i] = makeArray0.arguments[i];
}
// works out date suffix for printYear
function nths(day)
{
     if (day == 1 || day == 21 || day == 31)
          return 'st';
     else
          if (day == 2 || day == 22)
               return 'nd';
               if (day == 3 || day == 23)
                    return 'rd';
               else return 'th';
}

function printYear(){
var date = new Date();
document.write(y2k(date.getYear()));
}

function printDay (){
var days = new dateArray0("Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday");
var date = new Date();
document.write(days[date.getDay()]);	
}
function printDate(){
var days = new dateArray0("Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday");
var months = new dateArray0('January','February','March','April','May','June','July','August','September','October','November','December');
var date = new Date();
document.write(date.getDate() + nths(date.getDate()) + " " +  months[date.getMonth()] + " " +  (y2k(date.getYear())));	
}
/*

Scroller box code:

*/

function scrollBox()
{
//(-scrollObj.offsetHeight + 408
//document.title = ' - scrollOn:' + scrollOn + ' - Stop:' + Stop + ' - speed:' + speed + 'pos:' + pos + ' - speed:' + speed + ' - speed:' + speed);
if (scrollOn && !Stop)
{
pos += speed;
scrollObj.style.top = pos+'px';
if (pos > 0)
{
	pos = 0;
	speed = -speed;
}
else if (pos < (-scrollObj.offsetHeight + 408))
{
	//pos = end - 1;
	speed = -speed;
}
clearTimeout(timeouter);
timeouter = setTimeout('scrollBox()', 100);
}
}

function startScroll()
{
pos = (parseInt(scrollObj.style.top) ? parseInt(scrollObj.style.top) : 0);
if (!speed) speed = Defaultspeed;
Stop = false;
scrollBox();
}

function stopScroll()
{
clearTimeout(timeouter);
Stop = true;
}
/* ============Cookies ==============*/
function get_cookie(Name) {
//alert("cookiename" + Name);
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function getAllCookies() {
  var cookies  = document.cookie.replace(/ /g,"").split(";");
   for (var i = 0; i < cookies.length; i++){
     cookiePair = cookies[i].split("=");
     document.write("["+cookiePair[0]+"] = ["+
                    cookiePair[1]+"]<br><br>");
   }
 }
 function deletecookie(name)
{
    var d = new Date();
    document.cookie = name +"=1;expires=" + d.toGMTString() + ";" + ";";

    //alert(document.cookie);
}
var lasttag ="";
function fadeout(target) {
	var myMorph = new Fx.Morph(target, { 'duration': 1000 });
	//transition fader to no opacity
	myMorph.start({
				  'filter':['alpha(opacity=100','alpha(opacity=0'],
				  '-moz-opacity':[1,0],
				  '-khtml-opacity': [1,0],
				  'opacity': [1,0]
				  });
}
function fadein(target) {
	var myMorph = new Fx.Morph(target, { duration: 1000 });
	//transition fader to 100% opacity
	myMorph.start({
				  'display':['none','block'],
				  'filter':['alpha(opacity=100','alpha(opacity=100'],
				  '-moz-opacity':[0,1],
				  '-khtml-opacity': [0,1],
				  'opacity': [0,1]
				  });
}
function morphback(target) {
	var back = new Fx.Morph(target, { duration: 001 });
	//transition fader to no opacity
	back.start({
				  'color':'#fff',
				  'background-image':'none'
				  });

}
function morphtext(target) {
	var text = new Fx.Morph(target, { duration: 001 });
	//transition fader to no opacity
	text.start({
				  'color':'#00ffcc'
				  });

}

function splashmenu() {
	fadeout('clockwrapper');
	fadein('art');
	setTimeout("fadein('branding')", 500); 
	setTimeout("fadein('life')", 1500); 

	
}
function prodload() {

	fadeout('clockwrapper');
	morphback('strapline');
	morphback('art');
	morphback('branding');
	morphback('life');
	setTimeout("fadein('art')", 500); 
	setTimeout("fadein('branding')", 1000); 
	setTimeout("fadein('life')", 1500); 
	
}
function showurl() {
	if (lasttag != ""){
			var text = new Fx.Morph(lasttag, { duration: 001 });
			text.start({
				  'color':'#999999'
	});
	}
	
	var uritag = window.location.hash;
	var pos = uritag.indexOf("#");
    var fragment = uritag.substr(pos+1);
	//alert(fragment);	
	var text = new Fx.Morph(uritag, { duration: 001 });
	text.start({
				  'color':'#00ffcc'
				  });
	lasttag = uritag;
}
function pageFadeOut(w4live){
	//function w4go () {window.location = w4live;};
	//setTimeout(w4go,3000);
	var bdy = $$('body');
	bdy[0].setStyle('background-image','none');
	fadeout (bdy[0]);
	//alert($('fader').length());
	//$('fader').Play();
	window.location = w4live;
}

function playIntro(){
	var x = get_cookie("splashintro");
	if (x=='play'){
		$('fader').Play();
		document.cookie="splashintro=" + 'noplay';
	}
}
function setSplashIntro()
{
	document.cookie="splashintro=" + 'play'; 	
}

function textSize(direction) {
		var allfont = $$('div','p', 'ul', 'li', 'label', 'span');
		
		 for (var i = 0; i < allfont.length; i++){
			//alert("object: " + allfont[i] + "Fontsize: " + allfont[i].getStyle('font-size').toInt());
			//get current font size 
			//parseFloat gives you just the numerical value
			var currentSize = allfont[i].getStyle('font-size').toInt(); 
			if (direction == "bigger") {
				var size = 	(currentSize + 1) + "px";
				allfont[i].setStyle('font-size',size );
			//	alert(allfont[i].getStyle('font-size'));
			
			} else if (direction == "smaller") {
				var size = 	(currentSize - 1) + "px";
				allfont[i].setStyle('font-size',size );
			}
		 }
}
function ts2(direction) {
		var contentfont = $('pagecontent').getStyle('font-size').toInt(); 
		alert($('pagecontent').getStyle('font-size').toInt());

}
/*
===============================================
	Contact form code
	
	
	
	global variables
===============================================
*/

var contactError, invalidEmail, text1,text2, page, pagetitle, pagetext, config; 



/* 

===============================================
	Functions that make Ajax calls
===============================================
*/

function sendContact(themepath,pluginpath) 
{

	var subj = $("subject");
	var mess = $("message");
	var name = $("name");
	var email = $("customer_mail");
	var captcha = $("captcha_code");
	contactMessage = "subject=" + subj.value + "&message=" + mess.value + "&name=" + name.value + "&email=" + email.value + "&captcha=" + captcha.value;
	var sendurl = themepath +"/contact/send_contact.php";
	var request =  new Request(
					 
					{
							url:sendurl, 
							method: 'post',
							parameters: contactMessage,
							onSuccess: function(response) 
							{
								if (response=='captchaerror'){
									mess= '<p>Captcha code wrong - try again</p>';
									$('contactmessage').style.color = "red";
									$('contactmessage').set('html', mess);
								} else if (response=='mailerror'){
									mess= '<p>Message delivery failed - please try again later</p>';
									$('contactmessage').style.color = "red";
									$('contactmessage').set('html', mess);
								} else {
									mess= '<p>'+ response + '</p>';
									$('contactmessage').style.color = "green";
									$('contactmessage').set('html', mess);
									
								}
								refreshCaptcha();
							},
							onFailure: function(response) 
							{
								mess = '<p>The send request failed</p>'
								$('contactmessage').style.color = "red";
								$('contactmessage').set('html', mess );
			
							}
						
					}).send(contactMessage);

}
/* 
===============================================
	Functions that respond to unsuccessful Ajax calls
===============================================
*/
function reportError(request) {

	var response = request.responseText;
	var d = $('contactmessage');
		d.innerHTML = "998: Unknown Error: " + response;
}

/* 
===============================================
	Validate Form Functions 
===============================================
*/

function validateForm() {
	contactError = false;
	invalidEmail = false;
	if (!valField($("subject"))) {contactError = true} else {$("subject").className = "f_reset";};
	if (!valField($("message"))) {contactError = true} else {$("message").className = "f_reset";};
	if (!valField($("name"))) {contactError = true} else {$("name").className = "f_reset";};
	if (!valField($("captcha_code"))) {contactError = true} else {$("name").className = "f_reset";};
	if (!valEmail($("customer_mail"))) {contactError = true} else {$("customer_mail").className = "f_reset";};
	if (contactError && !invalidEmail) {
		var e = $("contactmessage");
		e.style.color = "red";
		e.innerHTML = "<p>You must complete all the highlighted fields.</p>" ;
	}
	else if (contactError && invalidEmail) {
		var e = $("contactmessage");
		e.style.color = "red";
		e.innerHTML = "<p>You must complete all the highlighted fields, and give a valid email address.</p>" ;
	}
	else {sendContact(themepath,pluginpath)}
}

function valField (field) {
	if (field.value==null||field.value=="")
  		 {field.className = "f_error";return false;}
	else {return true}
}
function valEmail (field) {
	apos=field.value.indexOf("@");
	dotpos=field.value.lastIndexOf(".");
	if (apos<1||dotpos-apos<2) 
  		 {field.className = "f_error";invalidEmail=true;return false;}
	else {return true}
}					
function formreset() {
	var e = $("contactmessage");
		e.style.color = "#008be5";
		e.innerHTML = "<p>All the fields are required:</p>" ;
		$("subject").className = "f_reset";
		$("message").className = "f_reset";
		$("name").className = "f_reset";
		$("customer_mail").className = "f_reset";
		$("captcha_code").className = "f_reset";
		refreshCaptcha();
}
function refreshCaptcha() {
	$('captcha').src =  pluginpath + '/si-captcha-for-wordpress/captcha-secureimage/securimage_show.php?sid=' + Math.random();
}
