
var ool = window.onload;
window.onload = function() {
  if (ool != null) ool();
  initExternalLinks();
  contactDHTML();
  slideDHTML('caseSlide', '.cases', 896, 0.6, 'btn_back', 'btn_moreWork' ); //solutions Page
  slideDHTML('navSlide', '#navSlide > ul', 698, 0.5, 'btn_flyout_back', 'btn_flyout_moreWork' ); //solutions Nav
  love();
  locations();
  nav();
  seo();
}

function seo() {
  var search = location.search;
  var showseo = null;
  search = search.replace(/\?/,'');
  var searchAttributes = search.split('&');
  if(search.length > 1){
    for(var no=0;no<searchAttributes.length;no++){
      var items = searchAttributes[no].split('=');
      eval("var "+items[0]+" = '"+items[1]+"';");
    }
  }

  if( $('seoSection') != null ){
    if(showseo != null && showseo == "true"){

    }
    else {
      $$('#seoSection')[0].className = 'seoClosed';
    }
  }

  if($('seoShow') != null ){
    $('seoShow').onclick = function(){
      $$('.seoClosed')[0].className = '';
      sIFR.replace(fmFont, {
        selector: '#seoSection h1',
        css: '.sIFR-root { color: #00AEEF; font-weight: bold; letter-spacing: -1; leading: -6; }',
        transparent: true,
        tuneHeight: '-10px',
        offsetTop: '-5px'
      });
      Effect.ScrollTo('seo1', {
        duration:'0.2',
        transition: Effect.Transitions.sinoidal
      });
      return false;
    } 
  }

  if($('seoHide') != null ){
    $('seoHide').onclick = function(){
      Effect.ScrollTo('wrapper', {
        duration:'0.1',
        afterFinish: function(){
          $$('#seoSection')[0].className = 'seoClosed';
          $('seoSection').setStyle("display", "block");
        },
        transition: Effect.Transitions.sinoidal
      });
      return false;
    }
  }

  if($('seoSection') != null){
    var count = 1;
    $('seoSection').select('.btn_next').each(function(elm){
      elm.count = count;
      count ++;
      elm.onclick = function(elm){
        seo = 'seo' + (this.count + 1);
        Effect.ScrollTo(seo, {
          duration:'0.1',
          transition: Effect.Transitions.sinoidal
        });
        return false;
      }
    });
  }


  if($$('.seoTop') != null){
    $$('.seoTop').each(function(elm) {
      elm.onclick = (function(){
        Effect.ScrollTo('seo1', {
          duration:'0.1',
          transition: Effect.Transitions.sinoidal
        });
        return false;
      });
    });
  }
}

function loveChoice(choice) {
  if($('webApplication').childElements()[0].hasClassName("selected")){
    $('webApplication').childElements()[0].toggleClassName("selected");
  }
  if($('webSite').childElements()[0].hasClassName("selected")){
    $('webSite').childElements()[0].toggleClassName("selected");
  }
  if($('webStrategy').childElements()[0].hasClassName("selected")){
    $('webStrategy').childElements()[0].toggleClassName("selected");
  }
  
  $(choice).toggleClassName("selected");
  $(choice).childElements()[0].checked = true;
}

function activateGallery(link) {
  $$('a[href="' + link +'"]')[0].simulate('click');
}

/**
 * Attaches an onclick event to each link with rel="external" that opens the
 * link in a new window.
 */

function initExternalLinks() {
  
  ($$('a[rel="external"]')).each(function(elm) {
    elm.onclick = openNewWindow;
  });

  function openNewWindow() {
    window.open(this.href);
    return false;
  }
}

//For 1 instance
var hover = null;
function nav() {
  ($$('.flyout')).each(function(elm) {
    Event.observe(elm.parentNode, 'mouseenter', function () {
      if(hover) {
        window.clearTimeout(hover);
      }
      elm.parentNode.addClassName('hover');
    })
    Event.observe(elm.parentNode, 'mouseleave', function () {
      function toggle(name) {
        elm.parentNode.toggleClassName(name);
      }
      hover = toggle.delay(0.6, 'hover');
    })
  });

  ($$('#navSlide > ul > li > a')).each(function(elm) {
    elm.onclick = (function() {
      elm.className = 'active';
    });
  });
 
}


