/**
 * Common page scripts
 * 
 * Also Peppered config (jQuery.ppprd) is defined here 
 * 
 * requires: jQuery (1.4)
 * 
 * load: head
 * 
 * @author AK|Peppered
 */

function isEmpty(str) { if (str === null || str === '' || str == 0) { return true; } return false; }

//if (window.console && window.console.debug) {
//	if  (window.console.firebug) {
//		$.log = console.debug;
//	}
//	else { // Safari doesn't like the above function mapping
//		$.log = function(obj) { console.debug(obj) };
//	}
//}
//else if (window.console && window.console.log) {
//	$.log = console.log;
//}
if (false) {} 
else {			
	$.log = function(){};
}


var common = {};
var imageCache = [];
common.imagePreload = {};
common.imagePreload.images = [];
common.imagePreload.imageCache = [];

/**
 * imagePreload
 */
$.fn.imagePreload = function(){	
	
	var ic = common.imagePreload.imageCache;
	
	return this.each(function(){
		var $this = $(this);
		
		if ($this.attr('src')) {			
			// images urls
			var src1 = $this.attr('src');
			var src2 = src1.replace(/(\/.*)\.(...)$/, '$1_over.$2');
			
			// preload
			var cacheImage = document.createElement('img');
			cacheImage.src = src2;
			ic.push(cacheImage);
			
			// hover
			$this.hover(function(){
				$(this).attr('src', src2);
			}, function(){
				$(this).attr('src', src1);
			});
		}
	});	
};


common.imagePreload.preloadImages = function(){
	$('input[type="image"]:not(:disabled)').imagePreload();
};


/**
 * Image Replace
 * Appends some spans to selection, to aid in IR (css)
 * 
 * @version 1.0.0
 * @author AK|Peppered
 */

$.fn.ImageReplace = function(){
	var $result = this.each(function(){
		$(this).addClass('ir');
		$(this).append('<span class="ir-aid"></span>');
	});
	$('body').addClass('ir-active');
	return $result;
};


// domready

