﻿$(document).ready(function() {
    $.each($('div.btn'), function() {
        $(this).find("span.ui-icon").css("float", "left");
        $(this).removeClass('btn');
        $(this).addClass('ui-state-default ui-corner-all');
        $(this).hover(
            function() { $(this).addClass('ui-state-hover'); },
            function() { $(this).removeClass('ui-state-hover'); }
        );
    });

    $.each($('button.btn'), function() {
        $(this).find("span.ui-icon").css("float", "left");
        $(this).removeClass('btn');
        $(this).addClass('ui-state-default ui-corner-all');
        $(this).hover(
            function() { $(this).addClass('ui-state-hover'); },
            function() { $(this).removeClass('ui-state-hover'); }
        );
    });
});

function ShowAlertDialog(title, msg, cssClass, afterUrl) {
    setTimeout("ShowAlertDialog1('" + title + "','" + msg + "','" + cssClass + "','" + afterUrl + "');", 1);
}

function ShowAlertDialog1(title, msg, cssClass, afterUrl) {
    $("#msgDialog").dialog('destroy');
    $("#msgDialog").attr("title", title);
    $("#dialogContent").html(msg);
    $("#msgDialog").dialog({
        bgiframe: true,
        modal: true,
        resizable: false,
        dialogClass: cssClass,
        buttons: {
            "確定": function() {
                $(this).dialog('close');
                if (afterUrl != "")
                    window.location = afterUrl;
            }
        },
        close: function(event, ui) {
            if (afterUrl != "")
                window.location = afterUrl;
        }
    });
}

//搜尋GoogleMaps
function SearchGoogleMaps() {
    window.open("http://maps.google.com.tw/maps?hl=zh-TW&source=hp&q=" + encodeURI($("#txtLocation").val()) + "&um=1&ie=UTF-8");
}

//輪播氣象
function SequenceWeather() {
    var nextCityIndex = $('select.city').attr("selectedIndex") + 1;
    if (nextCityIndex > $('select.city').find("option").length) {
        nextCityIndex = 0;
    }
    $('select.city').attr("selectedIndex", nextCityIndex);
    GetWeather($('select.city :selected').text(), $('select.city :selected').val());
}

//取得氣象資訊
function GetWeather(city, latlon) {
    $.ajax({
        type: "GET",
        url: "./Service/SrvProxy.asmx/GetGoogleWeather",
        data: "latlon=" + latlon,
        dataType: "xml",
        success: function(data) {
            $("#forecast_conditions").html("");
            if ($(data).find("problem_cause").length == 0) {
                var forecast_date = new Date($(data).find("forecast_information > forecast_date").attr("data").replace(/-/g, "/"));
                $("#city").text(city);
                //$("#forecast_date").text(forecast_date.formatDate("yyyy/MM/dd"));
                $("#condition").text($(data).find("current_conditions > condition").attr("data"));
                $("#temp_c").text($(data).find("current_conditions > temp_c").attr("data") + " °C");
                var humidity = $(data).find("current_conditions > humidity").attr("data");
                $("#icon").html("<img src='http://www.google.com/" + $(data).find("current_conditions > icon").attr("data") + "' />");
                var i = 0;
                $(data).find("forecast_conditions").each(function() {
                    if (i == 0) {
                        var tmpDate = new Date($(data).find("forecast_information > forecast_date").attr("data").replace(/-/g, "/"));
                        var next_forecast_date = new Date(tmpDate.setDate(tmpDate.getDate() + i));
                        var day_of_week = $(this).find("day_of_week").attr("data");
                        var tmp_low = $(this).find("low").attr("data");
                        var tmp_high = $(this).find("high").attr("data");
                        var f_icon = $(this).find("icon").attr("data");
                        var f_condition = $(this).find("condition").attr("data");
                        $("#forecast_date").text(next_forecast_date.formatDate("yyyy/MM/dd") + " (" + day_of_week + ")");
                        //$("#forecast_conditions").append("<div><img src='http://www.google.com/" + f_icon + "' />" + next_forecast_date.formatDate("yyyy/MM/dd") + " (" + day_of_week + ")</div>");
                        $("#temp_c").text(tmp_low + "~" + tmp_high + " °C");
                        //$("#forecast_conditions").append("<div>" + tmp_low + "~" + tmp_high + " °C</div>");
                        //$("#forecast_conditions").append("<div>" + f_condition + "</div>");
                    }
                    i++;
                });
            }
        }
    });
}

