//____init functions_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
function init_css3(v_elements) {

	if (typeof(v_elements)=="undefined") v_elements = ".css3, .btn, .shadowed, .rounded, .input-btn, .input-txt, .input-txtl";

	if (window.PIE) {
		$(v_elements).each(function() {
			PIE.attach(this);
		});
	}
}

// #### NAVIGATION MENU
function init_menu(v_menu, v_hover_class) {

	if (typeof(v_menu)=="undefined") v_menu = ".hmenu";
	if (typeof(v_menu)=="v_hover_class") v_hover_class = "hovered";

	if ($(v_menu).length<=0) return false;

	$(v_menu+" li").hover(function() {
		$(this).removeClass(v_hover_class).addClass(v_hover_class);
	},function() {
		$(this).removeClass(v_hover_class);
	});

	// menu auto-centering
	/*if ($(v_menu_id).attr("class").indexOf("centered")>=0) {
		var menu_width = $("#"+v_menu_id).width();
		$("#"+v_menu_id).css("left","50%");
		$("#"+v_menu_id).css("margin-left",parseInt(-(menu_width/2))+"px");
	}*/
}

// #### TEXTBOX BLUR/FOCUS
function init_textbox_focus(v_textbox_class) {

	if (typeof(v_textbox_class)=="undefined") v_textbox_class = "input-txt,input-txtl";

	var array_elements = v_textbox_class.split(",");

	for(var i=0; i<=array_elements.length-1; i++) {

		v_textbox_class_ok = array_elements[i];

		$("input[type=text]."+v_textbox_class_ok+", input[type=password]."+v_textbox_class_ok+", textarea."+v_textbox_class_ok).focus(function() {
			$(this).addClass(v_textbox_class_ok+"-focused");
		});
		$("input[type=text]."+v_textbox_class_ok+", input[type=password]."+v_textbox_class_ok+", textarea."+v_textbox_class_ok).blur(function() {
			$(this).removeClass(v_textbox_class_ok+"-focused");
		});
	}
}

function init_textbox_focusable(v_textbox_class) {

	if (typeof(v_textbox_class)=="undefined") v_textbox_class = "focusable";

	$("input[type=text]."+v_textbox_class).focus(function() {
		if ($(this).attr("title").length>0)
			if ($(this).val() == $(this).attr("title"))
				$(this).val("");
	});
	$("input[type=text]."+v_textbox_class).blur(function() {
		if ($(this).attr("title").length>0)
			if ($(this).val() == "")
				$(this).val($(this).attr("title"));
	});
}

// #### TOOLTIPS ####
function init_tooltips() {

	$('a[title]').tooltip({ 'fade':250, 'showURL':false });
	$('label[title]').tooltip({ 'fade':250, 'showURL':false });
	$('span[title]').tooltip({ 'fade':250, 'showURL':false });
	$('img[title]').tooltip({ 'fade':250, 'showURL':false });
	$('div[title]').tooltip({ 'fade':250, 'showURL':false });
}

// #### PICTURE LAZYLOADS ####
function init_lazyloads() {

	// #### pictures lazyload fadein effects
	$("img").each(function() {
		if (typeof($(this).attr("class"))!="undefined") {
			if ($(this).attr("class").indexOf("none")<0)
				$(this).lazyload({
					effect      : "fadeIn"
				});
		}
	});
}

//___miscs functions__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
function navigator_is_ie() {
	if (navigator.appName == 'Microsoft Internet Explorer')
		return true;
	else
		return false
}
function trim(myString) {
	return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}
function doc_id(v_element) {
	return document.getElementById(v_element);
}
function isNumeric(v_string, v_min) {

	try {

		if (typeof(v_string) == "undefined") return false;
		v_string = v_string.toString();

		if (v_string.length<=0) return false;

		var ValidChars = "-0123456789.";
		var IsNumber=true;
		var Char;

		for (i=0; i<v_string.length && IsNumber == true; i++) {
			Char = v_string.charAt(i);
			if (ValidChars.indexOf(Char) == -1) IsNumber = false;
		}

		if (IsNumber) {
			if (typeof(v_min)!="undefined") {
				if (parseFloat(v_string)<v_min) return false;
			}
		}

		return IsNumber;

	}catch(err) {

		catch_error("isNumeric",err);
	}
}
function isEmail(String){

	try {

		emailRegExp = /^[_a-z0-9A-Z-]+(\.[_a-z0-9A-Z-]+)*@[a-z0-9A-Z-]+(\.[a-z0-9A-Z-]+)*(\.([a-zA-Z]){2,4})$/
		return emailRegExp.test(String);

	}catch(err) {

		catch_error("isEmail",err);
	}
}

