
Drupal.behaviors.ViewsAjaxView = function(){
    if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) {
        var ajax_path = Drupal.settings.views.ajax_path;
        // If there are multiple views this might've ended up showing up multiple times.
        if (ajax_path.constructor.toString().indexOf("Array") != -1) {
            ajax_path = ajax_path[0];
        }
        $.each(Drupal.settings.views.ajaxViews, function(i, settings){
            var view = '.view-dom-id-' + settings.view_dom_id;
            if (!$(view).size()) {
                // Backward compatibility: if 'views-view.tpl.php' is old and doesn't
                // contain the 'view-dom-id-#' class, we fall back to the old way of
                // locating the view:
                view = '.view-id-' + settings.view_name + '.view-display-id-' + settings.view_display_id;
            }
            
            
            // Process exposed filter forms.
            $('form#views-exposed-form-' + settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-')).filter(':not(.views-processed)').each(function(){
                // remove 'q' from the form; it's there for clean URLs
                // so that it submits to the right place with regular submit
                // but this method is submitting elsewhere.
                $('input[name=q]', this).remove();
                var form = this;
                // ajaxSubmit doesn't accept a data argument, so we have to
                // pass additional fields this way.
                $.each(settings, function(key, setting){
                    $(form).append('<input type="hidden" name="' + key + '" value="' + setting + '"/>');
                });
            }).addClass('views-processed').submit(function(){
                $('input[@type=submit]', this).after('<span class="views-throbbing">&nbsp</span>');
                var object = this;
                $(this).ajaxSubmit({
                    url: ajax_path,
                    type: 'GET',
                    success: function(response){
                        // Call all callbacks.
                        if (response.__callbacks) {
                            $.each(response.__callbacks, function(i, callback){
                                eval(callback)(view, response);
                            });
                            $('.views-throbbing', object).remove();
                        }
                    },
                    error: function(){
                        alert(Drupal.t("An error occurred at @path.", {
                            '@path': ajax_path
                        }));
                        $('.views-throbbing', object).remove();
                    },
                    dataType: 'json'
                });
                
                return false;
            });
			
			
			
			
			//alert(view)
            
            $(view).filter(':not(.views-processed)')            // Don't attach to nested views. Doing so would attach multiple behaviors
            // to a given element.
            .filter(function(){
                // If there is at least one parent with a view class, this view
                // is nested (e.g., an attachment). Bail.
                return !$(this).parents('.view').size();
            }).each(function(){
                // Set a reference that will work in subsequent calls.
                var target = this;
                $(this).addClass('views-processed')                // Process pager, tablesort, and summary links.
                .find('ul.pager > li > a, th.views-field a, .views-summary a').each(function(){
                    var viewData = {};
                    // Construct an object using the settings defaults and then overriding
                    // with data specific to the link.
                    $.extend(viewData, settings, Drupal.Views.parseQueryString($(this).attr('href')),                    // Extract argument data from the URL.
                    Drupal.Views.parseViewArgs($(this).attr('href'), settings.view_base_path));
                    $(this).click(function(){
                        $(this).addClass('views-throbbing');
                        $.ajax({
                            url: ajax_path,
                            type: 'GET',
                            data: viewData,
                            success: function(response){
                                $(this).removeClass('views-throbbing');
                                // Scroll to the top of the view. This will allow users
                                // to browse newly loaded content after e.g. clicking a pager
                                // link.
                                var offset = $(target).offset();
                                // Call all callbacks.
                                if (response.__callbacks) {
                                    $.each(response.__callbacks, function(i, callback){
                                        eval(callback)(target, response);
                                    });
                                }
                            },
                            error: function(){
                                $(this).removeClass('views-throbbing');
                                alert(Drupal.t("An error occurred at @path.", {
                                    '@path': ajax_path
                                }));
                            },
                            dataType: 'json'
                        });
                        
                        return false;
                    });
                }); // .each function () {
            }); // $view.filter().each
        }); // .each Drupal.settings.views.ajaxViews
    } // if
};



var showPreloader=function(act){
	if (act) {
		$("<div id='preloader'></div>").appendTo(document.body);
		$(document.body).addClass('overflowHid');
	} else {
		$('#preloader').remove();
		$(document.body).removeClass('overflowHid');
	}
}



// $Id: dhtml_menu.js,v 1.18.2.8 2008/11/09 23:03:35 arancaytar Exp $
function debug(obj){
	for(var i in obj)
		console.info(i+" => "+obj[i])
}
/**
 * @file dhtml_menu.js
 * The Javascript code for DHTML Menu
 */
 
 
Drupal.dhtmlMenu = {};

/**
 * Initialize the module's JS functions
 */
