﻿var gContentId = "";
var gParams = "";
var isIE6 = false;

//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 called by body.onload() of default.aspx only because only that page has slide photos
    //Sys.WebForms.PageRequestManager.getInstance().add_endRequest(endAsynchRequestHandler);
    loadSlideIt();
}

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() {
    new SlideItMoo({
        overallContainer: 'SlideItMoo_outer',
        elementScrolled: 'SlideItMoo_inner',
        thumbsContainer: 'SlideItMoo_items',
        itemsVisible: 5,
        elemsSlide: 1,
        autoSlide: 10000,
        duration: 2000,
        itemsSelector: '.SlideItMoo_element',
        itemWidth: 110,
        showControls: 1,
        direction: -1,
        onChange: function(index) {
            //alert(index);
        }
    });
}

//function yearSelected(year) {
//this function would be called from the containing page, by JavaScript
//method to refresh content via update panel
//document.getElementById("<%=YearHiddenText.ClientID%>").value = year;
//document.getElementById("<%=UpdatePanelTriggerButton.ClientID%>").click();
//}

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;
        }
    }
}

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();
    }
}
//function endAsynchRequestHandler(sender, args) {
//}
