//var lastVisit = 1256204421;

$(document).ready(function(){
  
	/*** Preloading ***/  
	  
	//var prelod = ['image1.gif', 'image2.gif'];
	//$.preloadImages('image1.gif', 'image2.gif'); 
	 
		$.preloadImages = function() {
			var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
			for(var i = a.length -1; i > -1; i--) {
				jQuery("<img>").attr("src", a[i]);
			}
		}  
		
		var preload = ['http://www.actuvore.com/_img/ajaxload/arrows.gif'];
		$.preloadImages(preload); 
	
	/*** End of preloading ***/
	
	
	// Cookies
	function newSinceLastVisit() {		
    // Check the time of last visit if available    
    var lastVisit = $("meta[name=lastVisit]").attr("content");    
    if (isNaN(lastVisit)) {
       lastVisit = 0;    // New visitor or visitor who removed his cookie
    }	else
			lastVisit = lastVisit - (60 * 60 * 0.5); // Nouveau depuis la derničre visite ou nouveau depuis la derniere demi-heure (peut differencier entre les deux)	
	
	
		$("a").attr("rev", function() {
		  if ( this.rev  > lastVisit) {
		    $(this).addClass("new");	// Add class to div newsTitle
		  }
		});
	}	
	newSinceLastVisit();

  // Show and hide descriptions
  function showHideDescription() {
	  $(".newsTitle").hover(function(){  						// Hovering over the link
	    $(this).next().show();  									// Show description
	    //$(this).next().fadeIn( 100 )
	    //$(this).next().get().filter(".description").show();
	  },function(){    														// Mouse no longer over the link
	    $(this).next().hide();    								// Hide description
	    $(this).next().hover(function(){ 					// Hovering over description
	      $(this).prev().children().addClass("aNewsTitle");  // switch the link to white to emulate mouse over it
	      $(this).parent().css({ "background-color": "#1378E0" });
	      //$(this).prevAll().css({"background-image": "none"})
	      //$(this).parent().css({ "color": "#c00", "background-color": "#1378E0" });
	      $(this).children().css({ "background-color": "#f5f5f5" });
	      $(this).show();
	      //$(this).slideDown("slow")
	    },function(){ // Mouse no longer over description
	      $(this).prev().children().removeClass("aNewsTitle");		// remove the white color from the link
	      $(this).parent().css({ "background-color": "#f7f7f7" });
	      $(this).children().css({ "background-color": "#fff" });
	      $(this).hide();
	    });
	  });
	}
	showHideDescription();    
	
	//$(".newsBlock").draggable({ axis: 'x' });
  //$(”.newsBlock”).html(”");
    
  function handlePaginationClick(newPageIndex, paginationContainer) {

		var ajax_load = "<img id='loading' src='http://www.actuvore.com/_img/ajaxload/arrows.gif' alt='' />";
		var loadUrl = "/paginate.php";
		var blocSize = $(this)[0].items_per_page;	
		var blocName = $(paginationContainer).prevAll("h3").children().attr("title");
		var feedId = $(paginationContainer).parent().attr("id");
		var getValues = "feedId=" + feedId + "&startItem=" + newPageIndex * blocSize + "&blocSize=" + blocSize;		
		
		//var eventType = "n/a";
		var eventLabel = blocName + " :: Pagination";
		var eventCategory = "Newsblock pagination";

		//alert("eventType = " + eventType + " | eventLabel = " + eventLabel + " | eventCategory = " + eventCategory);
		//return false;
		// Try to distiguish between prev & next

		$(paginationContainer)
			.prev('.ajaxload') 	// Class not needed -> added for security (prevAll ?)
	  	.html(ajax_load);		// Load the image
						        
		$.ajax({								
		 	cache: false,
			type: "GET",
		 	url: loadUrl,
		 	data: getValues,
		 	
			complete: function(){
			 	//$('.ajaxload') 					// Works too -> Clears every div.ajaxlaod
				$(paginationContainer)
					.prev('.ajaxload')			// Line not needed -> Added for security		 
				 	.html("");							// Removes the loading arrow
			},							 	
			success: function(result){
				var options = {direction:  "right" };
				//var selectedEffect = "blind"; // blind, slide
				var speed = 100;

				$(paginationContainer)
					.nextAll('ul')					// Could use .next().next() or any other traversing method
					.parent().css({ "background-color": "#f7f7f7" }).end()
					
					//.hide(selectedEffect, options, speed, function() { // Need to know direction
					.fadeOut(speed, function() {
						$(this).html(result);	
					})
					
					//.show(selectedEffect, {direction: "left"}, speed, function () {
					.fadeIn(speed, function () {
						$(this).parent().css({ "background-color": "#bbb" });	
						showHideDescription();		// Needed because ajax call doesn't use the already loaded javascript (should find another solution)
						newSinceLastVisit();			
						clickTracking();									
					})
					
					;					
			}
		});		// End of $.ajax
		
		_gaq.push(['_trackEvent', eventCategory, 'next/prev', eventLabel]);	// ganalytics
		return false;
  }  
	 
	// ie6 fix -> remove when replace text with image  
	if ($.browser.msie && $.browser.version.substr(0,1) == 6){
			var previous	=	"&lt;";
			var next 			=	"&gt;";
			//var previous	=	"&laquo;";
			//var next 			=	"&raquo;";			
	}	else {
			var previous	=	"&#9668;";
			var next 			=	"&#9658;";	
	}   
	// End of ie fix
	   
	// First Parameter: number of items
	// Second Parameter: options object
	$(".newsPagination")
	.pagination(21, {
			items_per_page: 7,  
			prev_text: previous,
			next_text: next,			
			callback:handlePaginationClick
		});	
		
	//$.glamGA('UA-11701595-1'); // Track links		
	
 
	//Bind tracking classes
	//$(".trackDownload,.trackOutbound,.trackEmail").click(function(){
	
	function clickTracking() { 
	
		$(".trackDownload,.trigger,.trackEmail").click(function(){
 
			var eventType = this.className,
				relValue = $(this).attr("rel"),
				eventCategory;		
				
			var action = $(this).parents("ul").prevAll("h3").children().attr("title");
	
	 
			//Check if rel attribute is set. If so, use the rel value as the event label.
			if (relValue.length) {
	 
				var eventLabel = relValue;
	 
				if (eventType == "trackDownload") {
					eventCategory = "downloads";
				} //else if (eventType == "trackOutbound") {
					else if (eventType == "trigger") {
					eventCategory = "External news";
				} else if (eventType == "trackEmail") {
					eventCategory = "mailto";
				} 
			}
			//Otherwise we'll use the URL or filename as the event label.
			else {	
	 
				var trackURL = $(this).attr("href"),
						blocID = $(this).parent().prev("h3 a").attr("title"),
						eventLabel;
	 
				if (eventType == "trackDownload") {
					eventLabel = trackURL.substr(trackURL.lastIndexOf("/") + 1);
					eventCategory = "downloads";
				} //else if (eventType == "trackOutbound") {
					else if (eventType == "trigger") {
					eventLabel = trackURL;
					eventCategory = "External news";
				} else if (eventType == "trackEmail") {
					eventLabel = trackURL.substr(7);
					eventCategory = "mailto";
				} 
			}
			
			
			//alert("action = " + action + " | eventLabel = " + eventLabel + " | eventCategory = " + eventCategory);
			//return false;		
	 
			//Send the event to Analytics
			//pageTracker._trackEvent(eventCategory, 'click', eventLabel);		
			//pageTracker._trackEvent(category, action, optional_label, optional_value)
			//_gaq.push(['_trackEvent', 'name', value]);
			_gaq.push(['_trackEvent', eventCategory, action, eventLabel]); // Try this one
	 
		});	// End bind tracking classes
	} // End of function clickTracking
	clickTracking();
	
});
	
// End
