﻿$(document).ready(function () {

    //首页公司简绍
    var $tabs = $("div[SECTabs='true'] > div");
    var $tabsGT0 = $("div[SECTabs='true'] > div:gt(0)");
    var $tabsContent = $("div[SECTabContent='true'] > div");

    $tabs.removeClass();
    $tabs.eq(0).addClass('on');
    $tabsGT0.addClass('off');

    $tabsContent.hide();
    $tabsContent.eq(0).show();

    $tabs.click(function () {
        if ($(this).attr('class') == 'on') {
            return;
        }
        var top = $(document).scrollTop();
        $tabs.removeClass();
        $tabs.addClass('off');
        $(this).removeClass();
        $(this).addClass('on');

        $tabsContent.slideUp(400);
        $tabsContent.eq($(this).prevAll().length).slideToggle(400);

        setTimeout(function () {
            $(document).scrollTop(top)
        }, 10);
    });

    //右面搜索列表
    var $SearchMenus = $("div[SearchMenu='true'] select");
    $SearchMenus.change(function () {
        var indexUrl = $(this).find("option:selected").attr("value");
        if (!indexUrl || indexUrl.length < 0) {
            return;
        }
        if (indexUrl.length > 0) {
            var url = indexUrl.split(";")[1];
            var index = indexUrl.split(";")[0];

            if (url.indexOf("?") > 0) {
                if ($("div[SearchMenu='true']").attr("NewWindow") != 'true') {
                    window.location.href = url + "&SID=" + index;
                }
                else {
                    window.open(url + "&SID=" + index, "");
                }
            }
            else {
                if ($("div[SearchMenu='true']").attr("NewWindow") != 'true') {
                    window.location.href = url + "?SID=" + index;
                }
                else {
                    window.open(url + "?SID=" + index, "");
                }
            }
        }
    });

    var keyValue = ExtractQueryString();

    if (keyValue["SID"]) {
        $SearchMenus.each(function () {
            var $matchOption = $(this).find("option[value^='" + keyValue["SID"] + ";']");
            if ($matchOption && $matchOption.length > 0) {
                $(this).val($matchOption.eq(0).val());
            }
        });
    }

    //工厂列表
    var $FactoryList = $('div[FactoryList="true"] > div[Factory="true"]');
    var $FactoryContent = $('div[FactoryContent="true"] > div');
    if ($FactoryList.length > 0) {
        $FactoryList.removeClass();
        $FactoryList.addClass('off');
        $FactoryList.eq(0).removeClass();
        $FactoryList.eq(0).addClass('on');

        $FactoryContent.hide();
        $FactoryContent.eq(0).show();

        $FactoryList.click(function () {
            if ($(this).attr('class') == 'on') {
                return;
            }
            var top = $(document).scrollTop();

            $FactoryList.removeClass();
            $FactoryList.addClass('off');
            $(this).removeClass();
            $(this).addClass('on');

            var index = $(this).prevAll().length - 1;

            $FactoryContent.slideUp(400);
            $FactoryContent.eq(index).slideToggle(400);

            setTimeout(function () {
                $(document).scrollTop(top)
            }, 10);
        });
    }

    var $FactoryLink = $('div[ProductNav="true"] > div');

    $FactoryLink.each(function () {
        var url = $(this).children().eq(0).attr("href");

        if (url.length <= 0) {
            return;
        }

        var pageName = ExtractPageName(window.location.href);

        $(this).removeClass();
        if (url.indexOf(pageName) > 0) {
            $(this).addClass("on");
        }
        else {
            $(this).addClass("off");
        }
    });

    $GLTabs = $('[GLSECTabs="true"] div[TabItem="true"]');
    $GLContents = $('div[GLSECContent="true"] > table');
    if ($GLTabs.length > 0) {
        $GLTabs.removeClass();
        $GLTabs.addClass('off');
        $GLTabs.eq(0).removeClass();
        $GLTabs.eq(0).addClass('on');

        $GLContents.hide();
        $GLContents.eq(0).show();

        $GLTabs.click(function () {
            if ($(this).attr('class') == 'on') {
                return;
            }

            $GLTabs.removeClass();
            $GLTabs.addClass('off');
            $(this).removeClass();
            $(this).addClass('on');

            var index = $(this).attr('ItemIndex');
            $GLContents.slideUp(400);
            $GLContents.eq(index).slideToggle(400);

            setTimeout(function () {
                $(document).scrollTop(top)
            }, 10);
        });
    }

    //锅炉产品
    $GLProducts = $('ul[GLProducts="true"] > li');
    $GLProductDetails = $('td[GLProductDetail = "true"] > table');

    if ($GLProducts.length > 0) {
        $GLProductDetails.hide();
        $GLProductDetails.eq(0).show();
        $GLProducts.click(function () {
            var index = $(this).prevAll().length;
            $GLProductDetails.hide();
            $GLProductDetails.eq(index).slideToggle(400);
        });
    }

    $GLProductViews = $('ul[GLProducts="true"] > li');
    $GLProductViewDetails = $('td[GLProductDetail = "true"] > div');

    if ($GLProductViews.length > 0) {
        $GLProductViewDetails.hide();
        $GLProductViewDetails.eq(0).show();
        $GLProductViews.click(function () {
            var index = $(this).prevAll().length;
            $GLProductViewDetails.hide();
            $GLProductViewDetails.eq(index).slideToggle(400);
        });
    }

    var $GLProductLinks = $('[PopupLink="true"]');

    if ($GLProductLinks.length > 0) {
        $GLProductLinks.each(function () {
            var $Container = $("#" + $(this).attr("ContainerID"));
            $Container.hide();
        });
        $GLProductLinks.click(function () {
            $('<div id="ContainerID" style=\"flow:left;width:500px;\"></div>').appendTo('body');
            var $Container = $("#ContainerID");
            var $Url = $(this).attr("PopupUrl");
            var ContainerID = "ContainerID";

            $.ajax({
                type: "POST",
                url: $Url,
                success: function (str) {

                    $('<div id="SECBackLayer"></div>').appendTo('body');
                    $('#SECBackLayer').height($(document).height());
                    $('#SECBackLayer').width($(document).width());

                    var BodyContentStart = str.indexOf('<body>') + 6;
                    var BodyContentEnd = str.indexOf('</body>') - 1;
                    var BodyContent = str.substring(BodyContentStart, BodyContentEnd);

                    $('#SECBackLayer').show();
                    $Container.show();
                    $Container.html(BodyContent);
                    $Container.width('500');

                    var h = $(window).height();
                    var w = $(window).width();
                    var topLocation = h / 2 - $Container.height() / 2 + $(document).scrollTop();
                    var leftLocation = w / 2 - $Container.width() / 2 + $(document).scrollLeft();

                    if (topLocation < 0) {
                        topLocation = 0;
                    }

                    $('#SECBackLayer').css({ 'filter': 'alpha(opacity=50)', 'z-index': '100', position: 'absolute', top: 0, left: 0, 'background-color': 'gray' });

                    $Container.css({ position: 'absolute', top: topLocation, left: leftLocation, 'z-index': '101', width: '500' });

                    //if (AdustPage) {
                    AdustPage();
                    //}
                    $(document).click(function (event) {
                        $target = $(event.target);
                        if ($('#SECBackLayer').is(':visible') && $target.attr("id") != ContainerID
                            && $target.parents('#' + ContainerID).length <= 0) {
                            $('#SECBackLayer').fadeOut();
                            $Container.fadeOut();
                            $('body').remove('#SECBackLayer');
                            $('body').remove('#ContainerID');
                        }
                    });
                }
            });

        });
    }

    var $SECTabContainer = $('[SECTabContainer="true"]');
    var SharedContainers = new Array();
    var Clicked = false;
    if ($SECTabContainer.length > 0) {
        $SECTabContainer.each(function () {

            var isProcessed = false;
            if ($(this).attr('SharedContainerID')) {
                var SharedContainerID = $(this).attr('SharedContainerID');
                var DefaultContainer = $(this).attr('DefaultContainer');
                if (!DefaultContainer || DefaultContainer != 'true'
                || ExistsItemInArray(SharedContainers, SharedContainerID)) {
                    isProcessed = true;
                }
                else {
                    isProcessed = false;
                    SharedContainers[SharedContainers.length] = SharedContainerID;
                }
            }

            $Items = $(this).find('[SECTabItem="true"]');

            $Items.css({ cursor: 'hand' });

            $Items.each(function () {
                $('#' + $(this).attr('TargetContent')).hide();
            });
            $('#' + $Items.eq(0).attr('TargetContent')).show();

            if (!isProcessed) {
                if ($Items.eq(0).attr('ContentURL') && $Items.eq(0).attr('ContentURL').length >= 0) {
                    $Url = $Items.eq(0).attr('ContentURL');
                    $ContentContainer = $('#' + $Items.eq(0).attr('ContentContainer'));
                    $.ajax({
                        type: "POST",
                        url: $Url,
                        dataType: "html",
                        success: function (str) {
                            var BodyContentStart = str.indexOf('<body>') + 6;
                            var BodyContentEnd = str.indexOf('</body>') - 1;
                            var BodyContent = str.substring(BodyContentStart, BodyContentEnd);
                            $ContentContainer.html(BodyContent);
                        }
                    });
                }
            }

            $Items.click(function () {
                if ($(this).attr('RecordClick') && $(this).attr('RecordClick') == 'true') {
                    Clicked = true;
                }

                var SelectedClassName = "on";
                var UnselectedClassName = "off";

                if ($(this).attr('NeedHover') && $(this).attr('NeedHover').length > 0) {
                    SelectedClassName = $(this).attr('NeedHover').split("-")[1];
                    UnselectedClassName = $(this).attr('NeedHover').split("-")[0];
                }

                if ($(this).attr('ClearContainer') && $(this).attr('ClearContainer').length > 0) {
                    var ClearContainerID = $(this).attr('ClearContainer');
                    $('#' + ClearContainerID).find('[SECTabItem="true"]').removeClass();
                    $('#' + ClearContainerID).find('[SECTabItem="true"]').addClass(UnselectedClassName);
                }

                $(this).parents('[SECTabContainer="true"]').find('[SECTabItem="true"]').removeClass();
                $(this).parents('[SECTabContainer="true"]').find('[SECTabItem="true"]').addClass(UnselectedClassName);
                $(this).removeClass();
                $(this).addClass(SelectedClassName);

                $(this).parents('[SECTabContainer="true"]').find('[SECTabItem="true"]').each(function () {
                    $('#' + $(this).attr('TargetContent')).hide();
                });

                if ($(this).attr('ContentURL') && $(this).attr('ContentURL').length >= 0) {
                    $Url = $(this).attr('ContentURL');
                    $ContentContainer = $('#' + $(this).attr('ContentContainer'));
                    $.ajax({
                        type: "POST",
                        url: $Url,
                        dataType: "html",
                        success: function (str) {
                            var BodyContentStart = str.indexOf('<body>') + 6;
                            var BodyContentEnd = str.indexOf('</body>') - 1;
                            var BodyContent = str.substring(BodyContentStart, BodyContentEnd);
                            $ContentContainer.html(BodyContent);
                            AjustRightMenu($ContentContainer);
                        }
                    });
                }

                $('#' + $(this).attr('TargetContent')).show();

            });

            if ($(this).attr('NeedInit') && $(this).attr('NeedInit') == 'true') {
                var KVs = ExtractQueryString();
                if (KVs && KVs["t"]) {
                    $Items.eq(parseInt(KVs["t"] - 1)).click();
                }
            }
        });
    }

    var $ZoomItems = $('[ZoomItem="true"]');
    if ($ZoomItems.length > 0) {
        $ZoomItems.click(function () {

            var imageUrl = $(this).attr('LargeImage');
            var imageWidth = $(this).attr('LargeImageWidth');
            var imageHeight = $(this).attr('LargeImageHeight');

            if (!$('#ImageContainerID') || $('#ImageContainerID').length <= 0) {
                $('<img style="position:absolute;width:' + imageWidth + ';height:' + imageHeight + ';" id="ImageContainerID" src="' + imageUrl + '" border="0" />').appendTo('body');
            }
            else {
                $('#ImageContainerID').attr('src', imageUrl);
                $('#ImageContainerID').css({ width: imageWidth, height: imageHeight });
            }

            $('#ImageContainerID').hide();
            var h = $(window).height();
            var w = $(window).width();

            var topLocation = h / 2 - $('#ImageContainerID').height() / 2 + $(document).scrollTop();
            var leftLocation = w / 2 - $('#ImageContainerID').width() / 2; +$(document).scrollLeft();

            if (topLocation < 0) {
                topLocation = 0;
            }

            $('#ImageContainerID').css({ position: 'absolute', top: topLocation, left: leftLocation, 'z-index': '1000' });
            $('#ImageContainerID').show();

            $(document).click(function (event) {
                //alert('1');
                $target = $(event.target);
                if ($('#ImageContainerID').is(':visible') && $target.attr("id") != 'ImageContainerID'
                            && $target.parents('#ImageContainerID').length <= 0 && !$target.attr('ZoomItem')) {
                    $('#ImageContainerID').fadeOut();
                    $('body').remove('#ImageContainerID');
                }
            });
        });
    }

    var $HoverItems = $('[NeedHover]');
    var OldClassName;
    if ($HoverItems.length > 0) {
        $HoverItems.hover(function () {
            OldClassName = $(this).attr('class');
            if (!Clicked && OldClassName != 'on') {
                var hoverClass = $(this).attr('NeedHover').split('-')[1];
                $(this).removeClass();
                $(this).addClass(hoverClass);
            }
        },
        function () {
            if (!Clicked && OldClassName != 'on') {
                var leaveClass = $(this).attr('NeedHover').split('-')[0];
                $(this).removeClass();
                $(this).addClass(OldClassName);
            }
            Clicked = false;
        });
    }

    var $SECPopupWindows = $('[SECPopupWindows="true"]');

    if ($SECPopupWindows.length > 0) {
        $SECPopupWindows.click(function () {

            var imageUrl = $(this).attr('LargeImage');
            var imageWidth = parseInt($(this).attr('LargeImageWidth'));
            var imageHeight = parseInt($(this).attr('LargeImageHeight'));
            var backWidth = parseInt($(this).attr('LargeBackWidth'));
            var backHeight = parseInt($(this).attr('LargeBackHeight'));

            if (!$('#SECPopupWraperID') || $('#SECPopupWraperID').length <= 0) {
                $('<div ID="SECPopupWraperID" style="z-index:110;text-align:center;overflow:hidden;position:absolute;width:' + backWidth + 'px;height:' + backHeight + 'px;">' +
            '<div style="background-color:#254874;width:100%;height:30px;text-align:right;">' +
            '<span id="TestInfo"></span><img ID="SECHelpID" src="/_layouts/images/SEC.InternetPortal/images/group/Close.png"></div>' +
            '</div>' +
            '<img style="position:absolute;z-index:111;width:' + imageWidth + ';height:' + imageHeight + ';" id="ImageWinContainerID" src="' + imageUrl + '" border="0" />'
            ).appendTo('body');
            }
            else {
                $('#SECPopupWraperID').css({ width: backWidth, height: backHeight });
                $('#ImageWinContainerID').attr('src', imageUrl);
                $('#ImageWinContainerID').css({ width: imageWidth, height: imageHeight });
            }

            $('#SECPopupWraperID').hide();
            $('#ImageWinContainerID').hide();

            var WindowsTop;
            var WindowsLeft;

            if ($(this).attr('RelatedTo') && $(this).attr('RelatedTo').length > 0) {
                $('#' + RelatedObjID).css({ position: 'relative' });
                var RelatedObjID = $(this).attr('RelatedTo');
                var RelatedObjTop = $('#' + RelatedObjID).offset().top;
                var RelatedObjLeft = $('#' + RelatedObjID).offset().left;
                WindowsTop = RelatedObjTop - 30;
                WindowsLeft = RelatedObjLeft - (backWidth - $('#' + RelatedObjID).width()) / 2
            }
            else {
                var h = $(window).height();
                var w = $(window).width();

                var WindowsTop = h / 2 - $('#SECPopupWraperID').height() / 2 + $(document).scrollTop();
                var WindowsLeft = w / 2 - $('#SECPopupWraperID').width() / 2; +$(document).scrollLeft();

                if (WindowsTop < 0) {
                    WindowsTop = 0;
                }
            }

            $('#SECPopupWraperID').show();
            $('#ImageWinContainerID').show();
            $('#SECPopupWraperID').css({ 'filter': 'alpha(opacity=50)', top: WindowsTop, left: WindowsLeft, 'background-color': '#254874' });
            $('#ImageWinContainerID').css({ top: WindowsTop + 30, left: WindowsLeft - (imageWidth - backWidth) / 2 });
            //            $('#TestInfo').html("WindowsTop:" + WindowsTop + ",WindowsLeft:" + WindowsLeft
            //            + "ImageTop:" + (WindowsTop + 30) + "ImageLeft:" + (WindowsLeft - 90));
            $('#SECHelpID').click(function () {
                $('#SECPopupWraperID').hide();
                $('#ImageWinContainerID').hide();
            });
        });
    }
});