function slideDHTML(slider, holder, width, trans, back, forward) {
   
  if($(slider)) {
        
    var size = 0;
    ($$(holder)).each(function(elm) {
      size = size + width;
    });
    $(slider).style.width = size + 'px';
    var end = size - width;

    $(back).onclick = function() {
      if($(slider).style.left != '0px' ) {
        if(($(slider).style.left).gsub('px', '').gsub('-','')%width == 0 ) {
          if($(slider).style.left == '-' + width + 'px') {
            new Effect.Move($(slider), {
              x: width,
              transition: Effect.Transitions.sinoidal,
              duration: trans,
              afterFinish: function show1(){
                new Effect.toggle(back, 'appear', {
                  duration: 0.2
                });
                                    
                if($(forward).style.display == 'none') {
                  new Effect.toggle(forward, 'appear', {
                    duration: 0.2
                  });
                }
              }
            });
          } else {
            new Effect.Move($(slider), {
              x: width,
              transition: Effect.Transitions.sinoidal,
              duration: trans,
              afterFinish: function hide1() {
                if($(slider).style.left == '-' + (end - width) + 'px') {
                  new Effect.toggle(forward, 'appear', {
                    duration: 0.2
                  });
                }
              }

            });
          }
        }
      }
      return false;
    }

    $(forward).onclick = function() {
      if($(slider).style.left != '-' + end +'px' ) {
        if(($(slider).style.left).gsub('px', '').gsub('-','')%width == 0 ) {
          if($(slider).style.left == '-' + (end - width) + 'px' || $(slider).style.left == (end - width) + 'px' ) {
            new Effect.Move($(slider), {
              x: -width,
              transition: Effect.Transitions.sinoidal,
              duration: trans,
              afterFinish: function hide(){
                new Effect.toggle(forward, 'appear', {
                  duration: 0.2
                });
                                    
                if($(back).style.display == 'none') {
                  new Effect.toggle(back, 'appear', {
                    duration: 0.2
                  });
                }
              }
            });
                         
          } else {
            new Effect.Move($(slider), {
              x: -width,
              transition: Effect.Transitions.sinoidal,
              duration: trans,
              afterFinish: function show() {
                if($(slider).style.left == '-'+ width +'px') {
                  new Effect.toggle(back, 'appear', {
                    duration: 0.2
                  });
                                        
                }
              }
            });
          }
        }
      }
      return false;
    }
  }
}


function contactDHTML() {
  if($('employees')) {

    if($('EmailContactUs_EmailContactUs_disabled').value != 'Future Medium' && $('btn_fmEmail').style.display == 'none') {
      new Effect.toggle('btn_fmEmail', 'appear', {
        duration: 0.5
      });
    }


    $('btn_fmEmail').onclick = (function() {
      $('EmailContactUs_EmailContactUs_disabled').value = 'Future Medium';
      $('EmailContactUs_EmailContactUs_to').value = 'Future Medium';

      new Effect.ScrollTo('EmailContactUs_EmailContactUs', {
        duration: '0.5' ,
        offset:-20
      });
      new Effect.toggle('btn_fmEmail', 'appear', {
        duration: 0.2
      });
      return false;
    });

    $('reset').onclick = (function() {
      if($('EmailContactUs_EmailContactUs_disabled').value != 'Future Medium') {
        $('EmailContactUs_EmailContactUs_disabled').value = 'Future Medium';
        $('EmailContactUs_EmailContactUs_to').value = 'Future Medium';

        new Effect.ScrollTo('EmailContactUs_EmailContactUs', {
          duration: '0.5' ,
          offset:-20
        });
        new Effect.toggle('btn_fmEmail', 'appear', {
          duration: 0.2
        });
      }
    });

    var size = 0;
    ($$('.employee')).each(function(elm) {
      size = size + 126;
    });

    $('employeeList').style.width = size + 'px';

    var end = (Math.ceil(size/756)*756 - 756);
          
    ($$('.disabled')).each(function(elm) {
      elm.onclick = function() {
        return false;
      }
    });

    ($$('.employee > a')).each(function(elm) {
      elm.onclick = function() {
        new Effect.ScrollTo('EmailContactUs', {
          duration: '0.5' ,
          offset:-20
        });
        if($('EmailContactUs_EmailContactUs_disabled').value == 'Future Medium') {
          new Effect.toggle('btn_fmEmail', 'appear', {
            duration: 1
          });
        }
        $('EmailContactUs_EmailContactUs_disabled').value = elm.id;
        $('EmailContactUs#EmailContactUs_to').value = elm.id;
        return false;
      }
    });

        
    ($$('.btn_prev')).each(function(elm) {
      elm.onclick = function() {
        if(elm.className == "btn btn_prev disabled") {
          return false;
        }
        if($('employeeList').style.left == '-' + (end) + 'px') {
          $('btn_next').toggleClassName('disabled');
        }

        if($('employeeList').style.left != '0px' ) {
          if(($('employeeList').style.left).gsub('px', '').gsub('-','')%756 == 0 ) {
            if($('employeeList').style.left == '-756px') {
              new Effect.Move($('employeeList'), {
                x: 756,
                transition: Effect.Transitions.sinoidal,
                duration: 0.6,
                afterFinish: function show1(){
                  $('btn_prev').toggleClassName('disabled');
                }
              });
            } else {
              new Effect.Move($('employeeList'), {
                x: 756,
                transition: Effect.Transitions.sinoidal,
                duration: 0.6,
                afterFinish: function hide1() {
                  if($('slide').style.left == '-' + (end - 756) + 'px') {
                    $('btn_next').toggleClassName('disabled');
                  }
                }

              });
            }
          }
        }
        return false;
      }
    });

    ($$('.btn_next')).each(function(elm) {
      elm.onclick = function() {
        if(elm.className == "btn btn_prev disabled") {
          return false;
        }
        if($('employeeList').style.left == '0px') {
          $('btn_prev').toggleClassName('disabled');
        }

        if($('employeeList').style.left != '-' + end +'px' ) {
          if(($('employeeList').style.left).gsub('px', '').gsub('-','')%756 == 0 ) {
            if(($('employeeList').style.left == '-' + (end - 756) + 'px') || ($('employeeList').style.left == '' + (end - 756) + 'px')) {
              new Effect.Move($('employeeList'), {
                x: -756,
                transition: Effect.Transitions.sinoidal,
                duration: 0.6,
                afterFinish: function hide(){
                  $('btn_next').toggleClassName('disabled');
                }
              });
            } else {
              new Effect.Move($('employeeList'), {
                x: -756,
                transition: Effect.Transitions.sinoidal,
                duration: 0.6,
                afterFinish: function show() {
                  if($('employeeList').style.left == '-756px') {
                                    
                }
                }
              });
            }
          }
        }
        return false;
      }
    });
  }


       
}

