d = document;
$j = jQuery;
num = parseInt;
function tag(o, v) {
	return (v) ? (o.tagName.toLowerCase() == v.toLowerCase()) : o.tagName.toLowerCase();	
}


function menu() {
	var o = $('#menu'), s = $('.s', o).removeClass('s'),
		out = function(o) {
		if (!o) return;
		o.stop().animate({backgroundPosition:'-280px 0'}, 300, function() {o.removeClass('hov');});
	},
		over = function(o) {
		if (!o) return;
		o.stop().animate({backgroundPosition:(-300+o.innerWidth())+'px 0'}, 300).addClass('hov');
	}
	
	o.data('itms', []);
	$("a", o).mouseover(
			function(e) {			
				clearTimeout(this.t1);			
				if (s[0] != e.target) {
					out(s);				
					over($(this));
				}
			})
		.mouseout(			
			function(e){
				clearTimeout(this.t1);
				
				var o = $(this),
					f = function() {
						out(o);
						over(s);
					}; 
			
				if (s[0] != e.target) {
								
					this.t1 = setTimeout(f, 10);
				}			
				
				
			}
	)
		.css({backgroundPosition:'-280px 0'})
		.click(function() {
			o.h = false;				
			o.open_url(this.href, $(this).text().substr(4));
			s = $(this);
			return (false);
		})
		.each(function() {
			var o2 = $(this);
			o.data('itms').push(o2);
			o2.push = function(h) {
				this.mouseover().click();
				o.h = (h) ? h : false;
				//if (h) d.location.href = h;
			}
		})
	
	$('a b', o).hover(
		function() {
			this.parentNode.parentNode.flag = true;
		},
		function() {
		 	var t = this.parentNode.parentNode;
			t.t1 = setTimeout(function() {t.flag = false; out(t);}, 100);
		}
		
	);	
	
	if (s) over(s);
	
	o.reset = function() {
		out(s);
		s = false;
	}
	
	o.open_url = function(href, txt) {
		if ($('body').hasClass('index')) {
				$('#index')[0].hide();
			} else {
				$('#body').animate({opacity:0}, 500);
			}
			
			out(s);
			
			if (d.hdr1) d.hdr1.fadeOut();
			
			setTimeout(
				function() {
					 $.post(href+((href.indexOf('?') > -1) ? '&' : '?' )+'ajax=1'+((o.h) ? '&'+o.h : ''),{},function(e) {												   
						var block = $('<div class="bl" />').html(e), b = $('#body').css('overflow','hidden');
						
						set_hdr(txt);
						
						b.height(b.height()).html('').append(block);						
						
						if (d.init) {
							d.init(block);
							d.init = false;
						}									
							
						b.animate({height:block.attr('scrollHeight')}, 500, 'swing', function() {block.removeClass('bl'); b.animate({opacity:1}, 500, 'swing', function() {b.css({opacity:'', height:'auto', overflow:'visible'})}) });					
						
					});				
					 
				}, 500); 	
	}
	
	return o;
}


function make_tabs(tabs, pads, opts) {	
	
	var o1 = this, s = s || 0,	
	
	def = {
		t:300,
		eff:function() {this.fadeIn(def.t)},
		click:function(e) {
			if (tag(this, 'a')) {
				e.preventDefault();
				e.stopPropagation();				
			}
			
			var o = $(this), _p = this._p;
			_p.s.removeClass('s');
			_p.s[0].pad.hide();
			o.addClass('s');
			
			_p.s = o;
			def.eff.apply(this.pad)//this.pad.fadeIn(def.t);			
		}	
	};
	if (opts) $.extend(def, opts)
	
	tabs.each(function(i, o) {
		o.pad = $(pads[i]);
		o._p = o1;
		o.i = i;
		o.click = def.click;
		
		var o = $(o).click(o.click)		
		
		if (o.hasClass('s') && !s) s = i;
		
	});	
	
	o1.s = $(tabs[s]).addClass('s');
	o1.tabs = tabs;
	pads.filter(function(i) {return i != s}).hide();
	
	return o1;
	
}

function auto_tabs(tabs, pads, opts) {
	
	var def = {t2:5000};
	
	opts = (opts) ? $.extend(def, opts) : def;
	
	var o = make_tabs(tabs, pads, opts);
	
	o.auto = {};
	o.auto.f = function() {											 
			if (o.s.parent().is(':hidden')) return;			
			o.tabs[(o.s[0].i < o.tabs.length - 1) ? o.s[0].i+1 : 0].click(); 
		}
		
	o.start = function() {
		clearInterval(o.auto.t1);
		o.auto.t1 = setInterval(o.auto.f, opts.t2);
	}
	
	o.pause = function() {
		//clearInterval(this.auto.t1);	
	}
	
	
	o.start();
	
	return o;
}

