
//domain+='/~preprojet/06maquette';
// domain+='/cms';
var fixDomain = ''; // a retirer lors de l'upload

$(document).ready(function(){//on attend que la page soit charg�e
    tweetFeed();
    footerBoxes();
	swfImplements();
	derouleWrap();
	sliderShowcase();
	contactBox();
	tooltipLinkedIn();
	accordeon();
	styleTags();
	colorCheckbox('.listcategories');
	
	// Gestion en ajax du formulaire
	$('form#form_filtre').filtreCatalogue(
	{
		url_ajax:"/do/catalogueSearch.php",
		wrapperContent:'.colRight #product_catalogue',
		loadingImg:'loading_catalogue.gif',
		loadingBgcolor:'#FFF'
	});
});

$(window).load(function() //on attend que la page soit chargée
{
    reflexions();
	slider();
    iphone();
	blockPosition('blockDoc');
});

function colorCheckbox(where)
{
	$(where+' input[type=checkbox]').change(function()
	{
		var infoCheck = $(this).attr('checked');
		var idCheckbox = $(this).attr('id');
		if (infoCheck == 'checked') /* Si on vient de cliquer sur une checkbox, on doit afficher en couleur le label lié à la checkbox */
		{ 
			$(where+' label[for='+idCheckbox+']').removeClass('disabled'); 
		}
		else /* sinon on grise le label */
		{ 	
			$(where+' label[for='+idCheckbox+']').addClass('disabled'); 
		}
	});
}

/* function priceRange()
{
	$slider = $("#rangepriceslider");	// Caching slider object
	$amount = $("#amount");				// Caching amount object
	$slider.slider({
		range: true, 					// necessary for creating a range slider
		min: 0, 						// minimum range of slider
		max: 500, 						// maximimum range of slider
		values: [75, 300], 				// initial range of slider
		slide: function(event, ui) { 	// This event is triggered on every mouse move during slide.
			$amount.html('$' + ui.values[0] + ' - $' + ui.values[1]);		//set value of  amount span to current slider values
		}
	});
	$amount.html('$' + $slider.slider("values", 0) + ' - $' + $slider.slider("values", 1));			
} */

function styleTags()
{
		if ($('.tags').length)
		{
				$('.tags a')
						.each
						(
								function()
								{
										var value = $(this).attr('class').substr(1);
										value /=100;
										var color = 180-(Math.floor(120*value));
										// var size = 7+Math.floor(15*value);
										var size = 10+Math.floor(10*value);
										$(this).css({'color':'rgb('+color+','+color+','+color+')','font-size':size+'px'});
								}
						)
				
				
		}
}

function accordeon()
{
	$('.accordeon h3 a').live('click', function()
	{
		var onglet = $(this).parents('.accordeon_content').find('.accordeon_onglet');
		if (!onglet.hasClass('open'))
		{
			/* var oHeight = onglet.height();
			log(oHeight); */
			$('.accordeon_onglet').slideUp(500, function() { $(this).removeClass('open'); });
			onglet.slideDown(1000, function() { $(this).addClass('open'); });
		}
		return false;
	});
}

function contactBox()
{
	$('#formulaire_contact input.text, #formulaire_contact textarea').emptyFields();
	
	$('#contactBox a#contact_us').click(function() 
	{
		if ($('#contactBox').hasClass('open')) { $('#contactBox').stop().animate({ 'width':'29px' },500).removeClass('open'); }
		else {  $('#contactBox').stop().animate({ 'width':'699px' },500).addClass('open'); }
		
		$('*').live('click',function(event)
		{
			if ($(event.target).parents('#contactBox').length == 0 || $(event.target).attr('id') == 'contact_us') closeContactBox();
		});
		return false;
	});
	
	$('#contactBox .close').click(function()
	{
		closeContactBox();
		return false;
	});
}

function closeContactBox()
{
	$('#contactBox').stop().animate({ 'width':'29px' },500).removeClass('open');
}

function sliderShowcase()
{
	var idSlider;
	if ($('.mySlider').length)
	{
		$('.mySlider').each(function(i){
			idSlider = $(this).attr('id');
			mySlider(idSlider);
		});
	}
}

