var TinyMceActivated = false;
var loading_done = false;
var showQueue = new Array(); 

jQuery(document).ready(function()
{
	// Init actions
	loadSiteMenu('menu');
	loadPageContent(getCurrentPageName(),'',getCurrentPageID(),getHasChildren(),getCurrentPagePlugin());
});

// Function to center the object
jQuery.fn.centerScreen = function()
{
	var obj = this;
	obj.css('top', jQuery(window).height()/2-this.height()/2);
    obj.css('left', jQuery(window).width()/2-this.width()/2);
    jQuery(window).resize(function(){obj.centerScreen();});
}

// Function that gives a delay on the object
jQuery.fn.timeOut = function(time)
{
	var obj = this; 
    obj.queue(function(){setTimeout(function(){obj.dequeue()}, time)});
};

// Function checks if element exists
jQuery.fn.exists = function()
{
	return jQuery(this).length > 0;
}

// Get & set current page id
function setCurrentPageID(PageID)
{
	CurrentPageID = PageID;
}
function getCurrentPageID()
{
	return CurrentPageID;
}

// Get & set current page name
function setCurrentPageName(PageName)
{
	CurrentPageName = PageName;
}
function getCurrentPageName()
{
	return CurrentPageName;
}

// Get & set current page name
function setCurrentPageParentID(PageParentID)
{
	CurrentPageParentID = PageParentID;
}
function getCurrentPageParentID()
{
	return CurrentPageParentID;
}

// Get & set has children variable of current page
function setHasChildren(Children)
{
	HasChildren = Children;
}
function getHasChildren()
{
	return HasChildren;
}

// Get & set current page plugin
function setCurrentPagePlugin(Plugin, callback)
{
	if(typeof callback == "undefined")callback = function(){};
	CurrentPagePlugin = Plugin;
	
	callback();
}
function getCurrentPagePlugin()
{
	return CurrentPagePlugin;
}

// Get & set current plugin position in page
function setTextPosition(Position)
{
	TextPosition = Position;
}
function getTextPosition()
{
	return TextPosition;
}

// Get & set Tiny MCE Activated to true or false
function setTinyMceActivated(active)
{
	TinyMceActivated = active;
}
function getTinyMceActivated()
{
	return TinyMceActivated;
}

// Load dialog
function loadDialog(div,content_url,icon,name,callback,login,width)
{
	if (typeof callback == "undefined")callback = function(){};
	if (typeof width == "undefined")width = 700;
	
	setDialogWidth(div, width);
	jQuery('#'+div+'_content').empty().load(content_url,'',function(){callback();activateDialogWindow(div,login);});
	jQuery('#'+div+'_title').html(name);
	setDialogWindowIcon(div,icon+'.png','/img/framework/dialog_icons/'+icon+'.png');
}

function setDialogWidth(div, width)
{
	jQuery('#'+div).width(width);
}

// Activates the dialog window and deactivates the main menu
function activateDialogWindow(div,login)
{
	if(typeof login == "undefined" || login == null)
	{
		if(jQuery('#main_menu').is(':visible'))
		{
			hideMainMenu();
		}	
	}
	showDialogWindow(div);
}

// Shows the dialog window
function showDialogWindow(div)
{
	showFade('fade', 0.7);	
	jQuery('#'+div).centerScreen();
	jQuery('#'+div).mb_open();
}

// Hides the dialog window
function hideDialogWindow(div)
{
	jQuery('#'+div).mb_close();
	jQuery('#'+div).unbind();
}

// Set new dialog window icon
function setDialogWindowIcon(div, icon_name, icon_path)
{
	jQuery('#'+div).attr('icon',icon_name).find('.icon').attr('src',icon_path);
}

// Shows the ajax loader (position = 1 || 2)
function showAjaxLoader(position)
{
	switch(position)
	{
		case 1:
			jQuery('#ajax_loader').css('margin-left','-'+(jQuery('#ajax_loader').width()/2)+'px').show('fast');
			break;
		case 2:
			jQuery('#ajax_loader').css('margin-left','-'+((jQuery('#ajax_loader').width()/2)+107)+'px').show('fast');
			break;
	}
}

// Shows the site submenu
function showSubmenu(div)
{
	var slide_height = '30';
	//jQuery('#'+div).css('height', '0px').animate({height: slide_height+'px'},'fast');
}

// Hides the site submenu
function hideSubmenu(div)
{	
	if(jQuery('#'+div).is(':visible'))
	{
		//jQuery('#'+div).animate({height: '0px'},'fast',function(){jQuery('#'+div).hide();});
	}
}

// Shows the fade overlay
function showFade(div,transparency)
{
	if(jQuery('#'+div).exists() && jQuery('#'+div).is(':hidden'))
	{
		jQuery('#'+div).css('display','block').css('opacity', 0).fadeTo('fast', transparency);
	}
}

// Hides the fade overlay
function hideFade(div)
{
	if(jQuery('#'+div).exists() && jQuery('#'+div).is(':visible'))
	{
		jQuery('#'+div).fadeTo('fast', 0,function(){jQuery('#'+div).hide()});
	}
}

