var windowsList	= new Array();
var windowsL	= new Array();
var myWindow 	= Class.create();
var mySliders   = new Array();

myWindow.prototype = {
	initialize: function(name, container, left, top, width, height, scrolling, content, printable) {
		this.container 		= container;
		this.name			= name
		this.left			= left;
		this.top			= top;
		this.width			= width;
		this.height			= height;
		this.content		= content;
		
		if ($("window-"+this.name) != undefined)
		{
			alert('This window name already exist');
			return false;
		}
		
		this.id					= "window-"+this.name;
		windowsList[this.id] 	= this;
		windowsL.push (this);

		var printMe		= (printable) ? '<div class="ssNormal" id="print"><b><a href="javascript:window.print();">'+txtPrint+'</a></b></div>' : '';

		var content		= (scrolling) ? '<div id="'+this.id+'_content" style="width:'+(this.width-57)+'px; height:'+(this.height-47)+'px;" class="win_content_div">\n\
											<div class="win_content_divS" style="width:'+(this.width-57)+'px;" id="'+this.id+'_contentS">'+printMe+'\n\
												'+this.content+'\n\
											</div>\n\
										</div>\n\
										<div class="win_contant_data">\n\
											<div id="'+this.id+'_slideDiv">\n\
												<div id="'+this.id+'_slideTrack" style="width:7px;background-color:black;background:url(img/bar.gif);height:'+(this.height-57)+'px;">\n\
													<div id="'+this.id+'_slideHandle" style="width:7px;height:11px;background:url(img/slider.gif);cursor:move;"></div>\n\
												</div>\n\
											</div>\n\
											<!--<a href="javascript:windowsList[\''+this.id+'\'].scrollDown();"><img src="img/up.png" alt="" /></a><br/><a href="javascript:windowsList[\''+this.id+'\'].scrollUp();"><img src="img/down.png" alt=""/></a>--> \n\
										</div>\n' : '<div id="'+this.id+'_content" style="width:'+(this.width-57)+'px; height:'+(this.height-47)+'px;" class="win_content_div">\n\
											<div class="win_content_divS" id="'+this.id+'_contentS">\n\
												'+this.content+'\n\
											</div>\n\
										</div>\n';
										
		var win			= '<div class="win" id="'+this.id+'" style="left:'+this.left+'px; top:'+this.top+'px;">\n\
							<table width="'+this.width+'px" height="'+this.height+'px" class="win_tbl" border="0px">\n\
								<tr>\n\
									<td colspan="2" rowspan="2" class="win_content" onmousemove="maskBackground();">\n\
										'+content+'\
									</td>\n\
									<td class="win_r_0" onmouseover="restorBackground();" onclick="hideWindows();">\n\
									</td>\n\
								</tr>\n\
								<tr>\n\
									<td class="win_r_1" height="'+(this.height-37)+'px" onmouseover="restorBackground();" onclick="hideWindows();">\n\
									</td>\n\
								</tr>\n\
								<tr>\n\
									<td  class="win_b_0" onmouseover="restorBackground();" onclick="hideWindows();">\n\
									</td>\n\
									<td  class="win_b_1" width="'+(this.width-48)+'" onmouseover="restorBackground();" onclick="hideWindows();">\n\
									</td>\n\
									<td  class="win_b_2" onmouseover="restorBackground();" onclick="hideWindows();">\n\
									</td>\n\
								</tr>\n\
							</table></div>';
		new Insertion.Bottom(container, win);
		
		var d = this.id
		if (scrolling) mySliders[this.id] = new Control.Slider(this.id+'_slideHandle',this.id+'_slideTrack', {axis:'vertical',minimum:0,maximum:100,onSlide:function(v){
			var dimensions = ($(d+'_contentS').getHeight()  - $(d+'_content').getHeight());
			if (-((v * dimensions)) <= 0)
				$(d+'_contentS').style.top = -((v * dimensions))+"px";
		}/*, onChange:function(v){
			alert(v);
			var dimensions = ($(d+'_contentS').getHeight()  - $(d+'_content').getHeight());
			if (-((v * dimensions)) <= 0)
				alert('a');
				$(d+'_contentS').style.top = -((v * dimensions))+"px";
			
		}*/}); 
		
		if (printable) $('printArea').innerHTML = this.content;
		if (scrolling)
		{
			this.evt_scrollUp	= this.wheel.bindAsEventListener(this);
			var n = navigator.userAgent.toLowerCase();
			if (n.indexOf("firefox")!= -1 )
			{
				Event.observe(this.id+'_contentS', "DOMMouseScroll", this.evt_scrollUp);
			}
			else
			{
				$(this.id+'_contentS').onmousewheel = this.evt_scrollUp;
			}
		}
	},
	
	destroy: function()
	{
		windowsList[this.id] 	= null;
		$(this.container).removeChild($(this.id));
	},
	
	scrollUp: function()
	{
		if (stripPx($(this.id+'_contentS').style.top) > -($(this.id+'_contentS').getHeight()  - $(this.id+'_content').getHeight()))
			$(this.id+'_contentS').style.top = (stripPx($(this.id+'_contentS').style.top)-20)+"px";
			h = ($(this.id+'_contentS').getHeight()  - $(this.id+'_content').getHeight());
			p = stripPx($(this.id+'_contentS').style.top);
			mySliders[this.id].setValue(-p/h);
	},
	
	scrollDown: function()
	{
		if (stripPx($(this.id+'_contentS').style.top) < 0)
			$(this.id+'_contentS').style.top = (parseInt(stripPx($(this.id+'_contentS').style.top))+20)+"px";
			mySliders[this.id].setValue(-(stripPx($(this.id+'_contentS').style.top) *100)/$(this.id+'_contentS').getHeight());
			h = ($(this.id+'_contentS').getHeight()  - $(this.id+'_content').getHeight());
			p = stripPx($(this.id+'_contentS').style.top);
			mySliders[this.id].setValue(-p/h);
	},
	
	wheel: function(event)
	{
		if (!event)
			event = window.event;
			
		if (event.wheelDelta)
		{
			delta = event.wheelDelta/120;
			if (window.opera)
				delta = -delta;
		} 
		else if (event.detail)
		{
			delta = -event.detail/3;
		}
		
		if (delta > 0)
		{
			this.scrollDown();
		}
		else
		{
			this.scrollUp();
		}

		if (event.preventDefault)
			event.preventDefault();
			
		event.returnValue = false;
	}
};