function mySlider(idSlider)
{
	$('.showcase-content img').show();
	$('.showcase-content iframe').show();
	$("#"+idSlider).awShowcase(
	{
		content_width:			850,
		content_height:			480,
		fit_to_parent:			false,
		auto:					false,
		interval:				3000,
		continuous:				false,
		loading:				true,
		tooltip_width:			200,
		tooltip_icon_width:		32,
		tooltip_icon_height:	32,
		tooltip_offsetx:		18,
		tooltip_offsety:		0,
		arrows:					true,
		buttons:				true,
		btn_numbers:			false,
		keybord_keys:			true,
		mousetrace:				false, /* Trace x and y coordinates for the mouse */
		pauseonover:			true,
		stoponclick:			false,
		transition:				'hslide', /* hslide/vslide/fade */
		transition_delay:		0,
		transition_speed:		500,
		show_caption:			'onload', /* onload/onhover/show */
		thumbnails:				false,
		thumbnails_position:	'outside-last', /* outside-last/outside-first/inside-last/inside-first */
		thumbnails_direction:	'vertical', /* vertical/horizontal */
		thumbnails_slidex:		1, /* 0 = auto / 1 = slide one thumbnail / 2 = slide two thumbnails / etc. */
		dynamic_height:			false, /* For dynamic height to work in webkit you need to set the width and height of images in the source. Usually works to only set the dimension of the first slide in the showcase. */
		speed_change:			true, /* Set to true to prevent users from swithing more then one slide at once. */
		viewline:				false, /* If set to true content_width, thumbnails, transition and dynamic_height will be disabled. As for dynamic height you need to set the width and height of images in the source. */
		custom_function:		null /* Define a custom function that runs on content change */
	});
}

function tooltipLinkedIn()
{
	$('.socialBox#linkedIn')
		.click(function(event) {  if ($(event.target).hasClass('link_edin')) { return true; } else if(!$(this).hasClass('keyword_img')) { return false; }	})
		.hover(function()
		{
			$('#_linkedIn')
				.stop()
				.css({ 'opacity':'0', 'display':'block'})
				.animate({ 'bottom':'45px', 'opacity':'1'},500);
		},
		function()
		{
			$('#_linkedIn')
				.stop()
				.animate({ 'bottom':'40px', 'opacity':'0'},500)
				.hide();
		});
}

function derouleWrap()
{
	$('#deroulant').click(function()
	{
		return false;
	});
	
	$('.menuElement#first').hover(function()
	{
		$('#derouleWrap')
			.stop()
			.css({ 'opacity':'0', 'display':'block'})
			.animate({ 'top':'35px', 'opacity':'1'},500);
	},
	function()
	{
		$('#derouleWrap')
			.stop()
			.animate({ 'top':'30px', 'opacity':'0'},500)
			.hide();
	});
}

function shareBoxClick()
{
	$('#shareBox a.shareLink').click(function() 
	{ 
		if ($('#shareBox').hasClass('open')) { $('#shareBox').animate({ 'width':'30px' },500).removeClass('open'); }
		else {  $('#shareBox').animate({ 'width':'150px' },500).addClass('open'); }
		return false;
	});
	
	$('*').click(function(event)
	{
		if ($(event.target).parents('#shareBox').length == 0) { $('#shareBox').animate({ 'width':'30px' },500).removeClass('open'); }
	});
}

function blockPosition(classBloc)
{
	$('.block_up, .block_down').live('click', function() 
	{
		var blockDoc = $(this).parents('.'+classBloc);
		var positionInPage = parseInt(blockDoc.attr('data-title')); // exemple , positionInPage = 5
		
		var positionTemp = new Array();
		var positionTempClick;
		var lastBloc;
		$('.'+classBloc).each(function(i)
		{
			// on affiche toutes les options (éditer - remonter - descendre)
			$(this).find('.manage_position li').show();
			// on stocke les positions;  positionTemp[0] = 4; positionTemp[1] = 5; positionTemp[2] = 8; positionTemp[3] = 14;
			positionTemp[i] = $(this).attr('data-title'); 
			if (positionTemp[i] == positionInPage) positionTempClick = i; // positionTempClick = 1
			lastBloc = i;
		});
		
		// on remonte ou on descend le bloc ?
		if ($(this).hasClass('block_up')) 	{ var modAction = 'up';   newPositionInPage = positionTemp[positionTempClick-1]; }
		if ($(this).hasClass('block_down')) { var modAction = 'down'; newPositionInPage = positionTemp[positionTempClick+1]; }
		
		var blockSwitch = $("."+classBloc+"[data-title="+newPositionInPage+"]");
		
		if (blockSwitch.length > 0)
		{
			blockSwitch.attr('data-title',positionInPage);
			blockSwitch.find('.position').val(positionInPage);
			blockDoc.attr('data-title',newPositionInPage);
			blockDoc.find('.position').val(newPositionInPage);
			if (modAction == 'down') blockSwitch.after(blockDoc);
			if (modAction == 'up') blockSwitch.before(blockDoc);
			
			var parentId = $('input#parentId').val();
			var position = $('.form_position').serialize();
			
			var fieldsForm = position+'&parentId='+parentId+"&movingPosition=1";
			$.post(domain+'/do/blockPicker.php', fieldsForm, function(data){ log(data); });
		}
		
		// on cache les options "remonter" pour le bloc le + haut & "descendre" pour le bloc le + bas
		$("."+classBloc+"[data-title="+positionTemp[0]+"]").find('a.block_up').parent().hide();
		$("."+classBloc+"[data-title="+positionTemp[lastBloc]+"]").find('a.block_down').parent().hide();
		
		return false;
	});
	
	// bonus design des icones d'édit et déplacement
	$('.manage_position li a, .manage_add .block_add').tooltip(
	{
		track: false, 
		delay: 0, 
		showURL: false, 
		extraClass: "tooltip_position",
		fade: 250,
		opacity: 0.8,
		left:-250,
		top:-25
	}); 
}

