(function($)
{
	$.fn.tValue = function(options){
		var options = $.extend({val: 'Search'},options);
		return this.each(function() {
			var self = $(this);
			self.focus(function () {
				if (self.attr("value") == options.val) {
					self.attr({value: ""});
				}
			});
			self.blur(function () {
				if (self.attr("value") == "") {
					self.attr({value: options.val});
				}
			});
		});
	};
})(jQuery);

jQuery(function rdbb()
{
    jQuery(".radiobtn li").bind('click', function() 
	{
		try
		{
			var rdb = jQuery(this);
			if(rdb.hasClass('rdb-off'))
			{
				rdb.parent().children().removeClass('rdb-on');
				rdb.parent().children().addClass('rdb-off');
				rdb.parent().find('input').removeAttr('checked');
				rdb.removeClass('rdb-off');
				rdb.addClass('rdb-on');
				jQuery(this).find('input').attr("checked", "checked");
			}
			else
			{
				rdb.removeClass('rdb-on');
				rdb.addClass('rdb-off');
				jQuery(this).find('input').removeAttr('checked');
			}
		}
		catch(error){}
		return false;
	});
	
	jQuery("#js-hit-button input").bind('click', function()
	{
		jQuery(".hit-tnk").show();
		window.scrollTo(0, 11500);
		return false;
	});	
})

jQuery(function best()
{
	jQuery(".za").bind('click', function()
	{
		var za = jQuery(this);
		if( za.find(".best-radio span").hasClass('best-checked') ) {
			za.find('.best-radio span').removeClass('best-checked');
			za.parents(".best-artists-block").removeClass('best-good');
			za.find('input').removeAttr("checked");
		} else {
			
			jQuery('.best-good').find('.za span.best-checked').removeClass('best-checked');
			jQuery(".best-artists-block").removeClass('best-good');
			
			za.parents(".best-artists-block").removeClass('best-bad');
			za.find('.best-radio span').addClass('best-checked');
			za.next().find('.best-radio span').removeClass('best-checked');
			
			za.parents(".best-artists-block").addClass('best-good');
			za.find('input').attr("checked","checked");
			
		}
	});
	jQuery(".protiv").bind('click', function()
	{
		var protiv = jQuery(this);
		if( protiv.find(".best-radio span").hasClass('best-checked') ) {
			protiv.find('.best-radio span').removeClass('best-checked');
			protiv.parents(".best-artists-block").removeClass('best-bad');
			protiv.find('input').removeAttr("checked");
		} else {
			
			jQuery('.best-bad').find('.protiv span.best-checked').removeClass('best-checked');
			jQuery(".best-artists-block").removeClass('best-bad');
			
			protiv.parents(".best-artists-block").removeClass('best-good');
			protiv.find('.best-radio span').addClass('best-checked');
			protiv.prev().find('.best-radio span').removeClass('best-checked');
			
			protiv.parents(".best-artists-block").addClass('best-bad');
			protiv.find('input').attr("checked","checked");
			
		}
	});
})

jQuery(function clothes()
{
	jQuery(".best-clothes").bind('click', function() {
		var rdb = jQuery(this);
		if( rdb.hasClass('best-clothes-checked') ) {
			rdb.removeClass('best-clothes-checked');
			rdb.find('input').removeAttr('checked');
		} else {
			jQuery(".best-clothes").find('input').removeAttr('checked');
			jQuery(".best-clothes").removeClass('best-clothes-checked');
			rdb.addClass('best-clothes-checked');
			rdb.find('input').attr("checked", "checked");
		}
		return false;
	});
})

var hit_maxChecked = 3;
var hit_countCheckedArray = [];

jQuery(function hitlistInitialize()
{	
	hitlist_initList(jQuery("#js-hitlist"), 0, "li");
	hitlist_initList(jQuery(".js-survey-tab"), 1, "li");
	hitlist_initList(jQuery(".hitlist-tab"), 2, ".hl-check-block");
})

function hitlist_initList(list, i, selector)
{
	list.each(function(index)
	{
		hit_countCheckedArray[index] = 0;
		jQuery(this).find(selector).bind('click', function()
		{
			hitlistClickHandler(i, this, index);
		});
	});	
}

