$(document).ready(function(){
	if($.browser.msie) $("html").addClass("ie");
	if(isIOS()) $("html").addClass("ios");
	initFond();
	initPopover();
	initIndex();
	initAudio();
	
	$('#slider').nivoSlider({
		directionNav:false,
		pauseOnHover:false,
		captionOpacity: 1,
		pauseTime:4000,
		effect:'boxRain',
        boxCols: 8, // For box animations
        boxRows: 4
	});
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'dark_square',
		social_tools: false,
		show_title: false,
		deeplinking: false
	});
});

$(window).resize(function(){
	resizeFond();
});

function initIndex() {
	$("#page-index #slider_wrapper .caption").delay(1000).animate({"right":0}, 750, function(){
		//popUp();
	});
}

function isIOS() {
	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
	if (agentID == null) {
		return false;
	} else {
		return true;
	}
}

function initPopover() {
	$("a.video").each(function(){
		$(this).append("<span></span>");
	});
	
	/*$("a.video").click(function(){
		toggleVideoPopover(this);
		$(this).blur();
		return false;
	});*/
	
	$("#popover #btn_fermer").live("click", function(){
		toggleVideoPopover(this);
		$(this).blur();
		return false;
	});
}

function toggleVideoPopover(_link) {
	if($("#popover").is(":visible")) {
		toggleOverlay();
		$("#popover").remove();
	} else {
		toggleOverlay();
		var idVideo = $(_link).attr("rel");
		$("body").append("<div id=\"popover\"><p id=\"btn_fermer\"></p><div id=\"player_video\"><iframe src=\"http://player.vimeo.com/video/" + idVideo + "?title=0&amp;byline=0&amp;portrait=0&amp;autoplay=1\" width=\"560\" height=\"315\" frameborder=\"0\" webkitAllowFullScreen allowFullScreen></iframe></div></div>");
		positionPopover();
	}
}

function toggleOverlay() {
	if($("#overlay").is(":visible")) {
		$("#overlay").remove();
	} else {
		$("body").append("<div id=\"overlay\"></div>");
	}
}

function positionPopover() {
	var w = -$("#popover").outerWidth() / 2 + "px";
	var h = -$("#popover").outerHeight() / 2 + "px";
	$("#popover").css({"marginLeft" : w, "marginTop" : h});
	$("#popover").show();
}

/* ------------------------------------
	MAP
------------------------------------ */
var map;
var initialLocation = new google.maps.LatLng(46.805766,-71.217512);
var mapStyle = [{ featureType: "all", elementType: "all", stylers: [{ lightness: 0 }, { gamma: 1 }, { saturation: -100 }]}];

function initMap(markerIcon) {
	var mapOptions = {
		zoom: 16,
		panControl: false,
		zoomControl: true,
		center: initialLocation,
		mapTypeControl: false,
		scaleControl: false,
		streetViewControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	
	map = new google.maps.Map(document.getElementById("carte"), mapOptions);
	
	var styledMapOptions = {
        name: "Complexe Maurice"
    }

    var cmMapType = new google.maps.StyledMapType(mapStyle, styledMapOptions);
    map.mapTypes.set('cmmap', cmMapType);
    map.setMapTypeId('cmmap');
	
	var marker = new google.maps.Marker({
		position: initialLocation, 
		map: map,
		icon : markerIcon,
		title:"Complexe Maurice"
	});
}

/* ------------------------------------
	BACKGROUND
------------------------------------ */
function initFond() {
	//if(isIOS) $("body").css("background-image", "url(" + $("#fond img").attr("src") + ")");
	resizeFond();
	$("#fond").css("visibility", "visible");
}

function resizeFond() {
	var wWindow = $(window).width();
	var hWindow = $(window).height();
	var wInit = 1280;
	var hInit = 853;
	
	if(wWindow/hWindow > wInit/hInit){
		$("#fond img").css("width", wWindow);
		$("#fond img").css("height", (wWindow*hInit)/wInit);
	} else {
		$("#fond img").css("width", (hWindow*wInit)/hInit);
		$("#fond img").css("height", hWindow);
	}
	
	$("#fond img").css("margin-left", -($("#fond img").width()/2));
	$("#fond img").css("margin-top", -($("#fond img").height()/2));
}

/* ------------------------------------
	AUDIO
------------------------------------ */
function initAudio() {	
	var audiotag = $('<audio></audio>');
	/*if (audiotag.get(0).canPlayType && !$.browser.msie) { // if audio tag is supported
		audiotag.remove();
		$('a.audio').click(function() {
			initHtmlAudio($(this));
			return false;
		});
	} else {*/
		$('a.audio').click(function() {
			createSwfAudio($(this));
			return false;
		});
	/*}*/
	$("#player_audio a.audio:eq(0)").trigger("click");
}

/*

HTML 5 ----------------------------------------

*/
function initHtmlAudio(__el) {
	if (!__el.hasClass('load')) {
		var song = __el.attr('id');
		var audioplayer = $('#audioplayer');
	
		if (audioplayer.length) { // if the audio element exists
			if (audioplayer.hasClass(song)) { // if the audio element is set to the clicked song
				if (__el.hasClass('pause')) { // if the link is set to pause, we should just pause the song
					audioplayer.get(0).pause();
				} else { // if the link is not set to pause, we should restart the song
					audioplayer.get(0).currentTime = 0;
					audioplayer.get(0).play();
				}
			} else { // the song playing is different from what was just clicked
				audioplayer.get(0).pause();
				audioplayer.remove();
				audioplayer = createHtmlAudio(song);
			}
		} else { // if the audio element doesn't exist, always create a new one
			audioplayer = createHtmlAudio(song);
		}
	
		$('a.audio').not("#" + song).removeClass('pause').removeClass('load');
		__el.toggleClass('pause');
	}
}

function createHtmlAudio(song) {
	$("#" + song).addClass('load');
	var createNewAudio  = $('<audio autoplay controls></audio>')
		.hide()
		.attr('id','audioplayer')
		.addClass(song)
		.append('<source src="' + '/mp3/' + song + '.mp3">')
		.append('<source src="' + '/mp3/' + song + '.ogg">');
	createNewAudio.appendTo('#audioplayer_wrapper');
	$('#audioplayer').load();
	$('#audioplayer').get(0).addEventListener('play', function() {$("#" + song).removeClass('load');}, false);
	$('#audioplayer').get(0).addEventListener('ended', function() {$("#" + song).removeClass('pause');}, false);
}

/*

SWF ----------------------------------------

*/
function createSwfAudio(__el) {
	var song = __el.attr('id');
	
	var params = {
		menu:false,
		wmode:"transparent",
		allowFullScreen:true,
		allowScriptAccess:"always"
	};
	var flashvars = {
		mp3: "/mp3/" + song + ".mp3"
	};
	var attributes = {};
	swfobject.embedSWF("/swf/audioplayer.swf", song, "28", "28", "8", null, flashvars, params, attributes);
}

function popUp() {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open('/mp3/player.html', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=100,height=100');");
}
