var KEYCODE_ESC = 27;
var RETURN_URL = "";

$(document).ready(function()	{

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

	if ($.browser.msie && $.browser.version == 7)
	{
		$("#maincon").each(function(){
			var content = $(this).html();
			var style = $(this).attr('style');
			$(this).replaceWith('<table id="maincon" class="maincon" style="'+style+'"><tr><td>'+content+'</td></tr></table>');
		});
	}

	$('#bottom_menu .boutton').mouseenter(function(){
		$(this).find('a').animate({
			height: "77px",
			marginTop: "23px",
			fontSize: "18px"
		  }, 200 );
	}).mouseleave(function(){
		$(this).find('a').animate({
			height: "14px",
			marginTop: "7px",
			fontSize: "12px"
		  }, 200 );
	});

	$('#other_menu .boutton').each(function(){
		var elem = $(this).find('a');
		var lettre = elem.find('.lettre');
		var vertical_text = elem.find('.vertical_text');

		$(this).mouseenter(function(){
			lettre.hide();
			vertical_text.show();
			
			elem.animate(
				{
					height: "77px"
				},
				200
			);
		}).mouseleave(function(){
			elem.animate(
				{
					height: "14px"
				},
				200,
				function(){
					lettre.show();
					vertical_text.hide();
				}
			);
		});

	});
	
	$('#liste_images .case').each(function(){
		var voile = $(this).find('.voile');
		var artiste = $(this).dataset('artiste');
		var visible = false;
		$(this).mouseenter(function(){
			$('#projet_artiste').html(artiste);
			if(visible == false){
				voile.animate(
					{
						opacity: "1"
					},
					200
				);
				visible = true;
			}
		}).mouseleave(function(){
			$('#projet_artiste').html('');
			if(visible == true){
				voile.animate(
					{
						opacity: "0"
					},
					200
				);
				visible = false;
			}
		});
	});

	$('#projet_details').each(function(){
		var currentZoom = 0;
		var nb_image = $('#miniatures .liste_image').length;
		loadImg($('#image_num-'+currentZoom));
		$('#miniatures .liste_image').each(function(){
			var voile = $(this).find('.voile');
			var id_image = $(this).dataset('id_image');
			var voile_visible = false;
			
			$(this).click(function(){
				if(currentZoom != id_image){
					$('#image_num-'+currentZoom).hide();
					$('#mini_num-'+currentZoom).removeClass('active');
					$('#mini_num-'+id_image).addClass('active').find('.voile').attr('style','');
					voile_visible = false;
					loadImg($('#image_num-'+id_image));
					currentZoom = id_image;
				}
			})
			
			$(this).mouseenter(function(){
				if(voile_visible == false && id_image != currentZoom){
					voile.animate(
						{
							opacity: "1"
						},
						100
					);
					voile_visible = true;
				}
			}).mouseleave(function(){
				if(voile_visible == true && id_image != currentZoom){
					voile.animate(
						{
							opacity: "0"
						},
						100,
						function(){
							voile.attr('style','');
						}
					);
					voile_visible = false;
				}
			});
			
			
		});
	});
	
	$('.zoom_image').live('click',function(e){
		var img_url = $(this).dataset('original');
		var zoom_image = $(this);
		zoom_image.hide();
		$.post(baseUrl+'includes/zoom.php',{img_url:img_url}, function(data){	
			$('#zoomCon').replaceWith( data );
			initZoom(zoom_image,e);
		});
	});	

	$('.google').live('click',function(){
		window.open('http://www.dmbm.fr/googlemap/GoogleMap.html', "google", "width=500,height=600");
		event.preventDefault();
	});	
});
	

