﻿
var autoshowlgrid = true;
var currentlgrid = 0;

var autoshowmain = true;
var currentmain = 0;
var timermain = 5000;

$(document).ready(function() {

    ShowPages();
    //new win link
    $('a[target="_blank"][class!="noicon"]').addClass('newwin');


    //tr01
    $('.tr01 tr:odd').addClass('tr1');
    $('.tr01 tr:even').addClass('tr0');

    $('.tr10 tr:odd').addClass('tr0');
    $('.tr10 tr:even').addClass('tr1');


    /*
    //lgrid_content_header
    $('.lgrid_content_header').each(function(i) {
    $('.lgrid_content_header').eq(i).click(function() {
    autoshowlgrid = false;
    lgrid_content_header_click(i);
    });
    });
    */
    looplgrid();
    /*
    $('.main_content_header').each(function(i) {
    $('.main_content_header').eq(i).click(function() {
    //   autoshowmain   =false
    timermain = timermain * 2;
    currentmain = i;
    main_content_header_click(i);
    });
    });
  
    loopmain();*/

    //HighNav
    HighLight();
  //  setTimeout("loophm()", 5000);

    //hm_pages
    $('.hm_page').each(function(i) {
        $('.hm_page').eq(i).click(function() {
          
            autoshowhm = false;
            if (!hmhandler) clearTimeout(hmhandler);
            hm_page_click(i+1);
        });
    });


    //nav_fm

    $(".sf-menu").superfish();



});



function ShowPages() {
    var loc = location.href.toLowerCase();
    loc = loc.substr(loc.lastIndexOf('/') + 1);
    loc = loc.substr(0, loc.indexOf('.'));
    if (loc.length == 0) loc = 'default';
    var curi = -1;


    $('.ulpages_fm li').each(function(i) {

        if ($('.ulpages_fm li:eq(' + i + ') a').attr('href').indexOf(loc + '.aspx') != -1) {
            $('.ulpages_fm li').eq(i).html('<strong>' + $('.ulpages_fm li').eq(i).text() + '</strong>');
            curi = i;
        }
    });


    var hidel = false;
    var hider = false;

    var posl = 3;
    var posr = 3;

    if (curi < posl + 2) {
        posr = posr + posl - curi + 1;
    }
    if (curi > ($('.ulpages_fm li').length - posr - 3)) {
        posl = posl + posr - $('.ulpages_fm li').length + curi + 2;
    }

    for (var i = 1; i < curi - posl; i++) {
        hidel = true;
        $('.ulpages_fm li').eq(i).hide();
    }
    for (var i = curi + posr + 1; i < $('.ulpages_fm li').length - 1; i++) {
        hider = true;
        $('.ulpages_fm li').eq(i).hide();
    }

    if (hidel) {
        $('.ulpages_fm li:first').after('<li>...</li>');
    }
    if (hider) {
        $('.ulpages_fm li:last').before('<li>...</li>');
    }
}


function HighLight() {
    $(document).ready(function() {
        try {
            var arr = HightlightStr.split(",");
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].length > 0) {
                    if ($('.n_' + arr[i]).html()) {
                        $('.n_' + arr[i]).addClass('nav_on');
                        return;
                    }
                }
            }
        } catch (e) { }
        $('.n_home').attr('class', 'nav_on');

    });
}

var currenthm = 0;
var autoshowhm = true;
var hmhandler = null;

function loophm() {
    if (!autoshowhm) return;

    hm_page_click(currenthm);
    currenthm++;

    hmhandler = setTimeout("loophm()", 7000);
}

function hm_page_click(i) {
    currenthm = i;

    var c = currenthm % 2;
    var c1 = (currenthm + 1) % 2;
    $('.hm_page').removeClass('on');
    $('.hm_page').eq(c1).addClass('on');

    $('.hm' + c).fadeOut(1000, function() {
        //  $('.hm' + c1).css('z-index', '0');
    });
    $('.hm' + c1).fadeIn(2000, function() {
        //  $('.hm' + c).css('z-index', '1');
    });
}

function looplgrid() {
    if (!autoshowlgrid) return;

    var i = (currentlgrid % $('.lgrid_content_detail').length);
    $('.lgrid_content_detail').hide();
    $('.lgrid_content_detail').eq(i).show(1000);
    currentlgrid++;

    setTimeout("looplgrid()", 5000);
}

function looplgrid0() {
    if (!autoshowlgrid) return;

    lgrid_content_header_click(currentlgrid % $('.lgrid_content_header').length);
    currentlgrid++;
    setTimeout("looplgrid()", 10000);
}

function lgrid_content_header_click(i) {
    for (var j = 0; j < $('.lgrid_content_detail').length; j++) {
        if (i == j) {
            $('.lgrid_content_detail').eq(i).show();
            $('.lgrid_content_header').eq(i).css('font-weight', 'bold');
        } else {
            $('.lgrid_content_header').eq(j).css('font-weight', 'normal');
            $('.lgrid_content_detail').eq(j).hide();
        }
    }
}



function loopmain() {
    if (!autoshowmain) return;

    main_content_header_click($('.main_content_header').length - 1 - currentmain % $('.main_content_header').length);
    currentmain++;
    //timermain = 1000;
    setTimeout("loopmain()", timermain);
}

function main_content_header_click(i) {
    for (var j = 0; j < $('.main_content_detail').length; j++) {
        if (i == j) {
            $('.main_content_detail').eq(i).fadeIn(500);
            $('.main_content_header').eq(i).css('background', '#ddd');
            // $('.main_content_header').eq(i).css('font-weight', 'bold');
        } else {
            $('.main_content_header').eq(j).css('background', '#fff');
            //$('.main_content_header').eq(j).css('font-weight', 'normal');
            $('.main_content_detail').eq(j).fadeOut();

        }
    }
}


function getFCKSel() {
    var selection = "";
    if (FCKeditorAPI.GetInstance('ctl00_ContentPlaceHolder1_txtContent').EditorDocument.selection != null) {
        selection = FCKeditorAPI.GetInstance('ctl00_ContentPlaceHolder1_txtContent').EditorDocument.selection.createRange().text;
    } else {
        selection = FCKeditorAPI.GetInstance('ctl00_ContentPlaceHolder1_txtContent').EditorWindow.getSelection();
        selection = "" + selection;
    }
    return selection;
}


function InsertLinkToFCK(url, text) {
    var sel = getFCKSel();
    if (sel.length == 0) sel = text;
    FCKeditorAPI.GetInstance('ctl00_ContentPlaceHolder1_txtContent').InsertHtml('<a href="' + url + '">' + $.trim(sel) + '</a>');


}

