/*
################################################
Homepage
*/

function link_enter(link){
kill_queue(link);
new Effect.Highlight(link, {duration: 0.4, startcolor:'#000000', endcolor:'#ed1eab', restorecolor:'#ed1eab', queue:{position:'end', scope:link, limit:1}});
}

function link_exit(link){
kill_queue(link);
new Effect.Highlight(link, {duration:0.5,startcolor:'#ed1eab', endcolor:'#000000', restorecolor:'#000000', queue:{position:'end', scope:link, limit:2}})
}

function kill_queue(this_queue){
  var queue = Effect.Queues.get(this_queue);
  queue.each(function(e) {e.cancel()});
}


function show_newsletter(){

  if($('mailing_panel').visible() == false){
    Effect.SlideUp('news_panel', {duration:0.5, queue:{position:'end', scope:'news', limit:2}});
    $('email_submit').show();
    $('spinner').hide();
    $('homepage_signup').innerHTML = 'Close';
    Effect.SlideDown('mailing_panel', {duration:1, afterFinish: mail_box_glow, queue:{position:'end', scope:'news', limit:2}});
  } else {
    Effect.SlideUp('mailing_panel', {duration:0.5, afterFinish: mail_box_glow, queue:{position:'end', scope:'news', limit:2}});
    $('homepage_signup').innerHTML = 'Sign up to newsletter';
    Effect.SlideDown('news_panel', {duration:1, queue:{position:'end', scope:'news', limit:2}});
  }
  return false;
}

function mail_box_glow(){
  if($('mailing_panel').visible() == true){
    new Effect.HighlightText('text_label', {duration:0.4,startcolor:'#626262', endcolor:'#ffffff', restorecolor:'#ffffff'});
  } else {
    $('text_label').style.color = ('#626262');
    $('text_label').innerHTML = 'Stay informed with the latest news of<br />Pod1\'s latest websites.';
  }
}

Effect.HighlightText = Class.create();
Object.extend(Object.extend(Effect.HighlightText.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    var options = Object.extend({ startcolor: '#ffff99' }, arguments[1] || {});
    this.start(options);
  },
  setup: function() {
    // Prevent executing on elements not in the layout flow
    if(this.element.getStyle('display')=='none') { this.cancel(); return; }
    // Disable background image during the effect
    this.oldStyle = {};

    // init color calculations
    this._base  = $R(0,2).map(function(i){ return parseInt(this.options.startcolor.slice(i*2+1,i*2+3),16) }.bind(this));
    this._delta = $R(0,2).map(function(i){ return parseInt(this.options.endcolor.slice(i*2+1,i*2+3),16)-this._base[i] }.bind(this));
  },
  update: function(position) {
    this.element.setStyle({color: $R(0,2).inject('#',function(m,v,i){
      return m+(Math.round(this._base[i]+(this._delta[i]*position)).toColorPart()); }.bind(this)) });
  },
  finish: function() {
    this.element.setStyle(Object.extend(this.oldStyle, {
      color: this.options.restorecolor
    }));
  }
});

function home_newsletter(){

	var form_email = $('email_address');
	var form_submit = $('email_submit');

	if(validate_email(form_email.value))
	{
		
    form_submit.hide();
  
    new Ajax.Request('/ajax/mailinglist',
      {
        method:'get',
        parameters: $('email_address').serialize(true),
        onSuccess: function(transport){
          var response = transport.responseText || "no response text";
          new Effect.HighlightText('newsletter_response', {duration:1,startcolor:'#ffffff', endcolor:'#626262', restorecolor:'#626262', afterFinish: good_email});
        },
        onFailure: function(){ bad_server();},
        onUninitialized: function(){ bad_server();}
      });

		return false;
	}
	else
	{
		
		UserEmailAddress = $('email_address').value;
		$('newsletter_response').value = 'Your email address appears to be invalid';
		$('newsletter_response').show();
		
		new Effect.HighlightText('newsletter_response', {duration:1,startcolor:'#FF0000', endcolor:'#FFFFFF', restorecolor:'#FFFFFF', afterFinish: bad_email(UserEmailAddress)});


		return false;
	}

	return false;
}

function bad_email(UserEmailAddress)
{
	$('newsletter_response').show();
	new Effect.HighlightText('newsletter_response', 
	{duration:0.4,
	startcolor:'#FFFFFF', 
	endcolor:'#FF0000', 
	restorecolor:'#FF0000'
	});

}

function bad_server(){
  $('newsletter_response').value = 'A problem has occured contacting the server.';
   $('email_spinner').hide();
   
	new Effect.HighlightText('text_label', {duration:0.4,startcolor:'#626262', endcolor:'#ffffff', restorecolor:'#ffffff'});
}

function good_email(){
    
	$('newsletter_response').show();
	$('newsletter_response').vlaue = 'Thank you for joining the Pod1 Mailing List';
    
    $('email_spinner').hide();
  	new Effect.HighlightText('newsletter_response', {duration:0.4,startcolor:'#626262', endcolor:'#ffffff', restorecolor:'#ffffff'});
  	setTimeout("show_newsletter()",3000);
}

