var isIE = jQuery.browser.msie;
var isIE6 = isIE && parseInt(jQuery.browser.version) < 7;
var isIE7 = isIE && parseInt(jQuery.browser.version) == 7;

var textAdIntervalId = 0;
var textAds = null;
var textAdIdx = 0;
var textAdSpeed = 20000;

function changeTextAd() {
  if (textAds === null) textAds = jq("div.textad-text-holder");
  if (textAds.length <= 1) return;
  var selectedText = jq(textAds.get(textAdIdx));
  textAdIdx++;
  if (textAdIdx==textAds.length) textAdIdx = 0;
  var container = jq("#textad-container");
  if (isIE) {
    container.hide();
    setText();
  } else
    container.fadeOut("normal", setText);
}

function setText() {
  if (textAds === null) return;
  var text = jq(textAds.get(textAdIdx)).html();
  jq("#textad-container #textad-content").html(text);
  var container = jq("#textad-container");
  if (isIE)
    container.show();
  else
    container.fadeIn("normal");

}

jQuery(document).ready(function() {

  // pulldown
  var pulldownContent = jq("#pulldown-content"),
      pulldownTool = jq("#pulldown-tool"),
      textAdContent = jq("#textad-container"),
      pulldownArrow = jq("#pulldown-arrow-button"),
      buttons = [jq("#pulldown-arrow-button a")],
  
      contactToolContent = jq("#pulldown-tools-content .contact-tool-content"),
      orderCallToolContent = jq("#pulldown-tools-content .order-call-tool-content"),
      quicklinksToolContent = jq("#pulldown-tools-content .quicklinks-tool-content");

  function showTextAd() {
    if (isIE)
      textAdContent.show();
    else
      textAdContent.fadeIn("slow");
  }
  function hideTextAd() {
    if (isIE)
      textAdContent.hide();
    else
      textAdContent.fadeOut("slow");
  }

  function closePulldown(calledByTool) {
    if (isIE7) jq('div#pulldown-content .form-answer').css('visibility', 'hidden');
    if (!calledByTool) {
      if (!isIE6) pulldownContent.slideUp("normal", showTextAd);
      else showTextAd();
    } else {
      if (!isIE6) pulldownContent.slideUp("normal", function() {
        pulldownContent.html(calledByTool.html());
        pulldownContent.find("form").ajaxForm({ target: "#pulldown-content .form-script-reply-holder" });
        openPulldown();
      });
      else {
        pulldownContent.html(calledByTool.html())
        pulldownContent.find("form").ajaxForm({ target: "#pulldown-content .form-script-reply-holder" });
      }
    }
    
    pulldownContent.parent().removeClass("opened").addClass("closed");
    var url = (window.location + '').replace(/pulldown_menu=closed/g, "pulldown_menu=opened");
    jq.each(buttons, function() {
      jq(this).attr("title", "Odpri");
      jq(this).attr("href", url);
    });
    textAdIntervalId = setInterval("changeTextAd()", textAdSpeed);
    if (!pulldownContent.hasClass("hruska-tool")) {
      jq.cookie("pulldown-" + pulldownContent.attr("class"), "0", {path:'/'});
    }
    if (calledByTool && isIE6) openPulldown();
  }

  function openPulldown() {
    pulldownContent.parent().removeClass("closed").addClass("opened");
    if (!isIE6)
      pulldownContent.slideDown("normal", function() {
        if (isIE7) jq('div#pulldown-content .form-answer').css('visibility', 'visible');
      });
    else
      pulldownContent.attr("style", "");
    hideTextAd();

    var url = (window.location + '').replace(/pulldown_menu=opened/g, "pulldown_menu=closed");
    jq.each(buttons, function() {
      jq(this).attr("title", "Zapri");
      jq(this).attr("href", url);
    });
    clearInterval(textAdIntervalId);
    if (!pulldownContent.hasClass("hruska-tool")) {
      jq.cookie("pulldown-" + pulldownContent.attr("class"), "1", {path:'/'});
    }
  }

  jq.each(buttons, function() {
    this.click(function(event) {
      event.preventDefault();
      pulldownTool.hasClass("closed") ? openPulldown() : closePulldown();
    });
  });

  // pulldown-tool-links
  var pulldownTools = [
    { linkEl: jq("#contact-tool-link"), contentEl: contactToolContent },
    { linkEl: jq("#order-call-tool-link"), contentEl: orderCallToolContent },
    { linkEl: jq("#quicklinks-tool-link"), contentEl: quicklinksToolContent }
  ];
  
  jq.each(pulldownTools, function() {
    var linkEl = this.linkEl,
        contentEl = this.contentEl,
        contentClass = contentEl.attr("class");
    linkEl.click(function(event) {
      event.preventDefault();
      jq('#contact-tool-link, #order-call-tool-link, #quicklinks-tool-link').removeClass("selected");
      linkEl.addClass("selected");
      if (pulldownContent.attr("class") == contentClass) {
        if (pulldownTool.hasClass("closed")) {
          openPulldown();
        } else {
          closePulldown();
          linkEl.removeClass("selected");
        }
        return;
      }
      pulldownContent.attr("class", contentClass);
      if (pulldownTool.hasClass("opened")) {
        closePulldown(contentEl);
      } else {
        pulldownContent.html(contentEl.html());
        pulldownContent.find("form").ajaxForm({ target: "#pulldown-content .form-script-reply-holder" });
        openPulldown();
      }
    });
  });

  // gallery link activators
  var gallery_links = jq("span.gallery-link a").add(jq("a > span.gallery-link").parent()).add("a.gallery-link");

  gallery_links.click(function(event) {
    event.preventDefault();
    jq.post(jq(this).attr("href") + '/gallery_ajax_view', function(data) {

       jq("#gallery-images-holder").html(data);

       jq('#gallery #images a').lightBox({
          imageLoading: '/www.hruska.si/portal/lightbox-ico-loading.gif',
          imageBtnPrev: '/www.hruska.si/portal/lightbox-btn-prev.gif',
          imageBtnNext: '/www.hruska.si/portal/lightbox-btn-next.gif',
          imageBtnClose: '/www.hruska.si/portal/lightbox-btn-close.gif', 
          imageBlank: '/www.hruska.si/portal/lightbox-blank.gif',
          txtImage: 'Slika',
          txtOf: 'od'
        });

        jq("#gallery-images-holder #gallery #images a").eq(0).click();
    });
  });

  // pulldown tool link activators
  var kontakt_links = jq("a").filter(function() {
    var href = jq(this).attr("href");
    return href && href.indexOf("?pulldown_menu=kontakt") != -1;
  }).filter(":not(#contact-tool-link)");

  var narocite_klic_links = jq("a").filter(function() {
    var href = jq(this).attr("href");
    return href && href.indexOf("?pulldown_menu=narocite_klic") != -1;
  }).filter(":not(#order-call-tool-link)");

  var hitri_skoki_links = jq("a").filter(function() {
    var href = jq(this).attr("href");
    return href && href.indexOf("?pulldown_menu=hitri_skoki") != -1;
  }).filter(":not(#quicklinks-tool-link)");

  kontakt_links.click(function(event) {
    event.preventDefault();
    jq("#contact-tool-link").click();
  });
  narocite_klic_links.click(function(event) {
    event.preventDefault();
    jq("html, body").scrollTop(0);
    if (!jq("#pulldown-content").hasClass("order-call-tool")) {
      jq("#order-call-tool-link").click();
    }
    var regex = /&resitev=([^&]*).*$/;
    var match = jq(this).attr('href').match(regex);
    if (match) {
      var setSelect = function() {
        var select = jq('#pulldown-content select.juicy_solution');
        if (select.length) select.val(match[1]);
        else setTimeout(setSelect, 200);
      };
      setSelect();
    }
  });
  hitri_skoki_links.click(function(event) {
    event.preventDefault();
    jq("#quicklinks-tool-link").click();
  });
});

