/* ---------------------------------------------------------------------
   Copyright (C) 2008 IsitAtout Solutions Co., Ltd. All Rights Reserved.
   @license - Copyrighted Software
   Author: Stéphane Francel - IsitAtout Solutions Co., Ltd
   Websites:  http://www.iSitAtout.com/
   --------------------------------------------------------------------- */


/**
 *	jQuery-accordion
 *	----------------
 *	Source: /scripts/jQuery/accordion
 *
 *	Display a menu using 'accordion' behaviour
 *	For details see: 'comMenu_behaviour()' function, into: '/components/com_menu/menu_class.php' script.
 */
$(document).ready(function(){
	$(".accordion .accordion-link").next(".accordion .accordion-list").hide();

	$(".accordion span.accordion-link").click(function(){
	  /* onClick, close menu (if not active) */
	  if($(this).next(".accordion .accordion-list").is(":visible")){
		var isActive=0;
		$(this).next(".accordion .accordion-list").children().children().each(function(){
		  if($(this).is(".accordion .accordion-active")){
			isActive=1;
		  }
		});
		if (isActive==0) $(this).next(".accordion .accordion-list").slideUp();
	  }
	  /* Accordion behaviour */
	  if($(this).next(".accordion .accordion-list").is(":hidden")){
		$(".accordion .accordion-link").next(".accordion .accordion-list:visible").each(function(){
		  if(!$(this).children().children().is(".accordion .accordion-active")){
			$(this).slideUp();
		  }
		});
		$(this).next(".accordion .accordion-list").slideDown();
	  }
	});

	$(".accordion a.accordion-link").mouseover(function(){
	  /* onClick, close menu (if not active) */
	  if($(this).next(".accordion .accordion-list").is(":visible")){
		var isActive=0;
		$(this).next(".accordion .accordion-list").children().children().each(function(){
		  if($(this).is(".accordion .accordion-active")){
			isActive=1;
		  }
		});
		if (isActive==0) $(this).next(".accordion .accordion-list").slideUp();
	  }
	  /* Accordion behaviour */
	  if($(this).next(".accordion .accordion-list").is(":hidden")){
		$(".accordion .accordion-link").next(".accordion .accordion-list:visible").each(function(){
		  if(!$(this).children().children().is(".accordion .accordion-active")){
			$(this).slideUp();
		  }
		});
		$(this).next(".accordion .accordion-list").slideDown();
	  }
	});

	$(".accordion .accordion-active").parents(".accordion .accordion-list").show();
});

/*
$(document).ready(function(){
	$(".accordion .accordion-link").next(".accordion .accordion-list").hide();
	$(".accordion .accordion-link").mouseover(function(){
	  if($(this).next(".accordion .accordion-list").is(":hidden")){
		$(".accordion .accordion-link").next(".accordion .accordion-list:visible").each(function(){
		  if(!$(this).children().children().is(".accordion .accordion-active")){
			$(this).slideUp();
		  }
		});
		$(this).next(".accordion .accordion-list").slideDown();
	  }
	});
	$(".accordion .accordion-active").parents(".accordion .accordion-list").show();
});
*/


/**
 *	jQuery-fontResizer
 *	------------------
 *	Source: /scripts/jQuery/font_resizer
 *
 *	The script has been customized to allow 5 sizes (instead of 3)
 *
 *	Author: http://www.fluidbyte.net
 */