$(document).ready(function () {
    var ImageContainer = $('[SECSwitchImage="true"]');
    ImageContainer.each(function () {
        var SwitchImages = $(this).find('img');
        var ImageWidth = parseInt($(this).attr('SECImageWidth'));
        var ImageHeight = parseInt($(this).attr('SECImageHeight'));

        if (SwitchImages.length > 0) {
            SwitchImages.each(function () {
                var index = SwitchImages.index($(this));
                if (index <= 0) {
                    $(this).css({ 'width': ImageWidth, 'height': ImageHeight });
                }
                else {
                    $(this).css({ 'width': ImageWidth, 'height': ImageHeight });
                }
            });

            $(this).cycle({
                fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
            });
            //setInterval(SECSwitchImage($(this)), 2000);
        }
    });
});

function AjustRightMenu(content) {
    if (NeedAjustRight==true) {
        var contentHeight = content.height();
        $('#tdContentRight').css('height', contentHeight + 65 + 33);
        $('#tdContentRight').parent().removeClass();
        $('#tdContentRight').parent().addClass('wpzone_content_pg_searcharea1');
    }
}

function SECSwitchImage (obj) {
    return function () {
        var curIndex = obj.find('img').index(obj.find('img:visible').eq(0));
        var nextIndex = (curIndex + 1) % obj.find('img').length;
        obj.find('img').eq(curIndex).animate({ opacity: 0.5 }, 2000, function () {
            obj.find('img').eq(curIndex).hide();
            obj.find('img').eq(nextIndex).css({ 'opacity': '0.5' });
            obj.find('img').eq(nextIndex).show();
            obj.find('img').eq(nextIndex).animate({ opacity: 1 }, 2000, function () {
                //obj.find('img').eq(nextIndex).show();
            });
        });
    }
}

function ExistsItemInArray(ar,obj) {
    for (i = 0; i < ar.length; i++) {
        if ($(ar[i] == obj)) {
            return true;
        }
    }
    return false;
}

function RemoveItemFromArray(ar, obj) {
    var index = -1;
    for (i = 0; i < ar.length; i++) {
        if (ar[i] == obj) {
            index = i;
            break;
        }
    }

    ar = ar.splice(i, 1);
    return ar;
}

function MyAlert(str) {
    alert(str);
}
function ExtractPageName(url) {
    var path = url.split("?")[0];
    var beginIndex = path.lastIndexOf("/");
    return path.substring(beginIndex+1,path.length).replace("#","");
}
function ExtractQueryString() {
    // ***this goes on the global scope
    // get querystring as an array split on "&"
    var querystring = location.search.replace('?', '').split('&');
    // declare object
    var queryObj = {};
    // loop through each name-value pair and populate object
    for (var i = 0; i < querystring.length; i++) {
        // get name and value
        var name = querystring[i].split('=')[0];
        var value = querystring[i].split('=')[1];
        // populate object
        queryObj[name] = value;
    }

    return queryObj;
}
function SelectFactory(obj) {
    if (obj.value != "") {
        window.location.href = obj.value;
    }
}

