// Preload images - nav needs to load first
menuImage = new Array(
	'../images/principais/img16over.jpg',
	'../images/principais/img15over.jpg',
	'../images/principais/img14over.jpg',
	'../images/principais/img13over.jpg',
	'../images/principais/img12over.jpg',
	'../images/principais/img11over.jpg',
	'../images/principais/img10over.jpg',
	'../images/principais/img09over.jpg',
	'../images/principais/img08over.jpg',
	'../images/principais/img07over.jpg',
	'../images/principais/img06over.jpg',
	'../images/principais/img05over.jpg',
	'../images/principais/img04over.jpg',
	'../images/principais/img03over.jpg',
	'../images/principais/img02over.jpg',
	'../images/principais/img01over.jpg',
	'../images/menu/topnav/btn_educacao-over.gif',
	'../images/menu/topnav/btn_franquia-over.gif',
	'../images/menu/topnav/btn_helpdesk-over.gif',
	'../images/menu/topnav/btn_inicio-over.gif',
	'../images/menu/topnav/btn_internet-over.gif',
	'../images/menu/topnav/btn_loja-over.gif',
	'../images/menu/topnav/btn_novidades-over.gif',
	'../images/menu/topnav/btn_ouvidoria-over.gif',
	'../images/menu/topnav/btn_tv-over.gif',
	'../images/menu/topnav/ico_abertura-over.gif'	
);
imageLoad = new Array(18);
for (var i=0, img; img = menuImage[i]; i++) {
    img = new Image();
    img.src = menuImage[i];
}

DELAY = null;
INCREMENT = 20;         // time before resizing
SIZE = 950;
OFFSET = CURRENTLEFT = FINALLEFT = TOTALDISTANCE = STEPLEFT = 0;
SCROLLERDIV = null;
PRODUCTDIVS = null;
CURRENTPROD = 'franquia';
STYLING = new Array();

window.onload = function() {
    if (document.getElementsByTagName) {
        SCROLLERDIV = document.getElementById('scroller');
        PRODUCTDIVS = SCROLLERDIV.getElementsByTagName('div');
        CURRENTPROD = PRODUCTDIVS[0].id;
        j = 0;
        for (var i=PRODUCTDIVS.length-1, divvies; divvies = PRODUCTDIVS[i]; i--) {
            if (divvies.id == 'last') break;
            STYLING[j++] = divvies.id;
        }
        links = document.getElementsByTagName('a');
        for (var i=0, linkHandle; linkHandle = links[i]; i++) {
            if (linkHandle.href.indexOf('#') != -1) {
                linkHandle.onclick = handleLinks;
            } else if (linkHandle.href.indexOf('item') != -1) {
                linkHandle.onclick = callResults;
            }
        }
        transparentNav();
        sortPages(1);
        menuText();
        setButtons();
        promo();
    }
}

window.onresize = function() {
    if (TOTALDISTANCE == 0) {
        sortPages(0);
    }
    promo();
}

function handleLinks() {
    prodId = this.href.split('#');
    scrollPage(prodId[1]);
    return false;
}

function sortPages(resetPage) {
    if (resetPage) {
        for (var i=0, stylingPage; stylingPage = STYLING[i]; i++) {
            SCROLLERDIV.insertBefore(document.getElementById(stylingPage),SCROLLERDIV.firstChild);
        }
    }
    setLeft();
    SCROLLERDIV.style.left = FINALLEFT + 'px';
    CURRENTLEFT = FINALLEFT;
    FINALLEFT = 0;
}

function pageSize() {
    browserWidth = screen.width;
    browserHeight = screen.height;
    if (window.innerWidth != null) {
        browserWidth = window.innerWidth;
        browserHeight = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        browserWidth = document.documentElement.clientWidth;
        browserHeight = document.documentElement.clientHeight;
    } else if (document.body != null) {
        browserWidth = document.body.clientWidth;
        browserHeight = document.body.clientHeight;
    }
    return [browserWidth,browserHeight];
}

function setLeft() {
    pageDimensions = pageSize();
    OFFSET = pageDimensions[0] - SIZE;
    SCROLLERDIV.style.height = pageDimensions[1] - 20 + 'px';

    for(var i=1, divvies; divvies = PRODUCTDIVS[i]; i++) {
        if (divvies.id == CURRENTPROD) {
            if (divvies.className == 'styling') {
                FINALLEFT = -((i * SIZE) - OFFSET);
            } else if (divvies.id == 'franquia') {
                FINALLEFT = -((i * SIZE) - (OFFSET/2));
            } else {
                FINALLEFT = -(i * SIZE);
            }
        }
    }
}

function scrollPage(productName) {
    CURRENTPROD = productName;
    setMenuBar(document.getElementById('t-'+CURRENTPROD));
    setButtons();
    setLeft();
    slide();
}

function slide() {
    clearTimeout(DELAY);
    TOTALDISTANCE = FINALLEFT - CURRENTLEFT;
    STEPLEFT = TOTALDISTANCE / 10;
    minspeed = 1;
    if (STEPLEFT < minspeed && STEPLEFT > -minspeed) {
        STEPLEFT = (STEPLEFT < 0 ? -minspeed : minspeed);
    }
    if (TOTALDISTANCE > 1 || TOTALDISTANCE < -1) {
        CURRENTLEFT += STEPLEFT;
        DELAY = setTimeout('slide()',INCREMENT);
    } else {
        TOTALDISTANCE = 0;
        CURRENTLEFT = FINALLEFT;
        sortPages(0);
        promo();
    }
    SCROLLERDIV.style.left = CURRENTLEFT + 'px';
}

