String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
		
jQuery.fn.goCollapse = function(zone, visible) {
    if(visible) {
        $(this).prepend('<span class="collapse_moins">');
    }
    else {
        $(this).prepend('<span class="collapse_plus">');
        zone.hide();
    }
    
    $(this)
        .find('span')
            .click(function() {
                $(this).toggleClass('collapse_moins').toggleClass('collapse_plus');
                zone.toggle();
            });
};		

jQuery.fn.goEditable = function(settings) {
    settings = jQuery.extend({
        text: 'Cliquez ici pour ajouter du contenu'
    }, settings);
    
    if($(this).val() != '') {
        var val = $(this).val();
        $(this)
            .css('border', '1px solid white')
            .mouseover(function() { $(this).css({ border: '1px solid silver', background: '#edfebe' }); })
            .mouseout(function() { $(this).css({ border: '1px solid white', background: 'white' }); })
            .click(function() { $(this).css({ border: '1px solid silver', background: 'white' }).unbind(); });
    }
    else {
        var html = $(this).parent().html();
        $(this).parent().html('<a href="#">'+settings.text+'</a>').find('a').click(function() { $(this).parent().html(html); });
    }
};

function checkOblig(){	
	var nb_error = 0;
	$('.oblig')
		.each(function() {
          	var data = $(this).val();
          
              if(data.trim() == '') { // champ vide
              
                  $(this)
                      .addClass('error')
                      .focus(function() { $(this).removeClass('error'); });   
                  nb_error++;                  
              }
          });
    if(nb_error != 0) {
              alert('Please kindly fill in the red field.');
              scrollTo(0,0);
              return false;
          }
   return true;
}
$(document).ready(function() {
    $(this)
        .find('form')
            .submit(function() { // validation des champs obligatoires
                var nb_error = 0;
                $(this)
                    .find('.oblig')
                        .each(function() {
                        	var data = $(this).val();
                        
                            if(data.trim() == '') { // champ vide
                            
                                $(this)
                                    .addClass('error')
                                    .focus(function() { $(this).removeClass('error'); });   
                                nb_error++;
                            }
                        })
                    .end();
                if(nb_error != 0) {
                    alert('Please kindly fill in the red field.');
                    scrollTo(0,0);
                    return false;
                }
                return true;
            })
        .end();
    //for noborder class style
    
    $('form input.noborder').focus(function(){
	  return false;
	}).mousedown(function(){
	  return false;
	}) ;
     //set width for .flexible_list_title
    var flexible_list_title_width = 0;    
    $.each($('.flexible_list_title'), function() {
    	if($(this).width()>flexible_list_title_width)
    		flexible_list_title_width = $(this).width();
    });
    $('.flexible_list_title').width(flexible_list_title_width);
    $('#jsddm').css({'width':$('#jsddm').width()+'px','float':'left'});
    //fix menu top, display block in ie6
    /*if($.browser.msie && $.browser.version<7){
		jQuery.each($('#jsddm > li > ul'), function(i, val) {
		        $(this).css({'width':$(this).width()});
		});
    }*/
    $('.video_thumbnail').mouseover(function() {
    	$('> i',this).addClass('hover');
    }).mouseout(function(){
    	$('.video_thumbnail > i').removeClass('hover');
    });
	jQuery.each($('.video_thumbnail > i'), function(i, val) {//alert($(this).prev().height()+'w'+$(this).height()+'w'+$(this).prev().width()+'w'+$(this).width());
	    var item_top = ($(this).prev().height()/2)-($(this).height()/2);
		var item_left = ($(this).prev().width()/2)-($(this).width()/2);
		$(this).css({'top':item_top,'left':item_left,'display':'block'});
		pngfix_($(this)); 
	});
	var img_nav_width = $(".img_nav > img").width()+30;
	if(img_nav_width>0){
		$('.img_nav > .img_nav_page').css({'width':img_nav_width+'px'});
	}
    //alert($('#language_link_child').width());
});


/**********************************************************************************
 * To remove all accent characters, replace space by hiphen and return small letter  
 **********************************************************************************/
