$(document).ready(function() {
  Util.setMaps();

  //--- Lightbox ---//
  $("a[rel='gallery']").colorbox();

});



Util = {
  setMaps: function() {
    $("#google_map_static").load(function () {
      var address = $("#google_map_static").attr('alt');
      var iframe = '<iframe id="google_map_dynamic" width="340" height="340" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?source=s_q&amp;hl=de&amp;z=14&amp;q=' + address + '&amp;output=embed'
      + "&amp;iwloc=A" +
      '"></iframe>';
      $("#google_map_static").parent().html(iframe);
    });
    $("#google_map_static").click(function () {
      var address = $("#google_map_static").attr('alt');
      var iframe = '<iframe id="google_map_dynamic" width="340" height="340" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?source=s_q&amp;hl=de&amp;z=14&amp;q=' + address + '&amp;output=embed'
      + "&amp;iwloc=A" +
      '"></iframe>';
      $("#google_map_static").parent().html(iframe);
    });
  },
  externalLinks: function() {
    //Source: http://www.sitepoint.com/print/standards-compliant-world/
    if (!document.getElementsByTagName) {
      return;
    }
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
        anchor.target = "_blank";
      }
    }
  },
  getWindowSize: function(){
    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
      xScroll = window.innerWidth + window.scrollMaxX;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else if(document.body) {
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {
      if(document.documentElement.clientWidth){
        windowWidth = document.documentElement.clientWidth;
      } else {
        windowWidth = self.innerWidth;
      }
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }

    if(yScroll < windowHeight) {
      pageHeight = windowHeight;
    } else {
      pageHeight = yScroll;
    }

    if(xScroll < windowWidth) {
      pageWidth = xScroll;
    } else {
      pageWidth = windowWidth;
    }

    var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
  },
  validateForm: function(id) {
    var result= true;
    $("#"+id + " input, #"+id + " textarea").each(function() {
      if($(this).attr("class") && $(this).attr("class").match("required")) {
        // trim input field
        $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
        var itemresult= Util.validate($(this).attr("id"));
        result= result && itemresult;
      }
    });

    if(result) {
      $("#form_error").removeClass("error");
    } else {
      $("#form_error").addClass("error");
    }

    return result;
  },
  validate: function(id) {
    var result= true;
    var obj= $("#"+id);
    var commands= $("#"+id).attr("class").split(" ");
    for(var i=0; i<commands.length; i++) {
      var command= commands[i].replace(/[0-9]/g, "");
      switch(command) {
        case("minlength"):
          var length= parseInt(commands[i].replace(/minlength/g, ""));
          result= result && (obj.val().length>=length);
          break;
        case("maxlength"):
          var length= parseInt(commands[i]);
          result= result && (obj.val().length<=length);
          break;
        case("email"):
          result = result && ( obj.get(0).value.match(/\S@\S.\S{2,}/)!=null )
          break;
        default:
          result= result && (obj.val().length!=0);
          break;
      }
    }
    if(!result) {
      obj.addClass("error");
    } else {
      obj.removeClass("error");
    }
    return result;
  }

}

window.onload = Util.externalLinks;

