/*---Global variables ---*/
var wndHeight = 0;	//window height
var mainTxtHeight=0;// main text section height

// display image set size to fit un screen size
displayImgResize = function() {
	var img = $('#displayImg');
	var winH = $(window).height() * 0.9;
	var winW = $(window).width() * 0.9;
	var imgH = img.attr('realH');
	var imgW = img.attr('realW');
	var ratio = 1.0;
	if ( ( imgH > winH || imgW > winW ) && imgH && imgW )
		ratio = (winH / imgH < winW / imgW  ? winH / imgH : winW / imgW );
	
	if ( ratio < 1.0 ) {
		imgH *= ratio;
		imgW *= ratio;
	}
	
	var top = Math.round(($(window).height() - imgH) / 2);
	var left= Math.round(($(window).width () - imgW) / 2);
	
	img.height(imgH);
	img.width(imgW);
	img.css('top', top);
	img.css('left', left);

	$('#expandImg').css('top',  top + 5);
	$('#expandImg').css('left', left+ 5);
};

// resize main test section when resizing the browser
$(window).resize(function()
{
	if ( wndHeight > 0 && mainTxtHeight > 0 ) 
	{
		mainTxtHeight += $(window).height() - wndHeight;
		wndHeight = $(window).height();
		$('div#main_text').height(mainTxtHeight);
		
		if (document.getElementById('displayImg') != null ) {
			displayImgResize();
		}
	}
	
	winW = $(window).width();
	if ( winW < 640 )
	{
		if ( window.outerHeight ) 
		{
			outerH = window.outerHeight;
			window.resizeTo(640, outerH);
		}
		else
		{
			window.resizeTo(640, wndHeight);
			var deltaW = 640 - $(window).width();
			var deltaH = wndHeight - $(window).height();
			window.resizeTo(640 + deltaW, wndHeight + deltaH);
		}		
	}
});
		
viewClose = function() {
	var elt = $('#view');
	
	elt.hide();
	elt.children('#loadingImg').show();
	$('#expandImg').hide();
	
	var child = elt.children();
	var elt;
	for ( var i=0; i < child.length; i++ ) {
		elt = $(child[i]);
		if ( elt.attr('id') != 'loadingImg' && elt.attr('id') != 'expandImg' ) {
			elt.remove();
		}
	}
};

var previewImages = [];

