jQuery.noConflict();

var step_ref = 0;
var flash_enable = true;
var backgounds = [];
var image_width = 800;
var image_height = 600;
var book = null;
var image_ratio = image_width/image_height;
var ajax_load_part = "<div style='position:absolute; top:31px; left:238px; z-index:100; width:100%; text-align:center;'><img src='/images/rien.gif'></div>";

var menu_handler = function() {
    setStep(parseInt(jQuery(this).attr('step_id')));
};

var second_menu_handler = function() {
	jQuery("#page_container").height(100);
	//jQuery("#loading_logo").fadeIn(300);
	jQuery("#content2").load(jQuery(this).attr('href')+ " #content2_plus", null, function(responseText){
		jQuery("#content").hide();
                jQuery("#content2").show();		
                analyzePage();
                resizeWindow();
                j//Query('#loading_logo').fadeOut(500);
        });

};

var menu_part_handler = function() {
	jQuery("#page_container").height(100);
	//jQuery("#loading_logo").fadeIn(300);
	jQuery("#content").load(jQuery(this).attr('href')+ " #content_plus", null, function(responseText){  
                jQuery("#content").show();
                jQuery("#content2").hide();
                //jQuery("#content").effect("scale", { from: { height:0, width:0 }, percent: 100  }, 1000);
            analyzePage(true);
                //jQuery('#loading_logo').fadeOut(500);
        });
};


var close_handler = function() {
	jQuery("#content").hide();
	jQuery("#content2").hide();
	jQuery("#page_container").height(100);
	resizeWindow(); 
};

jQuery(document).ready(initPage);


function initPage(){
	
	if (!flash_enable)  disableFlash();

	jQuery(function(){
                if((document.location.href).toString().indexOf("portfolio") != -1) {
                    disableFlash();
                    setStep(4);
                    setBackgroundStep(4);
                    return;
                }
                
		jQuery('a[rel*=bg_0]').each(function() {
			jQuery(this).click(function() {
				setBackgroundStep(0);
			});
  		});
		jQuery('a[rel*=bg_1]').each(function() {
			jQuery(this).click(function() {
				setBackgroundStep(1);
			});
  		});
		jQuery('a[rel*=bg_2]').each(function() {
			jQuery(this).click(function() {
				setBackgroundStep(2);
			});
  		});
		jQuery('a[rel*=bg_3]').each(function() {
			jQuery(this).click(function() {
				setBackgroundStep(3);
			});
  		});
				
		jQuery('a[rel*=showIn_page]').each(function() {
			jQuery(this).unbind('click',menu_handler)
			jQuery(this).bind('click',menu_handler)
  		});

		jQuery('a[rel*=showIn_block]').each(function() {
                    if(jQuery(this).html() != 'Portfolio') {
			jQuery(this).unbind('click',second_menu_handler)
			jQuery(this).bind('click',second_menu_handler)
                    }
                    else {
                       jQuery(this).bind('click', function() {document.location.href=jQuery(this).attr('href')})
                    }
  		});	

		jQuery('#button_nav_left').click(function() {
				if (step_ref>0) {
					new_id = step_ref-1;
					setStep(new_id);
					setBackgroundStep(new_id);
				}
			});
		jQuery('#button_nav_right').click(function() {
				if (step_ref<3) {
					new_id = step_ref+1;
					setStep(new_id);
					setBackgroundStep(new_id);
				}
			});

		analyzePage();
		
	});
	
	resizeWindow();
}

