/**
 * @file ajaxView.js
 *
 * Handles AJAX fetching of views, including filter submission and response.
 */
(function ($) {

/**
 * Attaches the AJAX behavior to Views exposed filter forms and key View links.
 */
Drupal.behaviors.ViewsAjaxView = {};
Drupal.behaviors.ViewsAjaxView.attach = function() {
  if (Drupal.settings && Drupal.settings.views && Drupal.settings.views.ajaxViews) {
    // Retrieve the path to use for views' ajax.
    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) {
	  //alert(dump(settings));
	  
      var view = '.view-dom-id-' + settings.view_dom_id;
      var element_settings = {
        url: ajax_path,
        submit: settings,
        setClick: true,
        event: 'click',
        selector: view,
        progress: { type: 'throbber' },
		error: function(){
		}
      };
	  
      // 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 () {
        var button = $('input[type=submit]', this);
        button = button[0];
		
		if(settings.view_name == "article_content_overview"){
			// Overlay (FadeOut)
			fadeOutOverlay();
			
			// Custom bindings für die Seitenzahlen
			$('.pager a').bind('click', function(){
				// Overlay (FadeIn)
				fadeInOverlay();
			});
			
			var tid = settings.view_path.replace("taxonomy/term/", "")
			if (tid == "754"){
				$("#edit-term-node-tid-depth").css("display","none");
			}
		}
		
		// ---------------------------------------------------
		
		var ajax = new Drupal.ajax($(button).attr('id'), button, element_settings);
      })
      .addClass('views-processed')

      $(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 attachment summary links.
            .find('ul.pager > li > a, th.views-field a, .attachment .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)
              );

              // For anchor tags, these will go to the target of the anchor rather
              // than the usual location.
              $.extend(viewData, Drupal.Views.parseViewArgs($(this).attr('href'), settings.view_base_path));

              element_settings.submit = viewData;
              var ajax = new Drupal.ajax(false, this, element_settings);
            }); // .each function () {
      }); // $view.filter().each
    }); // .each Drupal.settings.views.ajaxViews
  } // if
  
	// ----------------------------------------------------------
	// OVERLAY FUNCTIONS (Warning: Copy changes to bps_cat_overview.js, too!!!)
	// ----------------------------------------------------------

	function fadeInOverlay() {
		// Filter Overlays erstellen
		var overlayFilterOben = jQuery('<div id="overlay"></div>');
		var overlayFilterUnten = jQuery('<div id="overlay"></div>');
		
		// Content Overlay erstellen
		var overlayContent = jQuery('<div id="overlay"></div>');
		overlayContent.css("display","none");
		
		// Filter Overlays an die Seite pappen
		$('.top_pager_wrapper .item-list').append(overlayFilterOben);
		$('.view-article-content-overview .item-list').append(overlayFilterUnten);
		
		// Content Overlay an die Seite pappen, und ein faden
		$('.view-article-content-overview div.view-content').append(overlayContent);
		overlayContent.fadeIn('slow');
	}
	
	function fadeOutOverlay() {
		// Content Overlay erstellen
		var overlayContent = jQuery('<div id="overlay"></div>');
		
		// Content Overlay an die Seite pappen, und aus faden
		$('.view-article-content-overview div.view-content').append(overlayContent);
		overlayContent.fadeOut('slow');
		
		// Ad Hack?
		//document.write('<div style="position:absolute; left:0px; top:0px;"><img border="0" width="1" height="1" alt="" src="http://biermann.adspirit.de/adview.php?tz=1310773252&pid=312&kid=41&wmid=127&wsid=7&uid=1&nvc=1&target1=-" /></div><scr'+'ipt language="Javascr'+'ipt1.1" type="text/javascr'+'ipt">\r\nvar ftClick = "http://biermann.adspirit.de/adclick.php?tz=1310773252&pid=312&kid=41&wmid=127&wsid=7&uid=1&ord=1310773252&target=";\r\nvar ftX = "";\r\nvar ftY = "";\r\nvar ftZ = "";\r\nvar ftContent = "";\r\nvar ft180x300_OOBclickTrack = "";\r\nvar ftRandom = Math.random()*1000000;\r\nvar ftBuildTag1 = "<scr";\r\nvar ftBuildTag2 = "</";\r\nvar ftTag = ftBuildTag1 + \'ipt language="javascr'+'ipt1.1" type="text/javascr'+'ipt" \';\r\nftTag += \'src="http://servedby.flashtalking.com/imp/2/15770;122066;201;js;biermannnetDE;offenerFachbereichCA/?click=\'+escape(escape(ftClick))+\'&ftx=\'+ftX+\'&fty=\'+ftY+\'&ftadz=\'+ftZ+\'&ftscw=\'+ftContent+\'&cachebuster=\'+ftRandom+\'" id="ftscr'+'ipt_180x300" name="ftscr'+'ipt_180x300"\';\r\nftTag += \'">\' + ftBuildTag2 + \'scr'+'ipt>\';\r\ndocument.write(ftTag);\r\n</scr'+'ipt>\r\n<noscr'+'ipt>\r\n<a href="http://biermann.adspirit.de/adclick.php?tz=1310773252&pid=312&kid=41&wmid=127&wsid=7&uid=1&ord=1310773252&target=http://servedby.flashtalking.com/click/2/15770;122066;0;209;0/?url=345485" target="_blank">\r\n<img border="0" src="http://servedby.flashtalking.com/imp/2/15770;122066;205;jpg;biermannnetDE;offenerFachbereichCA/?"></a>\r\n</noscr'+'ipt>\r\n\r\n');		
	}

	// ----------------------------------------------------------
	
	/**
	 * Function : dump()
	 * Arguments: The data - array,hash(associative array),object
	 *    The level - OPTIONAL
	 * Returns  : The textual representation of the array.
	 * This function was inspired by the print_r function of PHP.
	 * This will accept some data as the argument and return a
	 * text that will be a more readable version of the
	 * array/hash/object that is given.
	 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
	 */
	function dump(arr,level) {
		var dumped_text = "";
		if(!level) level = 0;
		
		//The padding given at the beginning of the line.
		var level_padding = "";
		for(var j=0;j<level+1;j++) level_padding += "    ";
		
		if(typeof(arr) == 'object') { //Array/Hashes/Objects 
			for(var item in arr) {
				var value = arr[item];
				
				if(typeof(value) == 'object') { //If it is an array,
					dumped_text += level_padding + "'" + item + "' ...\n";
					dumped_text += dump(value,level+1);
				} else {
					dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
				}
			}
		} else { //Stings/Chars/Numbers etc.
			dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
		}
		return dumped_text;
	}
};
})(jQuery);
;