// On document ready function
jQuery(document).ready(function() {

	//resise browser and initialize height variables
	wndHeight = $(window).height();
	
	/*-- main text height initialization --*/
	mainTxtHeight = wndHeight - 160;//$('#main_text').height() - ($(document).height() - wndHeight) - 1;
	$('div#main_text').height(mainTxtHeight);
	
	/*-- menu initialization --*/	
//	$('.hor-menu > ul > li > ul').hide();
	
	$('.hor-menu > ul > li').hover(function() {
     	$(this).children('ul').show();
     	
     	// align sub menu centered with menu name
     	if( $(this).children('a').length > 0 ) {
	     	pos = $(this).children('a').offset();
	     	$(this).children('ul').css('left', pos.left + $(this).children('a').width()/2 - $(this).children('ul').width()/2);
	    }
    	
   	},function(){
     	$(this).children('ul').hide();
    });
	
	// prepares the thumbnails effects
	$('.hoverBoxImg').each( 
		function(ind) 
		{
			var img = new Image;
			img.src = $(this).attr('src').replace('sq_thumbnails/','thumbnails/');
			
			previewImages[ind] = img;
			$(this).attr('id', ind+1);

			
			var par = $(this).parent().get(0);		
			if ( $(par).attr('onclick') || $(par).attr('href') )
				$(this).click(function() { $(par).click(); return false; });		// PB----- click n'est pas unbinde pour les preview precedemment survolees....
			else
				$(this).click(function() { showPicture($(this)); });
		})
					 .hover(
		function(){
			var preview = $('#preview');
			var img = preview.children('img');
	
			ind = parseInt($(this).attr('id')) - 1;
	
			var i = previewImages[ind];
			img.attr('src', i.src);
			img.attr('alt', $(this).attr('alt'));
			img.attr('title', $(this).attr('title'));
			
	     	pos = $(this).offset();
			
			var dw = ( i.width > 0 ? $(this).width() - i.width : 20 );
			preview.css('left', Math.round(pos.left + dw/2 ));
			dw = ( i.height > 0 ? i.height - $(this).height() : 10 );
			preview.css('top',  Math.round(pos.top - dw/2));
			
			img.attr('id', parseInt($(this).attr('id')));
			
			preview.show();
		},
		function(){
			$('#preview').hide();
		}
	 );
	 
	 $('#preview').hover(function(){
		$(this).show();
		},
		function(){
			$(this).hide().children('img.clickBoxImg').removeAttr('onclick').removeAttr('alt').removeAttr('title').attr('src', '');
		  return;
		}
	 )
		
	$('.clickBoxImg').click(function(){
		
		var i = $(this).attr('id');
		
		showPicture( $('img#'+i));
/*		
		var par = $('img[id='+i+']').parent('a');
		if ( par.length > 0 )
		{
			$(par).click();
		
//		if ( $(this).attr('onclick') )
			return;
		}
			
		var view = $('#view');
		view.show();
		
		var img = new Image;
		img.src = $(this).attr('src').replace('thumbnails/','');
		img.onload = function() {			
			var winH = $(window).height() * 0.9;
			var winW = $(window).width() * 0.9;
			var imgH = img.height;
			var imgW = img.width;
			var ratio = 1.0;
			if ( ( imgH > winH || imgW > winW ) && imgH && imgW )
				ratio = (winH / imgH < winW / imgW  ? winH / imgH : winW / imgW );
			
			if ( ratio < 1.0 ) {
				img.height = imgH * ratio;
				img.width  = imgW * ratio;
			}
			
			img.top = ($(window).height() - img.height) / 2;
			img.left= ($(window).width () - img.width) / 2;
			
			var imgE = document.createElement('img');
			var imgElt = $(imgE);
	
			imgElt.attr('src', img.src);
			imgElt.height(img.height);
			imgElt.width(img.width);
			imgElt.css('top', img.top);
			imgElt.css('left', img.left);
			imgElt.css('position', 'absolute');
			
			imgElt.attr('realW', imgW);
			imgElt.attr('realH', imgH);
			
			imgElt.attr('id', 'displayImg');
			
			imgElt.attr('class', 'framed');
			view.append(imgElt);

			if ( ratio < 1.0 ) {
				$('#expandImg').css('top',  img.top + 5);
				$('#expandImg').css('left', img.left+ 5);
				$('#expandImg').show();
								
				imgElt.click(function() {
					$(this).hide();
					$('#realSize').show();
					$('#expandImg').hide();
					return false;
				});
				$('#expandImg').click(function() {
					$(this).hide();
					$('#realSize').show();
					$('#displayImg').hide();
					return false;
				});
				
				imgE = document.createElement('img');
				imgElt = $(imgE);
				imgElt.attr('src', img.src);
				imgElt.css('display', 'none');
				imgElt.attr('id', 'realSize');
				imgElt.attr('class', 'framed');
				imgElt.click(function() {
					$(this).hide();
					$('#displayImg').show();
					$('#expandImg').show();
					return false;
				});
				view.append(imgElt);
			}
			
			view.children('#loadingImg').hide();
		};*/
			
	} )/**/
	
	$('.view').click(function(){
		if ( $(this).children('#donotclose').length > 0 )
			return;
		
		viewClose();
	});

	$('#seeComments').click(
	 function(){
		$('#comments').slideToggle('normal');
		return false;
	 }
	);
	$('#comments').hide();
		
	$('input').each( function( ind) { $(this).addClass($(this).attr('type')); });
	
	initModal();
});

showPicture  = function( im ) 
{
		if ( $(im).attr('onclick') )
			return;
			
		var view = $('#view');
		view.show();
		
		var img = new Image;
		img.src = $(im).attr('src').replace('sq_thumbnails/','');
		img.onload = function() {			
			var winH = $(window).height() * 0.9;
			var winW = $(window).width() * 0.9;
			var imgH = img.height;
			var imgW = img.width;
			var ratio = 1.0;
			if ( ( imgH > winH || imgW > winW ) && imgH && imgW )
				ratio = (winH / imgH < winW / imgW  ? winH / imgH : winW / imgW );
			
			if ( ratio < 1.0 ) {
				img.height = imgH * ratio;
				img.width  = imgW * ratio;
			}
			
			img.top = ($(window).height() - img.height) / 2;
			img.left= ($(window).width () - img.width) / 2;
			
			var imgE = document.createElement('img');
			var imgElt = $(imgE);
	
			imgElt.attr('src', img.src);
			imgElt.height(img.height);
			imgElt.width(img.width);
			imgElt.css('top', img.top);
			imgElt.css('left', img.left);
			imgElt.css('position', 'absolute');
			
			imgElt.attr('realW', imgW);
			imgElt.attr('realH', imgH);
			
			imgElt.attr('id', 'displayImg');
			
			imgElt.attr('class', 'framed');
			view.append(imgElt);

			if ( ratio < 1.0 ) {
				$('#expandImg').css('top',  img.top + 5);
				$('#expandImg').css('left', img.left+ 5);
				$('#expandImg').show();
								
				imgElt.click(function() {
					$(this).hide();
					$('#realSize').show();
					$('#expandImg').hide();
					return false;
				});
				$('#expandImg').click(function() {
					$(this).hide();
					$('#realSize').show();
					$('#displayImg').hide();
					return false;
				});
				
				imgE = document.createElement('img');
				imgElt = $(imgE);
				imgElt.attr('src', img.src);
				imgElt.css('display', 'none');
				imgElt.attr('id', 'realSize');
				imgElt.attr('class', 'framed');
				imgElt.click(function() {
					$(this).hide();
					$('#displayImg').show();
					$('#expandImg').show();
					return false;
				});
				view.append(imgElt);
			}
			
			view.children('#loadingImg').hide();
		};
			
};