//取得預測油價
function GetGasolineForecast() {
    $.ajax({
        type: "GET",
        url: "./Service/SrvProxy.asmx/GetGasolineForecast",
        dataType: "xml",
        success: function(data) {
            var html = "";
            var f_ron92 = parseFloat($(data).find("ADJPRICE").find("RON92").text());
            var f_diesel = parseFloat($(data).find("ADJPRICE").find("DIESEL").text());
            html += "<div style='float:left;'>";
            if (f_ron92 > 0) {
                html += "<div style='float:left;'><img src='App_Themes/Default/images/arrow_up.png' /></div>";
                html += "<div style='padding:16px 0px;float:left;color:red;font-size:32px;'>漲" + f_ron92 + "元</div>";

            }
            else {
                html += "<div style='float:left;'><img src='App_Themes/Default/images/arrow_dw.png' /></div>"
                html += "<div style='padding:16px 0px;float:left;color:green;font-size:32px;'>降" + Math.abs(f_ron92) + "元</div>";
            }
            html += "</div>";
            html += "<div style='clear:both;'>下週柴油每公升預計：";
            html += (f_diesel > 0) ? ("+" + f_diesel + "元") : (f_diesel + "元");
            html += "</div>";
            $("#f_price").html(html);
            $("#cpc92").text($(data).find("CPCRETAILPRICE").find("RON92").text());
            $("#cpc95").text($(data).find("CPCRETAILPRICE").find("RON95").text());
            $("#cpc98").text($(data).find("CPCRETAILPRICE").find("RON98").text());
            $("#cpcdie").text($(data).find("CPCRETAILPRICE").find("DIESEL").text());
        }
    });
} 

//設定版位
function SetShowCase(blockID, caseID, rootCate, blockCssClass, showTime, showGoodsName, showPrice, imgSize, imgWidth, imgHeight) {
    $.ajax({
        async: false,
        type: "POST",
        url: "./Service/SrvProxy.asmx/GetShowCase",
        data: "argMemberSerNo=&argCaseID=" + caseID + "&argRootCat=" + rootCate,
        success: function(xml) {
            ParseShowCaseXML(xml, blockID, blockCssClass, showTime, showGoodsName, showPrice, imgSize, imgWidth, imgHeight);
        }
    });
}

