﻿// JavaScript Document
addDomLoad(function() {
    if ($("txtKey") != null) {
        EventUtil.addEventHandler($("txtKey"), "keydown", SearchBefore);
    }
    if ($("btnsearch") != null) {
        EventUtil.addEventHandler($("btnsearch"), "click", Search);
    }
    if ($("btnZoom") != null) {
        EventUtil.addEventHandler($("btnZoom"), "click", quickContactFmTanggle);
    }
    if ($("txaMeg") != null) {
        EventUtil.addEventHandler($("txaMeg"), "focus", function() {
            $('quickContactFm').style.width = '470px';
            $('quickContactFm').style.height = '275px';
            $('txaMeg').style.height = '160px';
            $('txaMeg').style.width = '455px';
            $('windowBgFm').style.width = '472px';
            $('windowBgFm').style.height = '277px';
            $('ddSend').style.display = 'block';
            $("btnZoom").style.backgroundPosition = '0 -12px';
            $('fontCount').style.display = 'block';
            if ($('txaMeg').value == 'Click here to contact this company') {
                $('txaMeg').value = '';
            }
        });
    }
})

var _activeMenu, Class = { create: function() { return function() { this.init.apply(this, arguments) } } },
	aEvent = EventUtil.addEventHandler, rEvent = EventUtil.removeEventHandler,
	productViewer = Class.create();

//left link check set
function leftLinkCheck(obj, li) {
    obj = $(obj);
    if (obj.parentNode.className == 'leftTitleLink') {
        obj.className = 'title check';
    } else {
        obj.className = 'check';
        showList($(li));
    }
}

//contact window action
function quickContactFmTanggle() {
    if ($('ddSend').style.display == 'none') {
        $('quickContactFm').style.width = '470px';
        $('quickContactFm').style.height = '275px';
        $('windowBgFm').style.width = '472px';
        $('windowBgFm').style.height = '277px';
        $('txaMeg').style.width = '455px';
        $('txaMeg').style.height = '160px';
        $('ddSend').style.display = 'block';
        $('fontCount').style.display = 'block';
        $("btnZoom").style.backgroundPosition = '0 -12px';
    } else {
        $('quickContactFm').style.width = '165px';
        $('quickContactFm').style.height = '118px';
        $('txaMeg').style.width = '149px';
        $('txaMeg').style.height = '60px';
        $('windowBgFm').style.width = '167px';
        $('windowBgFm').style.height = '120px';
        $('ddSend').style.display = 'none';
        $('fontCount').style.display = 'none';
        $("btnZoom").style.backgroundPosition = '0 0';
    }
    if ($('txaMeg').value == 'Click here to contact this company') {
        $('txaMeg').value = '';
    }
}

function showList(_this) {
    var list = Public.Dom.getNextElement(_this.parentNode);
    list.style.display = list.style.display == 'block' ? 'none' : 'block';
    _this.parentNode.className = _this.parentNode.className == 'title down' ? 'title' : 'title down';

}

function activeLi(liActive, arg) {
    if (!arg) {
        _activeMenu = Public.Dom.getNextElement(liActive);
        Public.Dom.getNextElement(liActive).className = 'liChildShow';
    } liActive.className = 'active';
}

function SearchBefore() {
    if (EventUtil.getEvent().keyCode == 13)
        Search();
}

function Search() {
    var Keywords = $("txtKey").value.Trim();

    if (Keywords.IsNullOrEmpty())
        return;
    else if (Keywords.IsChinese()) {
        alert("Input characters contains Chinese characters");
        return;
    }
    else if (Keywords.IsHtml()) {
        alert("The character of input contains the illegal character");
        return;
    }
    else {
        var Url = "";
        Url += "key=" + DisposeKeyWords(Keywords);
        window.location.href = "/search?" + Url;
    }
}

function DisposeKeyWords(var1) {
    var regex = /(\s)+/g;
    return var1.replace(regex, function() { return "_" });
}

