﻿//*************
if (top.location != self.location) {
    top.location = self.location.href;
}


//******************************

var isIE = false;
var isIE8 = false;
var isMozilla = false;
var isSafari = false;
var isOpera = false;
var isChroms = false;

var detect = navigator.userAgent.toLowerCase();
var browser;

if (checkIt('konqueror')) {
    browser = "Konqueror";
} else if (checkIt('safari')) {
    browser = "Safari";
    isSafari = true;
} else if (checkIt('omniweb')) {
    browser = "OmniWeb"
} else if (checkIt('opera')) {
    browser = "Opera";
    isOpera = true;
} else if (checkIt('webtv')) {
    browser = "WebTV"
} else if (checkIt('icab')) {
    browser = "iCab";
} else if (checkIt('msie')) {
    browser = "Internet Explorer";
    isIE = true;
} else if (!checkIt('compatible')) {
    browser = "Netscape Navigator"
    isMozilla = true;
} else if (checkIt('Chrome')) {
    browser = "Chrome";
    isChroms = true
} else {
    browser = "An unknown browser";
    isIE = true;
}

function checkIt(string) {
    place = detect.indexOf(string) + 1;
    return place;
}


//**********************************8

function getCoords(element) {
    var coords = { left: 0, top: 0, right: element.offsetWidth, bottom: element.offsetHeight };

    while (element) {
        coords.left += element.offsetLeft;
        coords.top += element.offsetTop;
        element = element.offsetParent;
    }
    coords.right += coords.left;
    return coords;
}

var MenuDelay = 500;

var Menutimer;

var isIE = !!window.ActiveXObject;
var isIE6 = isIE && !window.XMLHttpRequest;
var isIE8 = isIE && !!document.documentMode;
var isIE7 = isIE && !isIE6 && !isIE8;

function HideMenu(DivName) {
    //清除 Timer
    window.clearTimeout(Menutimer);

    ypSlideOutMenu.hideMenu(DivName);
}

function initypSlideOutMenu(ImgName, DivName, ADName, ItemCount, DefaultDivWidth, LeftAlign) {

    //清除 Timer
    window.clearTimeout(Menutimer);

    var oImg = document.getElementById(ImgName);
    var oDiv = document.getElementById(DivName + "Container");
    var oMenuTable = document.getElementById(DivName + "MenuTable");
    var oAD = document.getElementById(ADName);

    var ADHeight = 0;
    var ADWidth = 0;
    if (oImg == null) {
        //alert("找不到" + ImgName);
        return false;
    }
    if (oDiv == null) {
        //alert("找不到" + DivName + "Container");
        return false;
    }
    if (oAD) {
        var rectAD;
        if (isIE) {
            rectAD = oAD.getBoundingClientRect();
            ADHeight = rectAD.bottom - rectAD.top;
            ADWidth = rectAD.right - rectAD.left;
        } else if (isMozilla || isSafari || isOpera) {
            rectAD = getCoords(oAD);
            ADHeight = rectAD.bottom;
            ADWidth = rectAD.right - rectAD.left;
        }
    }

    var rectCate;
    var CateHeight = 0;
    if (isIE) {
        rectCate = oMenuTable.getBoundingClientRect();
        CateHeight = rectCate.bottom - rectCate.top;
        //alert(oDiv.offsetWidth + ":" + oMenuTable.offsetWidth);
    } else if (isMozilla || isSafari || isOpera) {
        rectCate = getCoords(oMenuTable);
        CateHeight = rectCate.bottom; // + 128;	
    }
    //var DivHeight = getDivHeight(ItemCount) + ADHeight;
    var DivHeight = CateHeight + 1;

    var DivLeft = 0;
    var DivTop = 0;

    var DivWidth = DefaultDivWidth;
    if (ADWidth > DefaultDivWidth) {
        DivWidth = ADWidth;
        oMenuTable.style.width = DivWidth + "px"; //Menu Table 的寬度也要重設
    }

    if (isIE) {
        var rect = oImg.getBoundingClientRect();
        if (LeftAlign == 'True') {
            DivLeft = rect.left - 1;
            if (isIE8) {
                //DivLeft += 2;
            }
        } else {
            DivLeft = rect.right - DivWidth - 4;
            if (isIE8) {
                //DivLeft += 4;
            }
        }
        DivTop = rect.bottom;
        if (isIE8) {
            //DivTop += 2;
        }

        //DivTop += document.documentElement.scrollTop;

        DivTop += GetScrollTop();

        //alert("document.documentElement.scrollTop:" +  document.documentElement.scrollTop);
        //alert("document.body.scrollTop:" +  document.body.scrollTop);
        //alert("GetScrollTop:" +  GetScrollTop());

    } else if (isMozilla) {
        var rect = oImg.getBoundingClientRect();
        if (LeftAlign == 'True') {
            DivLeft = rect.left - 2;
        } else {
            DivLeft = rect.right - DivWidth - 4;
        }
        DivTop = rect.bottom;
        DivLeft = DivLeft + 2;
        DivTop += document.documentElement.scrollTop;
    } else if (isSafari || isOpera) {
        var rect = getCoords(oImg);
        if (LeftAlign == 'True') {
            DivLeft = rect.left;
        } else {
            DivLeft = rect.right - DivWidth - 3;
        }
        DivTop = 159; //  rect.bottom + 72;
        //DivTop += document.body.scrollTop;
    }


    // 沒有物件時要初始化
    if (ypSlideOutMenu.Registry[DivName] == null) {
        //alert("left-" + DivLeft + " /top-" + DivTop + "/ width-" + (DivWidth + 2) + " / height-" + DivHeight);
        new ypSlideOutMenu(DivName, "down", DivLeft, DivTop, DivWidth + 2, DivHeight);

    }

    DivLeft += GetScrollLeft();

    oDiv.style.left = DivLeft + "px";

    Menutimer = window.setTimeout("ypSlideOutMenu.showMenu('" + DivName + "')", MenuDelay);
    //ypSlideOutMenu.showMenu(DivName);  
    //alert("left-" + DivLeft + " /top-" + DivTop + "/ width-" + (DivWidth + 2) + " / height-" + DivHeight);

}