make_view = function() {
	var o = $('#view'), 
		fr1 = $('<div class="s_fr png" />'), s = false, v1 = $('#view1 #i1'), v = v1.parent(), hdr = $('<div class="hdr2" />').appendTo(d.hdr1), info = $('#view1 .info'), a = $('.area', o).fadeOut(1000), pr = $('#view2 .c').data('ratio', 613/220), fade = info.parent().parent().css({overflow:'hidden', height:info.parent().parent().innerHeight(), position:'relative'}),
	show_item = function(e) {
		var o = $(this);
		
		if (s) s.removeClass('s');
		o.append(fr1).addClass('s');
		
		hdr.text(this.hdr.innerHTML).hide().fadeIn();
		info.html(this.info.html())//.hide().fadeIn(1000);
		
		if (this.img.length) {
			v1.html(this.img.html()).filter(':hidden').show();
			var hr = this.img[0].href.split('.'), 
				ext = hr.pop(),
				img = $('<img src="'+this.img[0].href+'" />').load(
					function() {
						pr.html(img).scrollLeft(0).scrollTop(0);
						v[0].o = [pr.children(0), v1.children(0)];						
				});
			//pr.html();//this.img.html()
			//$('<img src="'+this.img[0].href+'" />');
			
			
			
			//if (v1.parent().css('height') != v1.attr('scrollHeight')) {
			//v1.parent().css('opacity',0).animate({height:v1.attr('scrollHeight'), opacity:1}, 700, 'swing');
		//}
		} else {
			v1.hide();
			
			
		}
		
		
		if (fade.height() != info.parent().attr('scrollHeight')) {
			fade.fadeTo(300, 0).animate({height:(this.img.length && v1.height() < 50) ? info.height()+180 : info.parent().attr('scrollHeight')+20, opacity:1}, 500, 'swing', function() {fade.css('opacity','')});
		}		
		
		s = $(this);
		return (false);
	}
	
	v1.resize = function(o) {			
		
		if (!v1.flag) {
			//$('#view2 .png').fadeOut(100);
			$('#view1').toggleClass('v2').animate({width:700}, 700, 'swing', function() {o.show_item(); info.hide().fadeIn(1000); $('#view2').hide(); })
			$('#view2').animate({width:1}, 400);			
			v1.flag = true;
			
		} else {
			//$('#view2 .png').fadeIn(100);
			fade.fadeTo(100, 0);
			$('#view1').animate({width:302}, 400)
			$('#view2').show().animate({width:402}, 700, 'swing', function() {o.show_item(); $('#view1').toggleClass('v2'); });			
			v1.flag = false;		
		}	
	}
		
	o.itms = $('.list li', o).click(function() {(!this.img.length && !v1.flag || this.img.length && v1.flag) ? v1.resize(this) : this.show_item(); return (false); })
	.each(
		function() {
			this.info = $('.info', this);
			this.hdr = $('h3', this)[0];
			this.img = $('.i', this);
			this.show_item = show_item;
			
			if (!this.img.length) $(this.hdr).after($('<div class="nopct" />'));
		}
	);
	
	$(o.itms[0]).click();
	
	a.mouseover(function() {clearTimeout(v[0].t1)}).mouseout(function() {v.mouseout()}).click(function(e) {return (false) });//.click(function() {d.location.href = $('.i' , s).attr('href'); });
	v.mouseover(function() {clearTimeout(this.t1); a.fadeIn(300)}).mouseout(function() {clearTimeout(this.t1); this.t1 = setTimeout(function() {a.fadeOut()}, 500)}).mousemove(function(e) {
		var o = $(this), offset = o.offset();
		
			pr.data('ratio', this.o[0].width()/this.o[1].width())
			
			a.wh = [a.attr('offsetWidth'), a.attr('offsetHeight')];				
			
			var xy = [e.pageX-offset.left-(a.wh[0]/2), Math.round(e.pageY-offset.top)-(a.wh[1]/2)];
			
			(xy[0]<0) ? xy[0]=0 : (xy[0]>o.attr('offsetWidth')-a.wh[0]) ? xy[0]=o.attr('offsetWidth')-a.wh[0] : xy[0];
			(xy[1]<0) ? xy[1]=0 : (xy[1]>o.attr('offsetHeight')-a.wh[1]) ? xy[1]=o.attr('offsetHeight')-a.wh[1] : xy[1];			
			
			a.css({'left':(xy[0]),'top':(xy[1])});
			//pr.css('backgroundPosition', -xy[0]*pr.data('ratio') + 'px ' + -xy[1]*pr.data('ratio')+'px')	
			pr.scrollLeft(xy[0]*pr.data('ratio')).scrollTop(xy[1]*pr.data('ratio'));
			
	})
	
	$('div#view2 img.png').click(function() {d.location.href = $('.i' , s).attr('href'); });
	
	return o;
	
	
}


