var _=jQuery.noConflict();

_(document).ready(function(){ _document_ready(); });
	
function _document_ready() {
			
	// general
//	fixIE6Transparency('#mt a, .popMT');
	fixIE6Transparency('#h3, #h4, #mt a, .popcntMT, #topRoomImg');
	
	// home page
	_('#h1').fadeIn(2000);
	_('#h2').fadeIn(2000,function(){ _('#h3').show('fast', function(){_('#h4').show('fast',function(){
		var time = Math.ceil(5 + 10 * Math.random());
		time = 6;
		setTimeout('homeAnim()', 1000 * time);
	});});});

	// left menu
	var hr=window.location.href;
	var hp=hr.indexOf('m=');
	if(hp==-1)
		_('.menuL1').eq(0).slideDown('slow');
	else{
		hp=parseInt(hr.substr(hp+2));
		_('.menuL1#is'+hp).slideDown('slow');
	}
	
	// search
	_('#q').focus(function(){var t=_(this); if (t.attr('value')=='търсене...') t.attr('value', '');});
	
	
	_('.menuL0').hover(function(){
		var n=_(this).next();
		var go = false;
		if (n.attr('style') == undefined) {
			go=true;	
		} else {
			var s =n.attr('style');
			go=(0 > s.indexOf('block')); 
		}
		if(go){
			_('.menuL1').slideUp('slow');
			n.slideDown('slow');			
		}
	},function(){});

	// popups
	_('.popMT, .pop').hover(function(){},function(){if (this.id!='popVid') pop_close(this);});
	_(document).keypress(function(e){if (e.keyCode == 27) popups_hide();});

	// top menu
	_('#mt a').hover(function(){		
		var pop = this.id.substr(0, 2) + this.id.substr(3);
		pop_position_show('#' + this.id,'#ACc','#' + pop);
	},function(){});
	_('#contactF').click(function(){
		_.get('x.php',{a:'contact'},function(data){
			var o=_('#A0').offset();
			_('#popCnt')
			.html(data)
			.css({top:4,left:o.left+200})
			.fadeIn('fast')
			;
			
			_('#quest_x').click(function(){_('#popCnt').hide(); return false;});
			
			contact_setup();
		});
		return false;
	});
	
	content_setup();
}