//___tab functions__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
function init_tab(tabs_control) {

	try {

		if (typeof(tabs_control) == "string")
			tabs_control_id = tabs_control;
		else {
			if (!tabs_control.id) return false;
			tabs_control_id = tabs_control.id;
		}

		v_tabs_height = $("#"+tabs_control_id+" .tabs").height();
		v_height = $("#"+tabs_control_id).height();
		v_final_height = parseInt(v_height) - parseInt(v_tabs_height)-5;
		if (v_final_height>0)
			$("#"+tabs_control_id+" .containers").css("height",v_final_height+"px");

	}catch(err) { }
}
function set_tab(v_tab_button, v_tab_index, flag_resize) {

	//try {

		if (typeof(v_tab_button)=="undefined") return false;
		if (typeof(v_tab_index)=="undefined") v_tab_index = "tab_index";
		if (typeof(flag_resize)=="undefined") flag_resize = true;

		var v_prefix_tab = "tab_";
		var v_prefix_container = "container_";

		var v_id = v_tab_button.id;
		if (!(v_id.substr(0, v_prefix_tab.length) == v_prefix_tab)) return false;

		var v_index = v_id.substr(v_prefix_tab.length, v_id.length-v_prefix_tab.length); // retrieve the tab index
		if (!isNumeric(v_index)) return false;

		var v_container_id = v_prefix_container+v_index;

		var v_parent1 = $(v_tab_button).parent();
		var v_parent2 = $(v_parent1).parent();
		var v_tabcontrol_id = $(v_parent2).attr("id"); 					// retrieve the tabcontrol container

		$("#"+v_tabcontrol_id +" .tabs .tab").each(function() {

			var v_parent_dummy1 = $(this).parent();
			var v_parent_dummy2 = $(v_parent_dummy1).parent();
			if ($(v_parent_dummy2).attr("id") == v_tabcontrol_id) {
				$(this).removeClass().addClass("tab");
			}

		});

		$(v_tab_button).addClass("selected");

		$("#"+v_tabcontrol_id +" .containers .container").each(function() {

			var v_parent_dummy1 = $(this).parent();

			if (v_parent_dummy1) {

				var v_parent_dummy2 = $(v_parent_dummy1).parent();

				if (v_parent_dummy2) {

					if ($(v_parent_dummy2).attr("id") == v_tabcontrol_id) {

						if ($(this).attr("id") == v_container_id) {
							$(this).fadeIn("fast");
						}else {
							$(this).removeClass().addClass("container");
							$(this).hide();
						}
					}
				}
			}
		});

		if ($("#"+v_tab_index)) $("#"+v_tab_index).val(v_index);

		if (flag_resize)
			init_tab(v_tab_button.offsetParent.offsetParent);

	//}catch(err){ catch_error("set_tab",err); }
}

