/**
* @package  crwmaroc
* @subpackage www
* @author
* @contributor
* @copyright
*/



if (typeof $.fn.switchSlideShowOff == 'undefined')
	$.fn.switchSlideShowOff = function(body) {
	    return this.each(function() {
	        // for each matching element, remove the current contents, hide the element,
	        // append the new contents and then fadeIn the element
	       $('#slideshow').hide();
	       // $(this).attr('id' , 'sideBar');

	    });
	};
if (typeof $.fn.switchSlideShowOn == 'undefined')
	$.fn.switchSlideShowOn = function(body) {
	    return this.each(function() {
	        $('#slideshow').show();
	    });
	};



if (typeof $.dynamisePage == 'undefined')
		$.dynamisePage = function() {

		var logCount = 0;
		function log() {
			if (typeof window.console !='undefined')
		   		window.console.log('[dynamisePage] ' + [].join.call(arguments,''));
		};

			// liens
	    log(' accordeon'  );
		// accordeon
		// Accordeon pour le catalogue
	    /*Query("div.series").accordion(
	    	{
				header	: 'h4',
				panel	: 'div.serieproduits',
				speed			: 300,
				autoheight:false,
				selectedClass:'selectedSerie'
			}
	    );*/
	  		// Accordeon pour le catalogue
	    jQuery("div.catalogue").accordion(
	    	{
				header	: 'h3',
				panel	: 'div.series',
				speed			: 300,
				autoheight:false,
				selectedClass:'selectedCatalogue'
			}
	    );

			    // menu catalogue
	jQuery("div.serieproduits").hide();
	jQuery("div.series h4").click(
			function(){
				/*if ( $('h4.clickedaccordionserie').size()){
						jQuery("div.serieproduits").hide();
						//return true;
				}*/

				if ($(this).hasClass('clickedaccordionserie')){
					//$(this).removeClass("clickedaccordionserie");
					//$(this).next('div.serieproduits').slideUp("slow");
					return false;
				}
				$('.visibleSerie').css({'display':'none'}).removeClass('visibleSerie');
				$('.clickedaccordionserie').removeClass('clickedaccordionserie');

				$(this).next().addClass('visibleSerie').show('normal');
				$(this).addClass("clickedaccordionserie");
				return false;

			}
	);
	jQuery("div.series h4:first-child").addClass("clickedaccordionserie").next().addClass('visibleSerie').show('normal');
	jQuery("div.catalogue h3").click(function(){$(this).next("div.series").find("h4:first-child").addClass("clickedaccordionserie").next().addClass('visibleSerie').show('normal');});






	// liens
		if ( !dontTaconitize ) {
		    log(' liens '  );
			/*jQuery("#extracmsSideBar a").click(function(){
				//alert($(this).attr('href')+'?taconite=1');
				$('body').addClass('loading');
				var urlTaconite =$(this).urlTaconite();
				$.get( urlTaconite , $.dynamisePage );

				//$.dynamisePage();
				log(' Dynamise page '+urlTaconite  );
				//$.dynamisePage();
				return false;

			});*/

			log(' taconitizeForms '  );
			jQuery("#mainContent form").taconitizeForms(
				function () {
					if (typeof window.console !='undefined')
				    	window.console.log('[taconitizeForms] complete' );
				}
			);
			jQuery(".secondMenu a , #extracms_children_list a.readmore , #wiki-links .navigation a , .catalogue .produit a").not('a.editMe').taconitizeLinks(
				function () {
					if (typeof window.console !='undefined')
				    	window.console.log('[taconitizeLinks] complete' );
				}
			);

			// liens
		    log(' liens edit internes'  );
			jQuery("#wiki-links a.editMe").taconitizeCrudLinks();
		}



		/*click(function(){
			//alert($(this).attr('href')+'?taconite=1');
			$('body').addClass('loading');
			var urlTaconite =$(this).urlTaconite();
			$.get( urlTaconite , $.dynamiseCrud );

			//$.dynamisePage();
			log(' Dynamise crud '+urlTaconite  );
			//$.dynamisePage();
			return false;

		});*/
		log(' lightbox '  );

		var lightboxOptions =	{
			// Configuration related to overlay
			overlayBgColor: 		'#000',		// (string) Background color to overlay; inform a hexadecimal value like: #RRGGBB. Where RR, GG, and BB are the hexadecimal values for the red, green, and blue values of the color.
			overlayOpacity:			0.8,		// (integer) Opacity value to overlay; inform: 0.X. Where X are number from 0 to 9
			// Configuration related to navigation
			fixedNavigation:		false,		// (boolean) Boolean that informs if the navigation (next and prev button) will be fixed or not in the interface.
			// Configuration related to images
			imageLoading:			'crwmaroc/js/lightbox/images/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
			imageBtnPrev:			'crwmaroc/js/lightbox/images/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
			imageBtnNext:			'crwmaroc/js/lightbox/images/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
			imageBtnClose:			'crwmaroc/js/lightbox/images/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
			imageBlank:				'crwmaroc/js/lightbox/images/lightbox-blank.gif',			// (string) Path and the name of a blank image (one pixel)
			// Configuration related to container image box
			containerBorderSize:	10,			// (integer) If you adjust the padding in the CSS for the container, #lightbox-container-image-box, you will need to update this value
			containerResizeSpeed:	400,		// (integer) Specify the resize duration of container image. These number are miliseconds. 400 is default.
			// Configuration related to texts in caption. For example: Image 2 of 8. You can alter either "Image" and "of" texts.
			txtImage:				'Image',	// (string) Specify text "Image"
			txtOf:					'of',		// (string) Specify text "of"
			// Configuration related to keyboard navigation
			keyToClose:				'c',		// (string) (c = close) Letter to close the jQuery lightBox interface. Beyond this letter, the letter X and the SCAPE key is used to.
			keyToPrev:				'p',		// (string) (p = previous) Letter to show the previous image
			keyToNext:				'n'		// (string) (n = next) Letter to show the next image.
			// Donīt alter these variables in any way

		}
		// lightbox pour image produit
		$(".produit a.original").lightBox(lightboxOptions);




};