function initZoom(zoom_image,clickEvent){
	var documentWidth = getDocumentWidth();
	var documentHeight = getDocumentHeight();

	zoom = $('#zoomCon');
	positionZoom(documentWidth,documentHeight);

	initZoomEscape(zoom_image);
	
	
	if(!zoom.hasClass('loaded')){
		var loading = zoom.find('.loading');
		var imgW = loading.dataset('img_width');
		var imgH = loading.dataset('img_height');
		var style = loading.attr('style');
		var img_url = loading.dataset('img');
		var img = $('<img />');

		img.attr('src', img_url);
		img.attr('style',style);
		img.addClass('image');
		img.load(function(){
			loading.replaceWith( $(this) );
			zoom.addClass('loaded');
			zoom.dataset('width',imgW);
			zoom.dataset('height',imgH);
			zoom.attr('style','width: '+imgW+'px; height: '+imgH+'px');
			positionZoom(documentWidth,documentHeight);
			
			if(isiPhone()){
				$('#zoomCon').draggable({
					cursor: 'move'			
				});
				zoom.unbind('dblclick').unbind('click').dblclick(function(){
					hideZoom(zoom_image);
				});
			}else{
				moveZoom(clickEvent,documentWidth,documentHeight);
				zoom.unbind('dblclick').unbind('click').click(function(){
					hideZoom(zoom_image);
				});
			}
		});
	}	
}

function positionZoom(documentWidth,documentHeight){
	var zoomH = $('#zoomCon').dataset('height');
	var zoomW = $('#zoomCon').dataset('width');
	
	var diffWidth = documentWidth-zoomW;
	var diffHeight = documentHeight-zoomH;
	
	var decalX = diffWidth/2;
	var decalY = diffHeight/2;

	$('#zoomCon').css('left',decalX).css('top',decalY);
	return {'decalX' : decalX, 'decalY' : decalY};
}

function initProjetEscape(){
	$(document).unbind('keyup').keyup(function(e) {
		if (e.keyCode == KEYCODE_ESC) { if( RETURN_URL != "" ) window.location = RETURN_URL; } 
	});
}

function initZoomEscape(zoom_image){
	$(document).unbind('keyup').keyup(function(e) {
		if (e.keyCode == KEYCODE_ESC) { 
			hideZoom(zoom_image);
		} 
	});
}

function hideZoom(zoom_image){
	zoom_image.show();
	$('#zoomCon').hide().html('');
	initProjetEscape();
}

function moveZoom(clickEvent,documentWidth,documentHeight){
	var zoomPos = positionZoom(documentWidth,documentHeight);
	var defaultMouseX = clickEvent.pageX;
	var defaultMouseY = clickEvent.pageY;
	
	$(document).mousemove(function(e){
		var mouseX = e.pageX;
		var mouseY = e.pageY;
		
		var diffX = defaultMouseX-mouseX;
		var diffY = defaultMouseY-mouseY;
		
		var left = zoomPos.decalX - diffX;
		var top = zoomPos.decalY - diffY;

		$('#zoomCon').css('top',top).css('left',left);
	}); 

}
		
function loadImg(zoom){
	zoom.show();
	if(!zoom.hasClass('loaded')){
		var loading = zoom.find('.loading');
		var style = loading.attr('style');
		var img_url = loading.dataset('img');
		var img = $('<img />');

		img.attr('src', img_url);
		img.attr('style',style);
		img.load(function(){
			loading.replaceWith( $(this) );
			zoom.addClass('loaded');
		});
	}
}

function resizeContent(){
	var documentWidth = getDocumentWidth();
	var documentHeight = getDocumentHeight();
	
	var menu_w = 512;
	
	var offset_l = (documentWidth-512)/2;
	if(offset_l < 220)
		offset_l = 220;
		
	var contentW = documentWidth-offset_l;
	$('#top_menu').css('left',offset_l);
	$('#bottom_menu').css('left',offset_l);
	$('#maincon').css('marginLeft',offset_l).css('height',documentHeight);//.css('width',contentW);
	$('#contact, #liens').css('width',contentW);
	$('#invisible').css('height',documentHeight).css('width',getDocumentWidth);
	$('#right_bar').css('height',documentHeight);
}

function getDocumentWidth(){
	var documentWidth = $(document).width();
	if ( $.browser.msie ) {
		documentWidth = $(window).width();
	}
	if ($.browser.webkit) {
		// documentWidth -= 20;
	}
	return documentWidth-10;
}

function getDocumentHeight(){
	var documentHeight = $(window).height();
	if ( $.browser.msie ) {
		documentHeight = $(window).height();
	}
	if ($.browser.webkit) {
		//documentHeight -= 20;
	}
	return documentHeight;
}

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