/* AUTO HEIGHT */
(function($) {
  $.fn.autoHeight = function(type) {
    return this.each(function(index, elm) {

      var parent_sibl_height = 0;
      var margins = 0;

      $(this).parent().siblings().each(function() {
        parent_sibl_height += $(this).height();

        /* ADDING TOP MARGIN TO HEIGHT */
        margins = $(this).css("margin-top");
        margins = margins.replace('px', '');

        if (margins != 'auto') {
          margins = parseInt(margins);

          parent_sibl_height += margins;
        }

        /* ADDING BOTTOM MARGIN TO HEIGHT */
        margins = $(this).css("margin-bottom");
        margins = margins.replace('px', '');

        if (margins != 'auto') {
          margins = parseInt(margins);

          parent_sibl_height += margins;
        }

      });

      $(this).parent().css("height", ($(window).height() - parent_sibl_height));

      var sibl_height = 0;
      var parent_height = $(this).parent().height();
      var margins = 0;

      $(elm).siblings().each(function() {
        sibl_height += $(this).height();

        /* ADDING TOP MARGIN TO HEIGHT */
        margins = $(this).css("margin-top");
        margins = margins.replace('px', '');

        if (margins != 'auto') {
          margins = parseInt(margins);

          sibl_height += margins;
        }

        /* ADDING BOTTOM MARGIN TO HEIGHT */
        margins = $(this).css("margin-bottom");
        margins = margins.replace('px', '');

        if (margins != 'auto') {
          margins = parseInt(margins);

          sibl_height += margins;
        }

        if (type == 'scroll') {
          $(elm).parent().css("min-height", ($(window).height() + $(window).scrollTop()) + 'px');
        }

      });

      $(this).css("min-height", (parent_height - sibl_height) + 'px');

    });
  }
})(jQuery);

$(document).ready(function() {
  $(".auto-height").autoHeight();
});

$(window).scroll(function() {
  $(".auto-height").autoHeight('scroll');
});

$(window).resize(function() {
  $(".auto-height").autoHeight();
});


/* A TARGET */
(function($) {
  $.fn.target = function() {
    return this.each(function(index, elm) {
      if (substr($(this).attr("class"), 0, 7) == 'target-') {
        $(this).attr("target", substr($(this).attr("class"), 7));
      }
    });
  }
})(jQuery);

$(document).ready(function() {
  $("a").target();


  /* TEMP LANG FIX */
//  $("div#DE").hide();
//  $("div#FR").hide();

  $("div.language_submenu").css("margin-top", "-108px");
  $("div.language_submenu").css("height", "100px");
});

/* SUBMIT BUTTON */
$(document).ready(function() {
  $('img[@src$=.png]').not("#map_canvas img").pngfix({
		imageFixSrc: "/images/pixel.gif"
	});

  $("div[class*='button']").each(function() {
    $(this).bind("mouseenter mouseleave", function(e) {
      if ($(this).hasClass("submit_button")) {
        $(this).toggleClass("submit_over");

        if ($(this).parents("form").length > 0) {
          $(this).bind("click", function() {
            $(this).parents("form").trigger("submit");
          });
        }
      } else {
        $(this).toggleClass("button_over");
      }
    });
  });

  /* LANGUAGE SUBMENU */
  $("div.language").bind("click", function() {
    $("div.language_submenu").toggle();
  });

//  $(document).bind("click", function() {
//    $("div.language_submenu").hide();
//  });
//
  $("div.language_submenu").children("div").each(function() {
    $(this).bind("mouseover mouseout click", function(evt) {
      if (!$(this).hasClass("language_selected")) {
        if (evt.type == 'mouseover') {
          $(this).addClass("language_over");
        } else if (evt.type == 'mouseout') {
          $(this).removeClass("language_over");
        }
      }

      if (evt.type == 'click') {
        if (!$(this).hasClass("language_selected")) {
          var id = $(this).attr("id");

          var values = {
            sl_id: id
          }

          $.POST(window.location.href, values);

        } else {
          $(this).parent().hide();
        }
      }

    });

    if ($(this).hasClass("language_selected")) {
      $("span.lang").text($(this).text());
    }
  });


  if ($.browser.mozilla) {
    $("div.mp3Player").css("margin-top", "-4px");
  }

});


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function stopMusic() {
  $.cookie('music', 'stop');
}

function playMusic() {
  $.cookie('music', 'play');
}