﻿var gContentId = "";
var gParams = "";
var isIE6 = false;
var sMoo = null;

////if (navigator.appName.indexOf("Microsoft Internet") == -1) {
//    if (document.domain != "localhost") {
//        document.domain = "igoviet.com";
//    }
//    else {
//        document.domain = "localhost";
//    }
////}

function initSite() {
    //This js file is included in two pages: default.aspx and media aspx.
    //But this function is only called by body.onload() of default.aspx because only that page has slide photos
    addEndRequestHandler();
    loadSlideIt();
}

function addEndRequestHandler() {
    //This is called here and in weatherStockAstrology.aspx
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endAsynchRequestHandler);
}

function endAsynchRequestHandler() {
    if (typeof(endAsynchRequestHandlerCoverSlider) != "undefined") endAsynchRequestHandlerCoverSlider(); //defined in coverSlider.ascx
    if (typeof(endAsynchRequestHandlerGetLastCoinQCTQ) != "undefined") endAsynchRequestHandlerGetLastCoinQCTQ(); //defined in quyCocThanQue.ascx
    if (typeof(endAsynchRequestHandlerGetLastCoinQQA) != "undefined") endAsynchRequestHandlerGetLastCoinQQA(); //defined in queQuanAm.ascx
    if (typeof(endAsynchRequestHandlerLoadDay) != "undefined") endAsynchRequestHandlerLoadDay(); //defined in weatherStockAstrology.aspx
}

function prepareToShowMagzPopup(params, contentId) {
    if (params != "-1") {
        gParams = params;
        gContentId = contentId;
        MWVietBao.Web.UI.AsynchWebServices.GetContentByIssue(params, succeededCallback);
    }
}

function succeededCallback(result, eventArgs) {
    document.getElementById(gContentId).value = result;
    showMagzPopup(gParams, gContentId);
}

function checkEnter(e) {
    e = e || event;
    return (e.keyCode || event.which || event.charCode || 0) !== 13;
}

function loadSlideIt() {
    sMoo = 
    new SlideItMoo({
        overallContainer: 'SlideItMoo_outer',
        elementScrolled: 'SlideItMoo_inner',
        thumbsContainer: 'SlideItMoo_items',
        itemsVisible: 5,
        elemsSlide: 1,
        autoSlide: 5000,
        duration: 1000,
        itemsSelector: '.SlideItMoo_element',
        itemWidth: 110,
        showControls: 0,
        direction: -1,
        transition: Fx.Transitions.Back.easeIn,
        onChange: function(index) {
            //alert(index);
        }
    });
}

function showMagzPopup(params, contentId) {
    initMagazine(params, contentId);
    document.getElementById("MWVietBaoPopupContent").style.height = (getWindowHeight() - 65) + "px";
    toggleIF("none");
    $find("MWVietBaoBehavior").show();
}

function showMediaPopup(path, title) {
    var flashString = "";
    flashString = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' " +
	                         "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' " +
	                         "width='600' height='385' id='flashMovie' name='flashMovie' align='middle'>" +
	                            "<param name='allowScriptAccess' value='always' />" +
	                            "<param name='allowFullScreen' value='false' />" +
	                            "<param name='movie' value='" + path + "' />" +
	                            "<param name='quality' value='high' />" +
	                            "<param name='bgcolor' value='#cccccc' />" +
	                            "<embed src='" + path + "' quality='high' bgcolor='#cccccc' " +
	                             "width='600' height='385' id='flashMovie' name='flashMovie' align='middle' " +
	                             "allowScriptAccess='always' allowFullScreen='false' " +
	                             "type='application/x-shockwave-flash' " +
	                             "pluginspage='http://www.macromedia.com/go/getflashplayer' />" +
	                        "</object>";
    document.getElementById("mediaContainer").innerHTML = flashString;
    document.getElementById("mediaPopupTitle").innerHTML = title;
    $find("MediaBehavior").show();
}

function showQCaoPopup(name) {
    var imgHeight = getImgHeight(name);
    document.getElementById("QCaoImg").src = name.replace('/small/', '/big/');
    if (imgHeight != 0) {
        document.getElementById("QCaoPopupContent").style.height = (imgHeight + 25) + "px";
        document.getElementById("qcaoContainer").style.height = imgHeight + "px";
    }
    else {
        document.getElementById("QCaoPopupContent").style.height = (getWindowHeight() - 65) + "px";
        document.getElementById("qcaoContainer").style.height = (getWindowHeight() - 65 - 25) + "px";
    }
    toggleIF("none");
    $find("QCaoBehavior").show();
}

function getImgHeight(name) {
    if (name.indexOf("_h") > 0) {
        var bIdx = name.indexOf("_h") + 2;
        var eIdx = name.indexOf(".", bIdx);
        return parseInt(name.substring(bIdx, eIdx), 10);
    }
    else {
        return 0;
    }
}

function resetQCaoImg() {
    //set default qcao image to mwvietbao logo
    document.getElementById("QCaoImg").src = 'http://www.igoviet.com/images/MidwestVietBao_Red_Small.png';
}

function toggleIF(visibility) {
    var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    if (isChrome || isIE6) {
        if (document.getElementById("WeatherIF")) {
            document.getElementById("WeatherIF").style.display = visibility;
        }
        if (document.getElementById("StockIF")) {
            document.getElementById("StockIF").style.display = visibility;
        }
        if (document.getElementById("NewsFeedsIF")) {
            document.getElementById("NewsFeedsIF").style.display = visibility;
        }
    }

    //it appears that the album flash object will cover the footer of the magazine popup. So, we are hidding
    //the album flash object (if the current page is media.aspx) here when we show the magazine popup.
    var ao = document.getElementById("albumOuter");
    if (ao) {
        ao.style.display = visibility;
    }
}

function stopFlash() {
    //var movie = getFlashMovieObject("flashMovie");
    var movie = window.document.flashMovie;
    if (movie != null) {
        movie.StopPlay();
        movie.TStopPlay("_level0/")
        document.getElementById("mediaContainer").innerHTML = "";
    }
}

function getFlashMovieObject(movieName) {
    if (window.document[movieName]) {
        return window.document[movieName];
    }
    if (navigator.appName.indexOf("Microsoft Internet") == -1) {
        if (document.embeds && document.embeds[movieName])
            return document.embeds[movieName];
    }
    else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
    {
        return document.getElementById(movieName);
    }
}

function handleKeyCode(e) {
    var unicode = e.keyCode ? e.keyCode : e.charCode;
    if (unicode == 13) {
        flippingBook.flipToPage();
    }
}
