﻿var hashCounter=0;
var currentFlap = 'main';

window.onload = function() {
    hashListener.init();
    hashListener.onHashChanged = function () {
	    hashAction();
    }
	hashAction();
    //loadIndexPage('main', 'main', '');
}
// load the main divs
function loadIndexPage(type, submenu, offset) {
    loadIndexPage2(type, submenu, offset, true)
}

function loadIndexPage2(type, submenu, offset, hashStep) {
    if (hashStep) {
        _setHash(type + '@' + submenu + '@' + offset + '@' + hashStep);
    }
    if (type == 'main') {
        getMain(submenu);
    } else if (type == 'por') {
        getPoradenstvo(submenu);
    } else if (type == 'marketing') {
        getMarketing(submenu);
    } else if (type == 'design') { 
        getDesign(submenu);
    } else if (type == 'price') { 
        getPrice(submenu);
    } else if (type == 'about') {
        getAbout(submenu);
    }
    document.getElementById('flap' + currentFlap).className  = '';
    document.getElementById('flap' + type).className  = 'current';
    currentFlap = type;
}

function _setHash(newHash) {
        hashCounter++;
        hashListener.setHash('#' + newHash + '+' + hashCounter);
}

function hashAction() {
    //var statusBar = document.getElementById('statusBarText');
    var vars = document.location.hash.substring(1).split('+');
    if (vars.length == 2) {
        if (vars[1] != hashCounter) {
            //showNotificationText('back action to: ' + document.location.hash);
            //var det = vars[0].split('.');
            doAction(vars[0]);
        }
    } else {
        // if no hash in url, show the main page
        loadIndexPage('main', 'main', '');
    }
}

function doAction(values) {
    var vars = values.split('@');
    if (vars.length > 0) {
        loadIndexPage2(vars[0], vars[1], vars[2], false);
    }
}

function onFailed(ex, ctx, methodName) { 
    //document.location.href = "Default.aspx";
    alert(ex.get_exceptionType() + " method: " + methodName); 
    // get_stackTrace(), get_message(), 
    // get_statusCode(), get_timedOut() 
}

function getMain(submenu) {
    //indexNewFaces
    PageMethods.getMainDiv(submenu, handleGetMain, onFailed);
}

function handleGetMain(value, ctx) {
    if (value == null) {
        alert ('null from server: handleIndexProfiles');
        //document.location.href = "index.aspx";
    }
    document.getElementById('leftNavDiv').innerHTML = value[0];
    document.getElementById('mainIndexBody').innerHTML = value[1];
}

function getMarketing(submenu) {
    //indexNewFaces
    PageMethods.getMarketingDiv(submenu, handleGetMarketing, onFailed);
}

function handleGetMarketing(value, ctx) {
    if (value == null) {
        alert ('null from server: handleIndexProfiles');
        //document.location.href = "index.aspx";
    }
    document.getElementById('leftNavDiv').innerHTML = value[0];
    document.getElementById('mainIndexBody').innerHTML = value[1];
}

function getDesign(submenu) {
    //indexNewFaces
    PageMethods.getDesignDiv(submenu, handleGetDesign, onFailed);
}

function handleGetDesign(value, ctx) {
    if (value == null) {
        alert ('null from server: handleIndexProfiles');
        //document.location.href = "index.aspx";
    }
    document.getElementById('leftNavDiv').innerHTML = value[0];
    document.getElementById('mainIndexBody').innerHTML = value[1];
}

function getPoradenstvo(submenu) {
    //indexNewFaces
    PageMethods.getPoradenstvoDiv(submenu, handleGetPoradenstvo, onFailed);
}

function handleGetPoradenstvo(value, ctx) {
    if (value == null) {
        alert ('null from server: handleIndexProfiles');
        //document.location.href = "index.aspx";
    }
    document.getElementById('leftNavDiv').innerHTML = value[0];
    document.getElementById('mainIndexBody').innerHTML = value[1];
}

function getPrice(submenu) {
    //indexNewFaces
    PageMethods.getPriceDiv(submenu, handleGetPrice, onFailed);
}

function handleGetPrice(value, ctx) {
    if (value == null) {
        alert ('null from server: handleIndexProfiles');
        //document.location.href = "index.aspx";
    }
    document.getElementById('leftNavDiv').innerHTML = value[0];
    document.getElementById('mainIndexBody').innerHTML = value[1];
}

function getAbout(submenu) {
    //indexNewFaces
    PageMethods.getAboutDiv(submenu, handleGetAbout, onFailed);
}

function handleGetAbout(value, ctx) {
    if (value == null) {
        alert ('null from server: handleIndexProfiles');
        //document.location.href = "index.aspx";
    }
    document.getElementById('leftNavDiv').innerHTML = value[0];
    document.getElementById('mainIndexBody').innerHTML = value[1];
}

//We are limited, not by our abilities, but by our vision
//“Vision is the art of seeing what is invisible to others.”
// www.etarget.hu/export/rpc.php
