re = new RegExp(/^http:\/\/([^\/]+\/)/);
var wwwRoot = re.exec(window.location)[0]+"";

var core = {

    loadmodule : function(moduleId, itemId){
        Effect.Fade('main-flyout', { duration: 0.5 });
        Effect.Appear('main-article', { duration: 0.5 });
    },

    popup : function (popupOptions){

        var title        = (popupOptions.Title) ? popupOptions.Title : '&nbsp;';
        var html         = (popupOptions.Body) ? popupOptions.Body : '&nbsp;';
        var dialogWidth  = (popupOptions.Width) ? popupOptions.Width : Extensions.defaultDialogWidth;
        var nonClosable  = (popupOptions.nonClosable) ? 'none' : 'block';

        PopupHandler.disableEscapeKey( (popupOptions.disableEscapeKey) ? true : false );

        $('dialog-close-button').setStyle({
          display: nonClosable
        });

        $('dialog-top-center').innerHTML = title;

        PopupHandler.showDialogBox(html, dialogWidth);
    },

    popupClose : function () {
        PopupHandler.deactivate();
    },
    
    getPageSize : function(){
            
        var xScroll, yScroll;
        
        if (window.innerHeight && window.scrollMaxY) {    
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
        
        var windowWidth, windowHeight;

        if (self.innerHeight) {    // all except Explorer
            if(document.documentElement.clientWidth){
                windowWidth = document.documentElement.clientWidth; 
            } else {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;

        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }    
        
        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        } else { 
            pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){    
            pageWidth = xScroll;        
        } else {
            pageWidth = windowWidth;
        }

        return {
            'windowHeight' : windowHeight,
            'pageWidth'    : pageWidth,
            'pageHeight'   : pageHeight,
            'windowWidth'  : windowWidth
        };
    },

    changeNews : function(id)
    {
    },
    
	changeImage : function(imagelist, index)
	{
		$('large-title').innerHTML = imagelist[index].title;
		$('large-image').src       = imagelist[index].image;

		$('large-next').onclick = function()
		{
			core.changeImage(imagelist, (imagelist[index+1]) ? index+1 : 0);
		}

		$('large-prev').onclick = function()
		{
			core.changeImage(imagelist, (imagelist[index-1]) ? index-1 : (imagelist.length-1));
		}

		$('large-image').onclick = function()
		{
			core.changeImage(imagelist, (imagelist[index+1]) ? index+1 : 0);
		}
	},
		
	StoreComment : function (id, comment, name, email)
	{
        new Ajax.Request(
            '/blogs/comment/'+id+'/store/',{
                method: "post",
                parameters: {
					comment : comment,
					name    : name,
					email   : email
                },
                onComplete: function(request){

                    eval("var response = " + request.responseText);

					if (response.errors != 0)
					{
						alert(response.data);
					}
					else
					{
						alert(response.data);
						window.location = window.location;
					}
                }
            }
        );
	}
};
