// выбор региона
$(function() {
	var regLink = $('#region small a').add('#main-menu a[href$="choose-region"]');
	regLink.click(function() {
		this.blur();
		regLink.eq(0).hide();
		$('#region div.list').css({ 'margin-left': (this == regLink.get(0)) ? 0 : '400px' }).get(0).showing = this;
		if (this.loaded) {
			$('#region div.list').html(this.loaded).show();
			return false;
		}
		var self = this;
		$('#region div.list').load(this.href, { 'choose-region': true, ajax: true }, function(res) {
			$('strong', this).parents('li').addClass('current').find('ul').show();
			$('span', this).click(function() {
				if ($(this.parentNode).find('ul:visible').length) return;
				$(this.parentNode.parentNode).find('ul:visible').hide();
				$(this.parentNode).find('ul').show();
			});
			self.loaded = $(this).html();
			$(this).show();
		});
		$(document).click(function(e) {
			if ($('#region div.list:visible').length && !$(e.target).parents('div.list').length) {
				$('#region div.list').hide();
				$('#region div.list').get(0).showing.loaded = $('#region div.list').html();
				regLink.eq(0).show();
			}
		});
		return false;
	});
});

// mp3 player
$(function() {
	window.getNodeTop = function(node){
		var coords = {top: 0, left: 0}
		while(node.tagName.toLowerCase() != 'body' && node.tagName.toLowerCase() != 'html') {
			coords.top  += node.offsetTop  - node.scrollTop;
			coords.left += node.offsetLeft - node.scrollLeft;

			node = node.offsetParent;
		}

		return coords;
	}

	window.fnClosePlayer = function() {
		$('#mp3').hide().empty();
	};

	$('<div id="mp3"><div id="mp3flash"></div></div>').appendTo('body');
	$('body.dt-content #mp3').css('margin-left', '-175px');

	$('a.mp3').click(function(){
		var mp3 = this;

		$('#mp3').hide().html('<div id="mp3flash"></div>').each(function() {
			swfobject.embedSWF(
				"/i/swf/mp3.swf", "mp3flash", 194, 167, "6.0.0", null,
				{
					sPath: $(mp3).attr('href') + ($.browser.msie ? '?' + Math.random() : ''),
					sTitle:  $(mp3).attr('mp3:title'),
					sArtist: $(mp3).attr('mp3:author') || '',
					sType:   $(mp3).attr('mp3:format') || '',
					sDllnk:  $(mp3).attr('mp3:path')
				},
				{ wmode: 'transparent', menu: 'false' }
			);

			var coords = getNodeTop(mp3);

			$(this).css({top: coords.top - 40, left: coords.left + 30});
		}).show();

		return false;
	});
});

// блок рекомендуем
$(function() {
	$.getJSON("/advice.action?list", function(advices) {
		if (advices.length == 0) return;
		var el = advices[Math.floor(Math.random()*advices.length)];
		var type = '';
		switch (el.t) {
			case 'music':
				type = 'мелодию';
				break;
			case 'picture':
				type = 'картинку';
				break;
			case 'java':
				type = 'игру';
				break;
			case 'video':
				type = 'видео';
				break;
		}
		$('#advice').find('img').attr({src: el.preview, alt: el.title}).end().
			find('b').html(el.title).end().
			find('span').html(type).end().
			find('a').attr({href: el.path, 'class': el.t}).end().
			show();
	});
});

// зебра в таблице совместимых телефонов
$(function() {
	$('#supported-devices table').each(function(){
		$('tr', this).each(function(index){
			if(index % 2 != 1) {
				$(this).addClass('odd');
			}
		});
	});
});

// зебра в разделе с мелодиями
$(function() {
	$('body.grand-music table.all').each(function(){
		$('tr', this).each(function(index){
			if(index % 2 == 1) {
				$(this).addClass('e');
			}
		});
	});
});

// зебра в разделе с картинками
$(function() {
	$('body.grand-picture table.all').each(function(){
		$('tr', this).each(function(index){
			if(index % 2 != 1) {
				$(this).addClass('e');
			}
		});

		$('td', this).each(function(index){
			if(index % 4 == 3) {
				$(this).addClass('end');
			}
		});
	});
});

