$(function(){
	//png fix
	if( 'undefined' !== typeof(DD_belatedPNG) ){
		DD_belatedPNG.fix('h1 a img, #nav, #nav ul li a, .video .play, .dispo, .other-products ul li a img, .product, .new, .info li div img, #coach-form #submit, .envie .product-image img');
	}
	
	//home
	if($('#content').hasClass('home')){
		var flashvars = { conf_url:"media/flash/xml/fr-fr/conf.xml" },
			params = { menu:"false", scale:"noScale", allowFullscreen:"true", bgcolor:"#FFFFFF", wmode:"transparent" },
			attributes = { id:"flash-content" };
		swfobject.embedSWF("media/flash/hpHome.swf", "alt-content", "952", "386", "9.0.0", "media/flash/expressInstall.swf", flashvars, params, attributes);
	}

	//bougies
	if($('#content').hasClass('bougies')){
		var params = { menu: "false", scale: "noScale", allowFullscreen: "true", allowScriptAccess: "always", bgcolor: "#FFFFFF" },
			attributes = { id:"video-player" },
			flashVars,
			video = '../movies/bougies/bougies.flv'; //url video home bougies par défaut

		if(	$('.bougies').hasClass('pomme-cannelle')	)	{ video = '../movies/bougies/pomme-cannelle.flv'; }
		if(	$('.bougies').hasClass('creme-vanille')		)	{ video = '../movies/bougies/creme-de-vanille.flv'; }
		if(	$('.bougies').hasClass('printemps')		)	{ video = '../movies/bougies/printemps.flv'; }
		if(	$('.bougies').hasClass('rosee-du-matin')	)	{ video = '../movies/bougies/rosee-du-matin.flv'; }
		
		flashVars = { url_video:video, color:"0xFF00FF" };
		
		$('.video').bind('click', function(){
			$('#video-data').modal({
				onOpen: function (dialog) {
					dialog.overlay.fadeIn('slow', function () {
						swfobject.embedSWF("../media/flash/videoPlayer.swf", "alt-content", "640", "386", "9.0.0", "../media/flash/expressInstall.swf", flashVars, params, attributes);
						$('.modalCloseImg').html('FERMER').attr('title','Fermer');
						dialog.container.fadeIn('fast');
						dialog.data.fadeIn('fast');
					});
				},
				opacity: 65
			});
		});
	}
	//aerosols et textiles
	if($('#content').hasClass('aerosols') || $('#content').hasClass('textiles')){
		$('.see-more a').bind('click', function(){
			$('#techno').modal({
				containerCss : { 'width':'762px', 'height':'335px', 'padding-top':'10px' },
				onOpen: function (dialog) {
					dialog.overlay.fadeIn('slow', function () {
						if( 'undefined' !== typeof(DD_belatedPNG) ){
							DD_belatedPNG.fix('#techno h3 img'); //must be fixed here, not on document ready
						}
						$('.modalCloseImg').html('FERMER').attr('title','Fermer').css({ "right":"19px", "top":"15px" });
						dialog.container.fadeIn('fast');
						dialog.data.fadeIn('fast');
					});
				},
				opacity: 65
			});
		});
		
		$('<div />')
			.attr('class','tooltip')
			.hide()
			.appendTo( $('body') )
			.append( $('<div />').attr('class','boxx') );

					
		$('.other-products ul li a').each(function(){
			var self = $(this);
			$(this).bind({
				'mouseover':function(){
					var desc = self.siblings('.hidden-description').html(),
						prdName = self.children('span').html();
					$('.tooltip .boxx')
						.html( $('<h4 />').html(prdName) )
						.append( $('<p />').append(desc) )
						.parent()
						.css({
							top:self.offset().top-$('.tooltip').height(),
							left:self.offset().left
						})
						.show();
				},
				'mouseout':function(){
					$('.tooltip').hide();
				}
			});
		});
	}
	//coach senteur
	if($('#content').hasClass('coach')){
		$('#submit').bind('click', function(e){
			e.preventDefault();
			var //smell location
				sLocation = parseInt($('#location-odeur option:selected').val()),
				//smell type
				sType = parseInt($('#type-odeur option:selected').val()),
				//question list
				qList = {
					q1:parseInt($('input:radio[name=textiles]:checked').val()),
					q2:parseInt($('input:radio[name=interieur]:checked').val()),
					q3:parseInt($('input:radio[name=allergies]:checked').val())
				},
				//product list
				pList = {
					a:0, //aerosols
					b:0, //bougies
					t:0, //textiles
					d:0  //diff continu
				},
				//url list
				url = {
					a:'../results/aerosols.html',
					b:'../bougies/',
					t:'../results/desodorisants-textiles.html',
					d:'http://www.google.com/'
				},
				//sorted pList object in an array
				sorted = [],
				//result list of the pList array
				results = [],
				//variables to add a priority to products
				A = B = T = D = false,
				//loops & storage vars
				x, len, finalUrl;
				
			switch(sLocation){
				case 1:
					switch(qList.q1){
						case 0:
							pList.a += 1;
							pList.b += 1;
							pList.d += 1;
							break;
						case 1:
							pList.t += 1;
							T = true;
							break;
						default:
							break;
					}
					switch(qList.q2){
						case 0:
							pList.t += 1;
							pList.b += 1;
							pList.d += 1;
							break;
						case 1:
							pList.b += 1;
							pList.d += 1;
							B = true;
							break;
						default:
							break;
					}
					switch(qList.q3){
						case 0:
							pList.a += 1;
							pList.b += 1;
							pList.d += 1;
							break;
						case 1:
							pList.t += 1;
							T = true;
							break;
						default:
							break;
					}
					break;
				case 2:
					switch(qList.q1){
						case 0:
							pList.a += 1;
							break;
						case 1:
							pList.t += 1;
							break;
						default:
							break;
					}
					switch(qList.q2){
						case 0:
							pList.t += 1;
							break;
						case 1:
							pList.a += 1;
							break;
						default:
							break;
					}
					switch(qList.q3){
						case 0:
							pList.a += 1;
							break;
						case 1:
							pList.t += 1;
							break;
						default:
							break;
					}
					break;
				case 3:
					switch(qList.q1){
						case 0:
							pList.a += 2;
							pList.d += 1;
							break;
						case 1:
							pList.t += 1;
							T = true;
							break;
						default:
							break;
					}
					switch(qList.q2){
						case 0:
							pList.t += 1;
							pList.b += 1;
							pList.d += 1;
							break;
						case 1:
							pList.b += 1;
							pList.d += 1;
							B = true;
							break;
						default:
							break;
					}
					switch(qList.q3){
						case 0:
							pList.a += 1;
							pList.b += 1;
							pList.d += 1;
							break;
						case 1:
							pList.t += 1;
							T = true;
							break;
						default:
							break;
					}
					break;
				default:
					break;
			}
			
			//console.log( 'initial data as a plain object: ', pList );
			
			for(x in pList){
				sorted.push([x, pList[x]]);
			}
			
			//console.log( 'initial data to array: ', sorted );
			
			sorted.sort(function(a,b){
				return a[1] - b[1];
			});

			//console.log( 'to sorted array: ', sorted );
			
			jQuery.map(sorted, function(elm){
				results.push(elm[0]);
			});

			//console.log( 'sorted array without values (results): ', results );
			
			len = results.length;
			
			//console.log( 'highest product..', results[len-1]);
			
			finalUrl = url[results[len-1]];
			
				//if duplicated result
				if( pList[results[len-1]] == pList[results[len-2]] ){
					
					//console.log('duplicate between: ', results[len-1], ' & ', results[len-2]);
					
					if ( eval(results[len-1].toUpperCase()) == true ){
						finalUrl = url[results[len-1]];
					}
					
					if ( eval(results[len-2].toUpperCase()) == true ){
						finalUrl = url[results[len-2]];
					}
					
					/*
					if( pList[results[len-1]] == pList[results[len-3]] ){
						
						console.log('duplicate between: ', results[len-1], ' & ', results[len-2], ' & ', results[len-3]);
						
						if ( eval(results[len-3].toUpperCase()) == true ){
							finalUrl = url[results[len-3]];
						}
						
					}
					*/
				}
				
			console.log(finalUrl)
			
			if(	finalUrl == url.b ){
				switch(sType){
					case 1:
						finalUrl = '../results/bougies_rosee-du-matin.html';
						break;
					case 2:
						finalUrl = '../results/bougies_pomme-cannelle.html';
						break;
					case 3:
						finalUrl = '../results/bougies_printemps.html';
						break;
					case 4:
						finalUrl = '../results/bougies_creme-de-vanille.html';
						break;
					default:
						break;
				}
			}
			
			window.location.href = finalUrl;
			
		});
	}
});