// Load site menu
function loadSiteMenu(div)
{
	jQuery('#'+div).load('/modules/edit_page/load.php?item=menu','',function()
	{
		// Bind the drop down menu
		jQuery('#'+div).ptMenu();
	});
}

// Load site submenu
function loadSiteSubmenu(div,parent_id)
{
	jQuery('#'+div).load('/modules/edit_page/load.php?item=submenu&id='+parent_id,'',function()
	{
		showSubmenu(div);
		
		// Set all sub items to have font-weight normal, and the active one to have font-weight bold
		jQuery('.sub_item').each(function()
		{
			jQuery(this).css('font-weight','normal');
			
			var split = jQuery(this).attr('id').split('sub_item');
			
			if(split[1] == getCurrentPageID())
			{
				jQuery(this).css('font-weight','bold');				
			}
		});
	});	
}

// Load the page content
// p_name	= page name
// s_name	= sub page name
// id		= page or sub page - id
function loadPageContent(p_name, s_name, id, has_children)
{
	showAjaxLoader(2);
	
	p_name = URLEncode(p_name);
	s_name = URLEncode(s_name);
	
	jQuery('#normal_content').hide('fast');
	//jQuery('#page_content_tbl').hide('fast');
	jQuery('#plugin_top').hide('fast').empty();
	jQuery('#plugin_bottom').hide('fast').empty();
	
	jQuery('#footer').empty();
	jQuery('#container').css('background-position','-1817px 0px');
	jQuery('#content_top').css('background-position','0px 0px');
	jQuery('#permanent_assets').show('fast');
	
	if(!getTinyMceActivated())
	{
		showQueue[showQueue.length] = '#normal_content';
		
		if(s_name == "")
		{
			setCurrentPageID(id);
			setCurrentPageName(p_name);
			jQuery('#normal_content').load('/modules/edit_page/load.php?item=page_content&p='+p_name, function(){});
			
			//if(has_children == 'true')
			//{
				loadSiteSubmenu('submenu',id);
			//}
		}
		else
		{
			setCurrentPageID(id);
			setCurrentPageName(s_name);
			
			if(jQuery('#sub_title').attr('class') != getCurrentPageParentID())
			{
				loadSiteSubmenu('submenu',getCurrentPageParentID());
			}
			
			jQuery('#normal_content').load('/modules/edit_page/load.php?item=page_content&p='+p_name+'&s='+s_name, function(){});
		}
	}
	else
	{
		showQueue[showQueue.length] = '#page_content_tbl';
		
		var ed = tinyMCE.get('page_content');
		if(s_name == "")
		{
			jQuery.get('/modules/edit_page/load.php?item=page_content&p='+p_name, function(data)
			{
				setCurrentPageID(id);
				setCurrentPageName(p_name);
				ed.setContent(data);
				
				//if(has_children == 'true')
				//{
					loadSiteSubmenu('submenu',id);
				//}
			});
		}
		else
		{
			jQuery.get('/modules/edit_page/load.php?item=page_content&p='+p_name+'&s='+s_name, function(data)
			{
				setCurrentPageID(id);
				setCurrentPageName(s_name);
				ed.setContent(data);
				
				var head_page = jQuery('.sub_title').attr('id').split('sub_title');				
				if(head_page[1] != getCurrentPageParentID())
				{
					loadSiteSubmenu('submenu',getCurrentPageParentID());
				}
				else
				{
					// Set all sub items to have font-weight normal, and the active one to have font-weight bold
					jQuery('.sub_item').each(function()
					{
						jQuery(this).css('font-weight','normal');
						
						var split = jQuery(this).attr('id').split('sub_item');
						
						if(split[1] == getCurrentPageID())
						{
							jQuery(this).css('font-weight','bold');				
						}
					});
				}
			});
		}
		
		// If a page plugin is active, load the tab
		handlePluginTab();
	}
	
	// Loads the page plugin
	if(getCurrentPagePlugin() != '')
	{
		jQuery.ajax
		({
            type: 'GET',
            url: '/modules/'+getCurrentPagePlugin()+'/index.php',
            data: '',
          	success: function(data)
          	{
          		if(getTextPosition() == 'bottom')
          		{
          			showQueue[showQueue.length] = '#plugin_top';
                	jQuery('#plugin_top').html(data);
           		}
           		else if(getTextPosition() == 'top')
           		{
           			showQueue[showQueue.length] = '#plugin_bottom';
           			jQuery('#plugin_bottom').html(data);
           		}
           		else
           		{
           			// Text Position is set to none
           			jQuery('#normal_content').hide('fast');
					jQuery('#page_content_tbl').hide('fast');
					showQueue[showQueue.length] = '#plugin_top';
           			jQuery('#plugin_top').html(data);
           		}
      		}
		});
	}
	else
	{
		jQuery.each(showQueue,function(i,item)
		{
			jQuery(item).show('fast');
		});
		showQueue = [];
		loading_done = true;
	}
}