 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('Merci de bien vouloir renseigner tous les éléments encadrés en rouge');
              scrollTo(0,0);
              return false;
          }
   return true;
}
var isIE6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1 && navigator.appVersion.indexOf("MSIE 7.0") == -1 && navigator.appVersion.indexOf("MSIE 8.0") == -1);
var isIE7 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") == -1 && navigator.appVersion.indexOf("MSIE 7.0") != -1 && navigator.appVersion.indexOf("MSIE 8.0") == -1);                       	
var FirstCallSmsError = 'f';
var XML_ERROR_OBLI = '';
//--Create By Ratanak
	$.getSmsErrorObliXML = function(params) {
		var language = $("html").attr('xml:lang');
		var text = '';		
			$(XML_ERROR_OBLI).find('tu').each(function(){				
				if(params == $(this).attr('tuid')){
					$(this).find('tuv').each(function(){
						if(language == $(this).attr('xml:lang').toLowerCase()){		 						
						 	return text = $(this).find('seg').text().trim();
						}
					});
				}
			});
		return text;
	};                     	
$(document).ready(function() {
	
    $(this)
        .find('form')
            .submit(function() { // validation des champs obligatoires
                var nb_error = 0;
               	var iam = false;
               	var num = 0;
               	var alertObli='';
                $(this)
                    .find('.oblig')
                        .each(function() {
                        	if(rep=='out'){
	                        	if(FirstCallSmsError=='f'){
	                        		 //--Create by RATANAK--//
									 var html_tem = $.ajax({
									 url: "../_ajax/constant.php",
									 async: false
									}).responseText;
									$.ajax({
									       type: "GET",
										url: html_tem + "smsError.xml",
										dataType: "xml",
										async: false,
										success: function(x) {
											XML_ERROR_OBLI = x ;
										}
									});
									FirstCallSmsError='t';
	                        	}
	                        	alertObli = $.getSmsErrorObliXML('oblig');
	                        }else {
	                        	alertObli = 'Merci de bien vouloir renseigner tous les éléments encadrés en rouges';
	                        }
                        	var data = $(this).val();
                        	if($(this).attr('id') == 'chca'){
                        		if(data.trim() == ''){	                       	
									//if(!$(this).is(':checked')) { // champ vide
						               $("#wrap_category").addClass('error_category');
					                   $("#wrap_category").click(function() { $(this).removeClass('error_category'); });   	                   	                    	                                   
				                       nb_error++;        
						            //}
						        }
                        	}
                        	if(data.trim() == 0) { // champ vide
	              
				                  $(this)
				                      .addClass('error')
				                      .focus(function() { $(this).removeClass('error'); });   
				                  nb_error++;                  
				            }
                        	if($(this).attr('id') == 'hidden_id_city'){                         		                  	
                        		if(data.trim() == ''){
                        			$("#id_city_1")
                        			.addClass('error')
                                 	.focus(function() { $(this).removeClass('error'); });  
                               		nb_error++ ;
                                	
                        		}else if(data.trim() == 'other'){
                        			if($("#other_city").val() == ''){
                        				$("#other_city")
	                        			.addClass('error')
	                                 	.focus(function() { $(this).removeClass('error'); });   
	                                	nb_error++;
                        			}
                        		}
                        	}
                        	if($(this).attr('id') == 'hidden_id_category'){
                        		if(data.trim() == ''){
	                         		 $("#id_category_list")
		                                 .addClass('error')
		                                 .focus(function() { $(this).removeClass('error'); });   
		                                nb_error++;
	                             }
                            }
                        	if($(this).attr('id') == 'price_rental'){
                        		if(parseFloat(data) <= 0 || data.trim()==''){
	                         		 $(this)
	                                 .addClass('error')
	                                 .focus(function() { $(this).removeClass('error'); });   
	                                nb_error++;
	                             }
                            }
                            if($(this).attr('id') == 'price_sell'){
                        		if(parseFloat(data) <= 0 || data.trim()==''){
	                         		 $(this)
	                                 .addClass('error')
	                                 .focus(function() { $(this).removeClass('error'); });   
	                                nb_error++;
	                             }
                            }
                             
                            if(data==null || $.trim(data)=="") { // champ vide
                                $(this)
                                    .addClass('error')
                                    .focus(function() { $(this).removeClass('error'); });   
                                nb_error++;
                            }
                            
                        }).end();

				
                if(nb_error != 0) {
                    alert(alertObli);
                    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':'none'});
    
    
    $('.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)); 
	});    
    
    
});


/**********************************************************************************
 * 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);
}
//=======Change button========//
function changeButton(){
	$(":button,:submit").each(function(){
			var pr = $(this).parent();
			var nr = $(this).next();
			//if(($(pr).attr("class").match(/btn_top_list/))||($(pr).attr("class").match(/active/))){}
			if(($(pr).attr('class').match(/btn_left/)) && ($(nr).attr('class').match(/btn_right/))){}
			else{
				$(this).wrap("<span id='btn_left'></span>");				
				$(this).after("<span id='btn_right'></span>");				
				$(this).addClass("btn_repeat");
			}
		});
		$(":button,:submit").mouseover(function(){
			var pr = $(this).parent();
			var nr = $(this).next();
				$(pr).addClass("btn_left_over");
				$(nr).addClass("btn_right_over");
				$(this).addClass("btn_center_over");
		});
		$(":button,:submit").mouseout(function(){
				$('.btn_left').removeClass("btn_left_over");
				$('.btn_right').removeClass("btn_right_over");
				$(this).removeClass("btn_center_over");
		});
}
