Event.observe(window, 'load', splash_init);

function splash_init(event){
		//Stop listen load Event
	Event.stop(event);
	Event.stopObserving(window, 'load', splash_init);

		//Make event listener on each element whit class splash
	elements = $$('.splash');
	elements.each(function(element){
		MSIE6 ? element.observe('click', splash_splashIt_IE6) : element.observe('click', splash_splashIt);
	});
}

function splash_splashIt(event){
		//Stop event and cancell action
	Event.stop(event);

	//Display background
	over.setStyle({'display':'block'});
	over.observe('click', splash_hide);
	over.update('');
	over.insert({'top':new Element('div')});
	over.childElements()[0].insert({'bottom':new Element('p', {'style':'background-color:#eeeeee;'})});
	over.childElements()[0].childElements()[0].update('<a><img id="close_over" src="' + GFX + 'graphic/shared/bt_close_splash.png" alt="Fermer" /></a>');
	over.childElements()[0].childElements()[0].setStyle({'textAlign':'right'});
	over.childElements()[0].childElements()[0].childElements()[0].setStyle({'cursor':'pointer'});
	over.childElements()[0].insert({'bottom':new Element('div', {'id':'popup'})});
	body.insert({'bottom':over});

	if(event.findElement('a').classNames().toArray().join(' ').substring(7) == 'splash_v'){
			//Local var init
		var prodIdRegExp = new RegExp('.*?-([0-9]+)([0-9\-]+)?(\.html)?$');
		var videoRegExp = new RegExp('video/(.*?)_.*$');

			//Fill popup
		var regExp = videoRegExp.exec(event.findElement('a').getAttribute('href'));

		$('popup').update('<div style="background-color:white;"><object width="480" height="365"><param name="movie" value="http://www.dailymotion.com/swf/' + regExp[1] + '&related=0&autoPlay=1" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><embed src="http://www.dailymotion.com/swf/' + regExp[1] + '&related=0&autoPlay=1" type="application/x-shockwave-flash" width="480" height="365" allowfullscreen="true" allowscriptaccess="always"></object><br /></div>');
		$('popup').setStyle({'width':'480px'});

			//Get other vids
		prodIdRegExp = prodIdRegExp.exec(document.location.pathname);
		new Ajax.Request('http://' + window.location.host + '/product_models/ajax_getVideo/', {
			'parameters':{
				'id':prodIdRegExp[1],
				'selected':prodIdRegExp[2]
			},
			'onComplete':function(e){
				var response = e.responseJSON;

				if(response.length > 1){
					var div = $('popup').childElements()[0];
					var ul = new Element('ul', {'style':'list-style-type:none; list-style-position:inside;'});
					var liste = new Element('span',{'style':'background:url(http://gfx.glisshop.com/graphic/moncompte/btPassPerduLogin.png) no-repeat; height:30px; padding-left:30px; padding-top:3px; font-size:12px;'});
					liste.insert({'bottom':'Liste des vidéos pour ce produit&nbsp;:'});
					div.insert({'bottom':liste});
					response.each(function(video){
						var aNew = new Element('a', {'href':video.url, 'style':'text-decoration:none; color:#1d99f3; font-weight:bold;'});
						var liNew = new Element('li',{'style':'margin-left:30px; margin-top:5px;'});
						
						aNew.update(video.title);
						aNew.observe('click', function(ev){
							Event.stop(ev);
							var regExp = videoRegExp.exec(ev.target.href);
							div.childElements()[0].remove();
							div.insert({'top':'<object width="480" height="365"><param name="movie" value="http://www.dailymotion.com/swf/' + regExp[1] + '&related=0&autoPlay=1" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><embed src="http://www.dailymotion.com/swf/' + regExp[1] + '&related=0&autoPlay=1" type="application/x-shockwave-flash" width="480" height="365" allowfullscreen="true" allowscriptaccess="always"></object>'})
						});
						liNew.insert({'top':aNew})
						ul.insert({'bottom':liNew});
					});
					div.insert({'bottom':ul});
				}
			}
		});
	}else{
			//Local var init
		var regExp = new RegExp('id=([0-9]+).*type=(.*)');

			//Fill popup
		regExp = regExp.exec(event.findElement('a').getAttribute('href'));
		switch(regExp[2]){
			case 'shoes':
				swfobject.embedSWF(SITE+'services/360/shoes.swf', 'popup', '550', '400', '9.0.000', null, {'idProduit':regExp[1], 'urlGFX':GFX});
				break;
			case 'boots':
				swfobject.embedSWF(SITE+'services/360/boots.swf', 'popup', '446', '560', '9.0.000', null, {'idProduit':regExp[1], 'urlGFX':GFX});
				break;
			default:
				swfobject.embedSWF(SITE+'services/360/generique.swf', 'popup', '446', '530', '9.0.000', null, {'idProduit':regExp[1], 'urlGFX':GFX});
				break;
		}
	}
	over.childElements()[0].setStyle({'width':$('popup').getWidth() + 'px'});

	Event.observe(window, 'scroll', splash_resize);
	Event.observe(window, 'resize', splash_resize);
	setTimeout(function(){splash_resize(null);}, 100);
}

function splash_splashIt_IE6(event){
		//Stop event and cancell action
	Event.stop(event);

	if(event.findElement('a').classNames().toArray().join(' ').substring(7) == 'splash_v'){
			//Local var init
		var regExp = new RegExp('video/(.*?)_.*$');

		regExp = regExp.exec(event.findElement('a').getAttribute('href'));
		window.open(SITE + 'services/video/?vuid=' + regExp[1], null, 'toolbar=no, location=no, directories=no, menubar=no, scrollbars=no, resizable=no, width=480, height=365');
	}else{
			//Local var init
		var regExp = new RegExp('.*type=(.*)');
	
		regExp = regExp.exec(event.findElement('a').getAttribute('href'));
		switch(regExp[1]){
			case 'shoes':
				window.open(event.findElement('a').getAttribute('href'), null, 'toolbar=no, location=no, directories=no, menubar=no, scrollbars=no, resizable=no, width=550, height=400');
				break;
			case 'boots':
				window.open(event.findElement('a').getAttribute('href'), null, 'toolbar=no, location=no, directories=no, menubar=no, scrollbars=no, resizable=no, width=446, height=560');
				break;
			default:
				window.open(event.findElement('a').getAttribute('href'), null, 'toolbar=no, location=no, directories=no, menubar=no, scrollbars=no, resizable=no, width=446, height=530');
				break;
		}
	}
}

function splash_hide(event){
		//Stop event
	//Event.stop(event);

	if(event.element().getAttribute('id') == 'over' || event.element().getAttribute('id') == 'close_over'){
		over.stopObserving('click');
		Event.stopObserving(window, 'scroll', splash_resize);
		Event.stopObserving(window, 'click', splash_resize);
		over.remove();
	}
}

function splash_resize(event){
	var popup = over.childElements()[0];

	popup.setStyle({'marginTop':parseInt((document.viewport.getHeight() - popup.getHeight()) / 2 + document.viewport.getScrollOffsets().top) + 'px', 'marginLeft':parseInt((document.viewport.getWidth() - popup.getWidth()) / 2 + document.viewport.getScrollOffsets().left) + 'px'});
}