var Tween = {

    Expo: {
        easeIn: function(t, b, c, d) {
            return (t == 0) ? b : c * Math.pow(2, 10 * (t / d - 1)) + b;
        },
        easeOut: function(t, b, c, d) {
            return (t == d) ? b + c : c * (-Math.pow(2, -10 * t / d) + 1) + b;
        },
        easeInOut: function(t, b, c, d) {
            if (t == 0) return b;
            if (t == d) return b + c;
            if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;
            return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;
        }
    },


    Back: {
        easeIn: function(t, b, c, d, s) {
            if (s == undefined) s = 1.70158;
            return c * (t /= d) * t * ((s + 1) * t - s) + b;
        },
        easeOut: function(t, b, c, d, s) {
            if (s == undefined) s = 1.70158;
            return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
        },
        easeInOut: function(t, b, c, d, s) {
            if (s == undefined) s = 1.70158;
            if ((t /= d / 2) < 1) return c / 2 * (t * t * (((s *= (1.525)) + 1) * t - s)) + b;
            return c / 2 * ((t -= 2) * t * (((s *= (1.525)) + 1) * t + s) + 2) + b;
        }
    },

    effect: function(t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; return c / 2 * ((t -= 2) * t * t + 2) + b }

}


productViewer.prototype = {
    init: function(arg) {
        this.build(arg)
    },
    build: function(p) {
        var _this = this;
        var c = this.c = {
            effect: p.effect || Tween.effect,
            itemSize: p.itemSize,
            itemCount: parseInt(p.itemCount, 10) || 0,
            itemGroup: parseInt(p.itemGroup, 10) || 0,
            step: parseInt(p.step, 10) || 0,
            iDefault: p.iDefault,
            delayRun: parseInt(p.delayRun, 10) || 20,
            etype: p.etype || 'click',
            urlList: p.urlList || [],
            checkImg: p.checkImg || true,
            imgList: null,
            reloadImgNum: 0
        };
        if (p.scrolls) {
            var s = $(p.scrolls);
            this.c.imgList = s.getElementsByTagName('img');
            if (this.c.checkImg) {
                this.c.iDefault = this.getIndex(s.getElementsByTagName('li'), $(this.c.iDefault)) + 1 || 0;
                this.c.reloadImgNum = this.c.iDefault;
            } else {
                this.c.reloadImgNum = this.c.iDefault = 0;
            }
            if (s && s.nodeType == 1) this.s = s; else return;
        } else return;

        if (p.btnPreId) {
            var pre = $(p.btnPreId);
            if (pre && pre.nodeType == 1) this.btnPre = pre; else return;
        } else return;

        if (p.btnNextId) {
            var next = $(p.btnNextId);
            if (next && next.nodeType == 1) this.btnNext = next; else return;
        } else return;


        this.thread = true;

        this.c.scrWidth = c.itemSize[0] * c.step;

        s.getElementsByTagName('ul')[0].style.width = _this.c.itemCount * _this.c.itemSize[0] + 'px';

        try {
            if (c.iDefault > 0) {
                setTimeout(function() {
                    s.scrollLeft = (c.iDefault - 3) * _this.c.itemSize[0];
                }, 100);
                s.getElementsByTagName('ul')[0].getElementsByTagName('li')[c.iDefault - 1].className = 'check';
            }
            _this.checkBtnIco();
            _this.reloadImg(c.imgList, c.reloadImgNum, 9);
        } catch (e) { }

        this.maxScrWidth = c.itemSize[0] * (c.itemCount - c.itemGroup);

        aEvent(this.btnNext, c.etype, this.display(1));
        aEvent(this.btnPre, c.etype, this.display(3));

    },

    display: function(direct) {
        var _this = this;
        return function() {
            if (!_this.thread) return;
            _this.toggle(direct);
        }
    },

    toggle: function(direct) {
        this.thread = false;
        var s = this.s, scrWidth = this.c.scrWidth, maxScr = this.maxScrWidth, _this = this, effect = this.c.effect;
        _this.c.reloadImgNum = direct == 1 ? _this.c.reloadImgNum + 5 : _this.c.reloadImgNum - 5;
        var b = s.scrollLeft, c = direct == 1 ? scrWidth : -scrWidth, d = this.c.delayRun, t = 0;
        if (b >= maxScr && c > 0) { this.thread = true; return; }
        (function Run() {
            s.scrollLeft = Math.ceil(effect(t, b, c, d));
            if (t < d) { t++; _this.clearRun = setTimeout(Run, 10) }
            else if (t == d) {
                _this.thread = true;
                _this.checkBtnIco();
                _this.reloadImg(_this.c.imgList, _this.c.reloadImgNum, 9);

            }
        })()
    },
    checkBtnIco: function() {
        var direct = 1;
        var s = this.s, scrWidth = this.c.scrWidth, maxScr = parseInt(this.s.getElementsByTagName('ul')[0].style.width), _this = this;
        var b = s.scrollLeft, c = direct == 1 ? scrWidth : -scrWidth;
        if (Math.abs(s.scrollLeft - maxScr + scrWidth) < 100 || maxScr < scrWidth) {
            _this.btnNext.className = 'rightNotActive';
        } else {
            _this.btnNext.className = 'right';
        }
        if (s.scrollLeft == 0) {
            _this.btnPre.className = 'leftNotActive';
        } else {
            _this.btnPre.className = 'left';
        }
    },
    reloadImg: function(list, i, count) {
        var max = i + count > list.length ? list.length : i + count + 1;
        for (i = (i - count < 0 ? 0 : i - count); i < max; i++) {
            if (list[i] && /loader/.test(list[i].src.toString())) {
                try { list[i].src = this.c.urlList[i]; } catch (e) { }
            }
        }
    },
    getIndex: function(list, child) {
        for (i = 0; i < list.length; i++) {
            if (list[i] == child) return i;
        }
    }
}



