(function ($) {
Drupal.settings.views = Drupal.settings.views || {'ajax_path': '/views/ajax'};

Drupal.quicktabs = Drupal.quicktabs || {};

Drupal.quicktabs.getQTName = function (el) {
  return el.id.substring(el.id.indexOf('-') +1);
}

Drupal.behaviors.quicktabs = {
  attach: function (context, settings) {
    $.extend(true, Drupal.settings, settings);
    $('.quicktabs-wrapper', context).once(function(){
      Drupal.quicktabs.prepare(this);
    });
  }
}

// Setting up the inital behaviours
Drupal.quicktabs.prepare = function(el) {
  // el.id format: "quicktabs-$name"
  var qt_name = Drupal.quicktabs.getQTName(el);
  var $ul = $(el).find('ul.quicktabs-tabs:first');
  $ul.find('li a').each(function(i, element){
    element.myTabIndex = i;
    element.qt_name = qt_name;
    var tab = new Drupal.quicktabs.tab(element);
    var parent_li = $(element).parents('li').get(0);
    if ($(parent_li).hasClass('active')) {
      $(element).addClass('quicktabs-loaded');
    }
    $(element).once(function() {$(this).bind('click', {tab: tab}, Drupal.quicktabs.clickHandler);});
  });
}

Drupal.quicktabs.clickHandler = function(event) {
  var tab = event.data.tab;
  var element = this;
  // Set clicked tab to active.
  $(this).parents('li').siblings().removeClass('active');
  $(this).parents('li').addClass('active');

  // Hide all tabpages.
  tab.container.children().addClass('quicktabs-hide');
  
  if (!tab.tabpage.hasClass("quicktabs-tabpage")) {
    tab = new Drupal.quicktabs.tab(element);
  }

  tab.tabpage.removeClass('quicktabs-hide');
  return false;
}

// Constructor for an individual tab
Drupal.quicktabs.tab = function (el) {
  this.element = el;
  this.tabIndex = el.myTabIndex;
  var qtKey = 'qt_' + el.qt_name;
  var i = 0;
  for (var key in Drupal.settings.quicktabs[qtKey].tabs) {
    if (i == this.tabIndex) {
      this.tabObj = Drupal.settings.quicktabs[qtKey].tabs[key];
      this.tabKey = key;
    }
    i++;
  }
  this.tabpage_id = 'quicktabs-tabpage-' + el.qt_name + '-' + this.tabKey;
  this.container = $('#quicktabs-container-' + el.qt_name);
  this.tabpage = this.container.find('#' + this.tabpage_id);
}

if (Drupal.ajax) {
  /**
   * Handle an event that triggers an AJAX response.
   *
   * We unfortunately need to override this function, which originally comes from
   * misc/ajax.js, in order to be able to cache loaded tabs, i.e. once a tab
   * content has loaded it should not need to be loaded again.
   *
   * I have removed all comments that were in the original core function, so that
   * the only comments inside this function relate to the Quicktabs modification
   * of it.
   */
  Drupal.ajax.prototype.eventResponse = function (element, event) {
    var ajax = this;

    if (ajax.ajaxing) {
      return false;
    }
  
    try {
      if (ajax.form) {
        if (ajax.setClick) {
          element.form.clk = element;
        }
  
        ajax.form.ajaxSubmit(ajax.options);
      }
      else {
        // Do not perform an ajax request for already loaded Quicktabs content.
        if (!$(element).hasClass('quicktabs-loaded')) {
          ajax.beforeSerialize(ajax.element, ajax.options);
          $.ajax(ajax.options);
          if ($(element).parents('ul').hasClass('quicktabs-tabs')) {
            $(element).addClass('quicktabs-loaded');
          }
        }
      }
    }
    catch (e) {
      ajax.ajaxing = false;
      alert("An error occurred while attempting to process " + ajax.options.url + ": " + e.message);
    }
    return false;
  };
}


})(jQuery);
;
(function ($) {

  // OVERRIDE DEFAULT AJAX WARNING STUFF
  if (Drupal && Drupal.ajax) {
    Drupal.ajax.prototype.error = function (response, uri) {
      //alert(Drupal.ajaxError(response, uri));
      // Remove the progress element.
      if (this.progress.element) {
        $(this.progress.element).remove();
      }
      if (this.progress.object) {
        this.progress.object.stopMonitoring();
      }
      // Undo hide.
      $(this.wrapper).show();
      // Re-enable the element.
      $(this.element).removeClass('progress-disabled').removeAttr('disabled');
      // Reattach behaviors, if they were detached in beforeSerialize().
      if (this.form) {
        var settings = response.settings || this.settings || Drupal.settings;
        Drupal.attachBehaviors(this.form, settings);
      }
    };
  }
  
  $(document).ready(function(){
    $("#block-block-68 p.ideanurkka_hover").hover(
      function() {
        $(this).stop().animate({"opacity": "1"}, "fast");
      },
      function() {
        $(this).stop().animate({"opacity": "0"}, "fast");
      }
    );
  });

  Drupal.behaviors.pirkkaEqualheights = {
    attach: function (context, settings) {
      //console.debug(settings.pirkka.equal_heights);
      if (jQuery().equalHeights && settings.pirkka) {
        for (var selector in settings.pirkka.equal_heights) {
          var $element = $(selector, context);
          var setEqual = settings.pirkka.equal_heights[selector].set_equal;

          $element.each(function() {
            $(setEqual, $(this)).equalHeights();
          });
        }
      }
    }
  };

	Drupal.behaviors.Pirkkatoggle = {
		attach: function(context, settings) {

      if (settings.pirkka) {
          for (var key in settings.pirkka.toggle_block) {
            blockToggle(key, settings.pirkka.toggle_block[key].open, settings.pirkka.toggle_block[key].top_margin_closed);
          }
     }

     function blockToggle($id, $open, $top_margin_closed) {
        var $block = $('#' + $id, context);
        var $block_wrap = $('.block-wrap', $block);
        var $block_title = $('h2.block-title', $block);
        var $block_content = $('.block-content', $block);
        var $block_content_height = $block_content.outerHeight();

        if (!$open) {
          $block_content.css('margin-top', $top_margin_closed);
          $block_content.css('height', 1);
          $block_content.css('display', 'block');
          $block_wrap.addClass('hidden');
        }

        $block_title.hover(function () {
          $(this).addClass("hover");
        },function () {
          $(this).removeClass("hover");
        });

        $block_title.click(function() {
          if ($block_wrap.hasClass('hidden')) {
           $block_wrap.removeClass('hidden');
           $block_content.stop().animate({
             height: $block_content_height,
             'margin-top': 0
           });
          }
          else {
           $block_wrap.addClass('hidden');
           $block_content.stop().animate({
             height: 1,
             'margin-top': $top_margin_closed
           }, 500);
          }
        });
      }
    }
	};


	Drupal.behaviors.PirkkaSetupLabel = {
		attach: function(context, settings) {

			function setupLabel($id) {
				if ($('label', $id).length) {
					$('label', $id).each(function(){
						$(this).removeClass('c_on');
					});
					$('*:checked, *:selected', $id).each(function(){
						if ($(this).next('label').length) {
							$(this).next('label').addClass('c_on');
						}
						else if ($(this).parent('label').length) {
							$(this).parent('label').addClass('c_on');
						}
					});                
				}
			}

      if (settings.pirkka) {

        for (var id in settings.pirkka.setuplabel) {

          var $id = $(id, context);

          // add active class on all selected filter labels
          setupLabel($id);

          // act also on changes in all exposed checkboxes
          $('input', $id).click(function() {
            setupLabel($id);
          });
        }

      }
    }
  };

  Drupal.behaviors.pirkkaDescriptionPopup = {
    attach: function (context, settings) {
      if (this.attached) return;
      this.attached = true;
      $('.form-item input').bind({
        focus: function() {
          $(this).parents('.form-item:first').addClass('hasFocus');
        },
        blur: function() {
          $(this).parents('.form-item:first').removeClass('hasFocus');
        }
      });
      $('.form-item select').bind({
        focus: function() {
          $(this).parents('.form-item:first').addClass('hasFocus');
        },
        blur: function() {
          $(this).parents('.form-item:first').removeClass('hasFocus');
        }
      });
      $('.form-item label').bind({
        focus: function() {
          $(this).parents('.form-item:first').addClass('hasFocus');
        },
        blur: function() {
          $(this).parents('.form-item:first').removeClass('hasFocus');
        }
      });
      $('.form-item').bind({
        mouseover: function() {
          $form = $(this).closest('form');
          $(this).addClass('hover');
          $(this).removeClass('blur');
          $('.form-item:not(.form-type-password-confirm, .hasFocus)', $form).not($(this)).addClass('blur');
        },
        mouseout: function() {
          $(this).removeClass('blur hover');
        }
      });
    }
  };

  Drupal.behaviors.mallihakemusForm = {
  	attach: function (context, settings) {
  		if($('.messages.error').length) {
  			$('#model-application-form-wrapper').addClass('visible');
  		}
   		$('#mallihakemus-form-toggle').bind('click', function() {
  			$('#model-application-form-wrapper').toggle();
  			return false;
  		});
  	}
  };

  Drupal.behaviors.pagerClickAnchoring = {
    attach: function (context, settings) {
      
      // Format variable if not set yet
      if (typeof(settings.scrollTop) == "undefined") {
        settings.scrollTop = false;
      }
      
      // Look for variable in case we need to scroll
      if (settings.scrollTop == true) {
        $.scrollTo($('.view-content'), 200, {offset: {top:-150}});
        settings.scrollTop = false;
      }
      
      // Defines the variable which gets readed later on triggers scrolling
      $('#block-system-main .view.ajax-enabled ul.pager li a').click(function() {
        settings.scrollTop = true;
      });
      
    }
  };
  
  Drupal.behaviors.albumScroll = {
  	attach: function (context, settings) {
      // Format variable if not set yet
      if (typeof(settings.scrollTopAlbum) == "undefined") {
        settings.scrollTopAlbum = false;
      }
      
      // Look for variable in case we need to scroll
      if (settings.scrollTopAlbum == true) {
        $.scrollTo($('#album-style-placeholder'), 200, {offset: {top:-150}});
        settings.scrollTopAlbum = false;
      }
      
      // Defines the variable which gets readed later on triggers scrolling
      $('.view-albums .node-album-item .album-content').click(function() {
        settings.scrollTopAlbum = true;
      });
      $('.view-albums .node-album-item .album-left-col').click(function() {
        settings.scrollTopAlbum = true;
      });
  	}
  };
  
})(jQuery);
;
/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights(400); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights(100,300); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest).css("overflow","hidden");
		});
	}
})(jQuery);;