function contact_setup() {
	_('#quest').each(function(){
		_('#qq4').attr('autocomplete', 'off');
		var ff = {
	    	url: 'x.php',
	    	success: function(data){
	    		
	    		_('#qqinfo_cnt').eq(0).hide().html(data.substr(2)).fadeIn(400);
	    		
	    		var code = data.substr(0, 2);
	    		if (code == 'OK') {
	    			_('#quest input.t').attr('value', '');
	    			_('#quest textarea').attr('value', '');
	    			_('#quest #sec').attr('src', location.protocol + '//' + location.hostname + location.pathname + '?a=400&t=' + (new Date()).getTime());
	    		}
	    		
	    	}
		};
		_(this).ajaxForm(ff);
	});
}
function homeAnim() {
	var t = _('#home');
	if (t.length != 1)
		return;
	
	var to = 0.2;	
	_('#h1').fadeTo(2000, to);
	_('#h2').fadeTo(2000, to, function(){

		var t = _('#h1 img');
		var s = t.attr('src');
		
		// hack - to ensure changes...
		//var n = 1 + Math.floor(2 * Math.random());
		var n = s.substr(s.length-6, 1);
		//console.log(n);
		if (n=='1') n='2'; else n='1';
		
		//
		t.attr('src', s.substr(0, s.length-6)+n+'1.jpg');
		//
		t = _('#h2 img');
		s = t.attr('src');
		t.attr('src', s.substr(0, s.length-6)+n+'2.jpg');
		//
		_('#h1,#h2').fadeTo(1000, 1.0);
	});
	setTimeout('homeAnim()', 2000 * Math.ceil(1 + 3 * Math.random()));
//	setTimeout('homeAnim()', 1000 * Math.ceil(15 + 10 * Math.random()));	
}
var drawID = '';
var drawReg = '';
function content_setup() {
	
	// prd page images
	_('.prdL .tn img').fadeIn(1500);
	
	// text images zoom
	_('.txttn img').hover(function(){
		var t = _(this);
		var to = t.offset();
		
		_('#popImg').css({left:to.left,top:to.top}).html('');
		_.get('x.php', {a:'popimg',id:this.id},function(data){
			if(''!=data) {
				popups_hide();
				_('#popImg').html(data).show();
			}
		});
	},function(){});

	// gallery images popup
	_('.galtn img').hover(function(){
		_.get('x.php',{a:'popgal',id:this.id},function(data){
			if(''!=data){
				popups_hide();
				
//				var a=_('#page .gal .galtxt').eq(0).offset();
//				var m=_('#AA');
//				var mo=m.offset();
//				var mh=m.height();
//				//
//				var pop=_('#popGal');
//				pop.html(data);
//				var h=pop.height();
//				var y=a.top;
//				if (y + h > mo.top+mh)
//					y -= (y + h - mo.top - mh);
//				pop.css({top:y,left:a.left,display:'block'});

				var a=_('#page .gal .galtxt').eq(0).offset();
				var y=Math.max(a.top - 190, _(window).scrollTop());				
				var pop=_('#popGal');
				pop.html(data);
				pop.css({top:y,left:a.left,display:'block'});
				
				
				popups_setup();
			}
		});
	},function(){});
	
	// video popup
	_('.vidline .tn img, .faqline .vidtn').hover(function(){
		var a = _(this).offset();
		_.get('x.php',{a:'popvid',id:this.id},function(data){
			if(''!=data){
				popups_hide();
				_('#popVid').html(data).css({top:a.top,left:a.left,display:'block'});
				popups_setup();
			}
		});
	},function(){});

	// the home page bottm image effects
	_('#menuHB img').hover(function(){swap_home_bottom_img(_(this), 1);},function(){swap_home_bottom_img(_(this), 0);});
	_('#menuHB .hbL a').hover(function(){swap_home_bottom_img2(this, 1);},function(){swap_home_bottom_img2(this, 0);});
	
	// the town map
	var c = _('#tmap').offset();
	if (c != undefined) {
		var z = 0;
		var xx = c.left;
		var yy = c.top;
		_('#tmap a').each(function(t){
			var p1 = this.id.indexOf('_');
			var p2 = this.id.lastIndexOf('_');
			var x = parseInt(this.id.substring(p1 + 1, p2));
			var y = parseInt(this.id.substr(p2 + 1));
			if (x > 0 && y > 0) {
				x += xx;
				y += yy;
				_(this).css({display:'block',left:x+'px',top:y+'px','z-index':(z++)});
				//console.log(x + ', ' + y + ', ' + xx + ', ' + yy);
			}
		});
	}
	
	// drawing setup
	if (_('.room').length) {
		//
		var o=_('.room').offset();		
		var c = 5;
		//
		var xx = o.left+1;
		var yy = o.top;
		_('.room .imgreg').each(function(){
			var id = this.id;			
			_(this).css({left:xx,top:yy,'z-index':(c++)}).show()
			
		});
		_('.room .top').css({left:xx,top:yy,'z-index':c}).show();
		//
		_('.roomrega').hover(function(){			
			var o = _(this).offset();
			_('#drawinger').css({left:o.left - 20,top:o.top-20}).show();			
						
			var id=this.id.substr(this.id.indexOf('-')+1);
			drawReg = _('#imgreg'+id+' .regdef').html();
			drawID = '#imgreg'+id;
			
		},function(){});
		
		//
		_('#drawinger').hover(function(){},function(){_(this).hide();});
		_('#drawinger a').click(function(){
			var t=_(this);
			var dcol=t.attr('title');
			drawRoomRegion(drawID, drawReg, dcol);
			// tell the server, to save a cookie
			_.get('x.php',{a:'clrreg','room':_('.room').attr('id'),'reg':drawID,'clr':dcol});
			return false;
		});

		// fill all necessary colors
		_('.imgreg').each(function(){
			var def=_(this).children('.regdef').eq(0);
			var clr=_(this).children('.regclr').eq(0);
			if (def.length==1 && clr.length==1){
				def=def.html();
				clr=clr.html();
				if (clr=='') clr='#ffffff';				
				drawRoomRegion('#'+this.id,def,clr);
			}
		});
		
	// color selection
	} else if (_('#colsel1').length) {
		_('table.colors a').hover(function(){
			var a = _(this);
			var o = a.offset();
			var t = a.attr('title');
			var d = _('#colorsample');
			var p = t.indexOf('(');
			d.html(
				'<h2>'+t.substr(0, p-1)+'</h2>'
				+t.substr(p)
				+'<div style="background:'+a.css('background')+';"></div>'
				)
				.css({left:o.left + 60,top:o.top - 200})
				.show();
			//console.log(_(this).attr('title'));
		},function(){});
		_('table.colors').hover(function(){},function(){_('#colorsample').hide();});
	}	
	
}	