Drupal.behaviors.dhtmlMenu = function() {
	
	
	
	if($('div.tabs-node').length>0){
		var lis=$('div.tabs-node a');
		$(lis[3]).addClass('tabs-node-translate');
		$(lis[3]).attr({title:'Translate'});
		$(lis[2]).addClass('tabs-node-track');
		$(lis[2]).attr({title:'Track'});
		$(lis[1]).addClass('tabs-node-edit');
		$(lis[1]).attr({title:'Edit'});
		$(lis[0]).addClass('tabs-node-view');
		$(lis[0]).attr({title:'View'});
	}
	
  // Do not run this function more than once.
  if (Drupal.dhtmlMenu.init) {
    return;
  }
  else {
    Drupal.dhtmlMenu.init = true;
  }

  // Get the settings.
  //var effects = Drupal.settings.dhtmlMenu;
  

	//Prepare, first open and collapse.
	$('ul.menu li.collapsed').each(function() {
		$(this).find('ul:first').css({display:'none'});
	});
	$('ul.menu a.active').parents('li').each(function(){
		$(this).removeClass('collapsed');
	  	$(this).addClass('expanded');
		$(this).find('ul:first').css({display:'block'});
	})



	$('ul.menu li a').addClass('mainLink');
	
	
        if(Drupal.settings.views) 
			var ajax_path = Drupal.settings.views.ajax_path;
		else
			var ajax_path="";

		if(!ajax_path)
			var ajax_path="";
		
        // If there are multiple views this might've ended up showing up multiple times.
        if (ajax_path.constructor.toString().indexOf("Array") != -1) {
            ajax_path = ajax_path[0];
        }
	
	$('ul.menu li a').each(function() {
		var a=this;
		var category=$(this).attr('href');
		if(!category.match(/category/gi)) return;
		var arg=category.match(/([0-9]*)$/gi);
		arg=arg[0];
		var viewData={
			view_name:'latest_articles',
			view_display_id:'default',
			view_args:arg,
			view_path:category,
			view_base_path:'', 
			view_dom_id:'3',
			pager_element:'0',
			page:'0'
		}


		if (
			ajax_path.length>0 && 
			$('.view-dom-id-3').length>0
			/* && $('.view-dom-id-4').length>0 && 
			$('.view-dom-id-5').length>0*/) {
			$(this).click(function(){
				//alert($(this).html())
				document.title=$(this).html()+" | "+"www.iier.ch";
				showPreloader(true);
				var all = 0;
				
				$('ul.menu li a.active').removeClass('active');
				$(this).addClass('active');
				
				viewData["view_dom_id"] = 3;
				viewData["view_name"] = "latest_articles";
				$.ajax({
					url: ajax_path,
					type: 'GET',
					data: viewData,
					success: function(response){
						all++;
						//if (all == 3) 
						if(all == 1)
							showPreloader(false);
						if (response.__callbacks) {
							$.each(response.__callbacks, function(i, callback){
								//alert(Drupal.Views.Ajax.ajaxViewResponse)
								Drupal.Views.Ajax.ajaxViewResponse($('.view-dom-id-3'), response);
								//Drupal.articles.starter();
							//var e=eval(callback+"("+target+","+response+")");
							
							
							});
						}
					},
					error: function(){
						all++;
						if (all == 3) 
							showPreloader(false);
						$(this).removeClass('views-throbbing');
						alert(Drupal.t("An error occurred at @path.", {
							'@path': ajax_path
						}));
					},
					dataType: 'json'
				});
				
				/*
				viewData["view_dom_id"] = 4;
				viewData["view_name"] = "guest_contribs";
				$.ajax({
					url: ajax_path,
					type: 'GET',
					data: viewData,
					success: function(response){
						all++;
						if (all == 3) 
							showPreloader(false);
						if (response.__callbacks) {
							$.each(response.__callbacks, function(i, callback){
								//alert(Drupal.Views.Ajax.ajaxViewResponse)
								Drupal.Views.Ajax.ajaxViewResponse($('.view-dom-id-4'), response);
							//var e=eval(callback+"("+target+","+response+")");
							
							
							});
						}
					},
					error: function(){
						all++;
						if (all == 3) 
							showPreloader(false);
						$(this).removeClass('views-throbbing');
						alert(Drupal.t("An error occurred at @path.", {
							'@path': ajax_path
						}));
					},
					dataType: 'json'
				});
				
				viewData["view_dom_id"] = 5;
				viewData["view_name"] = "news";
				$.ajax({
					url: ajax_path,
					type: 'GET',
					data: viewData,
					success: function(response){
						all++;
						if (all == 3) 
							showPreloader(false);
						if (response.__callbacks) {
							$.each(response.__callbacks, function(i, callback){
								//alert(Drupal.Views.Ajax.ajaxViewResponse)
								Drupal.Views.Ajax.ajaxViewResponse($('.view-dom-id-5'), response);
							//var e=eval(callback+"("+target+","+response+")");
							
							
							});
						}
					},
					error: function(){
						all++;
						if (all == 3) 
							showPreloader(false);
						$(this).removeClass('views-throbbing');
						alert(Drupal.t("An error occurred at @path.", {
							'@path': ajax_path
						}));
					},
					dataType: 'json'
				});
				*/
				return false;
			});
		}
	});

   $('ul.menu li:not(.leaf,.no-dhtml)').each(function() {
    var li = this;
    $(li).find('a:first').click(function(e) {
      Drupal.dhtmlMenu.toggleMenu($(li),false);
		if(
			ajax_path.length>0 &&  
			$('.view-dom-id-3').length>0 && 
			$('.view-dom-id-4').length>0 && 
			$('.view-dom-id-5').length>0
		) return false;
    });
  });
  
  
  $('ul.menu li:not(.leaf,.no-dhtml)>a').before('<a href="#" class="menusArrow">&rarr;</a>');
  
  
   $('ul.menu li:not(.leaf,.no-dhtml)').each(function() {
    var li = this;

    $(li).find('a:first').click(function(e) {
      Drupal.dhtmlMenu.toggleMenu($(li),true);
      return false;
    });
	$(li).find('a:first').mouseover(function(e) {
		$(this).next().addClass('hover');
	});
	$(li).find('a:first').mouseout(function(e) {
		$(this).next().removeClass('hover');
	});
  });
  
  
    var cookie = Drupal.dhtmlMenu.cookieGet();
	if (cookie.length > 0) {
		cookie = (cookie.split(","));
		//Collapse all opened leafs
		$('ul.menu li.expanded').each(function() {
			var a=$(this).find("a:first").next();
			if(!a.attr('id')) return;
			var id=($(a).attr('id').substr(18));
			var find=false;
			for(var i in cookie){
				if(id==cookie) {
					find=true;
					break;
				}
			}
			if(find===false){
				$(this).removeClass('expanded');
				$(this).addClass('collapsed');
				$(this).find('ul:first').css({display:'none'})
			}
			$(this).find('ul:first').css({display:'none'});
		});
		for (var i in cookie) {
			// If the cookie was not applied to the HTML code yet, do so now.
			var li = $('#dontnod_full_menu-' + cookie[i]).parents('li:first');
			if ($(li).hasClass('collapsed')) {
				//Drupal.dhtmlMenu.toggleMenu(li,true,{});
				$(li).removeClass('collapsed');
				$(li).addClass('expanded');
				$(li).find('ul:first').css({display:'block'});
			}
		}
		$('#mainMenu').css({display:'block'});
	} else {
		$('#mainMenu').css({display:'block'});
	}

	Drupal.dhtmlMenu.cookieSet();
	
	
}

