﻿function startShowAndHide(item, duration, ms) {
    startShowItem(item, null, 0, ms);
    setTimeout('startHideItem(\'' + item + '\', true, 0, ' + ms + ');', duration);
}

function startSwapItems(item1, item2, speed, onComp){
    if(item1.style.display == 'none') {
        var tmp = item1;
        item1 = item2;
        item2 = tmp;
    }
    startHideItem(item1, 1, 0, speed, function(){startShowItem(item2,0,1,speed,onComp);});
}

function startHideItem(item, start, stop, speed, keepPos, onComp){
    new fade(item.id).start(item, new option(start, stop, -1, speed, null, keepPos, onComp));
}

function startShowItem(item, start, stop, speed, onComp){
    new fade(item.id).start(item, new option(start, stop, 1, speed, null, false, onComp));
}

function option (start, stop, direction, speed, loop, keepPos, onComplete) {
    this.start = start;
    this.stop = stop;
    this.direction = direction;
    this.speed = speed;
    this.loop = loop;
    this.onComplete = onComplete;
    this.keepPos = keepPos;
};

var fades = [];
function findFade(name) {
    for(var i = 0; i < fades.length; i++)
        if(fades[i].name == name)
            return fades[i];
    return null;
}

function killFade(name) {
    for(var i = 0; i < fades.length; i++)
        if(fades[i].name == name) {
            fades[i].stop();
            fades.splice(i, 1);
        }
}

function fade(name)
{   
    this.name = name;
    this.start = start;
    this.stop = stop;
    this.setOpacity = setOpacity;
    this.tick = tick;
    this.proxy = null;
    this.options = null;
    this.current = null;
    this.el = null;
    
    if(findFade(name) != null) {
        var existing = findFade(name);
        this.proxy = existing.proxy;
        this.options = existing.options;
        this.current = existing.current;
        this.el = existing.el;
        killFade(name);
    }
    
    fades[fades.length] = this;
    
    function stop() {
        window.clearInterval(this.heartBeat);	
    };
    
	function start(el, options) {
	    this.options = options;
	    this.current = options.start;
	    this.el = el;

        if(this.proxy == null){
            this.proxy = el.cloneNode(true);
            /*
             * if this is MSIE 6/7, then we need to copy the innerHTML to
             * fix a bug related to some form field elements
             */
            if( !!document.all ) this.proxy.innerHTML = el.innerHTML;            
	        el.parentNode.replaceChild(this.proxy, el);
	    }
    	
        this.setOpacity(this.proxy, this.current);	
	    if (options.direction == 1)
	    {
		    this.proxy.style.visibility = 'visible';
		    this.proxy.style.display = '';
	    }

	    this.heartBeat = window.setInterval('if(findFade(\'' + this.name + '\') != null){findFade(\'' + this.name + '\').tick();}else{this.clearInterval();}', 25);
	};
	
	function heartbeat(name, id) {
	}
	
	function tick()
	{
		this.current += this.options.speed * this.options.direction;
		this.setOpacity(this.proxy, this.current);
		
		if ( ((this.options.direction == 1) && (this.current >= this.options.stop))
				|| ((this.options.direction == -1) && (this.current <= this.options.stop)))
		{
		    this.stop();
		    if(this.options.loop != null) {
                var newOptions = this.options.loop;
                this.options.loop = null;
                newOptions.loop = this.options;
                this.start(this.el, newOptions);
		    } else {
		        if(this.current > 0) {
	                this.el.style.visibility = 'visible';
	                this.el.style.display = '';
	            } else {
	        	    this.el.style.visibility = 'hidden';
	        	    if(!this.options.keepPos)
	                    this.el.style.display = 'none';
	            }
	            if(this.current <= 0 || this.current >= 1) {
	        	    if(this.proxy.parentNode != null)
	                    this.proxy.parentNode.replaceChild(this.el, this.proxy);
		            delete this.proxy;
		            killFade(this.name);		            
			    }
		    }
			if (this.options.onComplete)
            	this.options.onComplete();
		}
	};
	
	function setOpacity (el, opacity)
	{					
		var percent = Math.floor(opacity * 100);
			
		// IE
		el.style.zoom = 1;
		el.style.filter = 'alpha(opacity=' + percent + ')';
					
		// CSS 3
		el.style.opacity = opacity;
	};
};
function toggleMenu() {
    if(readCookie('menustate') == 'collapsed') {
        expandMenu();        
    } else {
        collapseMenu();        
    }
};
function toggleWindow() {
    if(readCookie('dashboardstate') == 'maximized') {
        restore();
    } else {
        maximize();        
    }
}; 
function collapseMenu() {
    startHideItem(document.getElementById('divAdminMenu'), 1, 0, .3, false, finishCollapse);
    createCookie('menustate','collapsed',365);
};
function expandMenu() {
    document.getElementById('divLeftMenuContainer').style.width='170px';
    document.getElementById('divTopSlider').style.marginLeft='156px'; 
    document.getElementById('divAdminMenuSmall').style.display='none';
    startShowItem(document.getElementById('divAdminMenu'), 0, 1, .3, null);
    createCookie('menustate','expanded',365);    
};
function finishCollapse() {
    document.getElementById('divAdminMenuSmall').style.display='';
    document.getElementById('divLeftMenuContainer').style.width='47px';
    document.getElementById('divTopSlider').style.marginLeft='33px';
};
function maximize() {
    document.getElementById('divAdminDashboardCloak').style.position='absolute';
    document.getElementById('divAdminDashboardCloak').style.top='0px';
    document.getElementById('divAdminDashboardCloak').style.left='0px';
    document.getElementById('divAdminDashboardCloak').style.width=screen.width + 'px';
    document.getElementById('divAdminDashboardCloak').style.height=(document.body.offsetHeight + document.body.scrollHeight) + 'px';
    document.body.style.overflowX = 'hidden';
    startShowItem(document.getElementById('divAdminDashboardCloak'), 0, 1, .3, finishMaximize);
    createCookie('dashboardstate','maximized',365);    
};
function finishMaximize() {
    scroll(0,0);
    document.getElementById('divAdminDashboard').style.position='absolute';
    document.getElementById('divAdminDashboard').style.top='0px';
    document.getElementById('divAdminDashboard').style.left='0px';
    document.getElementById('divAdminDashboard').style.width=(screen.width - 17) + 'px';
    document.getElementById('divAdminDashboard').style.height=(document.body.offsetHeight + document.body.scrollHeight) + 'px';
    startHideItem(document.getElementById('divAdminDashboardCloak'), 1, 0, .3, false, null);
};
function restore() {
    document.getElementById('divAdminDashboardCloak').style.position='absolute';
    document.getElementById('divAdminDashboardCloak').style.top='0px';
    document.getElementById('divAdminDashboardCloak').style.left='0px';
    document.getElementById('divAdminDashboardCloak').style.width=screen.width + 'px';
    document.getElementById('divAdminDashboardCloak').style.height=(document.body.offsetHeight + document.body.scrollHeight) + 'px';
    startShowItem(document.getElementById('divAdminDashboardCloak'), 0, 1, .3, finishRestore);  
    createCookie('dashboardstate','standard',365);     
};
function finishRestore() {
    document.getElementById('divAdminDashboard').style.position='';
    document.getElementById('divAdminDashboard').style.top='';
    document.getElementById('divAdminDashboard').style.left='';
    document.getElementById('divAdminDashboard').style.width='811px';
    document.getElementById('divAdminDashboard').style.height='';
    document.body.style.overflowX = '';
    startHideItem(document.getElementById('divAdminDashboardCloak'), 1, 0, .3, false, null);    
};
function createCookie(name,value,days) {
    if (days) {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
};
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
	    var c = ca[i];
	    while (c.charAt(0)==' ') c = c.substring(1,c.length);
	    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
};
function loadDashboardState() {
    if(readCookie('dashboardstate') == 'maximized') {
        document.getElementById('divAdminDashboard').style.position='absolute';
        document.getElementById('divAdminDashboard').style.top='0px';
        document.getElementById('divAdminDashboard').style.left='0px';
        document.getElementById('divAdminDashboard').style.width=(screen.width - 17) + 'px';
        document.getElementById('divAdminDashboard').style.height=(document.body.offsetHeight + document.body.scrollHeight) + 'px';
        document.body.style.overflowX = 'hidden';
    }
    if(readCookie('menustate') == 'collapsed') {
        document.getElementById('divAdminMenu').style.display='none';
        document.getElementById('divLeftMenuContainer').style.width='47px';
        document.getElementById('divTopSlider').style.marginLeft='33px';
        document.getElementById('divAdminMenuSmall').style.display='';
    }
};
var upperSearchInMotion = null;
var upperSearchLoaded = false;
var upperSearchState = 0;
function toggleUpperSearch() {
    if(upperSearchState == 0) {
        showUpperSearch();
    } else {
        hideUpperSearch();
    }
};

