var $nc=jQuery.noConflict();
function googleAnalytic()
{
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-22407563-10']);
	_gaq.push(['_trackPageview']);

	(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
}

function init()
{
	$nc('a[rel*=external]').live('click', function() { window.open(this.href); return false; });
	setTimeout('showfacoBookBox()', 2000);
	
	$nc('.green_btn').mousedown ( function(){ $nc(this).css('margin', '1px 0 0 0'); });
	$nc('.green_btn').mouseup	  ( function(){ $nc(this).css('margin', '0 0 1px 0'); });

	$nc("#infolettrebtn").click ( function() 
	{
		$nc('#infolettreform').hide();
		$nc.fancybox.showActivity();
						
		$nc.ajax({ type : "POST", cache : false, url : "../notif_infolettre.php", data : ({email : $nc('#infolettreemail').val()}), dataType: "html" })
		.complete ( function () { $nc.fancybox.hideActivity(); $nc('#infolettreresult').show(); });
		return false;
	});
	
	$nc(".infolettre").fancybox ({ 'scrolling' : 'no', 'titleShow' : false, 'overlayColor' : '#000', 'overlayOpacity': 0.8 });
}

function initAdmin()
{
	$nc('a[rel*=external]').live('click', function() { window.open(this.href); return false; });		
	$nc('.green_btn').mousedown ( function(){ $nc(this).css('margin', '1px 0 0 0'); });
	$nc('.green_btn').mouseup	  ( function(){ $nc(this).css('margin', '0 0 1px 0'); });
}

function initHome()
{
	$nc('a[rel*=external]').live('click', function() { window.open(this.href); return false; });
	setTimeout('showfacoBookBox()', 2000);
	
	$nc('.green_btn').mousedown ( function(){ $nc(this).css('margin', '1px 0 0 0'); });
	$nc('.green_btn').mouseup	  ( function(){ $nc(this).css('margin', '0 0 1px 0'); });

	$nc("#infolettrebtn").click ( function() 
	{
		$nc('#infolettreform').hide();
		$nc.fancybox.showActivity();
						
		$nc.ajax({ type : "POST", cache : false, url : "notif_infolettre.php", data : ({email : $nc('#infolettreemail').val()}), dataType: "html" })
		.complete ( function () { $nc.fancybox.hideActivity(); $nc('#infolettreresult').show(); });
		return false;
	});

	$nc(".infolettre").fancybox ({ 'scrolling' : 'no', 'titleShow' : false, 'overlayColor' : '#000', 'overlayOpacity': 0.8 });
}

function showfacoBookBox()
{
	$nc('#facebook').css('visibility', 'visible');
}

function menuEffects()
{
	$nc('#velos').hover(
		function()
		{
			$nc('.homeslidecontent').hide();			
			$nc('.top', this).addClass('select');
			$nc('#sub_velo').stop(true, true).fadeIn(100);
		},
		function()
		{
			$nc('.homeslidecontent').show();			
			$nc('.top', this).removeClass('select');
			$nc('#sub_velo').stop(true, true).fadeOut(100);
	});

	$nc('#techs').hover(
		function()
		{
			$nc('.homeslidecontent').hide();
			$nc('.top', this).addClass('select');			
			$nc('#sub_tech').stop(true, true).fadeIn(100);
		},
		function()
		{
			$nc('.homeslidecontent').show();
			$nc('.top', this).removeClass('select');
			$nc('#sub_tech').stop(true, true).fadeOut(100);
	});
}

function slider()
{
	$nc('#myslider').jcarousel(
	{
		wrap: 'circular',
		auto: 7,
		scroll: 1,
		easing: 'easeOutExpo',
		buttonPrevHTML: '<div><a><\/a><\/div>',
		buttonNextHTML: '<div><a><\/a><\/div>'
	});
}

function homeSlider()
{
	$nc('#myslider').jcarousel(
	{
		wrap: 'circular',
		scroll: 1,
		easing: 'easeOutExpo',
		buttonPrevHTML: '<div><a><\/a><\/div>',
		buttonNextHTML: '<div><a><\/a><\/div>'
	});
}

// Home //
function homeCycle()
{
	$nc('#homecycle').cycle(
	{
		//cleartype: false,
		cleartypeNoBg:true,
		fx:     'fade',
		timeout: 6000,
		speed:  1000,
		pager:  '#homepagenav',
		before: beforeHomeCycle  // transition callback (scope set to element to be shown):     function(currSlideElement, nextSlideElement, options, forwardFlag
	});
}

function beforeHomeCycle(currSlideElement, nextSlideElement, options, forwardFlag)
{
	$nc('#homeprogress div').stop(true, true).css('width', '0px').animate({width:'890px'}, 6000, 'linear');

	if ( currSlideElement.id != 'img1')
		$nc('#myslider').jcarousel('next');

	$nc('#slide' + currSlideElement.id).fadeOut('fast');
	$nc('#slide' + nextSlideElement.id).fadeIn('fast');
}

function initFAQ()
{
	$nc('.faqbtn').click( function(){
		$nc('#' + $nc(this).attr('id') + '_content').slideToggle('slow', 'easeInOutExpo');
		return false;
	});
}

function initSupport()
{
	// Date Dropdown
	$nc('#dpreg').mousedown 	( function(){ $nc(this).css('background-position', '9px 7px'); });
	$nc('#dpreg').mouseup		( function(){ $nc(this).css('background-position', '8px 6px'); });
	$nc('#dpreg').click	 	( function(){ $nc('#dpregdata').slideToggle(200, 'easeOutExpo'); });
	$nc('.dpregitem').click 	( function()
	{
		region = $nc(this).attr('id').substring(4);

		$nc('#dpregselect').html ($nc(this).html());
		$nc('#dpregdata').slideToggle(200, 'easeOutExpo');
	});
	
	$nc('#regsearch').click ( function()
	{
		location = 'detaillant.php?region=' + region;
		return false;
	});
	
	$nc('#regsearch_en').click ( function()
	{
		location = 'retailer.php?region=' + region;
		return false;
	});
}

// Velos //
function initBykes()
{
	$nc('.opensec').click ( function()
	{
		if ( $nc('#sectionplan').css('display') != 'none' )
		{ 
			$nc('#sectionplan').slideToggle('500', 'easeOutExpo');			
			$nc('#bykeback').fadeIn('fast');
		}
		
		$nc('#content_' + $nc(this).attr('id')).slideToggle('500', 'easeOutExpo');
		return false;
	});

	$nc('.plan').click ( function()
	{
		if ( $nc('#content_sec1').css('display') != 'none' ) { $nc('#content_sec1').slideToggle('500', 'easeOutExpo'); }
		
		if ( showplan )
		{
			showplan = false;
			$nc('#sectionplan').slideToggle('500', 'easeOutExpo');
			$nc('#bykeback').fadeIn('fast');
		}
		else
		{
			showplan = true;
			$nc('#sectionplan').slideToggle('500', 'easeOutExpo');
			$nc('#bykeback').fadeOut('fast');
		}
		
		return false;
	});

	$nc('#plancursor').Draggable(
		{
			zIndex: 	1000,
			ghosting:	false,
			containment : 'parent',
			onDrag : changePlan
		}
	);

	changePlan(56,40);
	
	// Modèle Dropdown
	$nc('#dpmodel').mousedown 	( function(){ $nc(this).css('background-position', '9px 7px'); });
	$nc('#dpmodel').mouseup		( function(){ $nc(this).css('background-position', '8px 6px'); });
	$nc('#dpmodel').click	 		( function(){ $nc('#dpmodeldata').slideToggle(200, 'easeOutExpo'); });
}

function changePlan( x, y )
{
	ratiox = (x/114) ;
	ratioy = (y/80) ;
	posx = parseInt(planWidth*ratiox);
	posy = parseInt(planHeight*ratioy);
	$nc('#sectionplan').css('background-position', '-' + posx + 'px ' + '-' + posy + 'px');
}

// Velos - Serie //
function initVelos()
{
	// Date Dropdown
	$nc('#dpreg').mousedown 	( function(){ $nc(this).css('background-position', '9px 7px'); });
	$nc('#dpreg').mouseup		( function(){ $nc(this).css('background-position', '8px 6px'); });
	$nc('#dpreg').click	 	( function(){ $nc('#dpregdata').slideToggle(200, 'easeOutExpo'); });
	$nc('.dpregitem').click 	( function()
	{
		region = $nc(this).attr('id').substring(4);

		$nc('#dpregselect').html ($nc(this).html());
		$nc('#dpregdata').slideToggle(200, 'easeOutExpo');
	});
	
	$nc('#regsearch').click ( function()
	{
		location = 'detaillant.php?region=' +  region;		
		return false;
	});
	
	$nc('#regsearch_en').click ( function()
	{
		location = 'retailer.php?region=' +  region;		
		return false;
	});
}

function serieEffects()
{
	$nc('.seriemodele').hover(
		function()
		{
			$nc('#' + $nc(this).attr('id') + '_over', $nc(this).parent()).fadeIn(50);
		},
		function()
		{
			$nc('#' + $nc(this).attr('id') + '_over', $nc(this).parent()).fadeOut(50);
	});
}

// Puissance //
function initPuissance()
{
	// Date Dropdown
	$nc('#dpreg').mousedown 	( function(){ $nc(this).css('background-position', '9px 7px'); });
	$nc('#dpreg').mouseup		( function(){ $nc(this).css('background-position', '8px 6px'); });
	$nc('#dpreg').click	 	( function(){ $nc('#dpregdata').slideToggle(200, 'easeOutExpo'); });
	$nc('.dpregitem').click 	( function()
	{
		region = $nc(this).attr('id').substring(4);

		$nc('#dpregselect').html ($nc(this).html());
		$nc('#dpregdata').slideToggle(200, 'easeOutExpo');
	});
	
	$nc('#regsearch').click ( function()
	{
		location = 'detaillant.php?region=' + region;
		return false;
	});
	
	$nc('#regsearch_en').click ( function()
	{
		location = 'retailer.php?region=' + region;
		return false;
	});
}

function initTechs()
{
	// Tech Dropdown
	$nc('#dptech').mousedown 		( function(){ $nc(this).css('background-position', '9px 7px'); });
	$nc('#dptech').mouseup		( function(){ $nc(this).css('background-position', '8px 6px'); });
	$nc('#dptech').click	 		( function(){ $nc('#dptechdata').slideToggle(200, 'easeOutExpo'); });
}

function puissanceEffects()
{
	delai = 500;
	$nc('.puissance img').each( function(index)
	{
		$nc(this).delay(delai).fadeIn(900);
		delai += 400;
	});

	$nc(window).scroll(function ()
	{
		//position =
		position = Math.max($nc(document).scrollTop() - 100,0);
		$nc('#founddet').stop(true, true).animate({top: position + 'px'}, 800, 'easeOutExpo');
	});
}

// Technologies //
function techEffects()
{
	delai = 500;
	$nc('#techimages p').each( function(index)
	{
		$nc(this).delay(delai).fadeIn(900);
		delai += 400;
	});
}

function loadFancyBox()
{
	// Local Fancy Box
	$nc("a.fancy").fancybox(
	{
		'showCloseButton'	: true,
		'overlayColor'		: '#000',
		'overlayOpacity'	: '1',
		'titleShow'			: false
	});
}

function initDetaillant()
{
	// Province dropdown
	$nc('#dppro').mousedown 	( function(){ $nc(this).css('background-position', '9px 7px'); });
	$nc('#dppro').mouseup		( function(){ $nc(this).css('background-position', '8px 6px'); });
	$nc('#dppro').click	 	( function()
	{
		if ( $nc('#dpregdata').css('display') != 'none' ) { $nc('#dpregdata').slideToggle('200', 'easeOutExpo'); }		
		$nc('#dpprodata').slideToggle(200, 'easeOutExpo'); 
	});
	$nc('.dpproitem').click 	( function()
	{
		province = $nc(this).attr('id').substring(4);
		$nc('#dpproselect').html ($nc(this).html());
		$nc('#dpprodata').slideToggle(200, 'easeOutExpo');
		
		$nc.ajax(
		{ 
			type : "POST", 
			cache : false, 
			url : "../ajax.php", 
			data: ({action:'getRegion', prov:province}),
			success : function (data) 
			{
				$nc('#regdropdown').html(data);
				region = $nc('.dpregitem').attr('id').substring(4);
			}
		});		
	});
	
	// Region dropdown
	$nc('#dpreg').live ('mousedown', function(){ $nc(this).css('background-position', '9px 7px'); });
	$nc('#dpreg').live ('mouseup', function(){ $nc(this).css('background-position', '8px 6px'); });
	$nc('#dpreg').live ('click', function()
	{
		if ( $nc('#dpprodata').css('display') != 'none' ) { $nc('#dpprodata').slideToggle('200', 'easeOutExpo'); }		
		$nc('#dpregdata').slideToggle(200, 'easeOutExpo'); 
	});
	
	$nc('.dpregitem').live ('click', function()
	{
		region = $nc(this).attr('id').substring(4);	
		$nc('#dpregselect').html ($nc(this).html());
		$nc('#dpregdata').slideToggle(200, 'easeOutExpo' );
	});
	
	$nc('#regsearch').click ( function()
	{
		location = 'detaillant.php?province=' + province + '&region=' + region;
		return false;
	});
	
	$nc('#codesearch').click ( function()
	{
		location = 'detaillant.php?code=' + $nc('#codepostal').val();
		return false;
	});
	
	$nc('#regsearch_en').click ( function()
	{
		location = 'retailer.php?province=' + province + '&region=' + region;
		return false;
	});
	
	$nc('#codesearch_en').click ( function()
	{
		location = 'retailer.php?code=' + $nc('#codepostal').val();
		return false;
	});
}

// Contact
function contactvalidateform()
{
	showsuccesmessage(false);

	/* FirstName field */
	if ( $nc('#prenom').val() == '' || $nc('#prenom').val() == 'Prénom' )
	{
		$nc('#errormsg').html('Vous devez entrer votre prénom.');
		$nc('#prenom').focus();
		showerrmessage(true);
		return;
	}

	/* LastName field */
	if ( $nc('#nom').val() == '' || $nc('#nom').val() == 'Nom' )
	{
		$nc('#errormsg').html('Vous devez entrer votre nom.');
		$nc('#nom').focus();
		showerrmessage(true);
		return;
	}

	/* Phone field */
	if ( $nc('#telephone').val() == '' || $nc('#telephone').val() == 'Téléphone' )
	{
		$nc('#errormsg').html('Vous devez entrer votre numéro de téléphone.');
		$nc('#telephone').focus();
		showerrmessage(true);
		return;
	}

	/* Email field */
	if ( $nc('#courriel').val() == '' || $nc('#courriel').val() == 'Courriel' )
	{
		$nc('#errormsg').html('Vous devez entrer votre courriel.');
		$nc('#courriel').focus();
		showerrmessage(true);
		return;
	}

	/* Email validation */
	if ( !validate_email($nc('#courriel').val()) )
	{
		$nc('#errormsg').html('Votre courriel est invalide.');
		$nc('#courriel').focus();
		showerrmessage(true);
		return;
	}

	/* Message field */
	if ( $nc('#message').val() == '' || $nc('#message').val() == 'Questions ou commentaires' )
	{
		$nc('#errormsg').html('Vous devez entrer votre message.');
		$nc('#message').focus();
		showerrmessage(true);
		return;
	}

	showerrmessage(false);

	$nc('#contactspinner').show();

	$nc.post('sendmail.php',  { 	prenom: $nc('#prenom').val(),
								nom: $nc('#nom').val(),								
								telephone: $nc('#telephone').val(),
								courriel: $nc('#courriel').val(),
								province: $nc('#province').val(),
								pays: $nc('#pays').val(),
								message:  $nc('#message').val()}, function(data, success)
	{
		if ( data == 'true')
		{
			showsuccesmessage(true);
			$nc('#contactsendrow').hide();
		}
		else
		{
			$nc('#errormsg').html('Désolé, une erreur s\'est produite. Recommencez ou écrivez-nous à l\'adresse <a href="mailto:info@velec.ca" title="info@velec.ca">info@velec.ca</a>');
			showerrmessage(true);
		}
	})
    .error(function()
	{
		$nc('#errormsg').html('Désolé, une erreur s\'est produite. Recommencez ou écrivez-nous à l\'adresse <a href="mailto:info@velec.ca" title="info@velec.ca">info@velec.ca</a>');
		showerrmessage(true);
	})

	$nc('#contactspinner').hide();
}

function contactvalidateform_en()
{
	showsuccesmessage(false);

	/* FirstName field */
	if ( $nc('#prenom').val() == '' || $nc('#prenom').val() == 'Name' )
	{
		$nc('#errormsg').html('You must enter your first name.');
		$nc('#prenom').focus();
		showerrmessage(true);
		return;
	}

	/* LastName field */
	if ( $nc('#nom').val() == '' || $nc('#nom').val() == 'Surname' )
	{
		$nc('#errormsg').html('You must enter your last name.');
		$nc('#nom').focus();
		showerrmessage(true);
		return;
	}

	/* Phone field */
	if ( $nc('#telephone').val() == '' || $nc('#telephone').val() == 'Telephone' )
	{
		$nc('#errormsg').html('You must enter your phone number.');
		$nc('#telephone').focus();
		showerrmessage(true);
		return;
	}

	/* Email field */
	if ( $nc('#courriel').val() == '' || $nc('#courriel').val() == 'E-mail' )
	{
		$nc('#errormsg').html('You must enter your e-mail.');
		$nc('#courriel').focus();
		showerrmessage(true);
		return;
	}

	/* Email validation */
	if ( !validate_email($nc('#courriel').val()) )
	{
		$nc('#errormsg').html('Your e-mail is invalid.');
		$nc('#courriel').focus();
		showerrmessage(true);
		return;
	}

	/* Message field */
	if ( $nc('#message').val() == '' || $nc('#message').val() == 'Questions or Comments' )
	{
		$nc('#errormsg').html('You must enter your message.');
		$nc('#message').focus();
		showerrmessage(true);
		return;
	}

	showerrmessage(false);

	$nc('#contactspinner').show();

	$nc.post('sendmail.php',  { 	prenom: $nc('#prenom').val(),
								nom: $nc('#nom').val(),								
								telephone: $nc('#telephone').val(),
								courriel: $nc('#courriel').val(),
								province: $nc('#province').val(),
								pays: $nc('#pays').val(),
								message:  $nc('#message').val()}, function(data, success)
	{
		if ( data == 'true')
		{
			showsuccesmessage(true);
			$nc('#contactsendrow').hide();
		}
		else
		{
			$nc('#errormsg').html('Sorry, an error occurred. Repeat or write to us at <a href="mailto:info@velec.ca" title="info@velec.ca">info@velec.ca</a>');
			showerrmessage(true);
		}
	})
    .error(function()
	{
		$nc('#errormsg').html('Sorry, an error occurred. Repeat or write to us at <a href="mailto:info@velec.ca" title="info@velec.ca">info@velec.ca</a>');
		showerrmessage(true);
	})

	$nc('#contactspinner').hide();
}

function validate_email(value)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$nc/;
	return reg.test(value);
}

function showerrmessage(show)
{
	if ( show )
	{
		if ( $nc('#errormsg').is(':hidden') )
		{
			$nc('#errormsg').slideDown('fast', 'easeOutExpo');
		}
	}
	else
	{
		if ( !$nc('#errormsg').is(':hidden') )
		{
			$nc('#errormsg').slideUp('fast', 'easeOutExpo');
		}
	}
}

function showsuccesmessage(show)
{
	if ( show )
	{
		if ( $nc('#successmsg').is(':hidden') )
		{
			$nc('#successmsg').slideDown('fast', 'easeOutExpo');
		}
	}
	else
	{
		if ( !$nc('#successmsg').is(':hidden') )
		{
			$nc('#successmsg').slideUp('fast', 'easeOutExpo');
		}
	}
}