if (typeof $.dynamiseCrud == 'undefined')
		$.dynamiseCrud = function() {

		var logCount = 0;
		function log() {
			if (typeof window.console !='undefined')
		    	window.console.log('[dynamiseCrud]' + [].join.call(arguments,''));
		};
		// liens actions ..
	   	/*jQuery(".crud a.crud-list , .crud-record-list a").click(function(){
			//alert($(this).attr('href')+'?taconite=1');
			$('body').addClass('loading');
			var urlTaconite =$(this).urlTaconite();
			$.get( urlTaconite , $.dynamiseCrud);

			//$.dynamisePage();
			log(' Dynamise crud  '+urlTaconite  );
			//$.dynamisePage();
			return false;

		}); */

		if ( !dontTaconitize ) {
			log(' taconitizeCrudForms '  );

			jQuery("#mainContent form").taconitizeCrudForms(
				function () {
					if (typeof window.console !='undefined')
				    	window.console.log('[taconitizeCrudForms] complete' );
				}
			);

			log(' taconitizeCrudLinks '  );
			jQuery("#mainContent a.crud-list , .crud-record-list a , #mainContent a.crud-link , #pagination a , .pagelinks a ").taconitizeCrudLinks();

			// menuSecond ..
			jQuery(".secondMenu a").not('a.editMe').taconitizeLinks(
				function () {
					if (typeof window.console !='undefined')
				    	window.console.log('[taconitizeLinks] complete' );
				}
			);
		}
			//alert('end taconitization');

		// editeur html
		log(' htmleditor '  );



	    jQuery(".extracms textarea[name=content] , textarea[name=detail]").wymeditor( {
	    	lang:    currentLang,
	    	toolsItems: [
	        {'name': 'Bold', 'title': 'Strong', 'css': 'wym_tools_strong'},
	        {'name': 'Italic', 'title': 'Emphasis', 'css': 'wym_tools_emphasis'},
	        {'name': 'InsertOrderedList', 'title': 'Ordered_List',
	            'css': 'wym_tools_ordered_list'},
	        {'name': 'InsertUnorderedList', 'title': 'Unordered_List',
	            'css': 'wym_tools_unordered_list'},
	        {'name': 'Paste', 'title': 'Paste_From_Word','css': 'wym_tools_paste'},
	        {'name': 'ToggleHtml', 'title': 'HTML', 'css': 'wym_tools_html'} ],
	     	updateSelector:    ".jcms .jforms-submit , .extracms .jforms-submit , .produit_submit , .extracms-controls .jforms-submit, .jforms-submit",
	    	updateEvent:       "click" ,
			classesItems:  [
	        {'name': 'note', 'title': 'Note' , 'expr': 'p'},
			{'name': 'important', 'title': 'Important' , 'expr': 'p'}
			]});

	// editeur html
		log(' autocomplete '  );

$('input[name=nomserie]').autocomplete('/index.php?module=produits&action=default:seriesdisponibles' , {
		selectFisrt:false
	});

		log(' tabs '  );
	jQuery("#edit_content").tabs({
    });


};