/**
 * Toggles the menu's state between open and closed.
 *
 * @param li
 *   Object. The <li> element that will be expanded or collapsed.
 */
Drupal.dhtmlMenu.toggleMenu = function(li,action,effects) {
  var effects = Drupal.settings.dhtmlMenu;
  if(!effects) 
  	effects = {'slide': true};


  // If the menu is expanded, collapse it.
  if($(li).hasClass('expanded')) {
  	if(action==false) return;
    if (effects.slide) {
      $(li).find('ul:first').animate({height: 'hide', opacity: 'hide'}, '1000');
    }
    else $(li).find('ul:first').css('display', 'none');

    // If children are closed automatically, find and close them now.
    if (effects.children) {
      if (effects.slide) {
        $(li).find('li.expanded').find('ul:first').animate({height: 'hide', opacity: 'hide'}, '1000');
      }
      else $(li).find('li.expanded').find('ul:first').css('display', 'none');

      $(li).find('li.expanded').removeClass('expanded').addClass('collapsed')
    }

    $(li).removeClass('expanded').addClass('collapsed');
  }

  // Otherwise, expand it.
  else {
    if (effects.slide) {
      $(li).find('ul:first').animate({height: 'show', opacity: 'show'}, '1000');
    }
    else $(li).find('ul:first').css('display', 'block');
    $(li).removeClass('collapsed').addClass('expanded');

    // If the siblings effect is on, close all sibling menus.
    if (effects.siblings) {
      var id = $(li).find('a:first').attr('id');

      // Siblings are all open menus that are neither parents nor children of this menu.
      $(li).find('li').addClass('own-children-temp');
	  
      // If the relativity option is on, select only the siblings that have the same parent
      if (effects.relativity) {
        var siblings = $(li).parent().find('li.expanded').not('.own-children-temp').not(':has(#' + id + ')');
      }
      // Otherwise, select all menus of the same level
      else {
        var siblings = $('ul.menu li.expanded').not('.own-children-temp').not(':has(#' + id + ')');
      }

      // If children should not get closed automatically...
      if (!effects.children) {
        // Remove items that are currently hidden from view (do not close these).
        $('li.collapsed li.expanded').addClass('sibling-children-temp');
        // Only close the top-most open sibling, not its children.
        $(siblings).find('li.expanded').addClass('sibling-children-temp');
        siblings = $(siblings).not('.sibling-children-temp');
      }

      $('.own-children-temp, .sibling-children-temp').removeClass('own-children-temp').removeClass('sibling-children-temp');

      if (effects.slide) {
        $(siblings).find('ul:first').animate({height: 'hide', opacity: 'hide'}, '1000');
      }
      else $(siblings).fid('ul:first').css('display', 'none');

      $(siblings).removeClass('expanded').addClass('collapsed');
    }
  }

  // Save the current state of the menus in the cookie.
  Drupal.dhtmlMenu.cookieSet();
}