function hitlistClickHandler(idx, hitl, index)
{
	hitl = jQuery(hitl);
	var classes = ["selected", "survey-checked", "hl-checked"]; // класс добавляем сюда
	var ci = -1;
	for(var i = 0; i < classes.length; i++)
		if(hitl.hasClass(classes[i]))
			ci = i;
	if(ci != -1)
	{
		hit_countCheckedArray[index]--;
		if(hit_countCheckedArray[index] < 0)
			hit_countCheckedArray[index] = 0;
		hitl.removeClass(classes[ci]);
		if(ci == 0)
			hitl.addClass('ie6-png');
		hitl.find('input').removeAttr('checked');
		return true;
	}
	else
	{
		if(hit_countCheckedArray[index] < hit_maxChecked)
		{
			hit_countCheckedArray[index]++;
			hitl.addClass(classes[idx]);
			if(idx == 0)
				hitl.removeClass('ie6-png');
			hitl.find('input').attr('checked','checked');
			return true;
		}
		else
		{
			alert('Вы можете отметить максимум 3 композиции');
			return false;
		}
	}
	return false;
}

jQuery(function media()
{
	jQuery("#js-media a").hover(function()
	{
		jQuery(this).addClass('hovered');
	}, function()
	{
		jQuery(this).removeClass('hovered');
	});
})

jQuery(function gid()
{
	jQuery("#js-close").bind('click', function(event) {
		var cl = jQuery(this);
		if( cl.hasClass('show') ) {
			cl.next().show();
			cl.parent().removeClass('gid-hidden');
			cl.removeClass('show');
		} else {
			cl.next().hide();
			cl.parent().addClass('gid-hidden');
			cl.addClass('show');
		}
		return false;
	});
})

jQuery(function su()
{
	jQuery("#js-survey-results").bind('click', function() {
		jQuery("#js-survey-question").fadeOut();
		jQuery("#js-survey-answer").fadeIn();
		return false;
	});
	jQuery("#js-survey-back").bind('click', function() {
		jQuery("#js-survey-answer").fadeOut();
		jQuery("#js-survey-question").fadeIn();
		return false;
	});
	
	jQuery(".n-tabs a").bind('click', function() {
		var path = jQuery(this).attr('href');
		jQuery(".n-tabs li.current").removeClass("current");
		jQuery(this).parent().addClass("current");
		var h = jQuery(path).height();
		
		jQuery(".material-cont").height(h);
		jQuery(".material-current").removeClass("material-current").fadeOut();
		jQuery(path).addClass("material-current").fadeIn();
		return false;
	});
})

function tabs()
{
	var h = jQuery(".material-current").height();
	jQuery(".material-cont").height(h);
}

function hei()
{
	var hei0 = jQuery("#js-survey-block");
	var hei1 = jQuery("#js-survey-question").height();
	var hei2 = jQuery("#js-survey-answer").height();
	if (hei1<hei2) {
		hei0.height(hei2);
	} else {
		hei0.height(hei1);
	}
}

jQuery(window).bind('load', function() {
	
	// New Index Page
	try{
	jQuery('.survey-scroller-inner').jScrollPane({showArrows:false, scrollbarWidth:10, scrollbarMargin:0});
	jQuery(".survey-search-field").tValue({val: "введите имя исполнителя"});
	jQuery(".survey-slider-container").scrollable({ next: '.survey-index-next', prev: '.survey-index-prev', mousewheel: false });
	var le = jQuery('.survey-scroller ul').length;
	le = le + 1;
	for (i=1;i<le;i++)
	{
		jQuery("#js-survey-field"+i).quicksearch("#js-survey"+i+" li");
	}
	}catch(e){}
	// Hit List
	try{jQuery(".js-hl-search").tValue({val: "введите имя любимого исполнителя"}).quicksearch(".hl-block", {'onAfter': function () { tabs(); }});}catch(e){}
	try{jQuery(".js-hl-artist").tValue({val: "введите имя любимого исполнителя"}).quicksearch(".hl-check-block", {'onAfter': function () { tabs(); }});}catch(e){}
	try{jQuery(".js-wiki-search").tValue({val: "найти"});}catch(e){}
	
	
	
	
	tabs();
	hei();
	try
	{
		jQuery(".js-login").tValue({val: "Ваш e-mail"});
	}catch(e){}
	try{jQuery(".js-pass").tValue({val: "пароль"});}catch(e){}
	try{jQuery(".js-search").tValue({val: "найти на сайте"});}catch(e){}
	try{jQuery("#js-hitlist li:odd").addClass("hit-even");}catch(e){}
	try{jQuery("#js-online-inner").tMask({radius: '100'});}catch(e){}
	try{jQuery(".js-news").tMask({radius: '50'});}catch(e){}
	try{jQuery(".round_b img").tMask({radius: '95'});}catch(e){}
	try{jQuery(".best-artists-pic img").tMask({radius: '50'});}catch(e){}
	try{
	if(!jQuery.fontAvailable('HeliosThinRegular'))
	{
		Cufon.replace('.current-song-name, .results-list h4, .info-block h1, .slides-title, .tabs a, .tabs span, .contest-rating-title, .contest-rating-number, .next-song-name, .next-song-time, .info h1, .song-block span, .survey-title, .edit-profile h4, .best-container h2, .best-container h3');
	}
	}catch(e){}
	try{ jQuery("#js-hit-field").quicksearch("#js-hitlist li"); }catch(error){}
	
	
	jQuery("#js-city").bind('click', function() {
		var current = jQuery("#panel-city-popup");
		if( current.hasClass("js-panel-opened") ) {
			current.hide().removeClass("js-panel-opened");
		} else {
			current.show().addClass("js-panel-opened");
		}
	});
	jQuery(".panel-city-popup-inner li a").bind('click', function() {
		var t = jQuery(this).text();
		jQuery("#js-city").text(t);
		jQuery("#panel-city-popup").hide().removeClass("js-panel-opened");
		return false;
	});

	try{ jQuery(".slides-block").scrollable(); }catch(error){}	
})