function hideWindows()
{
	windowsShown 				= false;
	$('content').style.cursor	= 'auto';
	$('newsletter').className 	= 'a';
	$('contact').className		= 'a';
	document.body.style.background 	= 'white';
	$('content').style.background 	= 'white';
	for (i = windowsL.length-1; i>-1; i--)
	{
		windowsL[i].destroy();
		windowsL.splice(i,1);
	}
}

function showWindowSet(set)
{
	maskBackground();
	windowsShown = true;
	for (i = 0; i< set.length; i++)
	{
		//texte (type C)
		if (set[i].type == 1)
		{
			var opt = {
			method: 'get',
			onSuccess: function(t) {
				var content 	= t.responseText;
				var num			= content.split('###')[0];

				if (set[num].onlytext)
				{
					content = content.split('###')[2];
				}
				else
				{
					//complex parsing
					title1 = (content.split('###')[7] == '1') ? content.split('###')[5]+' - '+content.split('###')[6] : content.split('###')[1];
					title2 = content.split('###')[3];
					head = content.split('###')[4];
					content = '<p class="ssNormal" style="margin-top:0px;"><b>'+title1+'</b></p><h1>'+title2+'</h1><h2>'+head+'</h2><div class="ssHr"></div><div class="ssTxt">'+content.split('###')[2]+'</div>';
				}

				new myWindow( set[num].name, 'windows', set[num].left, set[num].top, set[num].width, set[num].height, set[num].scrollable, content, set[num].printable)	
				}
			};
			new Ajax.Request(set[i].content+"&num="+i, opt);
		}
		
		//list (type D)
		if (set[i].type == 2)
		{
			var opt = {
			method: 'get',
			onSuccess: function(t) {
				var content 	= t.responseText;
				var num			= content.split('###')[0];

				if (set[num].onlytext)
				{
					content = content.split('###')[1];
				}
				else
				{
					//complex parsing
				}

				new myWindow( set[num].name, 'windows', set[num].left, set[num].top, set[num].width, set[num].height, set[num].scrollable, content, set[num].printable)	
				}
			};
			new Ajax.Request(set[i].content+"&num="+i, opt);
		}
		
		//list (type B)
		if (set[i].type == 3)
		{
			var opt = {
			method: 'get',
			onSuccess: function(t) {
				var content 	= t.responseText;
				
				var num			= content.split('###')[0];
			
				if (set[num].onlytext)
				{
					cont	= content.split('###');
					if (cont[1] == 'img')
					{
						first	= cont[2].split('---');
						if(cont[2] != '')
						{
							var final  = (first[0] == '') ? '' : first[0];
							final      = (first[1] == '') ? final : final+', <i>'+first[1]+'</i>';
							final      = (first[2] == '') ? final : final+', '+first[2];
							if (first[3].search(/swf/) != -1) {
								content = '<table width="365px" cellpadding="0xp" cellspacing="0px" border="0px"><tr><td valign="top" style="height:443px; width:365px"><div id="gdesc">'+final+'</div><div id="gvid"></div>'+addFlash(first[3], 365)+'</td></tr><tr><td>';								
							} else {
								content = '<table width="365px" cellpadding="0xp" cellspacing="0px" border="0px"><tr><td valign="top" style="height:443px; width:365px"><div id="gdesc">'+final+'</div><div id="gvid"></div><img id="gpic" src="'+first[3]+'" alt="" /></td></tr><tr><td>';
							}
							for (j = 2; j<cont.length-1; j++)
							{
								im = cont[j].split('---');
								if (first[3].search(/swf/) == -1) {
									content += '<a href="javascript:switchGal(\''+im[0]+'\',\''+im[1]+'\',\''+im[2]+'\',\''+im[3]+'\');"><img src="'+im[4]+'" alt="" /></a>&nbsp;';
								}
							}
							content += '</td></td></table>';
						}
						else
						{
							content = '';
						}
					}
					else
					{
						content = cont[1];
					}
				}
				else
				{
					//complex parsing
					title1 = (content.split('###')[7] == '1') ? content.split('###')[5]+' - '+content.split('###')[6] : content.split('###')[1];
					title2 = content.split('###')[3];
					head = content.split('###')[4];
					content = '<p class="ssNormal" style="margin-top:0px;"><b>'+title1+'</b></p><h1>'+title2+'</h1><h2>'+head+'</h2><div class="ssHr"></div><div class="ssTxt">'+content.split('###')[2]+'</div>';
				}

				new myWindow( set[num].name, 'windows', set[num].left, set[num].top, set[num].width, set[num].height, set[num].scrollable, content, set[num].printable)	
				}
			};
			new Ajax.Request(set[i].content+"&num="+i, opt);
		}
	}
}
