﻿/***********************************
	
	qPanel.jquery@hazg.net	
	
***********************************/
$(function(){
	
	$(document).keypress(function(e)
	{
		if(e.which == 96)
		{
			$('#qp_dbg').toggle();
		}
	});
})
function D(text)
{
	if(!$('#qp_dbg').length)
	{
		$('<div id="qp_dbg" style="padding:0px;margin:0px;top:0px;left:0px;overflow:auto;position:absolute;width:100%;height:150px;color:gray;background:white;"></div>').appendTo($('body')).
		css({opacity:.7})
		.mouseover(function()
		{
			$(this).css({opacity:1});
		})
		.mouseout(function(){
			$(this).css({opacity:.7});
		});
		$('#qp_dbg').click(function(){$(this).hide();});
	}
	/*if(console && console != undefined) console.debug(text);
	else*/
	
	$('#qp_dbg').html($('#qp_dbg').html()+'<div style="font-size:11px;font-family:Consolas;">'+text.toString()+'</div>').show();
	
}

var eventsTray = {

	icon : 			"/images/tray/events.gif",
	trayPanel : 	null,
	speed :			'2000 ms',
	checkMutex :	false,
	Start:function(trayPanel)
	{
		$('body').everyTime(this.speed,'eventsJSON',function(){eventsTray.checkNew();});
	},
	Stop:function()
	{
		$('body').stopTime('eventsJSON');
	},	
	checkNew:function(){
		/*if(eventsTray.checkMutex==true) return;
		eventsTray.checkMutext = true;*/
		$.get('/my/includes/json/events.php', function(data){
			var e = eventsTray.parseJSON(data);
			eventsTray.checkMutex = false;
			if(e.length&&e.length>0)
			{
				qp.setTray(eventsTray.icon);
				qp.Url('/my/events/list/');
				eventsTray.Stop();
			}
		});
	},
	
	parseJSON:function(data)
	{
		try
		{
			if (typeof(JSON) == 'object' && JSON.parse)
            	return JSON.parse(data);
	        return eval("(" + data + ")");
		}
		catch(e){return '';}
	}
};

