jQuery(function(){
  clearInputs();
  initFadeDrop();

    if(jQuery('#js-news')) {
        jQuery('#js-news').ticker();
  }
  if(jQuery('.clickable')) {
    jQuery('.clickable').fancybox();
  }
  
  if(jQuery('#main.systems img')) { 
    jQuery('#main.systems img').fancybox();
  }
});


function ucwords (str) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Waldo Malqui Silva
    // +   bugfixed by: Onno Marsman
    // +   improved by: Robin
    // +      input by: James (http://www.james-bell.co.uk/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: ucwords('kevin van  zonneveld');
    // *     returns 1: 'Kevin Van  Zonneveld'
    // *     example 2: ucwords('HELLO WORLD');
    // *     returns 2: 'HELLO WORLD'
    return (str + '').replace(/^([a-z])|\s+([a-z])/g, function (jQuery1) {
        return jQuery1.toUpperCase();
    });
}

function initFadeDrop(){
  var dur = 400; //ms
  var activeClass = "hover";
  jQuery('#nav > li').each(function(){
    var hold = jQuery(this);
     var  drop = hold.find('>div.drop');
    if(jQuery.browser.msie && jQuery.browser.version < 9){
      drop.hide();
      hold.mouseenter(function(){
        hold.addClass(activeClass);
        drop.show();
      }).mouseleave(function(){
        hold.removeClass(activeClass);
        drop.hide();
      });
    } else {
      drop.css({opacity: 0}).hide();
      hold.mouseenter(function(){
        hold.addClass(activeClass);
        drop.show().stop().animate({opacity:1},{duration: dur, queue: false})
      }).mouseleave(function(){
        hold.removeClass(activeClass);
        drop.show().stop().animate({opacity:0},{duration: dur, queue: false,complete:function(){
          drop.hide();
        }})
      });
    }
  });
  
}
// clear inputs
function clearInputs(){
  clearFormFields({
    clearInputs: true,
    clearTextareas: true,
    passwordFieldText: false,
    addClassFocus: "focus",
    filterClass: "default"
  });
}
function clearFormFields(o){
  if (o.clearInputs == null) o.clearInputs = true;
  if (o.clearTextareas == null) o.clearTextareas = true;
  if (o.passwordFieldText == null) o.passwordFieldText = false;
  if (o.addClassFocus == null) o.addClassFocus = false;
  if (!o.filterClass) o.filterClass = "default";
  if(o.clearInputs) {
    var inputs = document.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++ ) {
      if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass) == -1) {
        inputs[i].valueHtml = inputs[i].value;
        inputs[i].onfocus = function ()  {
          if(this.valueHtml == this.value) this.value = "";
          if(this.fake) {
            inputsSwap(this, this.previousSibling);
            this.previousSibling.focus();
          }
          if(o.addClassFocus && !this.fake) {
            this.className += " " + o.addClassFocus;
            this.parentNode.className += " parent-" + o.addClassFocus;
          }
        }
        inputs[i].onblur = function () {
          if(this.value == "") {
            this.value = this.valueHtml;
            if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
          }
          if(o.addClassFocus) {
            this.className = this.className.replace(o.addClassFocus, "");
            this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
          }
        }
        if(o.passwordFieldText && inputs[i].type == "password") {
          var fakeInput = document.createElement("input");
          fakeInput.type = "text";
          fakeInput.value = inputs[i].value;
          fakeInput.className = inputs[i].className;
          fakeInput.fake = true;
          inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
          inputsSwap(inputs[i], null);
        }
      }
    }
  }
  if(o.clearTextareas) {
    var textareas = document.getElementsByTagName("textarea");
    for(var i=0; i<textareas.length; i++) {
      if(textareas[i].className.indexOf(o.filterClass) == -1) {
        textareas[i].valueHtml = textareas[i].value;
        textareas[i].onfocus = function() {
          if(this.value == this.valueHtml) this.value = "";
          if(o.addClassFocus) {
            this.className += " " + o.addClassFocus;
            this.parentNode.className += " parent-" + o.addClassFocus;
          }
        }
        textareas[i].onblur = function() {
          if(this.value == "") this.value = this.valueHtml;
          if(o.addClassFocus) {
            this.className = this.className.replace(o.addClassFocus, "");
            this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
          }
        }
      }
    }
  }
  function inputsSwap(el, el2) {
    if(el) el.style.display = "none";
    if(el2) el2.style.display = "inline";
  }
}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
      ft = gy[i].split("=");
      if (ft[0] == ji) {
        return ft[1];
      }
    }
}


jQuery('document').ready(function() {
        /* If disabled class, take 1, if not take 2 */
  if(jQuery('.page-number-catch')) {
      if(jQuery('.page-number-catch span.disabled').length > 0) {
          pages_tr = jQuery('.page-number-catch a').length - 1;       
      } else {
          pages_tr = jQuery('.page-number-catch a').length - 2;         
      }
      pages_tr++;
      var poutput = '';
      for(i = 1; i <= pages_tr; i++) {
        if(jQuery('.pager').hasClass('blog')) {
          poutput = poutput + "<li><a href='http://www.solarpvtech.com/latestnews/?page=" +i +"&sort=published_at+DESC,+rating+DESC'>" + i + "</a></li>";  //<li><a href="#">2</a></li>
        } else {
           poutput = poutput + "<li><a href='http://www.solarpvtech.com/testimonials/?page=" +i +"&sort=published_at+DESC,+rating+DESC'>" + i + "</a></li>";          
        }
      }
      jQuery('.pager').html(poutput);
  }
  
  if(jQuery('.testimonials-block')) {
    jQuery('.testimonials-block blockquote:last').addClass('even');
   jQuery('.testimonials-block blockquote:first').removeClass('even');
  }
  
  if(jQuery('.pathclean')) {
    jQuery('.pathclean').each(function() {
       var texty = jQuery(this).text();
      texty = texty.substr(1);
      texty = texty.replace(/-/gi, " ");
      texty = ucwords(texty);
      jQuery(this).text(texty);
    }); 
  }
});