/**
 * Reads the dhtml_menu cookie.
 */
Drupal.dhtmlMenu.cookieGet = function() {
  var c = /dhtml_menu=(.*?)(;|$)/.exec(document.cookie);
  if (c) {
    return c[1];
  }
  else return '';
}

/**
 * Saves the dhtml_menu cooki.
 */
Drupal.dhtmlMenu.cookieSet = function() {
  var expanded = new Array();
  $('li.expanded').each(function() {
  	if($(this).find('a:first').next().attr('id'))
    	expanded.push($(this).find('a:first').next().attr('id').substr(18));
  });
  document.cookie = 'dhtml_menu=' + expanded.join(',') + ';path=/';
}


Drupal.articles = {}
Drupal.articles.progress = false;
Drupal.articles.starter = function(){
	Drupal.articles.processing = false;
	var $_ = $('.latestArticles .latestArticleSummary');
	if($_.length){
		Drupal.articles.processing = true;
		$_.css('display', 'none');
		$_.css('cursor','pointer');
		$_.bind('click', function(){
			var link = $('.articles_more a', $(this).parent()).attr('href');
			Drupal.articles.processing = false;
			window.location.href = link;
		})
		$('.latestArticles .articles_more').css('display', 'none');
		$('.latestArticles li').addClass('sum_collapsed').bind('mouseover', Drupal.articles.processor);
		if($('.latestArticles li:first .latestArticleSummary').html()) {
			Drupal.articles.expand($('.latestArticles li:first'));
		}
	}
}
Drupal.articles.processor = function(e){
	if(Drupal.articles.progress) return;
	$_ = $('.latestArticles .sum_expanded');
	if($('a',$_).html() == $('a',$(this)).html()) return;
	if(!$('.latestArticleSummary', $(this)).html().length) return;
	Drupal.articles.expand($(this));
	Drupal.articles.collapse($_);
}
Drupal.articles.expand = function(obj){
	Drupal.articles.progress = true;
	$('.articles_more',$(obj)).stop().animate({height: 'show', opacity: 'show'}, '2000');
	
	var h = $('.latestArticleSummary',$(obj)).height();
	//alert(h)
	//alert(h%29)
	var h1 = 29 - h%29 - 8 - 1;//110
	if(h1 < 0) h1 += 29;
	if(h1 < 10) h1 += 29;
	//alert(h1%29);
	
	
	$('.latestArticleSummary',$(obj)).stop().animate({
		height: 'show', 
		opacity: 'show', 
		paddingBottom: h1 + 'px',
		paddingTop: '8px'
	}, '2000', function(){
		Drupal.articles.progress = false;
	});
	$(obj).removeClass('sum_collapsed');
	$(obj).addClass('sum_expanded');
}
Drupal.articles.collapse = function(obj){
	Drupal.articles.progress = true;
	$('.articles_more',$(obj)).stop().animate({height: 'hide', opacity: 'hide'}, '2000');
	$('.latestArticleSummary',$(obj)).stop().animate({
		height: 'hide', 
		opacity: 'hide', 
		paddingBottom: '0px',
		paddingTop: '0px'
	}, '2000', function(){
		Drupal.articles.progress = false;
	});
	$(obj).addClass('sum_collapsed');
	$(obj).removeClass('sum_expanded');
}

$(document).ready(function() {
	//$('.view-dom-id-3').bind('load',Drupal.articles.starter());
	Drupal.articles.starter();
	if(Drupal.Views && Drupal.Views.Ajax && Drupal.Views.Ajax.ajaxViewResponse){
		var $_ = Drupal.Views.Ajax.ajaxViewResponse;
		Drupal.Views.Ajax.ajaxViewResponse = function(target, response){
			$_(target, response);
			$('body').trigger('ajaxView');
		}
	}
	$('body').bind('ajaxView', Drupal.articles.starter);
});