function getDocScroll(doc) {
    doc = doc || document;
    return [Math.max(doc.documentElement.scrollLeft, doc.body.scrollLeft),
		Math.max(doc.documentElement.scrollTop, doc.body.scrollTop)];
}


function hideCpContent(content, trans) {

    var d = 30, t = 0, b = parseInt(content.style.top), c = -parseInt(content.style.top) - 388;
    trans.className = "transHide";
    (function Run() {
        content.style.top = Math.ceil(Tween.Expo.easeIn(t, b, c, d)) + "px";
        if (t < d) {
            t++; setTimeout(Run, 10)
        } else if (t == d) {
            content.style.display = 'none';
        }
    })()
}

function showCpContent(content, trans) {
    var dw = document.body.offsetWidth,
			dh = Public.Dom.getDocumentSize()[1],
			vh = Public.Dom.getDocumentViewHeight(),
			sro = getDocScroll();
    trans.className = "transShow";
    trans.style.width = dw + 'px';
    trans.style.height = dh + 'px';
    content.style.left = dw / 2 - 288 + 'px';
    content.style.display = 'block';

    var d = 30, t = 0, b = -content.offsetHeight, c = content.offsetHeight + sro[1] + vh / 2 - content.offsetHeight / 2;

    (function Run() {
        content.style.top = Math.ceil(Tween.Back.easeOut(t, b, c, d)) + "px";
        if (t < d) {
            t++; setTimeout(Run, 10)
        }
    })()
}

function Rating(loginType) {
    if (loginType != 1)
        window.location.href = "/login/?jumpto=" + location.href;

    var var4 = $("taContent").value.Trim();
    var obj4 = $("ContentMsg");
    var obj5 = $("ResultMsg");

    ShowMsg(obj5, "", false);

    if (var4.IsNullOrEmpty()) {
        ShowMsg(obj4, "Please enter Rating content.", true);
        return false;
    }
    else if (var4.IsChinese() || var4.IsHtml()) {
        ShowMsg(obj4, "Please enter english characters", true);
        return false;
    }
    else
        ShowMsg(obj4, "", false);

    ShowMsg(obj5, "Processing...", true);
    $("btnSubmit").disabled = "disabled";
}

function ShowMsg(obj, Msg, IsDisplay) {
    obj.innerHTML = Msg;
    if (IsDisplay)
        obj.style.display = "block";
    else
        obj.style.display = "none";
}

//居中
function checkCenter(obj) {
    for (i = 0; i < obj.length; i++) {
        o = obj[i];
        o.onload = function() {
            var parent = this.parentNode;
            this.style.top = (parent.clientHeight - this.clientHeight) / 2 + 'px';
            this.style.left = (parent.clientWidth - this.clientWidth) / 2 + 'px';
        }
    }
}

//Quick Inquiry
function QuickInquiry() {
    var varMessage = $("taMsg").value.Trim();
    if (varMessage.IsNullOrEmpty()) {
        alert("Please enter your message");
        return false;
    }
    else if (varMessage.IsChinese() || varMessage.IsHtml()) {
        alert("Please enter english characters only");
        return false;
    }
}

function QuickInquiryByWindow() {
    var varMessage = $("txaMeg").value.Trim();
    if (varMessage.IsNullOrEmpty()) {
        alert("Please enter your message");
        return false;
    }
    else if (varMessage.IsChinese() || varMessage.IsHtml()) {
        alert("Please enter english characters only");
        return false;
    }
}