function fontResizer(extraSmall,smallFont,medFont,largeFont,extraLarge){

	function clearSelected(){
		$(".extraSmall").removeClass("curFont");
		$(".smallFont").removeClass("curFont");
		$(".medFont").removeClass("curFont");
		$(".largeFont").removeClass("curFont");
		$(".extraLarge").removeClass("curFont");
	}
	function saveState(curSize){
		var date = new Date();
		date.setTime(date.getTime()+(7*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = "fontSizer"+"="+curSize+expires+"; path=/";
	}

	$(".extraSmall").click(function(){
		$('html').css('font-size', extraSmall);
		clearSelected();
		$(".extraSmall").addClass("curFont");
		saveState(extraSmall);
	});
	$(".smallFont").click(function(){
		$('html').css('font-size', smallFont);
		clearSelected();
		$(".smallFont").addClass("curFont");
		saveState(smallFont);
	});
	$(".medFont").click(function(){
		$('html').css('font-size', medFont);
		clearSelected();
		$(".medFont").addClass("curFont");
		saveState(medFont);
	});
	$(".largeFont").click(function(){
		$('html').css('font-size', largeFont);
		clearSelected();
		$(".largeFont").addClass("curFont"); saveState(largeFont);
	});
	$(".extraLarge").click(function(){
		$('html').css('font-size', extraLarge);
		clearSelected();
		$(".extraLarge").addClass("curFont"); saveState(extraLarge);
	});

	function getCookie(c_name){
		if (document.cookie.length>0){
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1){
				c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
				return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	}

	var savedSize = getCookie('fontSizer');

	if (savedSize!=""){
		$('html').css('font-size', savedSize);
		switch (savedSize){
			case extraSmall: $(".extraSmall").addClass("curFont"); break;
			case smallFont: $(".smallFont").addClass("curFont"); break;
			case medFont: $(".medFont").addClass("curFont"); break;
			case largeFont: $(".largeFont").addClass("curFont"); break;
			case extraLarge: $(".extraLarge").addClass("curFont"); break;
			default: $(".medFont").addClass("curFont");
		}
	}
	else{
		$('html').css('font-size', medFont);
		$(".medFont").addClass("curFont");
	}
}



/**
 * LightBox
 */

$(function() {
  $('a.lightbox').lightBox({
	imageLoading:	'/scripts/jquery/lightbox/images/lightbox-ico-loading.gif',
	imageBtnPrev:	'/scripts/jquery/lightbox/images/lightbox-btn-prev.gif',
	imageBtnNext:	'/scripts/jquery/lightbox/images/lightbox-btn-next.gif',
	imageBtnClose:	'/scripts/jquery/lightbox/images/lightbox-btn-close.gif',
	imageBlank:		'/scripts/jquery/lightbox/images/lightbox-blank.gif',
	txtImage: '',
	txtOf: '/'
  });
});



/**
 * Toggle Long-text
 */

$(document).ready(function(){
  $(".toggle-longtext .longtext-content").hide();
  $(".toggle-longtext .longtext-title").css("cursor","pointer");

  $(".toggle-longtext").children(".longtext-title").click(function(){
	$(this).next(".longtext-content").slideToggle("slow");
	if (!$(this).hasClass("longtext-title-open")){
	  $(this).addClass("longtext-title-open");
	} else {
	  $(this).removeClass("longtext-title-open");
	}
	return false;
  });
});



/**
 * Hoverscroll (see into media_class.php for PHP integration)
 */

(function($) {

/**
 * @method hoverscroll
 * @param	params {Object}  Parameter list
 * 	params = {
 * 		vertical {Boolean},	// Vertical list or not ?
 * 		width {Integer},	// Width of list container
 * 		height {Integer},	// Height of list container
 *  	arrows {Boolean},	// Show direction indicators or not
 *  	arrowsOpacity {Float},	// Arrows maximum opacity
 * 		debug {Boolean}		// Debug output in firebug console
 * 	};
 */
$.fn.hoverscroll = function(params) {
	if (!params) { params = {}; }
	
	// Extend default parameters
	params = $.extend({
		vertical:	$.fn.hoverscroll.params.vertical,
		
		width:		$.fn.hoverscroll.params.width,
		height:		$.fn.hoverscroll.params.height,
		
		arrows:		$.fn.hoverscroll.params.arrows,
		arrowsOpacity: $.fn.hoverscroll.params.arrowsOpacity,
		
		debug:		$.fn.hoverscroll.params.debug
	}, params);
	
	// Loop through each found element
	this.each(function() {
		var $this = $(this);
		
		if (params.debug) {
			$.log('[HoverScroll] Trying to create hoverscroll on element ' + this.tagName + '#' + this.id);
		}
		
		// wrap list with a container
		$this.wrap('<div class="listcontainer"></div>');
		
		$this.addClass('list').addClass('ui-helper-clearfix');
		
		// store handle to container
		var listctnr = $this.parent();
		
		listctnr.wrap('<div class="ui-widget-content hoverscroll"></div>')
		
		var ctnr = listctnr.parent();
		
		if (params.debug) {
			if (ctnr.hasClass('hoverscroll')) { $.log('[HoverScroll] Wrapper created successfully'); }
			else { $.log('[HoverScroll] Could not create or select wrapper'); }
		}
		
		if (params.arrows) {
			if (!params.vertical) {
				listctnr.append('<div class="arrowleft"></div>').append('<div class="arrowright"></div>')
				//.append('<div class="hoverZoneLeft"></div>').append('<div class="hoverZoneRight"></div>');
			}
			else {
				listctnr.append('<div class="arrowtop"></div>').append('<div class="arrowbottom"></div>')
				//.append('<div class="hoverZoneTop"></div>').append('<div class="hoverZoneBottom"></div>');
			}
		}
		
		ctnr.width(params.width).height(params.height);
		listctnr.width(params.width).height(params.height);
		
		var size = 0;
		
		// determine content width or height
		if (!params.vertical) {
			$this.children().each(function() {
				size += $(this).addClass('horizontal-item').outerWidth(true);
			});
			
			$this.width(size);
			size = ctnr.outerWidth();
		}
		else {
			$this.children().each(function() {
				size += $(this).addClass('vertical-item').outerHeight(true);
			});
			
			$this.height(size);
			size = ctnr.outerHeight();
		}
		
		// Define hover zones on container
		var zone = {
			1: { action: 'move', from: 0, to: 0.06 * size, direction: -1 , speed: 16 },
			2: { action: 'move', from: 0.06 * size, to: 0.15 * size, direction: -1 , speed: 8 },
			3: { action: 'move', from: 0.15 * size, to: 0.25 * size, direction: -1 , speed: 4 },
			4: { action: 'move', from: 0.25 * size, to: 0.4 * size, direction: -1 , speed: 2 },
			5: { action: 'stop', from: 0.4 * size, to: 0.6 * size },
			6: { action: 'move', from: 0.6 * size, to: 0.75 * size, direction: 1 , speed: 2 },
			7: { action: 'move', from: 0.75 * size, to: 0.85 * size, direction: 1 , speed: 4 },
			8: { action: 'move', from: 0.85 * size, to: 0.94 * size, direction: 1 , speed: 8 },
			9: { action: 'move', from: 0.94 * size, to: size, direction: 1 , speed: 16 }
		}
		
		// Set default state values to container
		ctnr[0].isChanging = false;
		ctnr[0].direction  = 0;
		ctnr[0].speed      = 1;
		
		
		/**
		 * Check mouse position relative to hoverscroll container
		 * and trigger actions according to the zone table
		 *
		 * @param x {Integer} Mouse X event position
		 * @param y {Integer} Mouse Y event position
		 */
		function checkMouse(x, y) {
			x = x - ctnr.offset().left;
			y = y - ctnr.offset().top;
			
			var pos;
			if (!params.vertical) { pos = x; }
			else { pos = y; }
			
			for (i in zone) {
				if (pos >= zone[i].from && pos < zone[i].to) {
					if (zone[i].action == 'move') { startMoving(zone[i].direction, zone[i].speed); }
					else { stopMoving(); }
				}
			}
		}
		
		
		/**
		 * Sets the opacity of the left|top and right|bottom
		 * arrows according to the scroll position.
		 */
		function setArrowOpacity() {
			if (!params.arrows) { return; }
			
			var maxScroll;
			var scroll;
			
			if (!params.vertical) {
				maxScroll = listctnr[0].scrollWidth - listctnr.width();
				scroll = listctnr[0].scrollLeft;
			}
			else {
				maxScroll = listctnr[0].scrollHeight - listctnr.height();
				scroll = listctnr[0].scrollTop;
			}
			
			var opacity = (scroll / maxScroll);
			var limit = params.arrowsOpacity;
			
			if (opacity <= 0) { $('.arrowleft, .arrowtop', ctnr).hide(); }
			else if (opacity >= limit) { $('.arrowright, .arrowbottom', ctnr).hide(); }
			else {
				$('.arrowleft, .arrowtop', ctnr).show().css('opacity', (opacity > limit ? limit : opacity));
				$('.arrowright, .arrowbottom', ctnr).show().css('opacity', (1 - opacity > limit ? limit : 1 - opacity));
			}
		}
		
		
		/**
		 * Start scrolling the list with a given speed and direction
		 *
		 * @param direction {Integer}	Direction of the displacement, either -1|1
		 * @param speed {Integer}		Speed of the displacement (20 being very fast)
		 */
		function startMoving(direction, speed) {
			if (ctnr[0].direction != direction) {
				if (params.debug) {
					$.log('[HoverScroll] Starting to move. direction: ' + direction + ', speed: ' + speed);
				}
				
				stopMoving();
				ctnr[0].direction  = direction;
				ctnr[0].isChanging = true;
				move();
			}
			if (ctnr[0].speed != speed) {
				if (params.debug) {
					$.log('[HoverScroll] Changed speed: ' + speed);
				}
				
				ctnr[0].speed = speed;
			}
		}
		
		/**
		 * Stop scrolling the list
		 */
		function stopMoving() {
			if (ctnr[0].isChanging) {
				if (params.debug) {
					$.log('[HoverScroll] Stoped moving');
				}
				
				ctnr[0].isChanging = false;
				ctnr[0].direction  = 0;
				ctnr[0].speed      = 1;
				clearTimeout(ctnr[0].timer);
			}
		}
		
		/**
		 * Move the list one step in the given direction and speed
		 */
		function move() {
			if (ctnr[0].isChanging == false) { return; }
			
			setArrowOpacity();
			
			var scrollSide;
			if (!params.vertical) { scrollSide = 'scrollLeft'; }
			else { scrollSide = 'scrollTop'; }
			
			listctnr[0][scrollSide] += ctnr[0].direction * ctnr[0].speed;
			ctnr[0].timer = setTimeout(function() { move(); }, 50);
		}
		
		// Bind actions to the hoverscroll container
		ctnr
		// Bind checkMouse to the onmousemove
		.mousemove(function(e) { checkMouse(e.pageX, e.pageY); })
		// Bind stopMoving to the onmouseout
		.mouseleave(function() { stopMoving(); });
		
		if (params.arrows) {
			setArrowOpacity();
		}
		else {
			$('.arrowleft, .arrowright, .arrowtop, .arrowbottom', ctnr).hide();
		}
	});
	
	return this;
};



/**
 * HoverScroll default paramters
 */
$.fn.hoverscroll.params = {
	vertical:	false,
	width:		400,
	height:		50,
	arrows:		true,
	arrowsOpacity: 0.7,
	debug:		false
};



/**
 * log to firebug console if exists
 */
$.log = function(msg) {
	if (console && console.log) {
		console.log(msg);
	}
};


})(jQuery);