/*
################################################
WHO WE ARE
*/



function changeclass_wwa(box){
  var box = box.id+'inner';
  if($(box).visible() == false){

    $(box).up('div').toggleClassName('box_expand');
    $(box).up('div').toggleClassName('box');
    Effect.SlideDown(box, {duration:0.8,queue:{position:'end', scope:'whoweare'}, beforeStart: close_open_boxes_wwa(box)});
    $(box).addClassName('openbox');
  } else {


    Effect.SlideUp(box, {duration:0.5,queue:{position:'end', scope:'whoweare'}});
    $(box).up('div').toggleClassName('box_expand');
    $(box).up('div').toggleClassName('box');
    $(box).removeClassName('openbox');

  }

}

function box_fix(box){
    $(box).up('div').toggleClassName('box_expand');
    $(box).up('div').toggleClassName('box');
}

function close_open_boxes_wwa(box){
  var unfolded_box = document.getElementsByClassName('openbox', 'div');
  var nodes = $A(unfolded_box);
  nodes.each(function(node){
      Effect.SlideUp(node, {duration:0.5, queue:{position:'end', scope:'whoweare'}});
      $(node).up('div').toggleClassName('box_expand');
      $(node).up('div').toggleClassName('box');
      node.removeClassName('openbox');
  });
}

function link_enter_wwa(link){
  kill_queue(link);
  //new Effect.Highlight(link, {duration: 0.2, startcolor:'#ffffff', endcolor:'#2c2c2c', restorecolor:'#2c2c2c', queue:{position:'end', scope:link, limit:1}});
}

function link_exit_wwa(link){
  kill_queue(link);
  new Effect.Highlight(link, {duration: 0.5, startcolor:'#2c2c2c', endcolor:'#ffffff', restorecolor:'#ffffff', queue:{position:'end', scope:link, limit:1}});
}

/*
################################################
NEWS
*/



function changeclass_news(box){
  var box = box.id+'inner';
  if($(box).visible() == false){

    $(box).up('li').toggleClassName('box_expand');
    $(box).up('li').toggleClassName('box');
    Effect.SlideDown(box, {duration:0.8,queue:{position:'end', scope:'whoweare'}, beforeStart: close_open_boxes_wwa(box)});
    $(box).addClassName('openbox');
  } else {


    Effect.SlideUp(box, {duration:0.5,queue:{position:'end', scope:'whoweare'}});
    $(box).up('li').toggleClassName('box_expand');
    $(box).up('li').toggleClassName('box');
    $(box).removeClassName('openbox');

  }

}

function box_fix(box){
    $(box).up('div').toggleClassName('box_expand');
    $(box).up('div').toggleClassName('box');
}

function close_open_boxes_wwa(box){
  var unfolded_box = document.getElementsByClassName('openbox', 'div');
  var nodes = $A(unfolded_box);
  nodes.each(function(node){
      Effect.SlideUp(node, {duration:0.5, queue:{position:'end', scope:'whoweare'}});
      $(node).up('li').toggleClassName('box_expand');
      $(node).up('li').toggleClassName('box');
      node.removeClassName('openbox');
  });
}

function link_enter_wwa(link){
  kill_queue(link);
  //new Effect.Highlight(link, {duration: 0.2, startcolor:'#ffffff', endcolor:'#2c2c2c', restorecolor:'#2c2c2c', queue:{position:'end', scope:link, limit:1}});
}

function link_exit_wwa(link){
  kill_queue(link);
  new Effect.Highlight(link, {duration: 0.5, startcolor:'#2c2c2c', endcolor:'#ffffff', restorecolor:'#ffffff', queue:{position:'end', scope:link, limit:1}});
}

/*
################################################
CAREERS
*/



function changeclass_careers(box){
  var box = box.id+'inner';
  if($(box).visible() == false){

    $(box).up('li').toggleClassName('box_expand');
    $(box).up('li').toggleClassName('box');
    Effect.SlideDown(box, {duration:0.8,queue:{position:'end', scope:'whoweare'}, beforeStart: close_open_boxes_wwa(box)});
    $(box).addClassName('openbox');
  } else {


    Effect.SlideUp(box, {duration:0.5,queue:{position:'end', scope:'whoweare'}});
    $(box).up('li').toggleClassName('box_expand');
    $(box).up('li').toggleClassName('box');
    $(box).removeClassName('openbox');

  }

}

function box_fix(box){
    $(box).up('div').toggleClassName('box_expand');
    $(box).up('div').toggleClassName('box');
}

function close_open_boxes_wwa(box){
  var unfolded_box = document.getElementsByClassName('openbox', 'div');
  var nodes = $A(unfolded_box);
  nodes.each(function(node){
      Effect.SlideUp(node, {duration:0.5, queue:{position:'end', scope:'whoweare'}});
      $(node).up('li').toggleClassName('box_expand');
      $(node).up('li').toggleClassName('box');
      node.removeClassName('openbox');
  });
}