$(function(){
	
	// body js/jquery is enabled class
	$('body').removeClass('nojs').addClass('jsfx');
			
	// refresh queue (prevent timeout)
	if (typeof Xajax != 'undefined') {
		window.setInterval("xajax_UpdateActivity()", 270000);
	}
	
	/**
	 * cross browser stuff
	 */
	// correct boxmodel input[type="text"] FF<3
	if ($.browser.mozilla && parseFloat($.browser.version) < 1.9) {
		$("body").addClass("FF2");
	}
	
	// ie6 workarounds for unsupported css
	if ($.browser.msie && parseFloat($.browser.version) < 7) {
		$("body").addClass("IE6");
		$("acronym[title]").addClass('hasTitle'); /* note: ie6 doesn't support abbr element */
	}		
	
	/**
	 * Anchors
	 */				
	// hover images for certain anchor types
	$('a.more').addClass('more-fx').wrapInner('<span class="hoverHelper" />');
	$('a.next').addClass('next-fx').wrapInner('<span class="hoverHelper" />');
	$('a.prev').addClass('prev-fx').wrapInner('<span class="hoverHelper" />');
	
	/**
	 * external (rel="external")
	 */
	$("a[rel='external']").click(function(){
		window.open(this.href);
		return false
	});	
	
	/**
	 * Dropmenu
	 */
	(function(){		
		Array.prototype.in_array =  function(search_term) {
		   for (var i = 0; i < this.length; i++) {
	    	  if (this[i] === search_term) {
	         return true;
	   	   }
	  	 }
	   return false;
		}
		
		var currentMenuItem, currentSubMenuItem;
		var dropMenuHideTimeout, dropSubMenuHideTimeout, dropSubMenuShowTimeout, holdSubMenuTimeout;
		var hoveredMenuId, hoveredSubMenuId; // the items that are just hovered
		var holdSubMenu = false;
		var item;
		
		// dropmenu
		$("#menu .dropMenu").hide();
		$("#menu .dropMenuContainer").hover(function(){
			clearTimeout(dropMenuHideTimeout);
			if (hoveredMenuId && $(this).attr("id") != hoveredMenuId) {
				$('#'+hoveredMenuId).children(".dropMenu").hide();
			}
			$(this).children(".dropMenu").show();
							
		}, function(){
			//$("#menu .dropSubMenu").hide();
			currentMenuItem = $(this);
			hoveredMenuId = currentMenuItem.attr("id");
			dropMenuHideTimeout = setTimeout(function(){currentMenuItem.children(".dropMenu").hide()}, 500);
		});
		
		// dropsubmenu
		$("#menu .dropSubMenu").hide();
		$("#menu .dropSubMenuContainer").hover(function(){
			if (holdSubMenu) {
				// don't show this hovered submenu when another is just opened..
				itemId = $(this).attr("id");
				holdSubMenuTimeout = setTimeout(function(){clearHold(itemId)}, 350);
				return;
			}
	
			clearTimeout(dropSubMenuHideTimeout);
			clearTimeout(dropSubMenuShowTimeout);
			//$("#menu .dropSubMenu").hide();
			if (hoveredSubMenuId && $(this).attr("id") != hoveredSubMenuId) {
				$('#'+hoveredSubMenuId).children(".dropSubMenu").hide();
			}
			//$(this).children(".dropSubMenu").show();
			currentSubMenuItem = $(this);
			dropSubMenuShowTimeout = setTimeout(function(){showSubMenu(currentSubMenuItem)}, 100);
			//showSubMenu(currentSubMenuItem);
			//alert($(this).attr("id"));
			
		}, function(){
			currentSubMenuItem = $(this);
			hoveredSubMenuId = currentSubMenuItem.attr("id");
			//$(this).children(".dropSubMenu").slideUp();
			dropSubMenuHideTimeout = setTimeout(function(){currentSubMenuItem.children(".dropSubMenu").hide()}, 500);
		});
		 
		function showSubMenu(theSubMenu) {
			// show subMenu, don't hide it when user hovers over mainmenu item
			clearTimeout(dropSubMenuShowTimeout);
			$("#menu .dropSubMenu").hide(); // force all to hide
			theSubMenu.children(".dropSubMenu").show();
			holdSubMenu = true;
		}
		function clearHold(hoveredSubMenuId) {
			holdSubMenu=false;
			showSubMenu($('#'+hoveredSubMenuId));
			return;
			// if other than holded submenu...
			//if (hoveredSubMenu != currentSubMenuItem) {
			if (hoveredSubMenuId && hoveredSubMenuId != currentSubMenuItem.attr("id")) {
				currentSubMenuItem.children(".dropSubMenu").hide();
				$('#'+hoveredSubMenuId).children(".dropSubMenu").show();
			}
		}
		
		//togglebutton
		$(".toggleItem").hide();
		$(".toggleButton").click(function(){
			$(".toggleItem").toggle();
			$(this).toggleClass('toggleButtonHide');
		});
		
		// calendar columns
		$("table.calendar tr.weekdays th a").mouseover( function() {
			var iIndex = $(this).parent().parent().children('th').index($(this).parent()[0]);
			$("table.calendar tr td:nth-child(" + (iIndex+1) + ") a").addClass('active');
		 });
		$("table.calendar tr.weekdays th a").mouseout( function() {
			var iIndex = $(this).parent().parent().children('th').index($(this).parent()[0]);
			$("table.calendar tr td:nth-child(" + (iIndex+1) + ") a").removeClass('active');
		 });
		 
		// calendar rows
		$("table.calendar th.week a").mouseover( function() {
			$(this).parents('tr').find('a').addClass('active') });
		$("table.calendar th.week a").mouseout( function() {
			$(this).parents('tr').find('a').removeClass('active') });
	
			
		//styled select
		$('.selectorContainer').hover(
			function(){
				$(this).children('.pullDown').show();
			},
			function(){
				$(this).children('.pullDown').hide();
			});
		
		$(".pullDown span").hover(
			function(){
				$(this).addClass('dropMenuHover');
			},
			function(){
				$(this).removeClass('dropMenuHover');
			}
		)
	})();
	
});	
	

