document.documentElement.className += " hasJS";

$(document).ready(function(){
	//cluetip
	$('input.jtip').cluetip({attribute: "title", sticky:true, activation: 'focus'});
	
	//cluetip
	$('a.jtip').cluetip();
	
													
	//tabs
	$('.jtabs').idTabs();

													 
	//cycle for news (top)
	$('#news-widget ul').cycle();
	
	
	//cycle for news (homepage)
	$('#box-news .slides').cycle({
		fx:'fade',
		next: $('.btn-next', this), 
		prev: $('.btn-prev', this),
		pos: null
	});
	$('#box-news .btn-pause').click(function() { 
		$('#box-news .slides').cycle('pause'); 
		$(this).hide();
		$('#box-news .btn-play').show();
		return false;
	});
	$('#box-news .btn-play').click(function() { 
		$('#box-news .slides').cycle('resume'); 
		$(this).hide();
		$('#box-news .btn-pause').show();
		return false;
	});
	
	
	/* cleaning des champs input on focus/blur */
	$.fn.search = function(type) {
		return this.focus(function() {
			if( this.value == this.defaultValue ) {
				this.value = "";
			}
		}).blur(function() {
			if( !this.value.length ) {
				this.value = this.defaultValue;
			}
		});
	};
	$('.jfocus').search();
	
	
	/* jbtn: opacity */
	$('.jbtn').hover(function(){
		if($(this).is(':animated')) $(this).stop();
		$(this).css('opacity', 0.85);
	}, function(){
		if($(this).is(':animated')) return;
		$(this).animate({opacity: 1});
	});
	
	
	/* count click */
	$('a[href*=#]', '.jcountclick').data('clicked', false).click(function(){
		if($(this).data('clicked')) return false;
																																				
		var action = "/recordFaq.ajax.php";
		var href = $(this).attr('href');	
		var params = parseUri(href);

		$.post(action, { Id: params.anchor });
		$(this).data('clicked', true);
	});
	
	
	/* popup link */
	$('.jpopup').click(function(){
		var w = getParam(this.href, 'w') || 600;
		var h = getParam(this.href, 'h') || 400;
		var l = ($(window).width())/2 - w/2;
		var t = ($(window).height())/2 - h/2;
		var s = getParam(this.href, 's') || 1;
		window.open(this.href, '','top='+t+', left='+l+', width='+w+',height='+h+', scrollbars='+s+', resizable=1');
		return false;
	});	
	
	$('.jhover > *').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});
});


/* parsing of url parameters */
function getParam(url, name){
	var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url );
	if( results == null )
		return "";
	else 
		return results[1];
}


// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
function parseUri(str){var o=parseUri.options,m=o.parser[o.strictMode?"strict":"loose"].exec(str),uri={},i=14;while(i--)uri[o.key[i]]=m[i]||"";uri[o.q.name]={};uri[o.key[12]].replace(o.q.parser,function($0,$1,$2){if($1)uri[o.q.name][$1]=$2});return uri};parseUri.options={strictMode:false,key:["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],q:{name:"queryKey",parser:/(?:^|&)([^&=]*)=?([^&]*)/g},parser:{strict:/^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,loose:/^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/}};




/* flash replacement (title) */
var neosansbold = { src: '/flash/neosansbold.swf' };
sIFR.activate(neosansbold);
sIFR.replace(neosansbold, {
	wmode: 'transparent',
  selector: '.h01',
  css: '.sIFR-root {color:#4b4b4b;}'
});

var neosans = { src: '/flash/neosans.swf' };
sIFR.activate(neosans);
sIFR.replace(neosans, {
	wmode: 'transparent',
  selector: '.h02',
  css: '.sIFR-root {color:#192022;}'
});

var neosansbold = { src: '/flash/neosansbold.swf' };
sIFR.activate(neosansbold);
sIFR.replace(neosansbold, {
	wmode: 'transparent',
  selector: '.h07',
  css: '.sIFR-root {color:#4b4b4b;}'
});


//save as bookmark
function bookmarksite(){
	if (document.all){ // For IE
		 window.external.AddFavorite(location.href, document.title);
	}	else if (window.sidebar){ // For Firefox
		 window.sidebar.addPanel(document.title, location.href, "");
	}
}


;(function($){					 
	$.fn.jforminfo = function(content) {
		var container = this;
		var idle=true;
	
		//Extend
		$.extend(this, {
			init: function(){
				this.each(function(){			 
					$('.info', this).each(container.save);
					$('input', this).each(container.bindInput);
					container.buildContent();
				});	
			},

			save: function(){
				var element = $(this).prev()[0];
				element.info = this.innerHTML;
				element.label = $('label', this.parentNode).text().split('* ')[1];
				$(this).remove();
			},
			
			bindInput: function(){
				if(this.info){
					$(this).bind('focus', container.show);
					$(this).bind('blur', container.hide);			
				}
			},
			
			show: function(){
				var label = this.label;
				var info = this.info;
				var trigger = function(){
					idle=false;
					$(tip).fadeOut(function(){
						$(this).html('');
						$(this).append("<p class='icon-info'><b>"+label+"</b></p><p>"+info+"</p>").fadeIn();								
					});	
				}
				
				if(idle){
					$(constant).fadeOut(function(){ trigger(); });
				} else {
					trigger();
				}
			},
			
			hide: function(){
				idle=true;
				$(tip).fadeOut();
				var timer = function(){	if(idle) constant.fadeIn(); }
				setTimeout(timer, 500);
			},
			
			buildContent: function(){
				constant = $("<div class='jforminfo-constant'></div>");
				tip = $("<div class='jforminfo-tip'></div>");
				$(content).wrapInner(constant).append(tip);
				constant = tip.prev();
			}
		});

		//Initialize
		this.init();

		return this;
	}
	
	//Auto-run 
  $(function(){ $('#jforminfo').jforminfo('#jforminfo-content'); }); 
})(jQuery);


;(function($){					 
	$.fn.jcrop = function(settings) {
		var ex = this;

		//Extend
		$.extend(this, {
			init: function(){
				this.each(function(){
					ex.template();
					this.open=false;
				});	
			},
			
			template: function(){
				//build
				var anchor = $("<a class='jcrop-expand'><span>"+options.msgExpand+"</span></a>");
				$(this)
					.height(options.height)
					.append("<div class='fader'></div>")
					.after(anchor);
					
				//bind link
				anchor.bind('click', ex.toggle);
				anchor[0].crop=this;
			},
			
			toggle: function(){
				var open = this.crop.open;
				var linkClass = open ? 'jcrop-expand' : 'jcrop-retract';
				var msg = open ? options.msgExpand : options.msgRetract;
				
				var callback = function(){
					crop = $(this).prev();
					if(open) crop.addClass('jcrop').height(options.height);	else crop.removeClass('jcrop').removeAttr('style');
					$('span', this).html(msg);
					$(this)
						.fadeIn()
						.attr('class', linkClass);
				}
				$(this).fadeOut(callback);
				this.crop.open = !open;
			}
		});

		//Initialize
		var options = $.extend({}, $.fn.jcrop.defaults, settings);
		this.init();

		return this;
	}
	
	$.fn.jcrop.defaults = {
		height:300,	
		msgExpand:'<strong>Read more ...</strong>',
		msgRetract:'<strong>Retract the text?</strong>'
	}
	
	//Auto-run 
  $(function(){ $('.jcrop').jcrop(); }); 
})(jQuery);


;(function($){
	var img_path = '/img/layout/loader.gif';
					 
	$.fn.loading = function(action) {
		var document = $(document);
		var loader = $('#jloader');
		var refresh = function(){	loader.css({"top": pageYoffset, "left": pageXoffset}); }
		
		document[action ? 'bind' : 'unbind']('mousemove.refresh', refresh).trigger('mousemove.refresh');
		loader[action ? 'fadeIn' : 'fadeOut']();
	}
	
	$.fn.loading.init = function() {
		//creating the icon in dom
		$('body').append('<img id="jloader" style="display:none;position:absolute;opacity:0.3;filter:alpha(opacity=30);" src="'+img_path+'" alt="" />');
		
		$(document).bind('mousemove', function(e){
			pageYoffset = (e.pageY + 20);
			pageXoffset = (e.pageX + 20);
		});
	}
	
	//Auto-run 
  $(function(){ $.fn.loading.init(); }); 
})(jQuery);