 /* DOCUMENTATION plugin "prepareHtml"
	  * 
	  * itemsId : id des divs à manipuler - array
	  * 
	  * Ce plugin a été développé spécifiquement pour Varionet - il est difficilement réutilisable.
	  * 	  
 */

if(jQuery) (function(jQuery){
	jQuery.extend(jQuery.fn, {
		prepareHtml: function(params) {
			// DEFAULT OPTIONS 
			if( !params ) var params = {};
			if( params.itemsId == undefined ) params.itemsId = null;
			if( params.sliderType == undefined ) params.sliderType = null;
			if (params.minitabs == undefined) params.minitabs  = false;
			
			if (params.prescription == undefined) params.prescription = false;
			if (params.inputsIdToWrapForLeftEyeCorrectionWithPrescription == undefined) params.inputsIdToWrapForLeftEyeCorrectionWithPrescription = null;
			if (params.inputsIdToWrapForRightEyeCorrectionWithPrescription == undefined) params.inputsIdToWrapForRightEyeCorrectionWithPrescription = null;
			if (params.inputIdToWrapForBothEyeCorrectionWithPrescription == undefined) params.inputIdToWrapForBothEyeCorrectionWithPrescription = null;
			
			
			if (params.inputsIdToWrapForLeftEyeCorrectionNOPrescription == undefined) params.inputsIdToWrapForLeftEyeCorrectionNOPrescription = null;
			if (params.inputsIdToWrapForRightEyeCorrectionNOPrescription == undefined) params.inputsIdToWrapForRightEyeCorrectionNOPrescription = null;
			if (params.inputIdToWrapForBothEyeCorrectionNOPrescription == undefined) params.inputIdToWrapForRightEyeCorrectionNOPrescription = null;
			
			
			// INITIALISATION
			if (params.minitabs){
				jQuery(this).each( function() {
					var context = jQuery(this);
					jQuery(this).createUl();
					for (var i=0; i<params.itemsId.length; i++){
						jQuery('#'+params.itemsId[i]).addClass('tab-content');
						jQuery(this).createLi(params.itemsId[i], params.sliderType);
						jQuery(this).createA(params.itemsId[i]);	
						jQuery('h2', '#'+params.itemsId[i]).remove().appendTo('li#'+params.itemsId[i]+'-title a');
					}
				});
			}else if (params.prescription){
				// called on #product-options-wrapper
				jQuery(this).each( function() {
					// adapt correction for left glasses with prescription (custom option)
					for (var i=0; i<params.inputsIdToWrapForLeftEyeCorrectionWithPrescription.length; i++){
						jQuery('#'+params.inputsIdToWrapForLeftEyeCorrectionWithPrescription[i]).parents('dd').addClass('to-wrap-for-left-eye-with-prescription').prev().addClass('to-wrap-for-left-eye-with-prescription');
					}
					jQuery('.to-wrap-for-left-eye-with-prescription').removeClass('to-wrap-for-left-eye-with-prescription').wrapAll('<div id="left-eye-correction-with-prescription" />');
					
					// adapt correction for right glasses with prescription (custom option)
					for (var i=0; i<params.inputsIdToWrapForRightEyeCorrectionWithPrescription.length; i++){
						jQuery('#'+params.inputsIdToWrapForRightEyeCorrectionWithPrescription[i]).parents('dd').addClass('to-wrap-for-right-eye-with-prescription').prev().addClass('to-wrap-for-right-eye-with-prescription');
					}
					jQuery('.to-wrap-for-right-eye-with-prescription').removeClass('to-wrap-for-right-eye-with-prescription').wrapAll('<div id="right-eye-correction-with-prescription" />');
					
					// wrap both together
					jQuery('#right-eye-correction-with-prescription, #left-eye-correction-with-prescription').wrapAll('<div id="eye-correction-with-prescription" />');
					
				
					// insert glasses traitement to the right place
					jQuery('#'+params.inputIdToWrapForBothEyeCorrectionWithPrescription).parents('dd').prev().addClass('to-wrap-with-prescription').appendTo('#eye-correction-with-prescription');
					jQuery('#'+params.inputIdToWrapForBothEyeCorrectionWithPrescription).parents('dd').addClass('to-wrap-with-prescription').appendTo('#eye-correction-with-prescription');
					
					jQuery('.to-wrap-with-prescription').wrapAll('<div id="other-option-with-prescription" />');
					
					
					// adapt correction for left glasses no prescription (custom option)
					for (var i=0; i<params.inputsIdToWrapForLeftEyeCorrectionNOPrescription.length; i++){
						jQuery('#'+params.inputsIdToWrapForLeftEyeCorrectionNOPrescription[i]).parents('dd').addClass('to-wrap-for-left-eye-no-prescription').prev().addClass('to-wrap-for-left-eye-no-prescription');
					}
					jQuery('.to-wrap-for-left-eye-no-prescription').removeClass('to-wrap-for-left-eye-no-prescription').wrapAll('<div id="left-eye-correction-no-prescription" />');
					
					// adapt correction for right glasses no prescription (custom option)
					for (var i=0; i<params.inputsIdToWrapForRightEyeCorrectionNOPrescription.length; i++){
						jQuery('#'+params.inputsIdToWrapForRightEyeCorrectionNOPrescription[i]).parents('dd').addClass('to-wrap-for-right-eye-no-prescription').prev().addClass('to-wrap-for-right-eye-no-prescription');
					}
					jQuery('.to-wrap-for-right-eye-no-prescription').removeClass('to-wrap-for-right-eye-no-prescription').wrapAll('<div id="right-eye-correction-no-prescription" />');
					
					// wrap both together
					jQuery('#right-eye-correction-no-prescription, #left-eye-correction-no-prescription').wrapAll('<div id="eye-correction-no-prescription" />');

					// insert glasses traitement to the right place
					jQuery('#'+params.inputIdToWrapForBothEyeCorrectionNOPrescription).parents('dd').prev().addClass('to-wrap-no-prescription').appendTo('#eye-correction-no-prescription');
					jQuery('#'+params.inputIdToWrapForBothEyeCorrectionNOPrescription).parents('dd').addClass('to-wrap-no-prescription').appendTo('#eye-correction-no-prescription');
					
					jQuery('.to-wrap-no-prescription').wrapAll('<div id="other-option-no-prescription" />');
					
					
					
					
					// insert "correction" title before correction's CO (one title for both group).
					jQuery('#eye-correction-with-prescription').before('<dt><label id="correction-title-box" class="correction-title-box none"></label></dt>');
					jQuery('#custom-phrase').removeClass('none').prependTo('#eye-correction-with-prescription');
					jQuery('#correction-title').prependTo('.correction-title-box');
				
					// wrap to style : grey border and white background
					jQuery('#custom-phrase, #left-eye-correction-with-prescription, #right-eye-correction-with-prescription').wrapAll('<div id="eye-correction-with-prescription-has-a-border" />');
					jQuery('#left-eye-correction-no-prescription, #right-eye-correction-no-prescription').wrapAll('<div id="eye-correction-no-prescription-has-a-border" />');

				});
				
			}
			
			// Don't break the jQuery chain !
			return this;
		},
		createUl: function(){
			jQuery('<ul />', {
				className:'tabs-title'			
			}).prependTo(this);
		},
		createLi: function(customClassName, sliderType){
			jQuery('<li />', {
				id: customClassName+'-title',
				className:'tab-title'	
			}).appendTo('#'+sliderType+'-area .tabs-title', this);
		},
		createA: function(ref){
			jQuery('<a />', {
				href: '#'+ref
			}).appendTo('#'+ref+'-title', this);
		}
	});
}
)(jQuery);
