// ________________________________________________________
// Get query string
function __getQueryString(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if(results == null){
		return "";
	}else{
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}
}

// ________________________________________________________
// Get
function __get(href, callback){
	var arr = {};
	if(href.indexOf("?") >= 0){
		href = href.replace(/&amp;/g, "&");
		var elm = href.substring(href.indexOf("?") + 1).split("&");
		for(var i = 0; i < elm.length; i++){
			var p = elm[i].split("=");
			arr[p[0]] = p[1];
		}
		href = href.substring(0, href.indexOf("?"));
	}
	
	$.get(href, arr, callback);
}

// ________________________________________________________
// Post
function __post(id, callback){
	var arr = {};
	var elm = document.forms[id].elements;
	for(var i = 0; i < elm.length; i++){
		if($(elm[i]).attr("class") == "mceEditor"){
			arr[elm[i].name] = tinyMCE.get(elm[i].name).getContent();
		}else{
			if($(elm[i]).attr("type") == "checkbox" || $(elm[i]).attr("type") == "radio"){
				if($(elm[i]).attr("checked") == true){
					arr[elm[i].name] = $(elm[i]).val();
				}
			}else{
				arr[elm[i].name] = $(elm[i]).val();
			}
		}
	}
	
	$.post($("#" + id).attr("action"), arr, callback);
};

// ________________________________________________________
// Init corners
function __initCorners(){
	$("p.btn a, input.btn, div.item").css({
		"-moz-border-radius": ".6em",
		"-webkit-border-radius": ".6em",
		"border-radius": ".6em"
	});
}

// ________________________________________________________
// Init
function __init(){
	// External link
	$("a[rel*='external']").click(function(){
		this.target = "_blank";
	});
	
	// Menu
	$("#menu li").hover(function(e){
		$(this).find("ul").slideDown(400);
	}, function(e){
		$(this).find("ul").slideUp(0);
	});
	
	// Lightbox
	if($("a.lightbox").length > 0){
		$("a.lightbox").colorbox({
			width: "810px",
			height: "748px",
			onOpen: function(){ $("object").css("visibility", "hidden"); },
			onClosed: function(){ $("object").css("visibility", "visible"); }
		});
	}
	
	// Corners
	__initCorners();
	
	// Slider
	$("#slider").easySlider({
		auto: true,
		continuous: true,
		numeric: true,
		speed: 1000,
		pause: 6000,
		prevText: "<",
		nextText: ">",
		prevId: "prevNav",
		nextId: "nextNav"
	});
	$("#slider").css("visibility", "visible");
	
	// Safari hack
	if($.browser.safari && parseInt($(window).width()) % 2 > 0){
		$("#slider").css("borderRight", "1px solid #999999");
	}
	
	// Overlay
	$("ul.gallery a").append("<span></span>");
	$("ul.gallery a").hover(function(e){
		$(this).find("span").hide();
	}, function(e){
		$(this).find("span").show();
	});
	
	// Menu
	var x = 0;
	var arr = new Array();
	$("#menu li").each(function(i){
		if(!$(this).parent().hasClass("sm2")){
			arr.push(x + parseInt($(this).width()) / 2);
			x += parseInt($(this).width());
		}
	});
	var i = $("a[rel*='1']").index($("a.current"));
	$("#menu").css("backgroundPosition", (arr[i] - 5) + "px 36px");
	$("#menu li a[rel*='1']").parent().hover(function(e){
		var i = $("a[rel*='1']").index($(this).find("a"));
		$("#menu").stop().animate({
			backgroundPosition: (arr[i] - 5) + "px 36px"
		}, 500, "easeOutCubic");
	}, function(e){
		var i = $("a[rel*='1']").index($("a.current"));
		$("#menu").stop().animate({
			backgroundPosition: (arr[i] - 5) + "px 36px"
		}, 500, "easeOutCubic");
	});
}

$(document).ready(function(){
	$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);
	$.browser.ios = /iPhone/i.test(window.navigator.userAgent) || /iPod/i.test(window.navigator.userAgent) || /iPad/i.test(window.navigator.userAgent);
	$.lang = window.location.href.indexOf("/en/") > 0 ? "en" : "fr";
	$.path = window.location.href.indexOf($.lang + "/") > 0 ? "../" : "";
	
	__init();
});
