var serviceForm=false;
function getCookie(nombre){
	var dcookie=document.cookie;
	var cname=nombre+"=";
	var longitud=dcookie.length;
	var inicio=0;
	while(inicio<longitud)	{
		var vbegin=inicio+cname.length;
		if(dcookie.substring(inicio,vbegin)==cname){
			var vend=dcookie.indexOf(";",vbegin);
			if(vend==-1) vend=longitud;
			return unescape(dcookie.substring(vbegin,vend));
		}
		inicio=dcookie.indexOf(" ",inicio)+1;
		if(inicio==0) break;
	}
	return null;
}
function setCookie(name, value, expires){
	if(!expires)	{ 
		expires=new Date();
		expires.setTime(expires.getTime()+(24*3600*1000*365));
	}
	var str=name+"="+escape(value)+"; expires=" + expires.toGMTString()+ "; path=/";
	document.cookie=str;
}
function showPano(qty, id){
	if (typeof(deconcept) !== 'undefined' && deconcept.SWFObjectUtil.getPlayerVersion().major >= 8){
		var so = new SWFObject('/inc/vt/vt.swf', 'krpanoSWFObject', '752', '350', "8.0.0", "#999999");
		so.addParam('allowFullScreen','true');
		so.addParam('wmode','transparent');
		so.addVariable('pano','/pano_xml/'+qty+'/'+$('#p_id').val()+'/');
		so.useExpressInstall('/inc/vt/swfobject/expressinstall.swf');
		so.setAttribute('xiRedirectUrl', window.location);
		if (so.write('pano-shower')){ var mousewheelfixes=new SWFkrpanoMouseWheel(so); }
	}
	else{
		html='<table width="100%" height="100%"><tr valign="middle"><td align="center">'+
			'<b>Для просмотра панорамы необходимо <a href="http://www.adobe.com/go/getflash/">установить Adobe Flash Player</a>.</b></td></tr></table>';
		$('#pano-shower').html(html);
	}
}
function qtyPano(val){
	qty=val;
	setCookie("panorama_qty",qty);
	showPano(qty,$('#p_id').val());
}

String.prototype.str_replace = function(search, replace){
	return this.split(search).join(replace);
}
//function doEmbed(){
//	$.get('/ajax/pano/form_embed.php', {id:$('#p_id').val()}, function(data){
//		$('#service-form').hide().html(data).show('slow');
//		location.href='#service-form';
//	}, 'html');	
//}

function doEmbed(){
	if(!$('#shading').get(0))
		$('body').append('<div id="shading"></div><div id="embed-frame"><div id="embed-form"></div></div>');
	setOpacity();
	$('#embed-form').html('<div align="center"><img src="/images/loading.gif"></div>').show();
	$.get('/ajax/pano/form_embed.php', {id:$('#p_id').val()}, function(data){
		$('#embed-form').html(data).css('margin-top', Math.ceil(($('body').get(0).clientHeight-250)/2)+'px');
	}, 'html');	
}
function closeEmbed(){
	removeOpacity()
	$('#embed-form').hide().html('');
}
function resetEmbed(){
	nw=parseInt($('#em-w').val());
	nh=parseInt($('#em-h').val());
	if(!nw || nw<300 || nw>1000) nw=em_w;
	if(!nh || nh<200 || nh>1000) nh=em_h;
	$('#em-w').val(nw);
	$('#em-h').val(nh);
	code=$('#embed_html').val();
	code=code.str_replace('width="'+em_w+'"','width="'+nw+'"');
	code=code.str_replace('height="'+em_h+'"','height="'+nh+'"');
	$('#embed_html').val(code);
	em_w=nw; em_h=nh;
}
function doVote(id, rate){
	$.getJSON('/ajax/pano/pano_vote.php', {id:id,rate:rate}, function(data){
		if(data.res==1){
			$('#pano-vote').text(data.vote);
			$('#pano-rate').text((data.rate==0)?'нет':data.rate);
			$('#pano-scale').html('<li class="current" style="width: '+data.rate+'em;" alt="Текущий рейтинг '+data.rate+'">*</li>');
			alert('Ваш голос учтен!');
		}
		else if(data.res==2){
			alert('Вы уже голосовали за эту панораму!');
		}
	});
}
function doWallpaper(){
	location.href='#wallpaper-anhor';
}
function doScreensaver(){
	if(serviceForm=='screen') return;
	if(serviceForm!=''){
		$('#service-form').hide('fast', function(){
			serviceForm='';
			doScreensaver();
		});
		return;
	}
	serviceForm='screen';
	$('#service-form').html('<div align="center"><img src="/images/loading.gif"></div>');
	$.get('/ajax/pano/form_screensaver.php', {id:$('#p_id').val()}, function(data){
		$('#service-form').hide().html(data).show('slow');
	}, 'html');
}
function doReply(id){
	$('#author').val(id);
	$('#replayto').text(' для '+$('#com-author'+id).text());
	location='#comment-form';
}
function doComment(){
	vars={
		p_id:$('#p_id').val(),
		author:$('#author').val(),
		name:$('#com-name').val(),
		email:$('#com-email').val(),
		mess:$('#com-mess').val()
	};
	$.getJSON('/ajax/pano/pano_comment.php', vars, function(data){
		if(data.error==1) return alert(data.errorText);
		$('#comment-anchor').html(data.html);
		$("#com-count").text(data.count);
		if(data.count>0)$('#com-link').show();
		$('#comment-form input:text').val('');
		$('#comment-form textarea').val('');
		$('#comment-form input:checkbox').attr('checked', false);
		location.href='#li'+data.com_id;
	});
}
function doLink(){
	vars={p_id:$('#p_id').val(),to:$('#link-to').val(),from:$('#link-from').val(),name:$('#link-name').val(),mess:$('#link-mess').val()};
	$.getJSON('/ajax/pano/pano_link.php', vars, function(data){
		if(data.res)alert('Сообщение отправлено');
		else alert('Произошла ошибка, сообщение не отправлено. Попробуйте еще раз.');
	});
}
function doCard(){
	vars={p_id:$('#p_id').val(),to:$('#link-to').val(),from:$('#link-from').val(),name:$('#link-name').val(),mess:$('#link-mess').val()};
	$.getJSON('/ajax/pano/pano_card.php', vars, function(data){
		if(data.res)alert('Открытка отправлена');
		else alert('Произошла ошибка, открытка не отправлена. Попробуйте еще раз.');
	});
}
function doFriend(){
	if(serviceForm=='link') return;
	if(serviceForm!=''){
		$('#service-form').hide('fast', function(){
			serviceForm='';
			doFriend();
		});
		return;
	}
	serviceForm='link';
	$('#service-form').html('<div align="center"><img src="/images/loading.gif"></div>');
	$.get('/ajax/pano/form_link.php', {id:$('#p_id').val(),lang:lang}, function(data){
		$('#service-form').hide().html(data).show('slow');
		location.href='#pano-info';
	}, 'html');
}
function doGreeting(){
	if(serviceForm=='card') return;
	if(serviceForm!=''){
		$('#service-form').hide('fast', function(){
			serviceForm='';
			doGreeting();
		});
		return;
	}
	serviceForm='card';
	$('#service-form').html('<div align="center"><img src="/images/loading.gif"></div>');
	$.get('/ajax/pano/form_card.php', {id:$('#p_id').val(),lang:lang}, function(data){
		$('#service-form').hide().html(data).show('slow');
		location.href='#pano-info';
	}, 'html');
}