//____GOOGLE MAP FUNCTIONS__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
function set_google_map(v_title, v_content, v_address, v_lat_dummy, v_lng_dummy, zoom, div_container, zoom_max, icon, flag_show_window) {

	try {

		var zoom_default = 6;
		var zoom_max_default = 8;
		var div_container_default = "google_map_container";
		var icon_default = "http://www.samnaka.fr/prod/google_map_icons/default.png";

		if (typeof(div_container)=="undefined") div_container = div_container_default;
		if (typeof(zoom)=="undefined") zoom = zoom_default;
		if (typeof(zoom_max)=="undefined") zoom_max = zoom_max_default;
		if (typeof(icon)=="undefined") icon = icon_default;
		if (typeof(flag_show_window)=="undefined") flag_show_window = true;

		if (!doc_id(div_container)) return false;

		if ((typeof(v_lat_dummy)=="undefined")||(typeof(v_lng_dummy)=="undefined")) {

			//alert(v_address);
			// address resolution needed
			var geocoder = new google.maps.Geocoder();

			geocoder.geocode(
				{ address: v_address },

				function(results, status) {

					if (status == google.maps.GeocoderStatus.OK && results.length) {
						v_lat = results[0].geometry.location.lat();
						v_lng = results[0].geometry.location.lng()
						display_contact_google_map(v_lat, v_lng, zoom, zoom_max, div_container, v_title, v_content, icon);
					}
				}
			);

		}else {
			// directly display the map
			display_contact_google_map(v_lat_dummy, v_lng_dummy, zoom, zoom_max, div_container, v_title, v_content, icon, flag_show_window);
		}

	}catch(err) {

		catch_error("set contact google map",err);
	}
}
function display_contact_google_map(v_lat, v_lng, zoom, zoom_max, div_container, v_title, v_content, icon, flag_show_window) {

	try {

		// initialize the google map
		var myOptions = {
					  mapTypeControl: false,
			zoom: parseInt(zoom),
			center: new google.maps.LatLng(v_lat, v_lng),
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		var map = new google.maps.Map(doc_id(div_container), myOptions);			// create the map

		// adding the company marker
		var v_marker = new google.maps.Marker({
			position: new google.maps.LatLng(v_lat, v_lng),
			title:v_title,
			map:map,
			icon: icon
		});

		if (flag_show_window) {

			infowindow = new google.maps.InfoWindow({
				content: v_content
			});
			infowindow.open(map ,v_marker);
		}

		google.maps.event.addListener(v_marker, 'click', function() {

			infowindow = new google.maps.InfoWindow({
				content: v_content
			});
			infowindow.open(map ,v_marker);

			map.setCenter(v_marker.position);
			map.setZoom(parseInt(zoom_max));
		});


	}catch(err) {

		catch_error("display contact google map",err);
	}
}

function set_lang(v_lang, v_path) {

	if (typeof(v_path)=="undefined") v_path = "";

	v_return_code = ajax(v_path+"kernel/ajax/lang.php?lang=" +escape(v_lang));
	if (v_return_code == "0")
		window.location.reload(true);
	else
		alert("Une erreur s'est produite :\r\n" +v_return_code);
}

/* Catch an error incoming from a try catch */
function catch_error(v_function, v_err) {
	alert("An error occured in function [" +v_function +"]\r\n" +v_err.message);
}

/* Add an event listener to the specified object, and map it with the specified function */
function addEventListener2(obj,event,fct){

	try {

		if (obj == null) return false;

	     if(obj.attachEvent)
	        obj.attachEvent('on' + event,fct);
	     else
	        obj.addEventListener(event,fct,true);
	}catch(err) {
	}
}

function ajax(v_file) {

	try {

		if(window.XMLHttpRequest) // FIREFOX
		  	xhr_object = new XMLHttpRequest();
		else if(window.ActiveXObject) // IE
		  	xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		else
			return(false);

		xhr_object.open("GET", v_file, false);
		xhr_object.send(null);
		if(xhr_object.readyState == 4) return(xhr_object.responseText);
		else return(false);

	}catch(err) {

		catch_error("ajax",err);
	}
}

function fill_div(container_id, url, v_function) {

	$.ajax({
		url:url,
		cache:false,
		success:function(response) {
			$(container_id).html(response);
			if (typeof(v_function)!="undefined") v_function();
		},
		error:function(response) {
			alert("Erreur : \r\n"+response);
		}
	});
}


function jstring(v_string){

	try {

		if (typeof(v_string) == "undefined") {
			return "undefined";

		} else if (v_string.length<=0){
			return "";

		} else {

			var v_return_string;

			v_return_string = v_string.toString();

			v_return_string = v_return_string.replace(/'/g,"");
			v_return_string = v_return_string.replace(/!/g,"");

			v_return_string = v_return_string.replace(/é/g,"&eacute;");
			v_return_string = v_return_string.replace(/è/g,"&eagrave;");
			v_return_string = v_return_string.replace(/ê/g,"e");

			v_return_string = v_return_string.replace(/à/g,"a");
			v_return_string = v_return_string.replace(/â/g,"a");

			return v_return_string;
		}

	}catch(err) {

		catch_error("jstring",err);
	}
}

//________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
function get_mouse_position(ev){

	if(ev.pageX || ev.pageY){
	      return {x:ev.pageX, y:ev.pageY};
	}
	return {
	      x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
	      y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}


String.prototype.extractTags=function(tag) {

    var matchAll = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'img');
    var matchOne = new RegExp('(?:<'+tag+'.*?>)((\n|\r|.)*?)(?:<\/'+tag+'>)', 'im');
    return myMap( (this.match(matchAll) || []), function(scriptTag) {   return (scriptTag.match(matchOne) || ['', ''])[1]; });
}
function myEach(myElement, delegate, ownpropertiesonly) {

	try {

		if (typeof(delegate)=="function") {

			if (myElement instanceof Array && typeof(ownpropertiesonly)=="undefined") {
				ownpropertiesonly=true;
			}
			for (key in myElement) {
				var ok = (!ownpropertiesonly);
				if (!ok) {
					try {
					    ok=myElement.hasOwnProperty(key)
					} catch (ex) {}
				}
				if (ok) {
					try { delegate(myElement[key], key, myElement) } catch(e) {
					    // ...
					}
				}
			}
		}

		return false;
	}catch(err) { }

}
function myMap(myElement, iterator) {

	var results = [];
	myEach(myElement,function(value, index) { results.push(iterator(value, index)); });
	return results;
}

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}