function removeAccents(str) {
	
	str = str.toLowerCase();

	str = str.replace(/[\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5]/g,'a');
	str = str.replace(/[\u00E7]/g,'c');
	str = str.replace(/[\u00E8\u00E9\u00EA\u00EB]/g,'e');
	str = str.replace(/[\u00EC\u00ED\u00EE\u00EF]/g,'i');
	str = str.replace(/[\u00F2\u00F3\u00F4\u00F5\u00F6\u00F8]/g,'o');
	str = str.replace(/[\u00F9\u00FA\u00FB\u00FC]/g,'u');
	str = str.replace(/[\u00FD\u00FF]/g,'y');
	str = str.replace(/[\u00F1]/g,'n');
	str = str.replace(/[\u0153]/g,'oe');
	str = str.replace(/[\u00E6]/g,'ae');
	str = str.replace(/[\u00DF]/g,'ss');

	str = str.replace(/[^a-z0-9_\s\'\:\/\[\]-]/g,'');
	str = str.replace(/[\s\'\:\/\[\]-]+/g,' ');
	str = str.replace(/[ ]/g,'-');

	return str;
	
}

function minString(obj,val){
	//obj.length;	
	if($(obj).val().length < val) {
		return $(obj).attr('alt')+' require at least '+(val)+' characters';
	}else{return true;}
	
}

function getOffsetLeft(object){
	offset = $(object).offset(); 
	if($.browser.msie && $.browser.version<8){
		//alert(offset.left/2)+10;
		return (offset.left);
	}
	return(offset.left);	
}
function autoPairClass(){//parent_class_name,tag_child
	parent_class_name = arguments[1]?arguments[1]:'auto_pair';
	tag_child = arguments[2]?arguments[2]:'tr';
	jQuery.each($("."+parent_class_name+" > "+tag_child), function(i, val) {
		$(this).attr('class',(i%2?'impair':'pair'));
    });
}
pngfix_=function(els){
	if($.browser.msie && $.browser.version>6 || !$.browser.msie)return;
	var ip=/\.png/i;
	var i=$(els).size();
	if(i==1){
		var el=els;
		//var es=el.style;
		var es=$(el).attr('style');
		if($(el).attr('src')&&($(el).attr('src')).match(ip)&&!$(el).css('filter')){			
			//es.height=el.height;
			$(el).attr('height',$(el).height());
			//es.width=el.width;
			$(el).attr('width',$(el).width());
			//es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";			
			$(el).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')");
			//el.src=clear;
			$(el).attr('src',clear);
		}else{
			//var elb=el.currentStyle.backgroundImage;
			var elb=$(el).css('background-image');
			if(elb.match(ip)){
				var path=elb.split('"');
				var original_width = $(el).width();
				//var rep=(el.currentStyle.backgroundRepeat=='no-repeat')?'crop':'scale';
				var rep=($(el).css('background-repeat')=='no-repeat')?'crop':'scale';
				//es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";
				$(el).css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')");
				//alert(el.clientHeight+"=="+$(el).height()+'cssheight:'+$(el).css('height'));
				$(el).css('height',$(el).height());
				//alert($(el).width()+'=='+$(els).width()+'=='+original_width);
				$(el).css('width',original_width);
				$(el).css('background-image','none');
				//es.backgroundImage='none';
				//$('#mh_jouer').text('=>'+es.ClassName+'<=');					
			
			}
		}
	}
}

//**************************************//
function htmlspecialchars (string, quote_style, charset, double_encode) {
    // http://kevin.vanzonneveld.net
    // +   original by: Mirek Slugen
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Nathan
    // +   bugfixed by: Arno
    // +    revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +      input by: Ratheous
    // +      input by: Mailfaker (http://www.weedem.fr/)
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +      input by: felix
    // +    bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: charset argument not supported
    // *     example 1: htmlspecialchars("<a href='test'>Test</a>", 'ENT_QUOTES');
    // *     returns 1: '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;'
    // *     example 2: htmlspecialchars("ab\"c'd", ['ENT_NOQUOTES', 'ENT_QUOTES']);
    // *     returns 2: 'ab"c&#039;d'
    // *     example 3: htmlspecialchars("my "&entity;" is still here", null, null, false);
    // *     returns 3: 'my &quot;&entity;&quot; is still here'

    var optTemp = 0, i = 0, noquotes= false;
    if (typeof quote_style === 'undefined' || quote_style === null) {
        quote_style = 2;
    }
    string = string.toString();
    if (double_encode !== false) { // Put this first to avoid double-encoding
        string = string.replace(/&/g, '&amp;');
    }
    string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;');

    var OPTS = {
        'ENT_NOQUOTES': 0,
        'ENT_HTML_QUOTE_SINGLE' : 1,
        'ENT_HTML_QUOTE_DOUBLE' : 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE' : 4
    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') { // Allow for a single string or an array of string flags
        quote_style = [].concat(quote_style);
        for (i=0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;
            }
            else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }
        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/'/g, '&#039;');
    }
    if (!noquotes) {
        string = string.replace(/"/g, '&quot;');
    }

    return string;
}
function ajaxLoading(val){
	if(val==0)$('.ajax_loading').css({'display':'none'});
	else{
		var ajax_loading_top = (myWindow(2)/2)-($('.ajax_loading').height()/2);
	    var ajax_loading_left = (myWindow(1)/2)-($('.ajax_loading').width()/2);
	    $('.ajax_loading').css({'left':ajax_loading_left,'display':'block'});
	}
}
//pa = 1 mean width
function myWindow(pa) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return (pa=1?myWidth:myHeight);
}
