﻿var PAGE = {
    WIDTH: 0,
    HEIGHT: 0,
    TOP_MARGIN: 0,
    HORIZONTAL_MARGIN: 0,
    LEFT_MARGIN: 52,
    CONTENT_WIDTH: 0,
    CONTENT_HEIGHT: 0,
    NOTIFICATIONS: false,

    init: function(contextMenuAvailable) {
        this.WIDTH = getWindowDimensions()[0];
        this.HEIGHT = getWindowDimensions()[1];

        this.TOP_MARGIN = 2;
        this.HORIZONTAL_MARGIN = 2;

        this.CONTENT_WIDTH = this.WIDTH - this.LEFT_MARGIN - 2;
        this.CONTENT_HEIGHT = this.HEIGHT - 2 * this.TOP_MARGIN;

        if (contextMenuAvailable) {
            document.onmouseup = function(e) {
                PAGE.hideContextMenus();
            }
        }
    },

    notify: function() {
        var now = new Date();
        if (now - DataContainer.myLastActivity > 60000) {
            /*if (document.title.include('Masz wiadomość')) {
                document.title = '....................................................';
            }
            else {*/
            document.title = 'Masz wiadomość......................................';
            //}
            //setTimeout('PAGE.notify()', 1000);
        }
    },

    closeFullSizeWindows: function() {
        profileEditWindow.hideWindow();
        places.hideWindow();
    },

    onContextMenu: function(e) {
        var mousePos = this.getMousePosition(e);
    },

    hideContextMenus: function() {
        contacts.hideContextMenu();
    },

    getMousePosition: function(e) {
        var docX, docY;

        if (e) {
            //all other browsers
            docX = e.pageX;
            docY = e.pageY;
        }
        else {
            //IE
            docX = event.clientX;
            docY = event.clientY;
        }

        return [docX, docY];
    },

    setTitle: function(message) {
        document.title = message;
    }
}

var MainHelper = {
    unescapeHTML: function(text) {
        /*
        Wielkie litery 	Ą 	    Ć 	    Ę 	    Ł   	Ń 	    Ó 	    Ś 	    Ź 	    Ż
        HTML 	        &#260; 	&#262; 	&#280; 	&#321; 	&#323; 	&#211; &#346; 	&#377; 	&#379;
        
        małe litery 	ą 	    ć 	    ę 	    ł 	    ń 	    ó 	    ś 	    ź 	    ż
        HTML 	        &#261; 	&#263; 	&#281; 	&#322; 	&#324; 	&#243; 	&#347; 	&#378; 	&#380;
        */
        var newText = text.gsub('&#260;', 'Ą');
        newText = newText.gsub('&#262;', 'ć');
        newText = newText.gsub('&#280;', 'Ę');
        newText = newText.gsub('&#321;', 'Ł');
        newText = newText.gsub('&#323;', 'Ń');
        newText = newText.gsub('&#211;', 'Ó');
        newText = newText.gsub('&#346;', 'Ś');
        newText = newText.gsub('&#377;', 'Ź');
        newText = newText.gsub('&#379;', 'Ż');

        newText = newText.gsub('&#261;', 'ą');
        newText = newText.gsub('&#263;', 'ć');
        newText = newText.gsub('&#281;', 'ę');
        newText = newText.gsub('&#322;', 'ł');
        newText = newText.gsub('&#324;', 'ń');
        newText = newText.gsub('&#243;', 'ó');
        newText = newText.gsub('&#347;', 'ś');
        newText = newText.gsub('&#378;', 'ź');
        newText = newText.gsub('&#380;', 'ż');

        return newText;
    }
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function scrollChatWindowDown(chatWindowNodeName) {
	if(DataContainer.scrolling == true)
	{
	    var chatWindowNode = $(chatWindowNodeName);
	    if (chatWindowNode) {
	        var scrollY = 10000000;
	        $(chatWindowNodeName).scrollTop = scrollY;
	    }
	}

	/*if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    window.pageYOffset = scrollY;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    document.body.scrollTop = scrollY;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    document.documentElement.scrollTop = scrollY;
	  }*/
}

function getWindowDimensions()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
  	}

  	return [myWidth,myHeight];
}

function elementValignMiddle(elementID)
{
    var pageHeight = getWindowDimensions()[1];
    var elementHeight = $(elementID).getHeight();
    
    var middle = pageHeight / 2;
    var result = middle - elementHeight / 2;
    
    if(result < 0) result = 0;    
        
    $(elementID).style.marginTop = result + 'px';
}

function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}


