



Utils = {};

Utils.isIE = function() {
    return navigator.appName.indexOf('Microsoft') > -1;
}

Utils.isIE7 = function() {
    return navigator.userAgent.indexOf("MSIE 7.") > -1;
}

Utils.getPosition = function(pixelPos) {
    return parseInt(pixelPos.replace("px", ""));
}

Utils.changeImage = function(id, imgSrc) {

    if ( !imgSrc.contains('$') ) {
        document.getElementById(id).src = imgSrc;
    }
}

Utils.swapBackground = function(src, id) {
    document.getElementById(id).style.background = "url(" + src + ")";
}

Utils.toggleShowLayer = function(id) {
    if (document.getElementById(id).style.display == 'none' || document.getElementById(id).style.display == '') {
        document.getElementById(id).style.display = 'block';
    } else {
        document.getElementById(id).style.display = 'none';
    }
}

Utils.showLayer = function(id) {
    if ( Utils.isIE ) {
        var selectArray = new Array();
        selectArray = document.getElementsByTagName("select");
        for( var i = 0 ; i < selectArray.length ; i++ ) {
            selectArray[i].style.visibility = "hidden";
        }
    }
    document.getElementById(id).style.display = "block";
}

Utils.hideLayer = function(id) {
    if ( Utils.isIE ) {
        var selectArray = new Array();
        selectArray = document.getElementsByTagName("select");
        for( var i = 0 ; i < selectArray.length ; i++ ) {
            selectArray[i].style.visibility = "visible";
        }
    }
    document.getElementById(id).style.display = "none";
}

Utils.clearInputBox = function(id) {
    if (document.getElementById(id).value == "your email") {
        document.getElementById(id).value = "";
    }                                                       
}

Utils.resetInputBox = function(id, string) {
    if (document.getElementById(id).value == "") {
        document.getElementById(id).value = string;
    }
}


Utils.getWindowWidth = function() {
    if (Utils.isIE()) {
        winW = document.body.offsetWidth;
    } else {
        winW = window.innerWidth;
    }
    return winW;
}

Utils.getWindowHeight = function() {
    if (Utils.isIE()) {
        winH = document.body.offsetHeight;
    } else {
        winH = window.innerHeight;
    }
    return winH
}

Utils.enableStyleSheet = function(name, on) {
    for (var i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
        if (a.getAttribute("rel") &&
            a.getAttribute("rel").indexOf("style") != -1 &&
            a.getAttribute("title")) {
            if (a.getAttribute("title") == name) {
                a.disabled = !on;
                Utils.createCookie(name, on, 365);
            }
        }
    }
}