showTemporary = function( id ) {
	var vw = $('#view');
	
/*	var elt= $(id);
	var pos= elt.offset();
	
	vw.css('left', pos.left);
	vw.css('top',  pos.top );
	vw.height(elt.height());
	vw.width (elt.width ());
*/	
	vw.show();
};

showTemp = function( divId ) 
{
	$('#overlay').show();
	$('#loading_img').show();
    document.body.style.cursor = "wait";    
};

hideTemp = function() 
{
    $('#loading_img').hide();
    document.body.style.cursor = "default";    
  	if($('#dialog-box').is(':hidden'))
		$('#overlay').hide();
};


jQuery(window).keydown(function(event)
{
   	if($('#dialog-box').is(':hidden'))
     	return;

	rc = true;
    switch (event.keyCode)
    {
	case 13: $('#modalOK').click(); rc=false; break;
	case 27: $('#modalCancel').click(); rc=false; break;
	case 46: if ( $('#delete').length > 0 ) { $('#delete').click(); rc = false; } break;
  	}
  	return rc;
});


function initModal()
{
	$('#modalOK').click( function(event)
		{
			event.preventDefault(); 
			$.ajax( { url:        $('#modaledit form').attr('action'), type: "post", datatype: "html", 
					   beforeSend: function()	 { hideModal(); showTemp(); }, 
					   success:    function(data){ 
					   					$('#modaledit').html(data); 
					   					if ( $('.form-error').length > 0 )
					   						startModal();
					   			   }, 
					   complete:   function()    { hideTemp(); }, 
					   data:       $('#modaledit form').serialize(),
					   error:      function(response){alert(response);}}); 
			$(this).click(function(){return false;});
		}
	);
	
	$('#modalCancel').click( function(event)
		{
			hideModal();
		}
	);
	initLinks();
}

function initLinks()
{
	$("a.tomodal:not(.handled)").click( makeModal ).addClass('handled');
}

function makeModal()
{
	var dlgTitle = $(this).attr('title'); 
	if ( dlgTitle == null || dlgTitle == ''  )
		dlgTitle = $(this).text();
		
	$.ajax( { url: $(this).attr("href"), 
			   type: "post", datatype: "html", 
			   beforeSend: function()    { showTemp(); }, 
			   success:    function(data){ $('#modaledit').html(data); }, 
			   complete:   function()    { $('#dialog-title').html(dlgTitle); hideTemp(); startModal(); }
			  });
	return false;
}

function startModal()
{
   	if($('#overlay').not(':hidden'))
	   	$('#overlay').show();
   	if($('#dialog-box').not(':hidden'))
   	{
   		$('#modaledit > form > table').css('border', '0').css('background', 'none');

		$('#modaledit').attr('style', '');
	   	showModal();
	   	
	   	if ( $('#modaledit form').length )
	   		$('#modalOK').show();
	   	else
	   		$('#modalOK').hide();
		
		if ( $('#modaledit').height() > mainTxtHeight )
			$('#modaledit').height(mainTxtHeight).css('overflow', 'auto');
	}
}

function showModal(_elem)
{
    if(typeof(_elem) == 'undefined')
        _elem = '#dialog-box';
        
    my_width  = $(window).width();
    my_height = $(window).height();

    var posx = ( my_width  - $(_elem).width()  ) / 2;
    if ( posx < 0 )
    	posx = 0;
    var posy = ( my_height - $(_elem).height() ) / 2;    
    if ( posy < 0 )
    	posy = 0;   
	$(_elem).css('top', posy+'px')
			 .css('left', posx+'px')
			 .css('position', 'fixed')
			 .fadeIn();
}

function hideModal()
{
    $('#overlay').hide();	
    $('#dialog-box').hide();
  	$('#modaledit').html('');	
	return false;
}

function refreshImg() 
{
	$('#refreshLink').hide(); $('#refreshWait').show();
	var src = $("#captcha").attr('src');
	$("#captcha").attr('src', src + '/?' + (new Date()).getTime());
	$("#UserVerif").attr('value', '');
	$("#UserVerif").focus();
	$('#refreshLink').show(); $('#refreshWait').hide();
};