function swfImplements()
{
	var widthVideo = 850;
	var heightVideo = 480;
	var zvid=$('#myVid').attr('rel'); 	// /files/video/exemple.flv
	var flashvars = {vid:fixDomain+zvid,width:widthVideo};
	var params = {allowFullScreen:"true",wmode:"transparent"};
	var attributes = {};
	swfobject.embedSWF(domain+"/files/flash22/videoPlayer.swf?vid="+fixDomain+zvid+"&width="+widthVideo, "myVid", widthVideo, heightVideo, "9.0.0", "expressInstall.swf", flashvars, params, attributes);
 };
 
function reflexions()
{
    $('.imgg_contentLeft img').reflect({height: 0.15, opacity: 0.2});
    
}


function iphone()
{
   if ($('.carousel').length > 0) $('.carousel').fakeMac({});
}


function footerBoxes()
{
    var maxHeight = 0;
    $('.footerBox')
        .each
        (
            function()
            {
                if ($(this).outerHeight()>maxHeight)
                {
                    maxHeight = $(this).outerHeight();
                }
            }
        )
        
    $('.footerBox').height(maxHeight);
}


function socialBoxes()
{
    $('#menuList li:first').akiwiTooltip({align:'center',offsetY : 0,offsetX : 0});
    $('.socialBox').akiwiTooltip({verticalAlign:'bottom',offsetY : 0,offsetX : -100});
}


function shareLinks()
{
    if ($('#shareBox').length)
    {
        $('.shareLink').akiwiTooltip({align:'left',offsetX : 0,offsetY : -50});
        $('.share_text').emptyFields();
        
        var toLeft = $(window).width()-$('#shareBox').offset().left-$('#shareBox').width();
         
        $(window)
            .scroll
            (
                function()
                {
                   
                    
                    if ($(window).scrollTop()>50)
                    {
                        $('#shareBox')
                            .css
                            ({
                                'position':'fixed',
                                'top':'50px',
                                'right': toLeft+'px'
                            })
                            
                    }
                    else
                    {
                        $('#shareBox')
                            .css
                            ({
                                'position':'absolute',
                                'top':'20px',
                                'right':'0'
                               
                            })
                    }
                }
            )
    }
    
}



function tweetFeed()
{
	if ($('#tweetLogin').length > 0)
	{
		var usernameTwitter = $('#tweetLogin').val();
		$('#tweetFeedBox').jTweetsAnywhere({
			username: usernameTwitter,
			// searchParams: 'q=e-learning',
			count: 3,
			showTweetFeed: {
				showProfileImages : false,
				autorefresh: {
					mode: 'none',
					interval: 60
				},
				paging: {
					mode: 'none'
				},
				showTimestamp: {
					refreshInterval: 15
				}
			}
		});
	}
}


function slider()
{
	if ($('#slideShow').length > 0)
	{
		$('#slideShow').smoothSlide({
			width:'979px'
			,height:70
			,multipage:false
			,leftArrow:'<img src="'+domain+'/files/design/fleche_gauche3.png" alt="Gauche" />'
			,rightArrow:'<img src="'+domain+'/files/design/fleche_droite3.png" alt="Droite" />'
		});
		
		$('#slideShow a.reference')
			.css
			({
				'display':'block'   
			})
			.hover
			(
				function()
				{
					$(this).find('img').css({opacity:0});
				},
				function()
				{
					$(this).find('img').css({opacity:1});
				}
			)
    }
    
}