function link_enter_wwa(link){
  kill_queue(link);
  //new Effect.Highlight(link, {duration: 0.2, startcolor:'#ffffff', endcolor:'#2c2c2c', restorecolor:'#2c2c2c', queue:{position:'end', scope:link, limit:1}});
}

function link_exit_wwa(link){
  kill_queue(link);
  new Effect.Highlight(link, {duration: 0.5, startcolor:'#2c2c2c', endcolor:'#ffffff', restorecolor:'#ffffff', queue:{position:'end', scope:link, limit:1}});
}

/*
################################################

*/













function newsletter(){
	var form_email = $('email_address');
	var form_submit = $('email_submit');
	var form_spinner = $('spinner');
	
	if(validate_email(form_email.value)){
    form_submit.hide();
    
    form_spinner.show();
    
    new Ajax.Request('/ajax/mailinglist',
      {
        method:'get',
        parameters: $('email_address').serialize(true),
        onSuccess: function(transport){
          var response = transport.responseText || "no response text";
          repsonse_action(response);
        },
        onFailure: function(){ alert('Something went wrong...') }
      });
		
		return false;
	} else {
    new Effect.SlideDown('signup_error', {duration:0.5});
		return false;
	}
	return false;
}

function show_error(){
  if($('signup_error').visible()){
    Effect.SlideUp('signup_error', {duration:0.5});
  }
}

function repsonse_action(response){

    Effect.SlideUp('form_signup');
    Effect.SlideDown('signup_thanks');
}

function validate_email(address)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(address))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function show_sendtoafriend(){

  if($('email_friend').visible() == false){
    Effect.SlideDown('email_friend', {duration:0.5});
    $('submit').show();
    $('spinner').hide();
  } else {
    Effect.SlideUp('email_friend', {duration:0.5});
  }
}

function sendtoafriend(){
 	
  if (ValidateSendToAFriendForm() == true){
  
  	
    $('submit').hide();
    //$('spinner').show();
    
    $('fromname').value = $('fromname').value.strip();
    $('fromemail').value = $('fromemail').value.strip();
    $('toname').value = $('toname').value.strip();
    $('toemail').value = $('toemail').value.strip();
    $('message').value = $('message').value.strip();
    
    var param = $('pod1mailing').serialize(true)+"&"+$('sendmeacopy').serialize(true)+"&"+$('fromname').serialize(true)+"&"+$('fromemail').serialize(true)+"&"+$('toname').serialize(true)+"&"+$('toemail').serialize(true)+"&"+$('message').serialize(true)+"&location="+window.location;
    //alert(param);
    new Ajax.Request('/ajax/send-to-a-friend',
      {
        method:'post',
        parameters: param,
        onSuccess: function(transport){
          var response = transport.responseText || "no response text";
          response_sendtoafriend_action(response);
        },
        onFailure: function(){ alert('Something went wrong...')
		}
      });
  
    return false;
  } else {
    return false;
  }
  
  return false;
}

function response_sendtoafriend_action(response){

  if(response == "true"){
    
    Effect.SlideUp('email_friend');
    $('email_link').innerHTML = "Page sent to "+$('toemail').value+" (Send another?)";
    $('toname').value = "";
    $('toemail').value = "";
  }
}

function ValidateSendToAFriendForm()
{
  var return_message = "";
  var valid_form = true;

  if($('fromname').value.strip() == ""){
    valid_form = false;
    return_message += "&nbsp;&nbsp;&nbsp;&bull; 'Your name' is empty<br />";
    new Effect.Highlight($('fromname'),{duration: 2, startcolor:'#ffffff', endcolor:'#ff99cc'});
  }

  if($('toname').value.strip() == ""){
    valid_form = false;
    return_message += "&nbsp;&nbsp;&nbsp;&bull; 'Your friends name' is empty<br />";
    new Effect.Highlight($('toname'),{duration: 2, startcolor:'#ffffff', endcolor:'#ff99cc'});
  }
  
  if(validate_email($('toemail').value.strip()) == false){
    valid_form = false;
    return_message += "&nbsp;&nbsp;&nbsp;&bull; Your Email is not valid<br />";
    new Effect.Highlight($('toemail'),{duration: 2, startcolor:'#ffffff', endcolor:'#ff99cc'});
  }
  
  if(validate_email($('fromemail').value.strip()) == false){
    valid_form = false;
    return_message += "&nbsp;&nbsp;&nbsp;&bull; Your friends Email is not valid.";
    new Effect.Highlight($('fromemail'),{duration: 2, startcolor:'#ffffff', endcolor:'#ff99cc'});
  }

  if(valid_form != true)
  {
  	
  	$('emailafrienderror').update('You have fields missing! <br />' + return_message);
  }
  
  return valid_form;
  
}