//解析版位XML
function ParseShowCaseXML(xml, blockID, blockCssClass, showTime, showGoodsName, showPrice, imgSize, imgWidth, imgHeight) {
    var html = "";
    var showType = $(xml).find("SHOWTYPE").text();
    switch (showType) {
        case "1":
            //廣告
            $(xml).find("ADLIST").each(function() {
                $("#" + blockID).show();
                var $adList = $(this);
                var adType = $adList.find("ADTYPE").text();
                var adTitle = $adList.find("TITLE").text();
                var adDesc = $adList.find("DESC").text();
                var subTitle = $adList.find("SUBTITLE1").text();
                var imgDefault = $adList.find("DEFAULTIMG").text();
                var imgOnMouse = $adList.find("ONMOUSEIMG").text();
                var linkURL = $adList.find("LINKURL").text();
                var linkTarget = ($adList.find("TARGET").text() == "N") ? "_blank" : "_self";

                switch (adType) {
                    case "F":
                        //Flash
                        html = "<div class='" + blockCssClass + "'>";
                        html += "<script type='text/javascript'>";
                        html += "AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','" + imgWidth + "','height','" + imgHeight + "','src','http://benefit.ilohas24.com.tw/Backend/AdsImg/" + imgDefault + "','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','http://benefit.ilohas24.com.tw/Backend/AdsImg/" + imgDefault + "' );";
                        html += "</ script>";
                        html += "<noscript>";
                        html = "<div style='" + showStyle + "'>";
                        html += "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0' width='" + imgWidth + "' height='" + imgHeight + "'>";
                        html += "<param name='movie' value='http://benefit.ilohas24.com.tw/Backend/AdsImg/" + imgDefault + "' />";
                        html += "<param name='quality' value='high' />";
                        html += "<embed src='http://benefit.ilohas24.com.tw/Backend/AdsImg/" + imgDefault + "' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='" + imgWidth + "' height='" + imgHeight + "'></embed>";
                        html += "</object>";
                        html += "</div>";
                        html += "</noscript>";
                        html += "</div>";
                        break;
                    case "T":
                        //純文字
                        html = "<div class='" + blockCssClass + "'>";
                        html += "<div>";
                        html += "<a href='" + linkURL + "' target='" + linkTarget + "'>" + adTitle + "</a>";
                        html += "</div>";
                        html += "</div>";
                    case "V":
                        //Flv
                        break;
                    default:
                        html = "<div class='" + blockCssClass + "'>";
                        html += "<div><a href='" + linkURL + "' target='" + linkTarget + "'><img src='" + imgDefault + "' border='0' alt='" + adTitle + "' width='" + imgWidth + "' height='" + imgHeight + "' /></a></div>";
                        if (showGoodsName) {
                            html += "<div class='tt01' style='text-align:center;'>" + adTitle + "</div>";
                            html += "<div class='tt02'>" + subTitle + "</div>";
                            html += "<div class='chdesc'><a href='" + linkURL + "' target='" + linkTarget + "'>" + adDesc + "</a></div>";
                        }
                        html += "</div>";
                        break;
                }
                if (blockID == "head_block1")
                    $("#" + blockID).html(html);
                else
                    $("#" + blockID).append(html);
            });

            break;
        case "2":
            //商家
            break;
        case "3":
            //商品
            $(xml).find("GOODS_INF").each(function() {
                $("#" + blockID).show();
                var $goodsList = $(this);
                var goodsID = $goodsList.find("GOODSID").text();
                var goodsName = $goodsList.find("GOODSNAME").text();
                var subTitle = $goodsList.find("SUBTITLE1").text();
                var sellingPrice = $goodsList.find("SELLINGPRICE").text();
                var wsid = $goodsList.find("WSID").text();
                var dtStart = $goodsList.find("PUBDTSTART").text();
                var dtEnd = $goodsList.find("PUBDTEND").text();

                html = "<div class='" + blockCssClass + "'>";
                html += "<div>";
                html += "<a href='http://benefit.ilohas24.com.tw/Goods.aspx?gid=" + goodsID + "&wsid=" + wsid + "' target='_blank'><img src='http://benefit.ilohas24.com.tw/Backend/ImgGoods.aspx?no=1&gid=" + goodsID + "&size=" + imgSize + "' border='0' alt='" + goodsName + "' width='" + imgWidth + "' height='" + imgHeight + "' /></a>";
                html += "</div>";
                if (showTime) {
                    html += "<div style='font-size:13px; font-family:Arial;font-weight:bold;padding-top:6px;'>";
                    html += dtStart + " ~ " + dtEnd;
                    html += "</div>";
                }
                if (showGoodsName) {
                    html += "<div style='height:1.4em;overflow:hidden;word-break:break-all;'>";
                    html += "<div class='redTitle'>" + subTitle + "</div>";
                    html += "</div>";
                    html += "<div style='height:6.3em;overflow:hidden;word-break:break-all;'>";
                    html += "<a href='http://benefit.ilohas24.com.tw/Goods.aspx?gid=" + goodsID + "&wsid=" + wsid + "' target='_blank'>" + goodsName + "</a>";
                    html += "</div>";
                }
                if (showPrice) {
                    if (sellingPrice != "-")
                        html += "<div>福利價:<span>$" + DollarFormat(sellingPrice) + "</span></div>";
                }
                html += "</div>";
                $("#" + blockID).append(html);
            });
            break;
        case "4":
            //特約商店
            $(xml).find("DEALER_INF").each(function() {
                $("#" + blockID).show();
                var $dealerList = $(this);
                var did = $dealerList.find("DEALERID").text();
                var dName = $dealerList.find("DEALERNAME").text();
                var dIntro = $dealerList.find("DEALERINTRO").text();

                $("#DealerName").text(dName);
                $("#DealerIntro").html(dIntro);

                html = "<div class='spimg'><a href='http://benefit.ilohas24.com.tw/BENEFIT/AuthorizedDealer.aspx?did=" + did + "' target='_blank'><img id='imgDealerLogo' src='http://benefit.ilohas24.com.tw/Backend/ImgDealer.aspx?no=0&did=" + did + "' /></a></div>";
                html += "<div class='" + blockCssClass + "'>";
                html += "<div class='tt01'><a href='http://benefit.ilohas24.com.tw/BENEFIT/AuthorizedDealer.aspx?did=" + did + "' target='_blank' style='text-decoration:none;font-size:15px;color:#0099cc;'>" + dName + "</a></div>";
                html += "<div class='intro'>" + dIntro + "</div>";
                html += "</div>";

                $("#" + blockID).append(html);
            });
            break;
    }
    $("#" + blockID).append("<div class='clear'></div>");
}