function analyzePage(fbulle){
	jQuery(function(){
		
		var link = jQuery("<link>");
		link.attr({
	       type: 'text/css',
	       rel: 'stylesheet',
	       href: '/css/step_'+step_ref+'.css'
		});
		jQuery("head").append( link ); 
		
		jQuery('#button_close').hover(function() {
	        var currentImg = jQuery(this).attr('src');
	        jQuery(this).attr('src', jQuery(this).attr('hover'));
	        jQuery(this).attr('hover', currentImg);
	    }, function() {
	        var currentImg = jQuery(this).attr('src');
	        jQuery(this).attr('src', jQuery(this).attr('hover'));
	        jQuery(this).attr('hover', currentImg);
	    });
		jQuery('#button_close2').hover(function() {
	        var currentImg = jQuery(this).attr('src');
	        jQuery(this).attr('src', jQuery(this).attr('hover'));
	        jQuery(this).attr('hover', currentImg);
	    }, function() {
	        var currentImg = jQuery(this).attr('src');
	        jQuery(this).attr('src', jQuery(this).attr('hover'));
	        jQuery(this).attr('hover', currentImg);
	    });
			
		jQuery('#button_close').bind('click',close_handler)
		jQuery('#button_close2').bind('click',close_handler)
			
		jQuery('a[rel*=showIn_content]').each(function() {
			jQuery(this).unbind('click',menu_part_handler)
			jQuery(this).bind('click',menu_part_handler)
  		});	

		jQuery('a[rel*=bg_], a[rel*=showIn_]').each(function() {
			jQuery(this).click(function() {
				return false;
			});
  		});

	});
	
	if(jQuery('#page') !== null && fbulle) {
	    var height_bulles = jQuery('#page').height();
	    var height_page = jQuery('#book_footer').position().top;

	    if(height_bulles > height_page-180) {
		jQuery('#page').css(
		    'margin-top',
		    height_page/2-height_bulles/2-120
		);
	    }
	}
}

onresize = resizeWindow;

function resizeWindow(){ 
	var doc_width = jQuery(document).width();
	var doc_height = jQuery(document).height();
	var bg_width = jQuery("#background").width();
	var bg_height = jQuery("#background").height();
	
	jQuery('#page_container').height(doc_height-268);
	if (jQuery('#content2') && jQuery('#content2_plus').height() && jQuery('#content2_plus').height()<jQuery('#page_container').height() ) {
		jQuery('#content2_plus').height(jQuery('#page_container').height()-30);
	}
	if (!flash_enable){
		var new_width = doc_width;
		var new_height = Math.round(new_width/image_ratio);
		jQuery('#background_pict').width(new_width);
		jQuery('#background_pict').height(new_height);
		if(new_height<bg_height){
			new_height = bg_height;
			new_width = Math.round(new_height*image_ratio);
			jQuery('#background_pict').width(new_width);
			jQuery('#background_pict').height(new_height);
			var width_offset = Math.round((new_width-doc_width)/2);
			jQuery('#background_pict').css("left","-"+width_offset+"px");
		}		
		var height_offset = Math.round((new_height-bg_height)/2);
		jQuery('#background_pict').css("top","-"+height_offset+"px");
		
	}
}

function disableFlash(){
    for (var i = 0; i<4 ; i++) {
        var cacheImage = document.createElement('img');
        cacheImage.src = '/images/background_'+i+'.jpg';
        backgounds.push(cacheImage);
    }
    setBackgroundStep(step_ref);
}


function getFlashMovie(movieName) {   
	var isIE = navigator.appName.indexOf("Microsoft") != -1;   
	return (isIE) ? window[movieName] : document[movieName];  
}  

function setStep(id) {    
	jQuery("#menu_step_"+step_ref).toggleClass('selected');
    step_ref = id;
	jQuery("#menu_step_"+step_ref).toggleClass('selected');
	//jQuery("#loading_logo").fadeIn(300);
	jQuery("#page").load(jQuery("#link_step_"+step_ref).attr('href')+ " #page_plus", null,
	    function(responseText) {
		analyzePage();
		//jQuery('#loading_logo').fadeOut(500);
	    }
	); 
	if (step_ref==0) jQuery('#button_nav_left').hide();
	else jQuery('#button_nav_left').show();  
	if (step_ref==3) jQuery('#button_nav_right').hide();
	else jQuery('#button_nav_right').show();  
}

function setBackgroundStep(id) {    
    if(flash_enable) {
        getFlashMovie("background").sendStepToFlash(id);
    } else {
        jQuery("#background_pict").attr("src", backgounds[id].src);
        image_width = backgounds[id].width;
        image_height = backgounds[id].height;
        image_ratio = image_width/image_height;
    }
}    			


/************************************************************************************
*									En plus
************************************************************************************/

function px(x){
	return ''.concat(Math.round(x), 'px');
}


var voir_offre = function(numero) {
	jQuery.each(jQuery('.offre-description'), function(index, value) {
		if(jQuery(value).attr('id') != 'offre-'+numero.toString()) {
			jQuery(value).css('display', 'none');
		}
		else {
			jQuery(value).css('display', 'block');
		}
	});
}

