// JavaScript Document

/**
 *  This Javascript library file contains code that is necessary for the DLC Blog submenu module to function
 * 
 */


/******************************************************************************************
* Get QuerySring Parameter
******************************************************************************************/

function getParameterByName(name) {

    var match = RegExp('[?&]' + name + '=([^&]*)')
                    .exec(window.location.search);

    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));

}




/******************************************************************************************
* Tile Menu Click
******************************************************************************************/	

function handleTileMenuClick (linkObj) {
	
	  
	  var debug=0;
		  
	  // -- Get URL Parameter - Menu ID
	  
	  var itemID = getParameterByName('Itemid');   
	  
	  var linkHref = ""+linkObj.attr('href');   
	  
	  if (window.console && debug) console.log('Item:'+itemID);
	  
	  var patt1=/blog/gi;

	  
	  // -- Check for tiles
	  
	  if ( !$('#tileDiv') ) {
		  
		  if (window.console && debug) console.log('no tiles : exit');
		  document.location = linkHref;
		  return false;
		  
	  } else if ( $('#tileDiv').text() == '' ) {
		  
		  if (window.console && debug) console.log('no tiles : exit');
		  // take me directly to the requested link
		  document.location = linkHref;
		  return false;
	  
	  
		  
	  } else if ( 	linkObj.attr("rel").match(patt1) ||
					linkObj.attr("rel") == 'publications'
							   ) {
		  
		  if (window.console && debug) console.log('direct link');
		  // take me directly to the requested link
		  document.location = linkHref;
		  return false;
		  
	  }
	  
	  
		  
	  
	  var url = linkObj.attr('href');
	  
	  var list_name = linkObj.attr('rel');
	  
	  
	  //-- Get the current page's Itemid
	  
	  replaceOldItemid = "Itemid="+  itemID; 
	  
	  
	  //-- Get the Itemid from the link
	  
	  patt = /Itemid=(\d+)/,
	  clickItemid = patt.exec(url)[1];
	  withNewItemid = "Itemid="+clickItemid;
	  
	  // not sure why i need to set the item id but i'm sure during testing it will become obvious
	  //jQuery.query.set('Itemid', clickItemid);
	  
	  //-- Remove menu highlight from the current menu
	  
	  jQuery('#tileMenu #current').removeClass('active').attr('id', '');
	  
	  //-- Apply menu highlight to clicked link
	  
	  linkObj.parent().addClass('active').attr('id','current');
	  
	  //-- Get page
	  
	  getPageContents(url, list_name);
	  
	  
	  return true;
	  
	  
  }



/******************************************************************************************
* Get Page Contents
******************************************************************************************/	
		   
  
  function getPageContents(url, list_name) {
		
		
		
		var html = "";
		
		var debug=1;
		
		
		// append format=raw to ensure ajax response is clean
		
		var partsArray = url.split('?');
		
		var dataString = partsArray[1] + "&format=raw";
		
		
		// ---------------------------------
		// Call to AJAX 
		
		jQuery.ajax({
					
		  type: "GET",
		  
		  url: "index.php", 
		  
		  cache: false,
		  
		  data: dataString,
		  
		  //dataType: 'html',
		  
		// ----------------------------------------------------------------------
		// Network Error
		  
		  error: function(request,textStatus,errorThrown) {
			  
		  	if (window.console && debug) console.log('ERROR RESULT'); // log the result
			
			if (window.console && debug) console.log('textStatus: '+ textStatus); // log the result
			
			// do not handle the errors
			return "";
				
		  },
		  
		  
		  
		  
		  
		// ----------------------------------------------------------------------
		// Success
		  
		  
		 success: function(data,textStatus,xhr) { // data,textStatus,request
		  	
			var containerDiv = '#tileDiv';
			
			var itemList = '.projects';
			
			var sourceList = 'ul.items';
			
			var itemContainer = containerDiv+' .'+list_name ;
			
			
			//-- create the itemContainer
			jQuery( itemContainer ).remove();
			
			jQuery( containerDiv ).append("<ul class='"+ list_name +"'></ul>");
			
			
			//-- get the output from the response data
			
			var mydata = data;
			
			var divs = jQuery(data);
			
			var resultList = divs.find(sourceList).html();
			
			var paginationList = divs.find(sourceList).parent().next().html();
			
			if ( jQuery( itemContainer ).html() == null   ) {
				
				if (window.console && debug) window.console.log('no ('+list_name+') list found ');
				
				return true;
				
			} 
			
			
			
			// place result into the item container
			
			jQuery( itemContainer ).html('').append(resultList);
			
			jQuery('div.pagination').html('').append(paginationList);
			
			
			
			// -- Remove format=raw from each link
			
			jQuery('.pagination a').each(function() {
					
					newLink = jQuery(this).attr('href')
							
							.replace(/_=[0-9]+/, "") // get rid of the token variable
							.replace('&&', "") 
							.replace('format=raw', '' );
					
					jQuery(this).attr('href', newLink);
					
			});
			
			
				
			//-- trigger the quicksand animation
			
			
			jQuery('.items').quicksand( jQuery('.'+list_name+' li'), {
											   
											   
			  duration: 1000,
			  attribute: 'id',
			  easing: 'easeInOutExpo'
			  
			  
			}, function() {
				
				jQuery( itemContainer ).html('');
				DoTileResize();
				bindEvents();
				
			});
			
				
		} // end success condition
		
		
		  
		}); // end ajax post
		
		
		// -- END AJAX
		// ----------------------------------------------------------------------
		
		return "";


				
}
		  
		  
		  
