//System Purge
function purge(d)
{
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}

var serviceAlbumsDia = null;
var currentTag = null;
var flat = false;

var gTabsIdArray = new Array();
gTabsIdArray.push("tab_home");
gTabsIdArray.push("tab_create_book");
gTabsIdArray.push("tab_my_printings");

function showModuleTab(moduleName)
{
    for (var i = 0; i < gTabsIdArray.length; i++) {
        $(gTabsIdArray[i]).className = "tab_unselected";
    }
    if ($(moduleName) != null) {
        $(moduleName).className = "tab_selected";
    }
}

function loadModule(moduleNameValue)
{

    var moduleName = "";
    if (moduleNameValue != null || moduleNameValue.length != 0) {
        moduleName = moduleNameValue;
    }
    var isGuestCreatedBook;
    if (currentTag != null && currentTag == moduleName) {
        return;
    }
//    if (currentTag == "create_book" && flat == true && moduleName != 'tab_order') {
//        if (!confirm("Are you sure to leave this page? Please make sure save what you have done.")) {
//            return;
//        }
//        flat = false;
//    }

    if (moduleName.length == 0) {
        if ($("newUser").value == "true") {
            moduleName = "tab_create_book";
        }
        else if ($("albumSize").value == "0") {
            moduleName = "tab_create_book";
        }
        else if ($("guestUser").value == "true") {
            moduleName = "tab_create_book";
            isGuestCreatedBook = true;
        }
        else {
            moduleName = "tab_home";
        }
    }
    if (moduleName.length == 0) {
        moduleName = "tab_home";
    }

    showModuleTab(moduleName);

    //    purge($("home_content"));
    //    $("home_content").innerHTML = "<font color='#000000'>Loading...</font>";
    //    $("home_content").style.height = "550px";
    Tool.Wait("Waiting", "System is loading....");

    if (moduleName == "tab_home") {
        //        $("home_content").style.height = "500px";
        refreshMyHome();
    }
    else if (moduleName == "tab_create_book" && isGuestCreatedBook == true) {
        //        $("home_content").style.height = "600px";
        refreshCreateBook_2();
    }
    else if (moduleName == "tab_create_book") {
        //        $("home_content").style.height = "530px";
        refreshCreateBook();
    }
    else if (moduleName == "tab_myaccount") {
        //$("home_content").style.height = "530px";
        _openAccountPage();
    }
    else if (moduleName == "tab_choose_album_and_photo") {
        openChooseAlbumAndPhotoPage();
    }
    else if (moduleName == "tab_my_printings") {
    	// TODO add fix logic of FOTL-46:call the share&Print button
    	// function of swf file
    	var createBookSwf = getSwfObject('create_book_swf_test');
    	var create_book_swf_flg = $('create_book_swf_flg');
    	if (createBookSwf!=undefined && createBookSwf!=null&&create_book_swf_flg) {
    		Tool.CloseWait();
    		createBookSwf.orderPDF();
    	} else {
    		PrintingsUtil.showPrintingPage(); 
    	}
//    	PrintingsUtil.showPrintingPage(); 
    }
}

//Refresh My Home
function refreshMyHome()
{
    currentTag = "book";
    //    $("home_content").innerHTML = "<font color='#000000'>Loading...</font>";
    readRemoteUrl("PrivateMyHome.do", HomeUtil.MyHomeHandler);
}

//Refresh My Book
function refreshMyBook()
{
    currentTag = "book";
    $("home_content").innerHTML = "<font color='#000000'>Loading...</font>";
    readRemoteUrl("showBookList.do", null, "home_content");
}

//Refresh Create Book;
function refreshCreateBook()
{
    currentTag = "create_book";
    flat = false;
    var handler = function(data)
    {
        $("home_content").innerHTML = data;
        UploadStep.From_Settings[2].authUrl = $("flickr_auth").value;
        UploadStep.From_Settings[3].authUrl = $("facebook_auth").value;
        Tool.CloseWait();
        if (parseInt($("album_size").value) == 0) {
            $("div_album_location").style.height = (document.body.clientHeight - 250) + "px";
        }
    }
    readRemoteUrl("CreateBook-1.do", handler, null);
}

function showAlbum(albumId){
    $("album_title_"+albumId).style.display="block";
    $("album_action_"+albumId).style.display="block";
    $("album_container_"+albumId).style.marginTop="0px";
}

function hiddenAlbum(albumId){
    $("album_title_"+albumId).style.display="none";
    $("album_action_"+albumId).style.display="none";
    $("album_container_"+albumId).style.marginTop="15px";
}

function refreshCreateBook_2()
{
    currentTag = "create_book";
    flat = true;
    var handler = function(data)
    {
        Tool.CloseWait();
        $("home_content").innerHTML = data;
        $("home_content").style.height = (document.body.clientHeight - 100) + "px";
        var createBookContainer = document.getElementById("createBookContainer2");
        createBookContainer.style.width = (document.body.clientWidth - 20) + "px";
        createBookContainer.style.height = (document.body.clientHeight - 120) + "px";

        window.onresize = function()
        {
            var createBookContainer = document.getElementById("createBookContainer2");
            if (createBookContainer) {
                //                 alert('vvvvv');
                var h_height = document.body.clientHeight - 150;
                if (h_height < 400) {
                    h_height = 400;
                }
                document.getElementById("home_content").style.height = h_height + "px";
                var bookC_width = document.body.clientWidth - 20;
                if (bookC_width < 800) {
                    bookC_width = 800;
                }
                var bookC_height = document.body.clientHeight - 120;
                if (bookC_height < 400) {
                    bookC_height = 400;
                }
                document.getElementById("home_content").style.width = bookC_width + "px";
                document.getElementById("container").style.width = bookC_width + "px";
                document.getElementById("home_content").style.height = bookC_height + "px";
                createBookContainer.style.width = bookC_width + "px";
                createBookContainer.style.height = bookC_height + "px";
            }
        }
    }
    readRemoteUrl("CreateBook-2.do?bookId=" + $("guestBookId").value + "&guest=" + $("guestUser").value, handler, null);
}