function love() {
  if ($$('.btn_share_love')[0] != undefined) {
    $$('.btn_share_love')[0].onclick = function(){
      if ($('fm_love').getStyle('display') == 'none')
        new Effect.SlideDown('fm_love', {duration: 0.5});
      return false;
    }
  }
  
  if ($$('.btn_letsdoit')[0] != undefined) {
    $$('.btn_letsdoit')[0].onclick = function(){
      if ($('fm_love').getStyle('display') == 'none')
        new Effect.SlideDown('fm_love', {duration: 0.5});
      return true;
    }
  }
  
  if ($$('.btn_share_close')[0] != undefined) {  
    $$('.btn_share_close')[0].onclick = function() {
      new Effect.SlideUp('fm_love', {duration: 0.3});
      return false;
    }
  }
}


function checkLoveForm() {
  var form = document['forms']['FMLove'];
  var params = $('FMLove').serialize(true);
  
  if (params['interestType'] == undefined ||
      params['interestType'] == null ||
      params['interestType'] == '') {
    $('interestType_errorMessage').removeClassName('invisible');
    return false;
  }
    
    
 
  params['ajaxContext'] = 'true';
  $$('.ajaxLoading')[0].toggleClassName('invisible');
  var req = new Ajax.Request(form.action, {
    parameters: params,
    method: 'post',
    onSuccess: function(transport){
      var response = transport.responseText;
      $('fm_love_inner').replace(response);
      love();
      if ($$('.alertMessage')[0].innerHTML != '' &&
          $$('.alertMessage')[0].innerHTML != null &&
          $$('.alertMessage')[0].innerHTML != undefined) {
        
        $('fm_love_inner').removeClassName('show_form');
        $('fm_love_inner').addClassName('show_message');
      }
        
    }
  });

  return false;
  
  //return true;
}

function resetLoveForm() {
  $('fm_love_inner').removeClassName('show_message');
  $('fm_love_inner').addClassName('show_form');
  
  $('FMLove_name').clear();
  $('FMLove_organisation').clear();
  $('FMLove_contact').clear();
  $('appRadio').checked = false;
  $('siteRadio').checked = false;
  $('strategyRadio').checked = false;
  
  $('interestType_application').removeClassName('selected');
  $('interestType_website').removeClassName('selected');
  $('interestType_strategy').removeClassName('selected');
  
  return false;
}

function locations() {
  $$('#locations ul li a').each(function(elem) {
    elem.observe('mouseover', function() {
      if (this.innerHTML == "Hobart") {
        location_left = 812;
        location_top = 140;
      }
      else if (this.innerHTML == "Sydney") {
        location_left = 835;
        location_top = 85;
      }
      else if (this.innerHTML == "Melbourne") {
        location_left = 800;
        location_top = 110;
      }
      
      $$('#locations .marker')[0].setStyle({display: 'block', left: location_left + 'px', top: location_top + 'px'});
    });
    
    elem.observe('mouseout', function() {
      $$('#locations .marker')[0].setStyle({display: 'none'});
    });
  });
}
