var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

document.observe("dom:loaded", function() {
    initFilterMenu();
    //change images on mouseover:
    imgHover();
});

$j(document).ready(function(){

    // Adjust Home footer on resize and on load
    $j(window).bind('load resize', function() {
        adjustFooter();
    });

    // Hide (Collapse) the toggle containers on load
    $j("#form-search").hide();
    
    // Hide (Collapse) the promotion cart on load
    $j(".promotion-cart").hide();
    
    $j('a#discount-code').click(function() {
        $j('.promotion-cart').fadeIn("slow");
    });

    // Search SlideToggle on click
    $j("a#link-search").click(function (evt) {
        evt.stopPropagation();
        $j(this).toggleClass("menu-open");
        $j(this).next().slideToggle();
    });

    
    // Open quick login onclick
    $j(".header-account-title").click(function (evt) {
        evt.stopPropagation();
        $j(this).toggleClass("menu-open");
        $j('.header-account-inner').slideToggle();
    });
	
	// Open a newsletter lightbox for parameter ?newsletter=inscription
	if(location.search=='?newsletter=inscription/') {
	    Modalbox.show(('newsletter_subscribe'), {title: '', width: 600});
	};

    // toggle effect - product card description
    $j('#toggle-view li').click(function () {

        var text = $j(this).children('p');

        if (text.is(':hidden')) {
            text.slideDown('200');
            $j(this).children('span').addClass('moins');
        } else {
            text.slideUp('200');
            $j(this).children('span').removeClass('moins');
        }
		
    });
    
    
    // toogle menu left
    $j('.acc_trigger').next().hide(); //Hide/close all containers
    $j('.acc_trigger.open').next().show(); //Add "active" class to first trigger, then show/open the immediate next container
    //$j('.acc_trigger.open').next().show(); //Add "active" class to first trigger, then show/open the immediate next container

    //On Click
    $j('.acc_trigger').click(function(){
        if( $j(this).next().is(':hidden') ) { //If immediate next container is closed...
            $j('.acc_trigger').removeClass('open').next().slideUp(); //Remove all "active" state and slide up the immediate next container
            $j(this).addClass('open').next().slideDown(); //Add "active" state to clicked trigger and slide down the immediate next container
            }
        else{
        $j(this).removeClass('open').next().slideUp();   
        }
        return false; //Prevent the browser jump to the link anchor
    });
    
    
     
    //Error message address field length  
    if($j('.validate-street'))
    {
        $j('.validate-street').keyup(function(evt) {
            var streetValue = $j(this).val(); 
            $j(this).next('#address-error').remove();               
            if(streetValue.length >= 35 && $j('#address-error').length < 2 ) {
                $j(this).after("<span id='address-error'>Ce champs ne doit pas dépasser 35 caractères</span>");                 
                }
        });
    }
    
});


/*---------------------------------------------------------------------------*/
function imgHover() {

    var buttons = $$('#logout img, button.button img, .btn-proceed-checkout img, .btn-envoyer_projet input, .box-liens img, input.btn-validation, .parole-de-styliste img, .matiere-entretien img, button.button2 img');

    for(var i=0, images=[]; i<buttons.length; i++) {
        var newSrc = buttons[i].src.replace('.png', '-on.png');

        var j;
        var check = false;
        for(j=0; j<images.length; j++) {
            if(images[j].src == newSrc) {
                check = true;
                break;
            }
        }

        if(!check) {
            images.push(new Image());
            images.last().src = newSrc;
        }
    }

    var test = "";
    for(var i=0; i<images.length; i++) {
        images[i].onload = function() {
            for(j=0; j<buttons.length; j++) {
                origSrc = this.src.replace('-on', '');

                if(buttons[j].src == origSrc) {
                    if(buttons[j].parentNode.nodeName == 'BUTTON') {
                        Element.extend(buttons[j].parentNode);
                        buttons[j].parentNode.observe('mouseover', function(){
                            childImg = this.down('img');
                            if(childImg.src.indexOf("-on.png") == -1)
                                childImg.src = childImg.src.replace('.png', '-on.png');
                        });
                        buttons[j].parentNode.observe('mouseout', function(){
                            childImg = this.down('img');
                            childImg.src = childImg.src.replace('-on', '');
                        });

                    }
                    else {
                        buttons[j].observe('mouseover', function(){
                            if(this.src.indexOf("-on.png") == -1)
                                this.src = this.src.replace('.png', '-on.png');
                        });
                        buttons[j].observe('mouseout', function(){
                            this.src = this.src.replace('-on', '');
                        });
                    }
                }
            }
        }
        if(navigator.appName == "Microsoft Internet Explorer")
            images[i].src = images[i].src;
    }
}

/*---------------------------------------------------------------------------*/
function initFilterMenu() {

    $$('#narrow-by-list div dt').each(function(link){
        Event.observe(link,'click', function(event) {
            Event.stop(event);

            if(link.up().hasClassName('active')) {
                // console.log('active');
                link.up().removeClassName('active');
                link.adjacent('dd').first().fade({
                    duration: 0.3
                });

            }
            else {
                $$('#narrow-by-list div dd').each(function(i){
                    if(i.visible()) i.fade( {
                        duration: 0.3
                    } );
                });
                $$('#narrow-by-list div dl').each(function(i){
                    i.removeClassName('active');
                });

                link.up().addClassName('active');
                link.adjacent('dd').first().appear({
                    duration: 0.3
                });
            }

        });
    });
}

/*---------------------------------------------------------------------------*/

labelToInput = function() {
    var labels = $$('label.labelvalue');
    if(labels)
        labels.each(function(elem){        
            elem.hide(); 
            var txt = elem.innerHTML;
            var chpsTxt = elem.next('.input-box').down('input.input-text'); 
            
            if(!chpsTxt.value || chpsTxt.value == '')
                chpsTxt.value = txt;
            
            chpsTxt.observe('focus', function(e){if(e.target.value == txt) {e.target.value = '';}});
            chpsTxt.observe('blur', function(e){if(e.target.value == '') {e.target.value = txt;}});
        });
}

/*---------------------------------------------------------------------------*/

function adjustFooter()
{
    var requiredHeight = $j('.header-container:first').innerHeight() + $j('.menu:first').innerHeight() + 90;
    // console.log('requiredHeight', requiredHeight);
    // console.log('body:first', $j('body:first').innerHeight());

    if ($j('body:first').innerHeight()-140 < requiredHeight)
    {
        //console.log('youpi');
        $j('.footer-container:first').css('cssText', 'top: '+requiredHeight+'px !important');
    } else {
        $j('.footer-container:first').css('cssText', '');
    }
}

