function Block(){
	this.self = false;
}

Block.prototype.Show = function(oLink, block_id) {
	var position = this.GetPosition(oLink);
	this.self = $('#' + block_id);
	this.self.css('left', position[0] + 'px').css('top', position[1] + 'px').show();
	return false;
}
Block.prototype.Hide = function() {
	this.self.hide();
	return false;
}
Block.prototype.GetPosition = function(obj) {
	return [FindPosX(obj), FindPosY(obj)];
}

FindPosX = function(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

FindPosY = function(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}
function toggle_upload_field(count)
{
	$('#ufile_link_' + count).toggle();
	$('#ufile_link_' + count + ' input').val('');
	
	$('#ufile_data_' + count).toggle();
	$('#ufile_data_' + count + ' input').val('');
}
function add_photo_field(id) 
{
	if (id < 5) {
		id = id + 1;
		$("#ufile_multi_photo").append('<dd id="photo_field_' + id + '"><input type="file" name="load_photo_multiplefile[]" style="width: 220px;" />&nbsp;[<a href="#" onClick="remove_photo_field(' + id + '); return false;">X</a>]</dd>');
		$("#ufile_multi_photo_link").html('<dd><span class="ajax_span" onclick="add_photo_field(' + id + ');">+ еще фото</span></dd>');
	}
	if (id == 5 ) {
		$("#ufile_multi_photo_link").html('');
	}
}
function remove_photo_field(num) 
{
	$('#photo_field_' + num + '').html('');
}
function close_photo_loader() 
{
	$('#photo_loader').hide();
}
function close_link_loader() 
{
	$('#link_loader').hide();
}
function unlink(product_item_id, person_id, amplua_id, link_id)
{
	if(!confirm('Удалить связь между персоной и продуктом?'))
		return false;
	var obj = {};
	obj.product_item_id = product_item_id;
	obj.person_id = person_id;
	obj.amplua_id = amplua_id;
	$.get('/product/unlink/', obj, function(data){
		Alert.show('','Вы удалили ' + $('#' + link_id + ' > a').text(), 'message');
		$('#' + link_id).remove();
	});
}
function object_to_json(object)
{
	var _tmp = [];
	for (key in object) {
		_tmp[_tmp.length] = "'" + key + "': '" + object[key] + "'";
	}
	return '{' + _tmp.join(',') + '}';
}
function item_swither(link_id, data_id, url, params, type)
{
	var params_str = object_to_json(params);
	var cookie_param = {'domain':'.' + location.hostname.replace('www.',''),'expires':1};
	if($(link_id).text() == 'новые'){
		$('#sw1_' + type).html('новые<div>&nbsp;</div>').toggleClass('active');
		$('#sw2_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">популярные</a><div>&nbsp;</div>').removeClass('active');
		$('#sw3_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">ожидающие модерацию</a><div>&nbsp;</div>').removeClass('active');
		$('#sw4_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">черновики</a><div>&nbsp;</div>').removeClass('active');
		params.sort = 'new';
		$.cookie(type + '_block','new',cookie_param);
	}else if($(link_id).text() == 'популярные'){
		$('#sw1_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">новые</a><div>&nbsp;</div>').removeClass('active');
		$('#sw2_' + type).html('популярные<div>&nbsp;</div>').toggleClass('active');
		$('#sw3_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">ожидающие модерацию</a><div>&nbsp;</div>').removeClass('active');
		$('#sw4_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">черновики</a><div>&nbsp;</div>').removeClass('active');
		params.sort = 'best';
		$.cookie(type + '_block','grade',cookie_param);
	}
	else if($(link_id).text() == 'черновики'){
		$('#sw1_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">новые</a><div>&nbsp;</div>').removeClass('active');
		
		$('#sw2_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">популярные</a><div>&nbsp;</div>').removeClass('active');
		
		$('#sw3_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">ожидающие модерацию</a><div>&nbsp;</div>').removeClass('active');
		$('#sw4_' + type).html('черновики<div>&nbsp;</div>').toggleClass('active');
		params.sort = 'draft';
		$.cookie(type + '_block','grade',cookie_param);
	}
	else {
		$('#sw1_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">новые</a><div>&nbsp;</div>').removeClass('active');
		$('#sw2_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">популярные</a><div>&nbsp;</div>').removeClass('active');
		$('#sw3_' + type).html('ожидающие модерацию<div>&nbsp;</div>').toggleClass('active');
		$('#sw4_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">черновики</a><div>&nbsp;</div>').removeClass('active');
		params.sort = 'moderation';
		$.cookie(type + '_block','moderation',cookie_param);
	}
	params.path = location.pathname;
	$.get(url,params,function(html){
		$('#' + data_id).html(html);
		equalHeight($(".equal-height"));
		//moderation_bg();
		if(type == 'photo') 
			$("a[rel^='prettyPhoto']").prettyPhoto();
	});
}
function item_switcher_render(data_id, url, params, type)
{
	var params_str = object_to_json(params);
	if($.cookie(type + '_block') == 'new'){
		$('#sw1_' + type).html('новые<div>&nbsp;</div>').addClass('active');
		$('#sw2_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">популярные</a><div>&nbsp;</div>').removeClass('active');
		$('#sw3_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">ожидающие модерацию</a><div>&nbsp;</div>').removeClass('active');
		$('#sw4_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">черновики</a><div>&nbsp;</div>').removeClass('active');
	}
	else if($.cookie(type + '_block') == 'moderation'){
		$('#sw1_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">новые</a><div>&nbsp;</div>').removeClass('active');
		$('#sw2_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">популярные</a><div>&nbsp;</div>').removeClass('active');
		$('#sw3_' + type).html('ожидающие модерацию<div>&nbsp;</div>').addClass('active');
		$('#sw4_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">черновики</a><div>&nbsp;</div>').removeClass('active');
	}
	else if($.cookie(type + '_block') == 'drafts'){
		$('#sw1_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">новые</a><div>&nbsp;</div>').removeClass('active');
		$('#sw2_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">популярные</a><div>&nbsp;</div>').removeClass('active');
		$('#sw3_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">ожидающие модерацию</a><div>&nbsp;</div>').removeClass('active');
		$('#sw4_' + type).html('черновики<div>&nbsp;</div>').addClass('active');
	} else{
		$('#sw1_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">новые</a><div>&nbsp;</div>').removeClass('active');
		$('#sw2_' + type).html('популярные<div>&nbsp;</div>').addClass('active');
		$('#sw3_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">ожидающие модерацию</a><div>&nbsp;</div>').removeClass('active');
		$('#sw4_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="item_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\'); return false;" href="#">черновики</a><div>&nbsp;</div>').removeClass('active');
	}
}

/* Оценки */
function grade_swither(link_id,url,params)
{
	if($(link_id).text() == 'Любит'){
		$('#sw1_grade').html('Любит').toggleClass('active');
		$('#sw2_grade').html('<a id="switcher_control_grade" class="switcher_control" onclick="grade_swither(this,\''+ url +'\',\'filter=bad\'); return false;" href="#">Не любит</a>').removeClass('active');
	}else{
		$('#sw1_grade').html('<a id="switcher_control_grade" class="switcher_control" onclick="grade_swither(this,\''+ url +'\',\'filter=good\'); return false;" href="#">Любит</a>').removeClass('active');
		$('#sw2_grade').html('Не любит').toggleClass('active');
	}
	$.get(url,params,function(obj){
		$('#grade_content').html(obj.html);
		$('#user_grades_all').html(obj.count);
	},'json');
}
/* /Оценки */

function tvprog_index_swither(link_id,data_id,url,params,type)
{
	var params_str = object_to_json(params);

	if ($(link_id).text() == 'сейчас'){
		$('#sw1_' + type).html('<a>сейчас</a><div>&nbsp;</div>').toggleClass('active');
		$('#sw2_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="tvprog_index_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\');">сегодня</a><div>&nbsp;</div>').removeClass('active');
		$('#sw3_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="tvprog_index_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\');">скоро</a><div>&nbsp;</div>').removeClass('active');
	 	params.sort = 'now';
	} else if ($(link_id).text() == 'сегодня'){
		$('#sw1_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="tvprog_index_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\');">сейчас</a><div>&nbsp;</div>').removeClass('active');
		$('#sw2_' + type).html('<a>сегодня</a><div>&nbsp;</div>').toggleClass('active');
		$('#sw3_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="tvprog_index_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\');">скоро</a><div>&nbsp;</div>').removeClass('active');
	 	params.sort = 'today';
	} else {
		$('#sw1_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="tvprog_index_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\');">сейчас</a><div>&nbsp;</div>').removeClass('active');
		$('#sw2_' + type).html('<a id="switcher_control_' + type + '" class="switcher_control" onclick="tvprog_index_swither(this,\''+ data_id +'\',\''+ url +'\',' + params_str + ', \'' + type + '\');">сегодня</a><div>&nbsp;</div>').removeClass('active');
		$('#sw3_' + type).html('<a>скоро</a><div>&nbsp;</div>').toggleClass('active');
	 	params.sort = 'any';
	}

	$.get(url,params,function(html){
		$('#' + data_id).html(html);
	});
}

/* Форма жалобы */
$(document).ready(function(){
	$('#complaintLink').click(function(){
		$('#complaintFrom').toggle();
	});
	
	$('#complaintClose').click(function(){
		$('#complaintFrom').hide();
	});
});
/* /Форма жалобы */

/* Иконка ТВ-программы */
//$(document).ready(function(){
//	var a = $('#menu > li > a[@href*=tvprog]');
//	a.html('<span></span>' + a.html());
//});
/* /Иконка ТВ-программы */

/* удаление ссылки */
function delete_link(id)
{
	$.get('/link/delete/' + id + '/', {}, function(json){
		$('#link_item_' + id).remove();
	});
	return false;
}
/*/удаление ссылки */

function add_studio()
{
	$('#add_studio').toggle();
	var params = new Object();
	params.studio_name = $('#studio_name').val();
	$('#studio_name').val('');
	$.post('/studio/add/',params,function(obj){
		if(obj.status == 'ok'){
			$('#studio').append('<a href="#" id="#std_' + obj.id + '">' + obj.title + '</a>');
			Alert.show(AlertText.studio.title,AlertText.studio.ok,'message');
			hlStudio();
		}else{
			Alert.show(AlertText.studio.title,AlertText.studio.error,'error');
		}
	},'json');
}

/* Изменинение временного интервала */
$(document).ready(function(){
	$('#time_change_show').click(function(){
		$('#time_change_popup').show();
	})
	$('#time_change_submit').click(function(){
		$('#time_change_popup').hide();
	})
});
/* /Изменинение временного интервала */

function toggle_sphigel(url,ext_url,obj)
{
	$('#shpigel_h > a').attr('href',ext_url);
	$('#shpigel_links > li > a').each(function(o){$(this).removeClass('active');});
	$(obj).addClass('active');
	$.get(url,function(html){
		if(html != '')
			$('#shpigel_data').html(html);
	});
	return false;
}
/* Warning */
$(document).ready(function(){
	if ($.cookie('the_testing')) {
		$('#warning').hide();
	}
	else {
		$('#warning>a').text('Закрыть');
	}
	
	$('#warning > a').click(function(obj){
		$('#warning').hide();
		if (!$.cookie('the_testing')) {
			$.cookie('the_testing', 'True', {
				expires: 365/*,
				domain: '*.vokrug.tv'*/
			});
		}
		else {
			$('#warning>a').text('Закрыть');
			$.cookie('the_testing', null);
		}
	});
/* /Warning */

/* Guide */	
	if ($.cookie('bar_closed')) {
		$('#top_bar').html('');
	}
	$('#bar_link').click(function(obj){
		$('#top_bar').html('');
		if (!$.cookie('bar_closed')) {
			$.cookie('bar_closed', 'True', {
				expires: 365/*,
				domain: '*.vokrug.tv'*/
			});
		}
	});
/* /Guide */
})

function equalHeight(group) {
  tallest = 0;
  group.each(function() {
    thisHeight = $(this).height();
    if(thisHeight > tallest) {
      tallest = thisHeight;
    }
  });
  group.height(tallest);
}


function ShowAlert(type) {
	if(type == 'no_login') {
		Alert.show(AlertText.no_login.title, AlertText.no_login.text, 'error');
	}
	if(type == 'not_right') {
		Alert.show(AlertText.not_right.title, AlertText.not_right.text, 'error');
	}	
}
$(document).ready(function () {
	$.get('/notice.php',false,function(obj){
		if(obj) {
			var html = '';
			for(var i= 0; i < obj.length; i++) {
				html += '<div class="spacer"></div><div id="notice-block" class="notice ' + obj[i]['type'] + '">' + obj[i]['content'] + '</div>';
				Alert.show('Уведомление:', '' + obj[i]['content'] + '', '' + obj[i]['type'] +  '');
			}
			//$('#notice_list').html(html);
		}
	},'json');
});