function menuText() {
    menuLinks = document.getElementById('topmenu').getElementsByTagName('a');
    for (var i=0, menuSection; menuSection = menuLinks[i]; i++) {
        menuSection.onmouseover = function() {
            setMenuBar(this);
        }
        menuSection.onmouseout = function() {
            setMenuBar(document.getElementById('t-'+CURRENTPROD));
        }
    }
}

function setMenuBar(obj) {
    menuLinks = document.getElementById('topmenu').getElementsByTagName('a');
    for (var i=0, menuSection; menuSection = menuLinks[i]; i++) {
        if (menuSection.className = 'active') {
            menuSection.className = '';
        }
    }
    menuDesc = document.getElementById('descriptor');
    activeSection = '';
    if (obj) {
        titleText = obj.title;
        if (titleText.indexOf('Dual') != -1) {
            activeSection = 'dual';
        } else if (titleText.indexOf('deep') != -1) {
            activeSection = 'intensive';
        } else if (titleText.indexOf('leave-in') != -1) {
            activeSection = 'leave';
        } else if (titleText.indexOf('shampoo') != -1) {
            activeSection = 'shampoo';
        }
        obj.className = 'active';
        menuDesc.innerHTML = titleText;
    } else {
        menuDesc.innerHTML = '&nbsp;';
    }
    document.getElementById('t-dual').className = (activeSection == 'dual' ? 'live' : '');
    document.getElementById('t-intensive').className = (activeSection == 'intensive' ? 'live' : '');
    document.getElementById('t-leave').className = (activeSection == 'leave' ? 'live' : '');
    document.getElementById('t-shampoo').className = (activeSection == 'shampoo' ? 'live' : '');
}

function setButtons() {
    document.getElementById('promo').style.display = 'none';
    document.getElementById('promo-cont').style.display = 'none';
    listItems = document.getElementsByTagName('li');
    j = 0;
    for (var i=0, listSection; listSection = listItems[i]; i++) {
        if (listSection.className == 'top-buttons') {
            if (listSection.style.position != 'fixed') {
                listSection.style.position = 'fixed';
                listSection.style.left = '578px';
                listSection.style.top = '155px';
            }
            listSection.style.display = (listSection.id == 'buttons-' + CURRENTPROD ? 'block' : 'none');
        }
    }
}


function transparentNav() {
    var userAgent = navigator.userAgent.toLowerCase();
    leftNav = document.getElementById('leftnav');
    if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox') != -1) {
        leftNav.style.background = 'url(/images/macff.png)';
    } else {
        leftNav.style.backgroundColor = '#fff';
        leftNav.style.opacity = '0.95';
        leftNav.style.filter = 'Alpha(opacity=95);';
    }
}

function promo() {
    var content, type, promoWidth, promoHeight, promoTop, promoLeft, flashDiv;
    if (CURRENTPROD == 'minute-miracle') {
        content = 'book.swf';
        type = 'swf';
        promoWidth = 1050;
        promoHeight = 320;
        promoTop = 330;
        promoLeft = -30;
    } else if (CURRENTPROD == 'mega-instant') {
        content = 'kangaroo.swf';
        type = 'swf';
        promoWidth = 500;
        promoHeight = 300;
        promoTop = 327;
        promoLeft = 575;
    } else if (CURRENTPROD == 'colour-intensive') {
        content = 'gadget.swf';
        type = 'swf';
        promoWidth = 195;
        promoHeight = 236;
        promoTop = 325;
        promoLeft = 525;
    } else if (CURRENTPROD == 'dual-serum') {
        content = 'feel-fabulous.swf';
        type = 'swf';
        promoWidth = 550;
        promoHeight = 400;
        promoTop = 278;
        promoLeft = OFFSET + 450;
    } else if (CURRENTPROD == 'dual-spray') {
        content = 'promo-cont';
        type = 'html';
        promoWidth = 289;
        promoHeight = 403;
        promoTop = 260;
        promoLeft = OFFSET + 500;
    }
    if (content) {
        if (type == 'swf') {
            flashDiv = document.getElementById('promo');
            var so = new SWFObject('/promos/'+content,'',promoWidth,promoHeight,8,'#ffffff');
            so.addParam('wmode','transparent');
            so.write(flashDiv);
        } else {
            flashDiv = document.getElementById(content);
        }
        flashDiv.style.width = promoWidth + 'px';
        flashDiv.style.height = promoHeight + 'px';
        flashDiv.style.top = promoTop + 'px';
        flashDiv.style.left = promoLeft + 'px';
        flashDiv.style.display = 'block';
    }
}

http = null;

function generateRequest() {
    http = null;
    try {
        http = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try {
            http = new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e) {
        }
    }
    if (!http && typeof XMLHttpRequest != 'undefined') {
        http = new XMLHttpRequest();
    }
    return http;
}

function callResults() {
    http = generateRequest();
    if (0 && http) { // need a way of calling the page dynamically for it to work
        rating = this.href.split('&');
        http.open('GET','/promos/hot-result.jsp?' + split[1]);
        http.send(null);
        http.onreadystatechange = function() {
            if (http.readyState != 4) return;
            document.getElementById('result-section').innerHTML = http.responseText;
            http = null;
        }
        return false;
    } else {
        return true;
    }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}