var searchMapHandler = false;

//Search Map
function showSearchBar()
{
    document.getElementById("search_bar").style.display = "block";
    document.getElementById("search_bar").style.color = "black";
    // document.getElementById("search_bar").style.left = "1050px";
    // document.getElementById("search_bar").style.top = "90px";
    document.getElementById('search_bar_ctrl').onclick = hideSearchBar;
}

function hideSearchBar()
{
    document.getElementById("search_bar").style.display = "none";
    document.getElementById('search_bar_ctrl').onclick = showSearchBar;
}

//Refresh Session Albums
function refreshSessionAlbums()
{
    var handler = function(data)
    {
    }
    readRemoteUrl("refreshAlbums.do?rand=" + Math.ceil(Math.random() * 100000), handler);
}

function goToPublicHome()
{
    window.location = "Login.do";
}

function openAccountPage(orderId)
{
    //	if (orderId){
    //		var url=$('http').value+"PrivateHome.do?selectedTabName=tab_myaccount&orderId="+orderId;
    //	}else{
    //		var url=$('http').value+"PrivateHome.do?selectedTabName=tab_myaccount";
    //	}
    //
    //	window.location.href=url;
    _openAccountPage(orderId);
}

function _openAccountPage(orderId)
{
    flat = false;
    for (var i = 0; i < gTabsIdArray.length; i++) {
        $(gTabsIdArray[i]).className = "tab_unselected";
    }
    if ($("curOrderId").value != null && $("curOrderId").value != "") {
        orderId = $("curOrderId").value;
    }
    var handler = function(data)
    {
        $("home_content").innerHTML = data;
        Tool.CloseWait();
        if ($('userEmail')) {
            if ($('userEmail').value.indexOf('guest') == 0 || $('userEmail').value.indexOf('DEMOBOOK') == 0) {
                document.location = "PublicHome.do";
            }
        }
    };
    readRemoteUrl("getUserInformation.do?orderId=" + orderId, handler);

}

function openChooseAlbumAndPhotoPage()
{
    var handler = function(data)
    {
        showModuleTab('tab_create_book');
        $("home_content").innerHTML = data;
        var albumList = $("albumInJsonDiv").innerHTML.evalJSON();
        UploadUtil.init(albumList);
        Tool.CloseWait();
    };
    readRemoteUrl("chooseAlbumAndPhoto.do", handler);
}

function addNewOrder(count, orderId)
{
    if (count == null || count == "") {
        orderNum++;
    }
    else {
        orderNum = count;
    }
    orderSum++;
    updateTopRightStatus();
    if (orderId != null) {
        $("curOrderId").value = orderId;
    }
}

function updateTopRightStatus()
{
    //$("orderNum").innerHTML = orderNum;
    $("myAccountLink").title = "Total Orders: " + orderSum + "  New Orders: " + orderNum;
    if (orderNum <= 0) {
        $("orderGif").style.display = "none";
        //$("headerOrder").style.display = "none";
        $("orderJpg").style.display = "inline";
    }
    else {
        $("orderGif").style.display = "inline";
        //$("headerOrder").style.display = "inline";
        $("orderJpg").style.display = "none";
    }
}

function updateOrderStatus(orderNumber, orderSummary)
{
    orderNum = orderNumber;
    orderSum = orderSummary;
    updateTopRightStatus();
}

function removeOrder()
{
    orderNum--;
    updateTopRightStatus();
}

function goCreateNewBookPage()
{
    loadModule("tab_create_book");
}

/**
 * override the window onresize function.
 * 
 */
function loadMainInit()
{
  window.onresize = function()
  {
	// check if the lightwidow whether show
	if (typeof(myLightWindow) != 'undefined' && myLightWindow != null) {
		if ('block'==$('lightwindow').style.display) {
			myLightWindow.deactivate();
		}
	}
  	resizeContainer();
  	var div_confirm = $('div_confirm');
	if (div_confirm && 'block' == div_confirm.style.display) {
		Tool.CloseConfirm();
	}
  	// call the reShowTabHomePage
	reShowTabHomePage();
  }
  resizeContainer();
}
/**
 * call the HomeUtil.MyHomeHandler function to recreate tab_home page.
 */
function reShowTabHomePage() {
	var tabHome = $('tab_home');
	if (tabHome == undefined || tabHome == null) {
		return;
	}
	// check whether the tabHome class is the tab_selected
	if (tabHome.className != 'tab_selected') {
		return;
	}
	
	refreshMyHome();
}
/**
 * resize the window.
 * 
 */
function resizeContainer() {
	var bookC_width = document.body.clientWidth - 20;
	// 1024x768 min width
	var minWidth = 990;
	if (bookC_width < minWidth) {
		bookC_width = minWidth;
	}
	document.getElementById("container").style.width = bookC_width + "px";
}
/**
 * get the swf object with the specified id.
 * @param swfId
 * @return
 */
function getSwfObject(swfId) {
	if (window.document[swfId]) 
	  {
	      return window.document[swfId];
	  }
	  if (navigator.appName.indexOf("Microsoft Internet")==-1)
	  {
	    if (document.embeds && document.embeds[swfId])
	      return document.embeds[swfId]; 
	  }
	  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	  {
	    return document.getElementById(swfId);
	  }
}