function showUpperSearch() {
    if(upperSearchInMotion == null) {
        if(!upperSearchLoaded) {
            DashboardAjaxRequest('upperSearch');
            upperSearchLoaded = true;
        } else {
            upperSearchInMotion = window.setInterval('grow(\'divUpperSearch\', 50, 500);', 50); 
        }
    }
};
function hideUpperSearch() {
    if(upperSearchInMotion == null) {
        upperSearchInMotion = window.setInterval('grow(\'divUpperSearch\', -50, 0);', 50);
    }
};
function grow(name, speed, end) {
    var obj = document.getElementById(name);
    var size = Number(obj.style.height.replace('px','')) + speed;
    if(size <= 0) {
        if(size < 0)
            size = 0;
        obj.style.display = 'none';
    } else {
        obj.style.display = '';
    }
    
    if(end <= 0) { 
        upperSearchState = 0;
        document.getElementById('imgQuickAccess').src = '/DesktopModules/PremierPOS.AdminDashboard/Images/quickAccessTab.jpg';
    } else {
        upperSearchState = 1;
        document.getElementById('imgQuickAccess').src = '/DesktopModules/PremierPOS.AdminDashboard/Images/quickAccessTabHover.jpg';
    }   
    
    if(size == end || ((speed > 0 && size > end) || (speed < 0 && size < end)) || (speed > 0 && size > obj.scrollHeight)) {
        window.clearInterval(upperSearchInMotion);    
        upperSearchInMotion = null;   
    }
    
    if(!((speed > 0 && size > end) || (speed < 0 && size < end)))
        obj.style.height = size.toString() + 'px';
};
function GetCityState(postalCode) {
    var service = new Service();
    service.GetCityState(postalCode, GetCityStateCallback);
};
function GetCityStateCallback(value) {
    alert(value);
};
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 