function set_index() {
	var o1 = $('#index'), o = o1[0], b = $('body') , pct = $('#index_pct');
	
	rewr_addr(o1);
	
	if (b.hasClass('reg')) pct.html('');
	
	o.hide = function() {	
		
		this.flag = false;
	
		$('#index .pad1 .d, #index h2 .date, #index .c1, #ftr .copy').fadeOut(500);
		
		pct.html('');
		$('#index .pad1 .c').animate({height:39}, 500);
		//$('#index .pad1:even').animate({top:0}, 500);
		$('#index .pad1').each(function() {this.collapse()})//animate({top:39}, 500);
		$('.reg #body').css({height:1, overflow:'hidden'});
		
		setTimeout(
			function() {
				b.removeClass('index').addClass('reg');
				$('#ftr .copy').fadeIn(500);	
			}, 500);
	}
	
	o.show = function() {	
		
		if (this.flag) return (false);
		
		this.flag = true;
		
		d.menu.reset();
		
		$('.reg #body').css({overflow:'hidden', position:'relative', height:$('#body').height()}).animate({height:1}, 500);
		$('#body').empty();
		
		$('#ftr .copy').fadeOut(500);
		
		if (d.hdr1) d.hdr1.fadeOut(300);
		
		setTimeout(
			function() {
				$('#index .pad1 .d, #index h2 .date, #index .c1').fadeIn(500);	
				pct.load('inc/promo.inc.php');
				$('#index .pad1 .c').animate({height:91}, 500);
				$('#pad2, #pad4').animate({top:130}, 500); //.pad1:odd
			}, 700);
		
		setTimeout(
			function() {
				b.toggleClass('index').toggleClass('reg');
				$('#ftr .copy').fadeIn(500);	
			}, 1000);
	}
	
	o.flag = true;
	
	$('.pad1', o)
		.mousedown(function(e) {
				if (b.hasClass('index')) {
					this.flag = true; 
					//this.style.cursor = 'pointer';
					this.xy = [e.pageX, e.pageY];
					return (false);
				}
		})
		.mouseup(function(e) {
						  
			this.flag = false; //this.style.cursor = 'default';
			
			if (b.hasClass('index')) {				
				if (!tag(this.parentNode, 'body')) return (false);
				var o = $(this).appendTo(o1), p = o.parent().offset();
				o.css({zIndex:'', left:num(o.css('left'))-p.left, top:num(o.css('top'))-p.top})
			}		
			
			})
		.mousemove(function(e) {
							
			if (!this.flag) return (false);
			e.preventDefault();			
			(window.getSelection) ? window.getSelection().removeAllRanges() : d.selection.empty();
			
			
			var o = $(this);
			
			if (this.xy[0] == e.pageX && this.xy[1] == e.pageY) {
				return (false);				
			}
						
			if (!tag(this.parentNode, 'body')) {				
				o.appendTo(b.bind('mousemove', function() {$('body > .pad1').mouseup(); b.unbind('mousemove'); return (false);})).css({zIndex:900});
			}
			
			
			var xy = [e.pageX-50, (e.pageY)-this.wh[1]+20];	
			
			
			
			o.css({'left':(xy[0]),'top':(xy[1])});
			
			return (false);				
		})
		.each(function(i) {
			var o = $(this), coord =[num(o.css('left')), num(o.css('top'))];
			this.wh = [o.width(), o.height()];
			
			this.collapse = function() {
				
				o.animate({left:coord[0], top:(i % 2) ? 39 : 0}, 500);	
			}
		})
	
	if ($('body').hasClass('.reg')) {
		
		$('#index').hide().fadeIn(300);
		
		o.flag = false;	
		$('#index .pad1 .d, #index h2 .date, #index .c1').hide();
		$('#index .pad1 .c').css({height:39});
		$('#index .pad1:odd').css({top:39});
		
	}
}

function rewr_addr(o) {
	$('.rewr2', o || d).each(function() {var txt = this.innerHTML.split(' ').join('@'); this.innerHTML = '<a href="mailto:'+txt+'">'+(this.title || txt)+'</a>'});
}

function set_sel() {
	var o = $('#type_sel');
		
	$('.click', o).click(function(e) {
		e.stopPropagation();
		
		var o = $(this).next(), h = (o.height() < 24) ? o[0].scrollHeight : 22;
	
		o.removeClass('open').animate({height:h},300,'swing', (h > 24) ? function() {o.addClass('open')} : false);
	
		$('body').bind('click.close', function(e) {
			o.height('').removeClass('open'); $(this).unbind('click.close');
		}); 
	});
};

function set_hdr(txt) {
	if (!d.hdr1) d.hdr1 = $('<div id="hdr1"></div>').appendTo($('body'));
	d.hdr1.html('<h1><span>'+txt+'</span></h1>').fadeIn(1000);
}

$(d).ready(function() {d.menu = menu(); if ($('body').hasClass('reg')) {$('#index').load('index1.php?ajax=1', function() {set_index()}); d.hdr1 = $('#hdr1'); if (d.init) d.init();} else { set_index(); }; set_sel(); } )