Utils.createCookie = function (name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

Utils.readCookie = function (name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

Utils.enableStyleSheet("loRes", Utils.readCookie("loRes") != null && Utils.readCookie("loRes") == 'true');

Utils.validateEmailAddress = function(emailAddress) {
    emailAddress.trim();
    if (emailAddress.indexOf(' ') != -1) return false;
    var atPlacement = emailAddress.indexOf('@');
    if (atPlacement > 0) {
        if (emailAddress.indexOf('.', atPlacement) != -1) return true;
    }
    return false;
}

Utils.pureResponseSuccess = function(responseText, responseXML) {
    if (responseText == 'OK') {
        if (subscribePopup) subscribePopup.closePopup();
    } else {
        $('pureResponseError').innerHTML = '<p>'+responseText+'</p>';
    }
}

Utils.pureResponseFailure = function(xhr) {
    $('pureResponseError').innerHTML = '<p>'+xhr+'</p>';
}

Utils.submitPureSignUp = function() {
    if (!Utils.validateEmailAddress($('email').value)) {
        var errorStr = 'Invalid data... please re-enter';
        $('email').setStyles({
            'color' : 'red',
            'font-style' : 'italic'
        });
        $('email').value = errorStr;

        $('email').addEvent('click', function(e) {
            if ($('email').value = errorStr ) {
                $('email').value = '';
                $('email').setStyles({
                    'color' : '#000',
                    'font-style' : 'normal'
                });
            }

        });
        return false;
    }
    if (!pureRequest) {
        pureRequest = new Request({
            url: $('pureSignUpForm').action,
            method: 'post',
            onSuccess: Utils.pureResponseSuccess,
            onFailure: Utils.pureResponseFailure
        });
    } 
    pureRequest.send($('pureSignUpForm').toQueryString());
    return false;
}
menu = {};
menu.lastLevelOne = null;
menu.lastOver = null;

menu.mouseover = function(id) {
    if (menu.timer != null) {
        clearTimeout(menu.timer);
        menu.timer = null;
    }
    var elm = document.getElementById(id);
    if (menu.isLevel(elm, 1)) {
        if (menu.lastLevelOne != null && menu.lastLevelOne.id != elm.id) {
            menu.showHide(menu.lastLevelOne, false, true);
        }
        menu.lastLevelOne = elm;
    } else {
        if (menu.lastOver != null && menu.lastOver.id != elm.id) {
            menu.showHide(menu.lastOver, false, true);
        }
        menu.lastOver = elm;
    }
    menu.showHide(elm, true);
}

menu.isLevel = function(elm, level) {
    var levelelms = elm.id.split("_");
    if (levelelms.length > 1) {
        return levelelms[1] == '' + level;
    }
    return false;
}

menu.closeAll = function() {
    menu.timer = setTimeout(menu.closeAllTimed, 1000);
}

menu.closeAllTimed = function() {
    menu.showHide(document.getElementById('navigation'), false, true, true);
    menu.lastLevelOne = null;
    menu.lastOver = null;
}

menu.showHide = function(elm, show, cascade, excludeLevelOne) {
    var elms = elm.getElementsByTagName("ul");
    for (var i = 0; i < elms.length; i++) {
        if (excludeLevelOne && menu.isLevel(elms[i], 1)) {
            continue;
        }
        if (!Utils.isIE7() && Utils.isIE()) {
            elms[i].style.visibility = show ? 'visible' : 'hidden';
            elms[i].style.display = show ? 'block' : 'none';
        } else {
            elms[i].style.display = show ? 'block' : 'none';
        }
        if (cascade == null || !cascade) {
            break;
        }
    }
}

Fade = {};

browserName = navigator.appName;
isIE = function() {
    if (browserName == "Microsoft Internet Explorer") {
        return true;
    }
    return false;
}

Fade.max = (isIE()) ? 100 : 1.00;
Fade.inc = (isIE())? 3 : 0.02;
Fade.time = 40;
Fade.id = null;
Fade.fadeOutHandler = null;
Fade.fadeInHandler = null;

Fade.fadeOut = function(id) {
    clearTimeout(Fade.id);
    var elm = document.getElementById(id);
    var opac;
    if (isIE()) {
        if (!elm.filters[0]) {
            return;
        }
        opac = elm.filters[0].opacity;
    } else {
        if (elm.style.MozOpacity) {
            opac = elm.style.MozOpacity != '' ? parseFloat(elm.style.MozOpacity) : 0.8;
        } else {
            opac = elm.style.opacity != '' ? parseFloat(elm.style.opacity) : 0.8;
        }
    }

    if (opac > 0) {
        var val = 0;
        if (isIE()) {
            var tmpVal = (opac - Fade.inc);
            if (tmpVal > Fade.inc && tmpVal< 100) {
                val = tmpVal;
            }
            elm.filters[0].opacity = val;
        } else {
            var tmpVal = (parseFloat(opac) - Fade.inc);
            if (tmpVal > Fade.inc && tmpVal < 1) {
                val = tmpVal;
            }
            elm.style.MozOpacity = val;
            elm.style.opacity = val;
        }
        Fade.id = setTimeout("Fade.fadeOut('" + id + "')", Fade.time);
    } else {
        if (isIE()) {
            elm.filters[0].opacity = 0;
        } else {
            elm.style.MozOpacity = 0;
            elm.style.opacity = 0;
        }
        if (Fade.fadeOutHandler != null) {
            Fade.fadeOutHandler();
            Fade.fadeOutHandler = null;
        }
        if (elm.fadeLocation)
            window.location = elm.fadeLocation;
    }
}

Fade.fadeIn = function(id) {
    var done = false;
    clearTimeout(Fade.id);
    var elm = document.getElementById(id);
    if (isIE()) {
        if (!elm.filters[0]) {
            return;
        }
        opac = elm.filters[0].opacity;
    } else {
        if (elm.style.MozOpacity) {
            opac = (elm.style.MozOpacity != '' && elm.style.MozOpacity != 0) ? parseFloat(elm.style.MozOpacity) : 0;
        } else {
            opac = (elm.style.opacity != '' && elm.style.opacity != 0) ? parseFloat(elm.style.opacity) : 0;
        }
    }

    if (opac < Fade.max) {
        if (isIE()) {
            elm.filters[0].opacity = opac + Fade.inc;
        }
        else {
            var value = parseFloat(opac) + Fade.inc;
            elm.style.MozOpacity = value;

            elm.style.opacity = value;
        }

        Fade.id = setTimeout("Fade.fadeIn('" + id + "')", Fade.time);
    } else {
        if (elm.nextFadeId)
            setTimeout("Fade.fadeIn('" + elm.nextFadeId + "')", Fade.time);
        if(Fade.fadeInHandler != null){
            Fade.fadeInHandler();
            Fade.fadeInHandler = null;
        }
    }
}

Fade.reset = function(id) {
    var elm = document.getElementById(id);
    if (isIE()) {
        if (!elm.filters[0]) {
            return;
        }
        elm.filters[0].opacity = 0;
    }
    else {
        elm.style.MozOpacity = 0;
    }
}

//depends on fade.js

var SlideShow = {};

SlideShow.slideIds = [];
SlideShow.currentSlideNumber = 0;
SlideShow.timer = 100;

SlideShow.init = function() {
        SlideShow.fadeOutDiv()
}

SlideShow.fadeOutDiv = function() {
    if(SlideShow.slideIds.length>1) {
        Fade.fadeOutHandler = function () {
            SlideShow.changeDivDisplay();
        }
        setTimeout('Fade.fadeOut("ss")',SlideShow.timer);
    }
}

SlideShow.fadeInDiv = function() {
    Fade.fadeInHandler = function() {
        SlideShow.fadeOutDiv("ss");
    }
    Fade.fadeIn("ss");
}

SlideShow.changeDivDisplay = function() {

    var current = document.getElementById(SlideShow.slideIds[SlideShow.currentSlideNumber]);
    current.style.display = 'none';
    //Fade.reset(current.id);

    if(SlideShow.currentSlideNumber<(SlideShow.slideIds.length-1)) {
        SlideShow.currentSlideNumber++;
    } else {
        SlideShow.currentSlideNumber=0;
    }
    current = document.getElementById(SlideShow.slideIds[SlideShow.currentSlideNumber]);
    current.style.display = 'block';
    SlideShow.fadeInDiv();
}
var AutoSwiff = new Class({

    Extends : Swiff,

    initialize: function(path,options) {

        if (Browser.Plugins.Flash.version > 0 ) {
            this.parent(path,options);
            this.versionCheck(Browser.Plugins.Flash);
        } else {
            //Utils.showAllFlashAltContent(true);
        }

    },

    versionCheck : function() {

        var flashVersion = this.options.vars.version;

        if ( Browser.Plugins.Flash.version < flashVersion ) {

            var origSwf = this;

            var installSwf = new Swiff (origSwf.options.vars.expressinstall, {
                container: origSwf.options.container,
                width: origSwf.options.width,
                height: origSwf.options.height,

                params: {},

                vars: {}

            });

        }

    }

});

var Product = {

    pathToThumbnail : [],

    pathToThumbnailFaded : [],

    imageArray : [],

    selectedIndex : 0,

    swapProduct : function(index) {

        document.getElementById("currentProduct-" + Product.selectedIndex).style.display = "none";
        document.getElementById("actions-" + Product.selectedIndex).style.display = "none";
        //new one
        document.getElementById("currentProduct-" + index).style.display = "block";
        document.getElementById("actions-" + index).style.display = "block";

        Product.selectedIndex = index;

    },

    updateImage : function ( index ) {

        for ( var i = 0 ; i < Product.imageArray.length ; i++ ) {
            document.getElementById('imageFlashLink' + i ).style.display = "none";
        }

        document.getElementById('imageFlashLink' + index ).style.display = "block";

    },

    showFlash : function(index,articleId) {

        var so = new SWFObject('productViewer/productViewer.swf' ,'testFlash','740','700','8','#000000');
        so.addVariable("startPhoto",index);
        so.addVariable("articleId",articleId);
        document.getElementById('flashGallery').style.display = "block";
        so.write("flashGalleryInner");

    },

    closeFlash : function ( index, articleId ) {

        document.getElementById('flashGallery').style.display = "none";

    }

};
var TellAFriend = {

    validateForm : function () {

        var errorStr = "";

        if ( document.getElementById('yourName').value == "" ) errorStr += "<br />Please enter your name.";
        if ( !TellAFriend.validateEmail( document.getElementById('yourEmail').value ) )  errorStr += "<br />Please enter your valid email address.";
        if ( document.getElementById('friendsName').value == "" ) errorStr += "<br />Please enter your friend's name.";
        if ( !TellAFriend.validateEmail( document.getElementById('friendsEmail').value ) ) errorStr += "<br />Please enter your friend's valid address.";

        if (errorStr != "") {
            document.getElementById('validationErrors').innerHTML = errorStr;
            return false;
        } else {
            document.getElementById('tellafriend').submit();
            return true;
        }

    },

    validateEmail : function(emailAddress) {

        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        return emailPattern.test(emailAddress);

    }

}
var Popup = new Class({

    Implements: [Options,Events],

    options : {

        wrapperClass : 'popupContainer',
        wrapperId : 'popupWrapper',
        insertDivAfter : 'footer',
        containerDiv : '',
        requestUrl : '',
        activateButtonId: '',
        closeButtonId: ''


    },

    initialize : function (options) {

        this.setOptions(options);
        this.setActivateButton();


    },

    setActivateButton : function() {

        this.activateBtn = $(this.options.activateButtonId);
        this.setActivateBtnEvent();

    },

    setActivateBtnEvent : function() {

        var popupObj = this;

        popupObj.activateBtn.addEvent('click', function(e) {
            e.stop();
            popupObj.requestHandler();

        });

    },

    createTween : function () {

        var popupObj = this;

        this.wrapper.set('tween', {
            property: 'opacity',
            duration: 'short',
            onComplete: popupObj.tweenComplete.bind(this)
        });

    },

    tweenComplete : function () {

        if ( this._visible ) {
            this.visibleInComplete();
        } else {
            this.visibleOutComplete();
        }

    },

    visibleInComplete : function () {

    },

    visibleOutComplete  : function () {

    },

    requestHandler : function () {

        if ( this.wrapper != undefined && this.wrapper.getChildren.length > 0 ) {

            this.fadeInEffect(true);

        } else {

            this.insertWrapperElm();

        }

    },

    insertWrapperElm : function () {

        var popupObj = this;

        var wrapper = new Element('div', {
            'class': popupObj.options.wrapperClass,
            'id' : popupObj.options.wrapperId
        });

        wrapper.inject(popupObj.options.insertDivAfter,'after');

        this.wrapper = wrapper;

        this.requestElm();

    },

    requestElm : function() {

        var popupObj = this;

        var req = new Request.HTML({

            url: popupObj.options.requestUrl,
            evalScripts: true,
            evalResponse: true,

            onSuccess: this.responseRequestHandler.bind(this),

            onFailure: function() {
                popupObj.wrapper.set('text', 'The request failed.');
            }

        });

        req.send();
        
    },

    responseRequestHandler : function (responseTree) {
        //responseTree, responseElements, responseHTML, responseJavaScript

        this.createTween();
        this.wrapper.empty();
        this.wrapper.adopt(responseTree);
        this.fadeInEffect(true);
        this.setUpCloseBtn();

    },

    fadeInEffect : function ( directionIn ) {

        if ( directionIn ) {

            this._visible = true;
            this.wrapper.get('tween').start(0,1);

        } else {

            this._visible = false;
            this.wrapper.get('tween').start(1,0);

        }

    },

    setUpCloseBtn : function() {

        this.closeBtn = $(this.options.closeButtonId);
        this.setUpCloseBtnEvent();

    },

    setUpCloseBtnEvent : function () {

        var popupObj = this;

        this.closeBtn.addEvent('click', function(e) {
            e.stop();
            popupObj.closePopup();
        });

    },

    closePopup : function() {

        this.fadeInEffect(false);

    }


});
var SubscriptionForm = new Class({

    Extends : Popup,

    Implements: [Options,Events],

    options : {

        firstField : 'email'

    },

    initialize : function (options) {
        this.parent(options);
    },

    visibleInComplete : function () {
        $('pureSignUpForm').addEvent('submit',Utils.submitPureSignUp);
        $(this.options.firstField).focus();
    }

});
var FlashPopup = new Class({

    Extends : Popup,

    Implements: [Options,Events],

    options : {

        containerDiv : 'flashPopup',
        closeButtonId: 'flashPopupClose',
        swfPath: '',
        container: 'flashContainer',
        width: 800,
        height: 480,
        params: {
            wmode: 'opaque'
        },
        vars: {
            expressinstall : "swf/expressInstall.swf",
            version : "8"
        }

    },

    initialize : function (options) {
        this.parent(options);
    },

    requestHandler : function () {

        this.parent();

    },

    responseRequestHandler : function (responseTree) {
        this.parent(responseTree);
        this.setUpFlash();
    },

    setUpFlash : function() {

        var popupObj = this;

        var swiff = new AutoSwiff(popupObj.options.swfPath, {
            container: $(popupObj.options.container),
            width: popupObj.options.width,
            height: popupObj.options.height,
            params: popupObj.options.params,
            vars: popupObj.options.vars
        });

        this.swiff = swiff;

        // console.log("Swiff" + swiff);
        // console.log(swiff)
        // console.log(this.swiff)

    },

    closePopup : function () {

        this.wrapper.dispose();
        this.wrapper = undefined;

    }

});
var VideoPopup = new Class({

    Extends : FlashPopup,

    Implements: [Options,Events],

    options : {

        swfPath: 'video_800.swf',

        vars: {
            videoPath : "",
            expressinstall : "swf/expressInstall.swf",
            version : "8"
        }

    },

    initialize : function (options) {
        this.parent(options);
    }


});
var subscribePopup;
var pureRequest;

window.addEvent('domready', function() {

    subscribePopup = new SubscriptionForm({
        containerDiv : 'lwuniverse',
        requestUrl : 'include/signupForm.jsp',
        activateButtonId: 'newsLetterForm',
        closeButtonId: 'lwuniverseClose'
    });

});

