/** 
 * @projectDescription main script file
 *
 * @author 	MEDIAGONG Integration Team E.D.
 * @version 	1.5 
 */

function showGame(url){
	Popup(url,900,500);
	return false;
}

function showPopup(url){
	Popup(url,760,630);
	return false;
}

function Popup(url,w,h){
	var left = Math.max(0, (screen.width - w) / 2);
	var top = Math.max(0, (screen.height - h) / 2);
	window.open(url,'popup_lvqr','top='+top+',left='+left+',toolbar=no,status=no,scrollbars=no,resizable=no,menubar=no,location=no,directories=no,width='+w+', height='+h+'');
}

function trace(obj){
	if (!jQuery.browser.msie) {
		console.log(obj);
	}else{
		//alert(obj);
	}
}

/**
 * @classDescription LVQR main Javascript Class
 */
var LVQR = {	
	/**
	 *  settlement
	 * @method
	 */
	intro: function(){
		if (!jQuery.browser.msie) {
		}
	},
	/**
	 * Initialization of LVQR class
	 * All calls of other methods go in there
	 * @method
	 */
	init: function(){
		var Manage = {
			Init: function(){
				//if (jQuery('body').hasClass('index') || jQuery('body').hasClass('espace_parents')) {
					LVQR.verticalCenter();
					jQuery(window).resize(function(){
						LVQR.verticalCenter();
					});
				//}
				//LVQR.scrollPane('.scroll-pane');
				// scrollPane for fiche perso
				if (jQuery('body').hasClass('fiche')) {
					LVQR.scrollPane('.fiche_perso ul li.history p.scroll-pane');
				}				
				// scrollPane for mentions_legales
				if (jQuery('body').hasClass('mentions_legales')) {
					LVQR.scrollPane('#global_content div.scroll-pane');
				}
				// scrollPane for reglement
				if (jQuery('body').hasClass('reglement')) {
					LVQR.scrollPane('#global_content div.scroll-pane');
				}				
				
				/* campain e-StatTracking */
				LVQR.eStatCampaign();
				
				if (jQuery('body').hasClass('mentions_legales')) {
					if (!document.referrer) return;
					var myReferrer 		= document.referrer;
					var text 			= myReferrer.toString() ;
					
					//appel de la page via index.html
					var stringToFind 	= "index.html";
					var regExp 			= new RegExp( stringToFind, "gi") ;
					if (text.search(regExp) == -1){
						LVQR.eStatClic('Homepage_Footer_Clic_InfosLegales-Site_Parents');
					}else{
						//appel de la page via espace-parents.html
						stringToFind 	= "espace-parents.html";
						regExp 			= new RegExp( stringToFind, "gi") ;
						if (text.search(regExp) == -1) {
							LVQR.eStatClic('Parents_Footer_Clic_InfosLegales-Site_Parents');
						}
					}
				}
				
				/* open external url http://... in a new window */ 
				//jQuery('a.external').live("click", function(event){
				jQuery('a').bind("click", function(event){					
					// eStatClic
					if(jQuery(this).hasClass('eStatClic')){
						event.preventDefault();
						LVQR.eStatClic(jQuery(this).attr('rel'));
						jQuery(this).queue(function(){
					      	if (jQuery(this).hasClass('external')) {
								window.open(jQuery(this).attr('href'));
							}
							else 
								if (jQuery(this).hasClass('popup')) {
								//do nothing
								}
								else {
									if (jQuery(this).attr('href') !== undefined){
										window.location = jQuery(this).attr('href');
									} else {
										return false;
									}
									
								}
					       jQuery(this).dequeue();
						});	
					}else{
				      	if (jQuery(this).hasClass('external')) {
							window.open(jQuery(this).attr('href'));
						}
						else 
							if (jQuery(this).hasClass('popup')) {
							//do nothing
							}
							else {
								if (jQuery(this).attr('href') !== undefined){
									window.location = jQuery(this).attr('href');
								} else {
									return false;
								}
							}
					}
					event.preventDefault();
				});	
				
				// manage bouton close to show or hide if window popup
				var locationHref 	= window.location.href;
				reg 				= new RegExp("#popup", "g");
				if (locationHref.search(reg) == -1){
					//do nothing
				}else{
					jQuery('body').addClass('popup');
				}
				// /manage bouton close to show or hide if window popup				
								
				// animate html
				var Duration = 200; //time in milliseconds

				//animate logo
				jQuery('.espace_parents #header h1 a img').attr('src','/img/content/logo-lvqr_parents.png').mouseenter(function() {
					jQuery(this).effect('shake', { distance: 2, times:1 }, Duration/2);
				})
				.mouseleave(function() {
					//
				});
				jQuery('.index #header h1 a img').attr('src','/img/content/logo-lvqr.png').mouseenter(function() {
					jQuery(this).effect('shake', { distance: 2, times:1 }, Duration/2);
				})
				.mouseleave(function() {
					//
				});
				
				jQuery('.index #header h2 img')
				.mouseenter(function() {
					jQuery('#sun a')
					.css('backgroundPosition','0px -344px')
				})
				.mouseleave(function() {
					jQuery('#sun a')
					.css('backgroundPosition','0px -172px')	
				});		
				// /animate logo
			
				// lvqr animation up effect
				if (!jQuery.browser.msie) {
					jQuery('img#lvqr')
					.css('marginTop','258px')
					.animate(
						{ 
							marginTop: "-286px"
						}
						, Duration*4
						, function(){
						    //callback
						}
					);
				}else{
					jQuery('img#lvqr')
					.css('marginTop','258px')
					.animate(
						{ 
							marginTop: "-30px"
						}
						, Duration*4
						, function(){
						    //callback
						}
					);
				}				
				// /lvqr animation up effect				
			
				// sun animation
				jQuery('.index #header, .espace_parents #header').prepend('<div id="sun"><a style="cursor:default;">&nbsp;</a></div>');
				jQuery('#sun a')
				.mouseenter(function() {
					jQuery(this)
					.css('backgroundPosition','0px -344px')
				})
				.mouseleave(function() {
					jQuery(this)
					.css('backgroundPosition','0px -172px')	
				});	
				// /sun animation
				
				// bt_play animation skake effect
				jQuery('#bt_play img')
				.attr('src','/img/content/bt_play.png')
				.mouseenter(function() {
					jQuery(this).effect('shake', { distance: 10, times:1 }, Duration);
				})
				.mouseleave(function() {
					//
				});
				// /bt_play animation skake effect					
			
				// bt_parents animation shake effect				
				jQuery('#bt_parents img')
				.attr('src','/img/content/bt_parents_space.png')
				.mouseenter(function() {
					jQuery(this).effect('shake', { distance: 5, times:1 }, Duration);
				})
				.mouseleave(function() {
					//
				});
				// /bt_parents animation shake effect				
				
			
				// open link in a new window					
				jQuery('#col2 p a').bind("click", function(event){
					window.open(jQuery(this).attr('href'));			
					event.preventDefault();					
				});				
				// /open link in a new window
				
				// bt_close link to clode window popup
				jQuery('#bt_close').bind("click", function(event){
					window.close();
					event.preventDefault();					
				});				
				
				// /bt_close link to clode window popup			
				
				// gestion formulaire inscription NL
				if (jQuery('body').hasClass('espace_parents')) {
					jQuery('form#newsletterForm input[type="text"]').focus(function(){
						jQuery('form#newsletterForm input[type="text"]').val('');
					});
					jQuery('form#newsletterForm').submit(function(event){
						event.preventDefault();
						LVQR.eStatClic(jQuery(this).attr('name'));
						jQuery(this).queue(function(){
							jQuery.ajax({
								url: 'newsletter_parents.php',
								type: 'POST',
								data: {
									email: jQuery('form#newsletterForm input[type="text"]').val()
								},
								success: function(data){
									jQuery('form#newsletterForm label:first').html(data);
								}
							});
						});
					});
				}
				// /gestion formulaire inscription NL
			},
			Action: function(obj){
			}
		}
		Manage.Init();
	},
	verticalCenter: function(){	/* verticalCenter */	
		var Manage = {
			Init: function(){								
			},
			Action: function(){
				if (jQuery(window).height() > 658) {
					//jQuery('body.index, body.espace_parents').css({
					jQuery('body.index').css({
						background: "#321B0F"
					});
					//jQuery('body.index #wrapper_page, body.espace_parents #wrapper_page').css({
					jQuery('body.index #wrapper_page').css({
						position: "absolute",
						top: "50%",
						height: "735px",
						margin: "-329px auto 0"
					});
					jQuery('body.produits #wrapper_page').css({position:"absolute",top:"50%",height:"735px",margin:"-329px auto 0"});

				}
				else {
					//jQuery('body.index, body.espace_parents').css({
					jQuery('body.index').css({
						background: "#321B0F url(/img/css/bg_body_medium.jpg) no-repeat 50% 0%"
					});
					//jQuery('body.index #wrapper_page, body.espace_parents #wrapper_page').css({
					jQuery('body.index #wrapper_page').css({
						position: "relative",
						top: "0",
						margin: "0 auto"
					});
					jQuery('body.produits #wrapper_page').css({position:"relative",top:"0",margin:"0 auto"});
				}
			}
		}
		Manage.Init();
		Manage.Action();
	},	
	scrollPane: function(obj){	/* scroll-pane */
	
		var Manage = {
			Init: function(){								
			},
			Action: function(obj){
				jQuery(obj).jScrollPane({scrollbarWidth:13, showArrows:true});
			}
		}
		Manage.Init();
		Manage.Action(obj);
	},		
	eStatClic: function(obj){	
		var Manage = {
			Init: function(){				
			},
			Action: function(obj){
				var element = obj.split("-"); 
				if(_PJS) _estatl('254054204156', '234034204142', element[0],element[1]);
				/*
				jQuery.ajax({
					url: 'http://prof.estat.com/m/web/254054204156?g=234034204142&p='+obj,
					type: 'POST',
					success: function(data){
					}
				});				
				*/
			}
		}
		Manage.Init();
		Manage.Action(obj);
	},		
	eStatCampaign: function(obj){	
		var Manage = {
			Init: function(){	
				// variables
				var myUrl = document.location.href;		
				var temp = myUrl.lastIndexOf("/");
				myUrl = myUrl.substr(temp+1);				
				Manage.Action(myUrl);							
			},
			Action: function(obj){
				var stringToFind = "le-monde-du-rire.html#";
				var regExp = new RegExp( stringToFind, "gi") ;
				if (obj.search(regExp) == -1){
					// notfound
				}else{
					// found
					// no time to optimise Regex 
					var stringToFind2 = "-campaign";
					regExp = new RegExp( stringToFind2, "gi") ;
					if (obj.search(regExp) == -1) {
					// notfound
					}
					else {
						result = obj.replace(stringToFind, '');
						LVQR.eStatClic(result);
					}
				}
			}
		}
		Manage.Init();
	},		
	someFunction: function(obj){	
		var Manage = {
			Init: function(){		
				
			},
			Action: function(obj){
			}
		}
		Manage.Init();
	}		
}   
/* launching scripts */
jQuery(document).ready(function(){
	LVQR.init();	
});

/*
** @description popup modale
** @author gduval
*/
function modalPopup(popID, popWidth) {
    if (typeof(popID) == 'undefined') popID = 'modal-popup';
	if (typeof(popWidth) == 'undefined') popWidth = 500;

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="/img/css/close_pop.png" class="btn_close" title="Fermer la fen&ecirc;tre" alt="Fermer" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 40) / 2;
    var popMargLeft = ($('#' + popID).width() + 40) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({'margin-top' : -popMargTop, 'margin-left' : -popMargLeft});
    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter': 'alpha(opacity=70)'}).fadeIn(); //Fade in the fade layer filter fixes IE Bug on fading transparencies

	// sending play to the flash player (IE FIX)
	if ($('#nonverblaster').length) { sendToNonverBlaster('play'); }

	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
		$('#fade , .popup_block').fadeOut(); //fade them both out
		$('#fade').remove();
		// sending pause to the flash player (IE FIX)
		if ($('#nonverblaster').length) { sendToNonverBlaster('pause'); }
		return false;
	});
    return false;
}