function GetScrollTop() {
    // IE 很怪, 有時要用 document.documentElement.scrollTop, 有時要用 document.body.scrollTop.

    if (document.body.scrollTop > 0) {
        return document.body.scrollTop;
    }
    else {
        return document.documentElement.scrollTop;
    }
}

function GetScrollLeft() {
    // IE 很怪, 有時要用 document.documentElement.scrollTop, 有時要用 document.body.scrollTop.

    if (document.body.scrollLeft > 0) {
        return document.body.scrollLeft;
    }
    else {
        return document.documentElement.scrollLeft;
    }
}

function getDivHeight(ItemCount) {
    if (ItemCount == 0) {
        return 0;
    }
    if (ItemCount >= 9) {
        return 180 + 9;
    }
    return ItemCount * 20 + 9;
}

//**************************8

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-8001254-2']);
_gaq.push(['_trackPageview']);

(function () {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


//******************************

//var xhttp;
//var t = window.setTimeout("ReadService()", 2000);

function ReadService() {
//    xhttp = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
//    xhttp.onreadystatechange = BindData;

//    var tf = (new Date()).getTime();

//    xhttp.open("GET", "/tch/message/servicestatus.aspx?" + tf, true);
//    xhttp.send(null);
}

function BindData() {
//    if (xhttp.readyState == 4 && xhttp.status == 200) {
//        var xdoc = xhttp.responseXML;
//        var url = xdoc.selectSingleNode("Status/InviteFlash").text;
//        var width = xdoc.selectSingleNode("Status/InviteFlash").getAttribute('width');
//        var height = xdoc.selectSingleNode("Status/InviteFlash").getAttribute('height');
//        var play_time = xdoc.selectSingleNode("Status/InviteFlash").getAttribute('play_time');

//        if (url != '') PlayService(url, width, height, play_time)

//        t = window.setTimeout("ReadService()", 2000);
//    }
}
function PlayService(_url, _width, _height, _play_time) {
    var div = $("divService");
    div.style.top = document.body.scrollTop + 100;
    div.style.display = 'inline';
    //alert(_url);
    var so = new SWFObject(_url, "mymovie", _width, _height, "7", "transparent");
    so.addParam("Movie", _url);
    so.addParam("Src", _url);
    so.addParam("Loop", 1);
    so.addParam("Play", 1);
    so.addParam("WMode", "transparent");
    so.write("divService");

    setTimeout("StopService()", _play_time * 1000);
}
function StopService() {
    var div = $("divService");
    div.style.display = 'none';
    div.innerHTML = '';
}
function OpenDialog(_url) {
    window.open(_url, 'Dialog', 'height=480,width=540,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');
}
function ScrollService() {
    var div = $('divService');
    div.style.top = (document.body.scrollTop + 100) + "px";
    //alert(document.documentElement.scrollTop);
}


// **************
function gotoPage(pageurl) {
    window.location.href = '/Header_Stat.aspx?pageurl=' + escape(pageurl);
}

function RefreshCart() {
    $.ajax({
        url: "/tch/GetCartItemCount.aspx",
        async: false,
        type: "POST",
        data: ({
        }),
        dataType: "html",
        success: function (msg) {
            var oRes;
            try {
                oRes = eval(msg);
            } catch (e) {
                alert("Error Name: " + e.name + ". Error message: " + e.message);
            }

            if (oRes.success) {
                CartItemCounterOutIn(oRes.msg);
            } else {
                alert(oRes.msg);
            }
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    })    
}

function CartItemCounterOutIn(Counter) {
    $(".CartItemCounter").html(Counter);
    $(".CartItemCounterRefreshUnit a").css('color', 'red');
    $(".CartItemCounterRefreshUnit").fadeOut(
        500,
        function () {
            $(".CartItemCounterRefreshUnit").fadeIn(500,
                function () {
                    window.setTimeout("$('.CartItemCounterRefreshUnit a').css('color', '');", 2000);
                }
            );
        }
    );
}


//<DIV id=several class="CartItemCounterRefreshUnit"><A href="http://mpt1.shopunt.com.tw/tch/Shopping/Cart.aspx"><span class="CartItemCounter">#購物車商品數#</span>項商品</A></DIV>


function SetDefaultfancybox() {
    $(".iframe85").fancybox({
        'type': 'iframe',
        'autoScale': true,
        'width': '85%',  //如果要設定 px, 記得要用整數.
        'height': '85%',
        'transitionIn': 'none',
        'transitionOut': 'none'
    });

    $(".iframe95").fancybox({
        'type': 'iframe',
        'autoScale': true,
        'width': '95%',  //如果要設定 px, 記得要用整數.
        'height': '95%',
        'transitionIn': 'none',
        'transitionOut': 'none'
    });

    $(".iframe3x6").fancybox({
        'type': 'iframe',
        'autoScale': true,
        'width': 300,  //如果要設定 px, 記得要用整數.
        'height': 600,
        'transitionIn': 'none',
        'transitionOut': 'none'
    });
    $(".iframe6x3").fancybox({
        'type': 'iframe',
        'autoScale': true,
        'width': 600,  //如果要設定 px, 記得要用整數.
        'height': 300,
        'transitionIn': 'none',
        'transitionOut': 'none'
    });
    $(".iframe3x3").fancybox({
        'type': 'iframe',
        'autoScale': true,
        'width': 300,  //如果要設定 px, 記得要用整數.
        'height': 300,
        'transitionIn': 'none',
        'transitionOut': 'none'
    });
    $(".iframe6x6").fancybox({
        'type': 'iframe',
        'autoScale': true,
        'width': 600,  //如果要設定 px, 記得要用整數.
        'height': 600,
        'transitionIn': 'none',
        'transitionOut': 'none'
    });
    $(".iframe6x6NoClose").fancybox({
        'type': 'iframe',
        'autoScale': true,
        'width': 600,  //如果要設定 px, 記得要用整數.
        'height': 300,
        'transitionIn': 'none',
        'transitionOut': 'none',
        'showCloseButton': false,
        'hideOnOverlayClick': false
    });
}

$(document).ready(function () {
    SetDefaultfancybox();
})

function ShowUNTMessage(Title, Message) {
    var HTML = '<div style="background: url(/images/PopupHeadBG.jpg); width: 400px; height: 19px; padding: 8px 5px 4px 5px; color: #000; border-right: 1px solid #ff9933; border-left: 1px solid #ff9933; ' +
                    'color: #663300; font-family: Arial, Helvetica, sans-serif;">' +
                    '<img src="/images/PopupHeadICON.jpg" width="15" height="15" style="display: block; ' +
                        'float: left; margin-right: 5px;" alt="" />' +
                    '<span style="font-size: 11pt; font-weight: bold; display: block; width: 150px; float: left; ' +
                        'margin-top: 2px;">' + Title + '</span><img src="/images/PopupHeadCancel.jpg" width="20" ' +
                            'height="21" style="float: right; margin-top: -3px; cursor:pointer;" alt="" onclick="$.fancybox.close();" /></div>' +
                '<div style="background: #ffffff; width: 400px; border-bottom: 1px solid #ff9933; ' +
                    'border-right: 1px solid #ff9933; border-left: 1px solid #ff9933; padding: 10px; ' +
                    'line-height: 17pt; font-family: Arial, Helvetica, sans-serif; font-size: 9pt; '
    if ($.browser.msie) {
        HTML += 'width: 400px; " '
    }
    else {
        HTML += 'width: 390px; " '
    }
    HTML += 'id="divAlertBoxInnerMessage">' + Message +
                    '<div style="height: 27px;">' +
                        '<img src="/images/PopupConfirm.jpg" width="78" height="27" style="display: block; ' +
                            'float: right; cursor:pointer" alt="" onclick="$.fancybox.close();" /></div>' +
                '</div>';

    $.fancybox(
		                HTML,
		                {
		                    'width': 'auto',
		                    'height': 'auto',
		                    'modal': true,
		                    'padding': 0,
		                    'centerOnScroll': true,
		                    'overlayOpacity': 0.1,
		                    'transitionIn': 'none',
		                    'transitionOut': 'none',
		                    'speedIn': 0,
		                    'speedOut': 0
		                }
	                );
}
