try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}
var fancyboxDefOptions = { overlayShow: true, overlayOpacity: 0.70, speedIn: 300, speedOut: 400, padding: 5 };
var coordIndex = 0;
var iesucks = '';

$(function() {
    // image preload
    $.preloadImages(_appPath + "/images/loader-poll.gif", _appPath + "/images/background/menu-" + _lang + "-hover.png", _appPath + "/images/background/menu-sub.png", _appPath + "/images/background/menu-sub-first.png");

    // png fix for ie6
    $('#footer div, #shadow, .gallery li, a.image, a.more, a.main-image, #product-list li, #header .poll, #poll a').supersleight({ shim: _appPath + '/images/blank.gif' });

    // fancybox init
    if ($.isFunction($.fn.fancybox)) {
        $("#header a.poll").fancybox($.extend(fancyboxDefOptions, { autoDimensions: true }));
        $(".gallery a, a.main-image").fancybox(fancyboxDefOptions);
        $(".media a, a.media").fancybox($.extend(fancyboxDefOptions, { width: 680, height: 520, type: 'iframe' }));
        $("#googlemap a").fancybox($.extend(fancyboxDefOptions, { width: 500, height: 500, autoDimensions: false, type: '', onComplete: showMap }));
    }

    // auto clear input fields
    $(".autoClear").each(function(index) {
        $(this).focus(function() { if ($(this).val() == $(this).attr("title")) { $(this).val(''); } });
    });
    $(".autoClearBlur").each(function(index) {
        $(this).blur(function() { if ($(this).val() == '') { $(this).val($(this).attr("title")); } });
    });

    // main menu
    $('#header #menu > li').hover(
      function() {
          //if ($(this).find("ul").size() > 0) {
          var ul = $(this).find("ul");
          if ((ul.size() > 0 && !ul.first().is(":visible")) || ul.size() == 0) {
              $('#header #menu ul').hide();
              $(this).find("ul").show().find("li").hide().fadeIn("fast");
          }
      },
      function() {
          //$(this).find("ul").hide();
          //$('#header #menu > li.active ul').show();
      });

    // video image mask
    $(".media a").each(function() {
        $img = $("<img>").addClass("mask").attr("src", _appPath + "/images/play.gif").css({ 'opacity': 0.70 }).hover(
			function() { $(this).dequeue().animate({ "opacity": 0.35 }, 250, "easeOutBack") },
			function() { $(this).dequeue().animate({ "opacity": 0.70 }, 250, "easeOutBack") }
		);
        $(this).append($img);

    });
    $("#poll input[type=text]").live('focus', function() { $("#poll input[type=radio]:last").attr("checked", true) });
    // $("#poll label").live('click', function() { $("#" + $(this).attr("for")).attr("checked", true) });
    $("#poll a.vote").live('click', function() {
        id = $("#poll input[name=answer]:checked").val();
        text = $("#poll input[type=text]").val();
        if (id > 0 || (id == 0 && text && $.trim(text).length > 0)) {
            text = $.trim(text);
            $("#poll ul").html("<li><p style=\"margin: 30px 0; text-align: center;\"><img src=\"" + _appPath + "/images/loader-poll.gif\" alt=\"\" /></p></li>");
            $.post(_appPath + "/" + _lang + "/vote.aspx", { id: id, text: text }, function(data) {
                $("#poll").parent().hide().html(data).fadeIn("slow")
            }, "html");
        }
        return false;
    });

    // news filter
    $("#newsFilter").change(function() { val = $(this).find(":selected").val(); if (val != iesucks) { $(location).attr('href', val); } });
    iesucks = $("#newsFilter :selected").val();

    // map
    $("#googlemap a").click(function() { coordIndex = $(this).attr('rel'); });

    // survey
    $(".openAnswer").focus(function() {
        $(this).parent().find("input[type=radio]:last").attr("checked", true);
    });
});

jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
}


function showMap() {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(coord[coordIndex][0], coord[coordIndex][1]), 15);
        map.setMapType(G_NORMAL_MAP);
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.openInfoWindow(map.getCenter(), document.createTextNode(mapTitle));
        map.addOverlay(new GMarker(map.getCenter()));
    }
};