//var XML_URL = './datas.xml';
//var TEMPLATE_PATH = './templates/'

var WizardXml = null;
var previousSteps = new Array();

jQuery(document).ready(function() {
	jQuery("#startBesoin").click(function() {
		startWizard();
		return false;
	});
	jQuery("#startSolution").click(function() {
		startSolution();
		return false;
	});
    jQuery(".enter").click(function()
	{
		startWizard();
		return false ;
	})
    
    jQuery('.Etape h2 img').click(previousClick);
});

function startSolution() {
	jQuery('#navigationSolution').animate({'top':'-1px'});
}

function displaySolutionLinks(section) {
	$('.solutionLinks').fadeOut(600);
	setTimeout("$('#solution"+section+"').fadeIn();", 650);
}

function closeSolution() {
	jQuery('#navigationSolution').animate({'top':'338px'});
}

function closeBesoin() {
	jQuery('#Etape_1').animate({'top':'338px'});
}

function startWizard() {
    // Get datas file
    jQuery.ajax({
        url: XML_URL,
        success: function(data) {
            WizardXml = jQuery(data);
            step1Node = WizardXml.find('step1');
            loadStep(jQuery('#Etape_1'), 'step1', buildStepObject(step1Node), showStep1);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert('Error : ' + textStatus + ' - '+errorThrown);
        }
    });
}

function loadStepBesoin(stepContainer, stepPrefix, stepObject, callback) {
	jQuery('#Etape_1').animate({'top':'-1px'});
}

function loadStep(stepContainer, stepPrefix, stepObject, callback) {
    stepContainer.find('.Content_Etape').load(TEMPLATE_PATH + stepPrefix + '_' + stepObject.template + '.html', '', function(responseText, status, requestObject) {
        stepContainer.find('h2 strong').text(stepObject.title);
        
        list = stepContainer.find('ul');
        
        // Construct button list
        for(var i=0 ; i<stepObject.links.length ; i++) {
            var li = jQuery('<li></li>');
            li.append(stepObject.links[i]);
            li.find('a').data('step', stepPrefix);
            li.find('a').data('stepObject', stepObject);
            li.find('a').click(stepClick);
            list.append(li);
        }
        
        // "En savoir plus"
        if(stepObject.knowMore) {
            knowMore = stepContainer.find('.knowMore');
            knowMore.append(stepObject.knowMore);
        }
        
        // Image
        imgP = stepContainer.find('p.image');
        imgP.append(stepObject.image);
        
        // Description
        desc = stepContainer.find('div.description');
        desc.append(stepObject.text);
        
        // Launch transition effect
        callback();
    });
}

function stepClick() {
    action = jQuery(this).attr('rel');
    
    if(!action)
        return true;
    
    previousStep = jQuery(this).data('step');
    previousSteps.push({
        step : previousStep,
        object: jQuery(this).data('stepObject')
    });

    switch(previousStep) {
        case 'step1':
            previousContainer = jQuery('#Etape_1');
            stepContainer = jQuery('#Etape_2');
            callback = showStep2;
            step = 'step2'
            break;
        case 'step2':
            previousContainer = jQuery('#Etape_2');
            newTitle = jQuery('#Etape_3 h2 strong');
            stepContainer = jQuery('#Etape_3');
            callback = showStep3;
            step = 'step3'
            break;
    }

    previousContainer.find('h2 strong').text(jQuery(this).attr('title'));
    
    stepNode = WizardXml.find(step + '[action='+action+']');
    
    loadStep(stepContainer, step, buildStepObject(stepNode), callback);

    return false;
}

function previousClick() {
    step = previousSteps.pop();
    
    switch(step.step) {
        case 'step1':
            stepContainer = jQuery('#Etape_1');
            effect = hideStep2;
            break;
        case 'step2':
            stepContainer = jQuery('#Etape_2');
            effect = hideStep3;
            break;
    }
    
    stepContainer.find('h2 strong').text(step.object.title);
    effect();
    
    return false;
}

function buildStepObject(stepNode) {
    step = {};
    step.title = stepNode.find('title').text();
    step.template = stepNode.attr('template');
    step.links = [];
    stepNode.find('buttons').children().each(function() {
        step.links.push(trim(jQuery(this).text()));
    });
    step.text = trim(stepNode.find('text').text());
    step.image = trim(stepNode.find('image').text());
    step.knowMore = trim(stepNode.find('knowMore').text());
    return step;
}

function trim (myString)
{
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}

function showStep1() {
    //jQuery('#Appel').slideToggle(100);
	jQuery('#Etape_1').animate({'top':'0px'});
}

function showStep2() {
	jQuery('#Etape_2').animate({'top':'38px'});
	jQuery('#Etape_1 img.back').fadeOut();
	jQuery('#Etape_2 img.back').fadeIn();
    /*jQuery('#Etape_2 .Content_Etape').slideToggle(100);
	jQuery('#Etape_1 .Content_Etape').slideToggle(100);
	jQuery('#Etape_2 h2 img').toggle("slow");*/
}

function hideStep2() {
	jQuery('#Etape_2').animate({'top':'338px'});
	jQuery('#Etape_2 img.back').fadeOut();
	jQuery('#Etape_1 img.back').fadeIn();
/*    jQuery('#Etape_2 h2 img').toggle("slow");
    jQuery('#Etape_2 .Content_Etape').slideToggle(100);
    jQuery('#Etape_1 .Content_Etape').slideToggle(100);*/
}

function showStep3() {
	jQuery('#Etape_3').animate({'top':'76px'});
	jQuery('#Etape_2 img.back').fadeOut();
	jQuery('#Etape_3 img.back').fadeIn();
  /*  jQuery('#Etape_2 .Content_Etape').slideToggle(100);
	jQuery('#Etape_3 .Content_Etape').slideToggle(100);
	jQuery('#Etape_3 h2 img').toggle("slow");
	jQuery('#Etape_2 h2 img').toggle("slow");*/
}

function hideStep3() {
	jQuery('#Etape_3').animate({'top':'338px'});
	jQuery('#Etape_3 img.back').fadeOut();
	jQuery('#Etape_2 img.back').fadeIn();
    /*jQuery('#Etape_2 h2 img').toggle("slow");
    jQuery('#Etape_3 h2 img').toggle("slow");
    jQuery('#Etape_3 .Content_Etape').slideToggle(100);
    jQuery('#Etape_2 .Content_Etape').slideToggle(100);*/
}