// зебра в разделе с java
$(function() {
	$('body.grand-java table.all').each(function(){
		$('tr', this).each(function(index){
			if(index % 2 != 1) {
				$(this).addClass('e');
			}
		});

		$('td', this).each(function(index){
			if(index % 2 == 1) {
				$(this).addClass('end');
			}
		});
	});
});


// зебра в разделе с video
$(function() {
	$('body.grand-video table.all').each(function(){
		$('tr', this).each(function(index){
			if(index % 2 != 1) {
				$(this).addClass('e');
			}
		});

		$('td', this).each(function(index){
			if(index % 2 == 1) {
				$(this).addClass('end');
			}
		});
	});
});

// зебра в результатах поиска
$(function() {
	$('body.search table.all').each(function(){
		$('tr', this).each(function(index){
			if(index % 2 == 1) {
				$(this).addClass('e');
			}
		});
	});
});

// зебра в разделе услуг
$(function() {
	$('body.services #content table').each(function(){
		$('tr', this).each(function(index){
			if ((index != 0) && ($(this).prev('.e').length == 0) && $('th', this).length == 0) {
				this.className = 'e';
			}
		});
	});
});

// табы в витрине магазина
$(function() {
	$('body.shop-window ').each(function(){
		$('#content .tab-new a').click(function() {
			window.fnClosePlayer();
			$(this.parentNode.parentNode.parentNode.parentNode).removeClass('best').addClass('new');
			$(this.parentNode.parentNode).removeClass('best').addClass('new');
		});

		$('#content .tab-best a').click(function() {
			window.fnClosePlayer();
			$(this.parentNode.parentNode.parentNode.parentNode).removeClass('new').addClass('best');
			$(this.parentNode.parentNode).removeClass('new').addClass('best');
		});

		// зебра на витрине
		$('#content .music table').each(function(){
			$('tr', this).each(function(index){
				if(index % 2 == 1) {
					$(this).addClass('e');
				}
			});
		});
	});
});

// форма Управления подпиской
$(function() {
	$('#subscribeManage input[@name=subscribe]').click(function(){
		var status = (this.value == '1') ? true : false;

		p = $('#formatTR')[0];

		p.disabled = !status;

		tgns = ['input', 'legend', 'label'];

		for(var k = 0; k < tgns.length; k++) {
			var elms = p.getElementsByTagName(tgns[k]);
			for(var i = 0; i < elms.length; i++) {
				elms[i].disabled = !status;
			}
		}
	});

	$('#subscribeManage input[@type=reset]').click(function(){
		this.form.reset();
		$('#subscribeon')[0].click();
	});
});

// кнопка "печать"
$(function() {
	$('p.print-link a').click(function() {
		$('link[@title="print version"]').attr('media', 'all'); // В пизду Firefox

		var pageHref = this.href.replace('?print', '');

		$(this.parentNode.parentNode).prepend('<a href="'+pageHref+'" id="notprinted-version">обычная версия</a>');

		$('#notprinted-version').click(function() {
			$('link[@title="print version"]').attr('media', 'print');

			this.parentNode.removeChild(this);
			return false;
		});

		window.print();
		this.blur();

		return false;
	});
});

// кеширование фоновых картинок
$(function() {
	try {
		document.execCommand('BackgroundImageCache', false, true);
	} catch(err) {

	}
});

// витрина услуг
$(function() {
	$('ul.chooser').click(function() {
		try{
			event.target.blur();
		} catch (err) {
		}
	});

	$('ul.chooser li a').click(function() {
		// убрать текущий элемент
		$('li.current', this.parentNode.parentNode).each(function() {
			$('dl.'+this.className.replace(' current', ''), this.parentNode.parentNode).hide();
		}).removeClass('current');

		// выделить новый
		$(this.parentNode).each(function() {
			$('dl.'+this.className.replace(' current', ''), this.parentNode.parentNode).show();
		}).addClass('current');

		return false;
	});
});