//load editor for textarea inside jcms , check jquery.wymeditor.js for valid options
jQuery(function() {
	locale = $('body').attr('class') ;
	currentLang = locale.substring(0,2);
	if ($.browser.msie && $.browser.version < 6 )
		dontTaconitize=true;
	else dontTaconitize=false;


	// lazy load images
/*	$("#sideBar img, #mainContent img").lazyload({ 
   // placeholder : "img/grey.gif",
  //  effect : "fadeIn"
});*/

	if ($('body').hasClass('crud'))
		$.dynamiseCrud();
	else $.dynamisePage();




	// menu principal

	jQuery("#menuPrincipal li").hover(
			function(){
				$(this).addClass("over");
				$(this).find('ul').show('fast');
			}
			, function(){
				$(this).find('ul').slideUp("fast" , function(){$(this).parent('.over').removeClass("over");});
				if ($(this).hasClass('over'))
					$(this).removeClass("over");
			}
	).find('ul').slideUp("fast" );
	// menu catalogue
	jQuery(".produits li.categories").click(
			function(){
				if ( $('li.clickedproduct').size())
						return true;
				if ($(this).hasClass('selected')){
					$(this).removeClass("selected");
					$(this).find('ul').slideUp("fast");
				} else {
					$(this).addClass("selected");
					$(this).find('ul.listeseries').show("fast");
				}
				return false;
			}
	).find('ul').hide();
	// menu catalogue
	jQuery(".produits li.series").click(
			function(){
				if ( $('li.clickedproduct').size())
						return true;
				if ($(this).hasClass('selected')){
					$(this).removeClass("selected");
					$(this).find('ul').slideUp("fast");


				} else {
					$(this).addClass("selected");
					$(this).find('ul').show("fast");

				}
				return false;

			}
	).find('ul').hide();

	// Produits .. click sur image active une classe que les series vont verifier avant de renvoyer un false
	jQuery("li.series li").click(
			function(){

					$(this).addClass("clickedproduct");


			}
	);

	/*
	$('#extracms_photo_viewer_centre_page').cycle({
	   		slideExpr: 'dd.photopreview img' ,
	   		height:'27em',
	   		cleartype:     1
		});
	*/
	if ( !dontTaconitize ) {
	    jQuery("#header a ,  .produits .series li a, .contentHeader a ").taconitizeLinks(
				function () {
					if (typeof window.console !='undefined')
				    	window.console.log('[taconitizeLinks] complete' );
				}
			);
		jQuery(" .contentHeader a ").taconitizeHistory();

		jQuery("#menuAdministration a , #menuAide a").taconitizeCrudLinks();
	}
});

