
var ool = window.onload;
window.onload = function() {
    if (ool != null) ool();
		navSetup();
    initExternalLinks();
    love();
}


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;
    }
}

function navSetup() {
	new Ajax.Request('http://blog.mu.staging.futuremedium.com.au/solutionsNav/', {
        onSuccess: function(response) {
            $('flyoutInner').insert(response.responseText);
            nav();
            slideDHTML('navSlide', '#navSlide > ul', 698, 0.5, 'btn_flyout_back', 'btn_flyout_moreWork' ); //solutions Nav
  }
});

}


//For 1 instance
var test = null;
function nav() {
   ($$('.flyout')).each(function(elm) { 
      Event.observe(elm.parentNode, 'mouseenter', function () { if(test) { window.clearTimeout(test); } elm.parentNode.addClassName('hover'); })
      Event.observe(elm.parentNode, 'mouseleave', function () { function toggle(name) { elm.parentNode.toggleClassName(name); } test = 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_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;
    }
  }
  
  // Check if something is selected
  if ($('appRadio').checked) {
    $('webApplication').childElements()[0].addClassName("selected");
  }
  
  if ($('siteRadio').checked) {
    $('webSite').childElements()[0].addClassName("selected");
  }
  
  if ($('strategyRadio').checked) {
    $('webStrategy').childElements()[0].addClassName("selected");
  }
}


function checkLoveForm() {
  var form = document['forms']['FMLove'];
  var params = $('FMLove').serialize(true);
  var error = false;
  
  // Validate form
  if (params['interestType'] == undefined ||
      params['interestType'] == null ||
      params['interestType'] == '') {
    $('interestType_errorMessage').removeClassName('invisible');
    error = true;
  }
  else {
    $('interestType_errorMessage').addClassName('invisible');
  }
    
  if (params['name'] == undefined ||
      params['name'] == null ||
      params['name'] == '') {
    $('wwerr_FMLove_name').removeClassName('invisible');
    error = true;
  }
  else {
    $('wwerr_FMLove_name').addClassName('invisible');
  }
  
  if (params['organisation'] == undefined ||
      params['organisation'] == null ||
      params['organisation'] == '') {
    $('wwerr_FMLove_organisation').removeClassName('invisible');
    error = true;
  }
  else {
    $('wwerr_FMLove_organisation').addClassName('invisible');
  }
  
  if (params['contact'] == undefined ||
      params['contact'] == null ||
      params['contact'] == '') {
    $('wwerr_FMLove_contact').removeClassName('invisible');
    error = true;
  }
  else {
    $('wwerr_FMLove_contact').addClassName('invisible');
  }
  
  if (error)
    return false;
    
  // Send form
 
  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;
      $$('.ajaxLoading')[0].toggleClassName('invisible');
      $$('.alertMessage')[0].innerHTML = response;
      love();
      $('fm_love_inner').removeClassName('show_form');
      $('fm_love_inner').addClassName('show_message');
    },
    onFailure: function(transport) {
      alert(transport.status);
    }
  });

  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 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;
}
