/* Author: Brad Richardson */

// Anomymous Function for cleaner javascript
(function ($) {
  
  // Extensions

  // Form Input Focus
  $.fn.extend({
    focusValue: function() {
      return this.each(function() {
        var _switch = function() {
          if($(this).val() == '' || $(this).val() == $(this).data('initial_value')) {
            var val = $(this).val() == '' ? $(this).data('initial_value') : '';
            $(this).val(val);
          }
        };
        $(this).data('initial_value', $(this).val());
        $(this).focus(_switch).blur(_switch);
      });
    }
  });


  // JQuery Scripting
  $(document).ready(function () {

    // Show the page
    $('#x1-wrapper').fadeIn(300);
    $('#x2-wrapper').fadeIn(300);
    $('#x3-wrapper').fadeIn(300);


    // Browser Reject
    $.reject();


    // Set browser smooth scroll sections
    $('.middle-backtotop').localScroll();


    // Image Crossfade
    $("img.a").hover(function () {
      $(this).stop().animate({ "opacity": "0" }, 100);
    }, function () {
      $(this).stop().animate({ "opacity": "1" }, 600);
    });


    // Form Remove / Add Input Value
    $('input[type=text]').focusValue();


    // Home Header Change
    // Create a variable for all of the flippers
    //var flippers = [];
    
    // Grab all of the flippers, flipper panels html and flipper buttons and drop them into the array
    //$('.flipper').each(function() {
    //  flippers.push(this);
    //});
    
    // Hide all of the header except for the first one
    //$(flippers).hide().first().show();
    
    // Set all of the tabs' html to a variable
    //var locationsContent = $('#locations-panel').html();
    //var specialtyContent = $('#specialty-panel').html();
    //var commodityContent = $('#commodity-panel').html();
    //var testimonialContent1 = $('#testimonials-panel').html();
    //var testimonialContent2 = $('#testimonials-panel-2').html();
    //var testimonialContent3 = $('#testimonials-panel-3').html();
    
    // Delete the Testimonials Panel for clean html
    //$('#testimonials-panel').remove();
    //$('#testimonials-panel-2').remove();
    //$('#testimonials-panel-3').remove();
    
    // Set the click function for each button
    /*$.listen('click', '#locations-button', function(e) {
      e.preventDefault();
      
      $(flippers).hide().filter(flippers[1]).fadeIn(300);
      
      $('.middle-backbutton').fadeIn(300);
      
      $('#locations-panel').html(testimonialContent1);
      
      if ($('#specialty-panel').html() != specialtyContent) { $('#specialty-panel').html(specialtyContent); }
      if ($('#commodity-panel').html() != commodityContent) { $('#commodity-panel').html(commodityContent); }
      
      $('#locations-panel').find('.middle-bottom-test').jScrollPane();
    });
    
    $.listen('click', '#specialty-button', function(e) {
      e.preventDefault();
      
      $(flippers).hide().filter(flippers[2]).fadeIn(300);
      
      $('.middle-backbutton').fadeIn(300);
      
      $('#specialty-panel').html(testimonialContent2);
      
      if ($('#locations-panel').html() != locationsContent) { $('#locations-panel').html(locationsContent); }
      if ($('#commodity-panel').html() != commodityContent) { $('#commodity-panel').html(commodityContent); }
      
      $('#specialty-panel').find('.middle-bottom-test').jScrollPane();
    });
    
    $.listen('click', '#commodity-button', function(e) {
      e.preventDefault();
      
      $(flippers).hide().filter(flippers[3]).fadeIn(300);
      
      $('.middle-backbutton').fadeIn(300);
      
      $('#commodity-panel').html(testimonialContent3);
      
      if ($('#locations-panel').html() != locationsContent) { $('#locations-panel').html(locationsContent); }
      if ($('#specialty-panel').html() != specialtyContent) { $('#specialty-panel').html(specialtyContent); }
      
      $('#commodity-panel').find('.middle-bottom-test').jScrollPane();
    });
    
    $('.backtowelcome').live('click', function(e) {
      e.preventDefault();
      
      $(flippers).hide().filter(flippers[0]).fadeIn(300);
      
      if ($('#locations-panel').html() != locationsContent) { $('#locations-panel').html(locationsContent); }
      if ($('#specialty-panel').html() != specialtyContent) { $('#specialty-panel').html(specialtyContent); }
      if ($('#commodity-panel').html() != commodityContent) { $('#commodity-panel').html(commodityContent); }
    }); */
    
    // More Button Image Change
    $('img.change').live('hover', function() {
      var src = $(this).attr("src");
      var onTest = /Off/gi;
      
      if (src.match(onTest)) { this.src = this.src.replace("Off","On"); }
      else { this.src = this.src.replace("On","Off"); }
    });
    


    // Email a Friend Link
    var pageLink = window.location.protocol + '//' + window.location.host + $.query.get('page');
    $('.contourField.url').find('input[type=hidden]').val(pageLink);

  });

})(window.jQuery);