function drawRoomRegion(aDrawID, aDrawReg, aColor) {
//console.log('ID: '+aDrawID+', C: '+aColor+', DEF: '+aDrawReg);	
	var p = aDrawReg.indexOf(']');
	var xx=aDrawReg.substr(0, p);
	p = aDrawReg.lastIndexOf('[');
	var yy=aDrawReg.substr(p+1);
	xx=xx.replace(' ','');
	xx=xx.replace('[','');
	xx=xx.replace(']','');
	yy=yy.replace(' ','');
	yy=yy.replace('[','');
	yy=yy.replace(']','');
	xx=xx.split(',');
	yy=yy.split(',');
	var i=xx.length;
	var x=[];var y=[];
	for(var k=0; k<i;k++) {
		x.push(parseInt(xx[k]));
		y.push(parseInt(yy[k]));
	}
	_(aDrawID).fillPolygon(x, y, {color:aColor, alpha: 1});	
}

function swap_home_bottom_img2(who, big) {
	var p = _(who).parent().next().children().eq(0).children().eq(0);
	swap_home_bottom_img(p, big);
}

function swap_home_bottom_img(w, big) {
	var s = w.attr('src');
	var n = s;
	var p = s.lastIndexOf('/');
	if (p > -1) {
	
		if (big == 0) {
			if (s.substr(p + 1, 1) == '_')
				n = s.substr(0, p+1) + s.substr(p + 2);
		} else {
			if (s.substr(p + 1, 1) != '_')
				n = s.substr(0, p + 1) + '_' + s.substr(p + 1);			
		}
	}
	if (n != s)
		w.attr('src', n);
}

function pop_close(who){
	
	if (who.id=='popCnt')
		return;
//	if (who.id == 'media')
//		_('#media .popcnt').html('');
//	else if (who.id == 'inj')
//		injector_timer_clear();
		
	_(who).hide();
}

function pop_position_show(place, container, who) {

	popups_hide();

	var p = _(place);
	var po = p.offset();
	var c = _(container);
	var co = c.offset();		
	var w = _(who);
	var left = Math.min(co.left + c.width() - w.width(), po.left + 1);
	w.css({left: left, top: po.top + p.height()});
	if (jQuery.browser.msie)
		w.show();
	else
		w.fadeIn('slow');
}

function popups_setup() {
	_('.pop_btn').hover(function(){if(this.id.substr(0, 6) == 'close_') popups_hide();},function(){});
}
function popups_hide(){
	_('.popMT, .pop').hide();
}
function fixIE6Transparency(w) {
	if (_.browser.msie && _.browser.version < 7) 
		_(w).each(function() {
			var path = this.style.backgroundImage;
			path = [/url\(([\u0000-\uFFFF]*)\)$/.exec(path) || ['','']][0][1];
			this.style.background="none";
			this.style.filter='progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+path+'", sizingMethod="scale");';
		});
}