var qp = {
	trayListeners : Array(),
	Tray:function(o)
	{
		o.Start(this);
		this.trayListeners.push(o);
	},
	//
	
	options:{
		TrueClickOnBackground :	false,
		layerOpacity :			'0.8',
		layerBackgound :		'#FFF'
	},
	
	
	panel : 		null,
	layer : 		null,
	myFrame : 		null,
	myTitle : 		null,
	normalTitle : 	null,
	smallTitle : 	null,
	minimized : 	{width:34,height:24},
	maximized : 	{width:660,height:600},
	defaultUrl : 	'/my/',
	
	
	
	Init:function()
	{
		this.panel = $('\
		<div class="qpanel" style="z-index:16777271;">\
			  <table class="qtable"><tr id="qtitle"><td style="padding-left:5px;">test</td><td id="minimize" class="qpToggle">свернуть</td></td><td id="qtray" style="width:24px;height:24px;"><img class="qpToggle" src="/images/qpanel/maximize.png" id="minmax" /></td></tr></table>\
			<iframe allowtransparency="true" frameborder="0" id="qframe" src=""></iframe>\
		</div>'
		).appendTo('body');
		this.layer = $('<div id="qlayer" class="qpToggle" style="z-index:16777270;background:'+this.options.layerBackgound+';left:0px;top:0px;position:absolute;"></div>').appendTo('body')
		.css('opacity',this.options.layerOpacity);
		$('a[target="qpanel"]').click(function(e)
		{
			e.preventDefault();
			qp.Url($(this).attr('href'));
			qp.Toggle();
			return false;
		});
		this.myFrame = $('#qframe');
		$('.qpToggle')			.click (function(e){e.preventDefault(); qp.Toggle(); return false;});
		$('.qpanel')			.select(		function(){return false;}		);
		$(window)				.resize(		function(){/*qp.Minimize();*/	}) /*!!!!!!!!! IE7-8 */
								.scroll(		function(){qp._scrolling = true;});
		
		this.myTitle = $('#qtitle').find('td').eq(0);
		
		this.Minimize();
		this.panel.everyTime('300ms', function(){qp.Posit();});
		return this;
	},
	
	Title:function(html)
	{
		if(html==undefined) return this.normalTitle;
		if(!this.IsMinimized())
			this.myTitle.html(html);
		this.normalTitle = html;
		return this;
	},
	
	SmallTitle:function(html)
	{
		if(this.IsMinimized())
			this.myTitle.html('');
		
		/*if(html==undefined) return this.smallTitle;
		if(this.IsMinimized())
			this.myTitle.html(html);
		this.smallTitle = html;*/
		return this;
	},
	_isminimized : true,
	IsMinimized:function()
	{
		return this._isminimized;
	},
	currentUrl : null,
	Url:function(url)
	{
		if(url==undefined)
		{
			var u = this.currentUrl?this.currentUrl:this.defaultUrl;
			return ((u==undefined)?false:u);
		}
		
		this.currentUrl = url;
		return this;
	},
	Show:function(){qp.panel.show();return this;},
	Hide:function(){qp.panel.hide();return this;},
	
	currentSize : null,
	wind : $(window),
	
	Posit:function(size)
	{
		if(this._scrolling == 1){this._scrolling = 0; return this};
		var show = false;
		var wW = this.wind.width();
		var wH = this.wind.height();
		

		if(size&&(!this.currentSize||this.currentSize.width!=size.width||this.currentSize.height!=size.height))
		{		
			qp.panel.hide();
			show = true;
			this.currentSize = size;
			if((wW<size.width)||(wH<size.height))
			{
				this.currentSize.width = ((wW<size.width)?wW:(size.width));
				this.currentSize.height = ((wH<size.height)?wH:(size.height));
			}
			
			qp.panel
				.width	(this.currentSize.width)
				.height	(this.currentSize.height);
		}
		qp.myFrame.height (this.currentSize.height-this.minimized.height-2);
		
		qp.panel.css(
			{
				'left':wW+this.wind.scrollLeft()-this.currentSize.width-2,
				'top':wH+this.wind.scrollTop()-this.currentSize.height-2
			}
		);
		if(show)qp.panel.show();
		
		return this;
	},
	
	Maximize:function()
	{
		//if(!this._isminimized) return this;
		//console.log('MAX');
		this._isminimized = false;
		this.layer
			.width($(document).width())
			.height($(document).height())
		.show();
		
		this.Title(this.normalTitle);
		if(false == this.Url())	
		{
			this.Url(this.defaultUrl);
		}
		
		this.clearSelection();
		$('.qpanel #minimize').show();
		$('.qpanel #minmax').attr('src', '/images/qpanel/minimize.png');
		this.Posit({width:this.maximized.width,height:this.maximized.height});
		this.clearSelection();
		qp.myFrame.attr('src',this.Url());
		$('.qpanel #qtitle').removeClass('qpToggle');
		return this;
	},
	InitTray:function()
	{
		$('.qpanel #minmax').attr('src', '/images/qpanel/maximize.png');
		for(var i = 0; i<this.trayListeners.length; i++)
		{
			this.trayListeners[i].Stop();
			this.trayListeners[i].Start();
		}
	},
	Minimize:function()
	{
		//if(this._isminimized == true && this._isminimized != undefined) return this;
		//console.log('MIN');
		this._isminimized = true;
		$(this.layer).hide();
		this.SmallTitle(this.smallTitle);
		$('.qpanel #minimize').hide();
		$('.qpMaximize, .qpMinimize').unbind('click');
		$('.qpanel #minmax').attr('src', this.trayIcon);
		this.Posit({width:this.minimized.width,height:this.minimized.height});
		this.InitTray();
		
		return this;		
	},
	
	Toggle:function(){ return this.IsMinimized()?qp.Maximize():qp.Minimize();	},
	
	clearSelection:function()
	{
		if (window.getSelection) { window.getSelection().removeAllRanges(); }
		else if (document.selection && document.selection.clear)		
			document.selection.clear();
	},

	trayIcon : '/images/qpanel/maximize.png',
	setTray:function(url)
	{
		if(url == undefined)
		{
			$('.qpanel #minmax').attr('src', 
				(this.IsMinimized()?'/images/qpanel/maximize.png':'/images/qpanel/minimize.png')
			);
		}
		else
		{
			$('.qpanel #minmax').attr('src', 
				(this.IsMinimized()?url:'/images/qpanel/maximize.png')
			);
		}
		this.trayIcon = url;
	}
};

