var cur_woj;
function swapLayers(id) {
  if (cur_woj) hideLayer(cur_woj);
  showLayer(id);
  cur_woj = id;
}

function showLayer(id) {
  var woj = getElemRefs(id);
  if (woj && woj.css) woj.css.display = "block";
}

function hideLayer(id) {
  var woj = getElemRefs(id);
  if (woj && woj.css) woj.css.display = "none";
}

function getElemRefs(id) {
	var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? document.layers[id]: null;
	if (el) el.css = (el.style)? el.style: el;
	return el;
}




var _drag_drop = false;
$(document).ready(function() {
	if(!_drag_drop)
		return true;
	domOperations = {
		
		_list: new Array,	
		_speed : "slow",
		
		init: function() {
			eval($.cookie('order'));
			if(typeof(a) == 'object') {
				this.load(a);
			}
			var div_list = $('div.lap3_gradient').get();
			for(i in div_list) {		
				if(typeof(div_list) == 'object') {
					this._list[i] = div_list[i];
					if(div_list[i].id != undefined && $.cookie(div_list[i].id) != null) {						
						if(parseInt($.cookie(div_list[i].id)) > 0)
							showLi($(div_list[i]).children('.content_container').children('.content').children(),$.cookie(div_list[i].id));
					} 
				}
			}			
		},		
		
		check: function(div) {
			id = div[0].id;
			for(i in this._list)
				if(this._list[i].id == id)
					return this._list[i];
		},
		
		checkNo: function(div) {				
			id = div[0].id;
			for(i in this._list)
				if(this._list[i].id == id)
					return i*1;
		},
		
		doUpButtons: function(a_list) {						
			$(a_list).bind('click', function() { 
					domOperations.goUp($(this.parentNode.parentNode.parentNode));
				});
		}, 
		
		doDownButtons: function(a_list) {
			$(a_list).bind('click', function() { 
					domOperations.goDown($(this.parentNode.parentNode.parentNode));
				});
		},				
				
		goUp: function(el) {
			no = this.checkNo(el);
			if(no == 0)
				return;
			next = no -1;	
			a = this;
			$(this._list[next]).fadeOut(this._speed);		
			$(this._list[no]).fadeOut(this._speed, function() {				
 						$(a._list[no]).fadeIn(a._speed).insertBefore($(a._list[next]));
				$(a._list[next]).fadeIn(a._speed);
				_tmp = a._list[next];
				a._list.splice(next,2,el[0],_tmp);
			});			
			$.cookie('order', domOperations.toSave(), {expires: 30});
		},
						
		goDown: function(el) {
				
		
			no = this.checkNo(el);
			
			if(no == this._list.length-1)
				return ;
			next = no +1;
			a = this;
			$(this._list[next]).fadeOut(this._speed);
			$(this._list[no]).fadeOut(this._speed, function() {		
				$(a._list[next]).fadeIn(a._speed).insertBefore($(a._list[no]));
				$(a._list[no]).fadeIn(a._speed);					
				_tmp = a._list[next];
				a._list.splice(no,2,_tmp,el[0]);
			});
			$.cookie('order', domOperations.toSave(), {expires: 30});
		},
		
		load: function(ar) {
			for(i=ar.length;i>=0;i--) {
				$('#'+ar[i]).prependTo($('#c_right'));
			}
		},
		
		toSave: function() {
			var div_list = $('div.lap3_gradient');
			for(i in div_list)		
				this._list[i] = div_list[i];
			var string = new Array();
			for(i=0;i<this._list.length-1;i++) {
				string[i] = "'"+this._list[i].id+"'";	
			}
			_string='a = [ '+string.join(', ')+' ]';
			return _string;
		}
	};
	
	domOperations.init();
	domOperations.doUpButtons($('div.scrolls > a.up'));
	domOperations.doDownButtons($('div.scrolls > a.down'));
	
	$('div#c_right').Sortable(
    {
        accept :	'lap3_gradient',
        opacity:	0.4,
        fx:	1,
        axis:	'vertically',
        revert:	true,
		ghosting:	true,
		onchange:	function() {
			$.cookie('order', domOperations.toSave(), {expires: 30});
		}
    });
	
	function showLi(ul,n) {
		$.cookie(ul.parent().parent().parent()[0].id,n, {expires: 30});
		$.each(ul.parent().parent().parent().children('.info').children('.n').children('a'),function(i,el) {
			$(el).removeClass();
			if(el.href.substring(el.href.length-1) == n) {
				$(el).addClass('selected');
			}
		});
		var li = ul.children('li');
		for(i=1;i<=8;i++)
		{
			if(i < n) {
				$(li[i]).css('display','block');
			}
			else
				$(li[i]).css('display','none');
		}
	}
	
	$('div.n > a[@href*=#1]').click(function(){		
		showLi($(this).parent().parent().parent().children('.content_container').children('.content').children('ul'), 1);
	});
	
	$('div.n > a[@href*=#2]').click(function(){
		showLi($(this).parent().parent().parent().children('.content_container').children('.content').children('ul'), 2);
	});
	
	$('div.n > a[@href*=#3]').click(function(){
		showLi($(this).parent().parent().parent().children('.content_container').children('.content').children('ul'), 3);
	});
	
	$('div.n > a[@href*=#4]').click(function(){
		showLi($(this).parent().parent().parent().children('.content_container').children('.content').children('ul'), 4);
	});
	
	$('div.n > a[@href*=#5]').click(function(){
		showLi($(this).parent().parent().parent().children('.content_container').children('.content').children('ul'), 5);
	});
	
	$('div.n > a[@href*=#6]').click(function(){
		showLi($(this).parent().parent().parent().children('.content_container').children('.content').children('ul'), 6);
	});
	
	$('div.n > a[@href*=#7]').click(function(){
		showLi($(this).parent().parent().parent().children('.content_container').children('.content').children('ul'), 7);
	});
	
	$('div.n > a[@href*=#8]').click(function(){
		showLi($(this).parent().parent().parent().children('.content_container').children('.content').children('ul'), 8);
	});
	
	$('div.n > a[@href*=#9]').click(function(){
		showLi($(this).parent().parent().parent().children('.content_container').children('.content').children('ul'), 9);
	});
	
	$('div.h3 > a.adapt').click(function() {
		/*if($(this).parent().parent().children('.info').css('display') == 'block') {
			$(this).parent().parent().children('.info').css('display','none');
		} else {
			$(this).parent().parent().children('.info').css('display','block');
		}*/
		$(this).parent().parent().children('.info').animate({height: 'toggle', opacity: 'toggle'}, "slow");
	});
	
	$('div.lap3_gradient > .h3').ToolTip({className: 'inputsTooltip', position: 'mouse', delay: 200}).mouseover(function(){
		ile = $.cookie('ile');
		ile++;
		$.cookie('ile', ile);
		if(ile > 3) {
			$('div.lap3_gradient > .h3').attr('title', '');
		}
	});
	
		
	$('div.count > div.name > a[@href*=#1]').click(function() {
		$('div.count > div.name > a').removeClass();
		$(this).addClass('count_select')
		$('div.content > div').css('display','none');
		$('div.content > div#news1').css('display','block');
	});
	
	$('div.count > div.name > a[@href*=#2]').click(function() {
		$('div.count > div.name > a').removeClass();
		$(this).addClass('count_select')
		$('div.content > div').css('display','none');
		$('div.content > div#news2').css('display','block');
	});
	
	$('div.count > div.name > a[@href*=#3]').click(function() {
		$('div.count > div.name > a').removeClass();
		$(this).addClass('count_select')
		$('div.content > div').css('display','none');
		$('div.content > div#news3').css('display','block');
	});
	
	$('div.count > div.name > a[@href*=#4]').click(function() {
		$('div.count > div.name > a').removeClass();
		$(this).addClass('count_select')
		$('div.content > div').css('display','none');
		$('div.content > div#news4').css('display','block');
	});
	
	$('div.count > div.name > a[@href*=#5]').click(function() {
		$('div.count > div.name > a').removeClass();
		$(this).addClass('count_select')
		$('div.content > div').css('display','none');
		$('div.content > div#news5').css('display','block');
	});
	
	$('div.topmenu > a').click(function() {
			var href = this.href.substring(this.href.indexOf('#')+1);
			$('.submenu1').css('display', 'none');
			$('.submenu2').css('display', 'none');
			$('.submenu3').css('display', 'none');
			$('.submenu4').css('display', 'none');
			$('.submenu5').css('display', 'none');
			$.each($('div.topmenu > a > img'), function() {
				this.src = this.src.replace('_a.gif', '.gif');
			});
			$(this).children()[0].src = $(this).children()[0].src.replace('.gif', '_a.gif');
			if(href == 'dyrektorszkoly') {
				$('.submenu1').css('display', 'block');
				//$(this).children()[0]
			}
			else if(href == 'szkolenia')
				$('.submenu2').css('display', 'block');
			else if(href == 'katalogodeslan')
				$('.submenu3').css('display', 'block');
			else if(href == 'kontakt')
				$('.submenu5').css('display', 'block');
			else if(href == 'aktualnosci')
				$('.submenu4').css('display', 'block');
	});
	
	$('div.submenu1 > a > img').click(function() {
		$.each($('div.submenu1 > a > img'), function () {
			this.src = this.src.replace('_a.gif', '.gif');
		});
		this.src = this.src.replace('.gif', '_a.gif');		
//		return false;
	});	
	$('div.submenu2 > a > img').click(function() {
		$.each($('div.submenu2 > a > img'), function () {
			this.src = this.src.replace('_a.gif', '.gif');
		});
		this.src = this.src.replace('.gif', '_a.gif');
//		return false;
	});	
	$('div.submenu3 > a > img').click(function() {
		$.each($('div.submenu3 > a > img'), function () {
			this.src = this.src.replace('_a.gif', '.gif');
		});
		this.src = this.src.replace('.gif', '_a.gif');
	
//		return false;
	});	
	$('div.submenu4 > a > img').click(function() {
		$.each($('div.submenu4 > a > img'), function () {
			this.src = this.src.replace('_a.gif', '.gif');
		});
		this.src = this.src.replace('.gif', '_a.gif');
	
//		return false;
	});	
	$('div.submenu5 > a > img').click(function() {
		$.each($('div.submenu5 > a > img'), function () {
			this.src = this.src.replace('_a.gif', '.gif');
		});
		this.src = this.src.replace('.gif', '_a.gif');
	
//		return false;
	});	
	
	if($.browser.opera) {
		$('div.content > ul > li > a').click( function () {
//			return false;
		});
	}
});