jQuery(function cl()
{
	jQuery(".close-mes").bind('click', function(event)
	{
		jQuery(this).parent().fadeOut();
	});
})

jQuery(function nav()
{
	jQuery("#js-nav > li").hover(
	  function () {
	    jQuery(this).addClass("nav-hover");
	  }, 
	  function () {
	    jQuery(this).removeClass("nav-hover");
	  }
	);
})

function getMovie(movieName)
{
	if (navigator.appName.indexOf("Microsoft") != -1)
		return window[movieName];
	else
		return document[movieName];
}

function loadFrame(swf, frame){}

function jsProxy(arg)
{
	jQuery("#accordion").accordion('activate', parseInt(arg));
}

jQuery(document).ready(function()
{
	try
	{
		var footerButtons = jQuery(".footer_buttons");
		jQuery(".footer-inner").prepend(footerButtons);
		footerButtons.css("display", "block");
	}
	catch(e){}
	if(window.location.pathname != "/")
	{
		try{ jQuery("div.advert-sidebar").prepend(jQuery("div.results-list")); }catch(e){}
		try{ jQuery(".preroll").hide(); }catch(e){}
	}
	else
		addPrerollHiding();
	
	if(window.location.pathname.indexOf("/tvguide") != -1)
		try{ loadXml(formatDate(new Date())); }catch(e){}
	else
	{
		jQuery("#accordion").accordion({autoHeight: false, header: "a.accordion-header", active: false, collapsible:true});
		jQuery(".accordion-header.ui-state-active").click(function(){jQuery("#accordion").accordion("activate", false);});
	}
	try
	{
		jQuery('.show-c-add-field').click(function()
		{
			jQuery('.hidden-form').show();
		});
	}catch(e){}
});

function addPrerollHiding()
{
	jQuery(".preroll-close").click(function(){jQuery(".preroll").hide(); return false;});
	setTimeout(function(){jQuery(".preroll").hide();}, 5000);
}

function handleLoginClick()
{
	var loc = location.pathname + location.search;
	if(loc.search("/auth/") == -1)
		location.href = "/auth" + loc;
}

function handleCitySelect()
{
	var city = jQuery(this);
	jQuery.cookie("muz_timezone_cid", city.attr("cid"), {path: "/"});
	jQuery.cookie("muz_timezone_tz", city.attr("tz"), {path: "/"});
	jQuery.cookie("muz_timezone_rtz", city.attr("rtz"), {path: "/"});
	loadSelectedCity();
	window.location.reload();
}

function loadSelectedCity()
{
	var cid = jQuery.cookie("muz_timezone_cid");
	cid = (cid == null) ? -1 : cid;
	var city = jQuery("div.panel-city-block ul li a[cid='" + cid + "']");
	jQuery("div.panel-city-block div#js-city").text(city.text());	
}

jQuery.noConflict();