// Printer Friendly Version
var gAutoPrint = true; // Tells whether to automatically call the print function

function printSpecial() {
  if (document.getElementById != null) {
    var html = '<HTML>\n<HEAD>\n';

    if (document.getElementsByTagName != null) {
      //var headTags = document.getElementsByTagName("head");
      //if (headTags.length > 0)
      //html += headTags[0].innerHTML;
    }

    html += '\n<link rel="stylesheet" type="text/css" href="/css/style.css" />\n';
    html += '\n<link rel="stylesheet" type="text/css" href="/css/element.css" />\n';
    html += '\n<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>\n';
    html += '\n<script type="text/javascript" src="/scripts/mylibs/GoogleMapping.js"></script>\n';
    html += '\n</HEAD>\n<BODY style="background-color: #ffffff; padding: 20px;"  onload="OnLoad()" onresize="OnLoad()">\n';

    var printReadyElem = document.getElementById("printReady");

    if (printReadyElem != null) {
      html += '\n<div class="print-content-container">\n'
      html += printReadyElem.innerHTML;
    }
    else {
      alert("Could not find the printReady function");
      return;
    }

    var printReadyElem2 = document.getElementById("printerfriendlycontent");

    if (printReadyElem2 != null) {
      html += printReadyElem2.innerHTML;
      html += '\n<div class="form-sub container"><p><br /><br />All rights reserved. Copyright 2011 - 2012</p></div>\n';
      html += '\n</div>\n';
    }
    else {
      html += '\n</div>\n';
    }

    html += '\n</BODY>\n</HTML>';

    var printWin = window.open("", "printSpecial");
    printWin.document.open();
    printWin.document.write(html);
    printWin.document.close();
    if (gAutoPrint)
    printWin.print();
  }
  else {
    alert("The print ready feature is only available if you are using a browser. Please update your browser.");
  }
}


// Newsletter Manager Pro Sign Up Validation
function $Npro(field) {
  var element =  document.getElementById(field);
  return element;
  return false;
}
function emailvalidation(field, errorMessage) {
  var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.sex)|(\.biz)|(\.aero)|(\.coop)|(\.museum)|(\.name)|(\.pro)|(\..{2,2}))$)\b/gi);
  apos = field.value.indexOf("@");
  dotpos = field.value.lastIndexOf(".");
  lastpos = field.value.length - 1;
  var badEmail = (apos<1 || dotpos-apos<2 || lastpos-dotpos<2)
  if (goodEmail && !badEmail) { return true; }
  else {
    alert(errorMessage);
    //$Npro("Error").innerHTML = errorMessage;
    //$Npro("Error").style.display = "inline";
    field.focus();
    field.select();
    return false;
  }
}
function emptyvalidation(entered, errorMessage) {
  $Npro("Error").innerHTML = "";
  with (entered) {
    if (value==null || value=="") {
      alert(errorMessage);
      //$Npro("Error").innerHTML = errorMessage;
      //$Npro("Error").style.display = "inline";
      return false;
    }
    else { return true; }
  }
}
function formvalidation(thisform) {
  with (thisform) {
    if (emptyvalidation(name,"Please enter your name") == false) { 
      name.focus();
      return false;
    };
    if (emptyvalidation(lastname,"Please enter your last name") == false) {
      lastname.focus();
      return false;
    };
    if (emailvalidation(email,"Please enter your valid email address") == false) {
      email.focus();
      return false;
    };
  }
}


// Highslide Configuration
hs.graphicsDir = '/highslide/graphics/';
hs.showCredits = false;
hs.outlineType = 'custom';
hs.dimmingOpacity = 0.5;
hs.blockRightClick = true;
hs.captionEval = 'this.a.title';
hs.registerOverlay({
  html: '<div class="closebutton" onclick="return hs.close(this)" title="Close"></div>',
  position: 'top right',
  useOnHtml: true,
  fade: 2 // fading the semi-transparent overlay looks bad in IE
});

