$(function(){

	
	$('#voteBtn').click(vote);
	$('#send_question').click(send_question);
	
	$("#jQ_printLink").click( open_print_window );
	
	$('#jQ_printDialog').click( open_print_dialog );
	
	
	
	
	$("#spam_notification").click(function(){
		add_notification_box();
		$('#notification_status').attr('value',1);
	});
	$("#wrong_cat_notification").click(function(){
		add_notification_box();
		$('#notification_status').attr('value',2);
	});
	$("#rules_notification").click(function(){
		add_notification_box();
		$('#notification_status').attr('value',3);
	});
	$("#obsolete_notification").click(function(){
		add_notification_box();
		$('#notification_status').attr('value',4);
	});
	
	$("#notification_submit").click(function(){
		if ($('#notification_info').val()!='') {
			setNotification($('#notification_status').val(), $('#notification_info').val(), $('#notification_spider').val());
		}
		else{
			$('#notification_error').show();
		}
		close_box();
		$('#notification_info').attr('value','');
	});
	
	
	$('#spider_contact').click(function(){
		add_contact_box();
	});
	
	$("#contact_submit").click(function(){
		
		if($('#contact_name').val()=='')
			$('#contact_name_error').show();
		else	
			$('#contact_name_error').hide();
		
		var email = $('#contact_email').val();
		
	
		if($('#contact_email').val()=='' || email.match(/^([A-Za-z0-9\-\._]+)@([A-Za-z0-9\-\._]+(\.[A-Za-z]+)+)$/)==null)
			$('#contact_email_error').show();
		else	
			$('#contact_email_error').hide();
			
		if($('#contact_info').val()=='')
			$('#contact_info_error').show();
		else	
			$('#contact_info_error').hide();
			
	
		
		if($('#contact_name').val()!='' && $('#contact_email').val()!='' && $('#contact_info').val()!='' && email.match(/^([A-Za-z0-9]+)@([A-Za-z0-9]+(\.[A-Za-z]+)+)$/)!=null)
		{
			sendInfo($('#contact_name').val(),$('#contact_email').val(),$('#contact_info').val(),$('#contact_title').val());
			close_box();
			$('#contact_info').attr('value','');
			$('#contact_email').attr('value','');
			$('#contact_name').attr('value','');
		}
		
		
	});
	
});

function vote(){

	var voteValue = $('input:checked').val();
	
	if(!isValidVote(voteValue)) return;
	
	$('#voteLoadIco').show();
	$('#voteInfos span').hide();
	$('#voteBtn').attr('disabled','disabled');
	
	data = { vote: voteValue, offer_id: $('input[name=offer_id]').val(),spider: $('input[name=spider]').val()};
	$.post('/ajax/vote',data,responseVote, "json");
	
}


function responseVote(data){

	
	$('#voteLoadIco').hide();
	
	$('#voteBtn').removeAttr('disabled');
	switch(data.status){
	
		case 0:
		{
			$('#voteError').show();
			break;
		}
			
		case 1:
		{
			//$('#voteSuccess').show();
			$('#voteBox').hide();
			$('#afterVoteBox').show();
			break;
		}	
			
		case 'allready_voted':
			$('#voteAlreadyVoted').show();
	}
	
	var html = '';
	
	for(i=0;i<Math.floor(data.vote_value);i++)
		html += ' *';
	
	$('#vote_star').html(html);
	$('#vote_value').html(parseFloat(data.vote_value).toPrecision(3));
	$('#vote_value_box').show();
}


function isValidVote(voteValue){

	if(!voteValue){
		$('#voteCheck').show();
		return 0;
	}
	else{
		$('#voteCheck').hide();
		return 1;
	}
	
}	
	
	
function send_question(){

	$('#questionLoadIco').show();
	
	data = { author_id: $('input[name=author_id]').val(),
			 trade_id: $('input[name=trade_id]').val(),
			 firm_id: $('input[name=firm_id]').val(),
			 body: $('textarea[name=body]').val(),
			 author_name: $('input[name=author_name]').val(),
			 author_email: $('input[name=author_email]').val()};
	$.post('/ajax/send_question?lang='+lang,data,responseQuestion, "json");
	
}


function responseQuestion(data){

	$('#questionLoadIco').hide();
	
	switch(data.status){
	
		case 0:
		{
			$('#questionError').show();
			$('#questionNoError').hide();
			break;
		}
			
		case 1:
		{
			$('#questionNoError').show();
			$('#questionError').hide();
			break;
		}	
	}
	
	$('textarea[name=body]').attr('value','');
}


function open_print_window() {
	var windowFeatures = "left=80,top=100,width=1005,height=700,menubar=no,location=no,resizable=yes,scrollbars=yes,status=yes";
	WindowObjectReference = window.open($(this).attr('href'), "magazynyp_printwindow", windowFeatures);
	WindowObjectReference.focus();
	
	return false;
};

function open_print_dialog() {
	window.print();
	return false;
}

function setNotification(status,info,spider)
{
	$('#notification_saved').hide();
	$('#notification_error').hide();
	$('#notificationLoadIco').show();
	
	data = { status: status, offer_id: offer_id, lang: lang, info: info,spider:spider};
	$.post('/ajax/notificationstatus',data,responseNotification, "json");
}



function responseNotification(data){

	$('#notificationLoadIco').hide();
	if (data == 1) {
		$('#notification_error').hide();
		$('#notification_saved').show();
	}
	else {
		$('#notification_saved').hide();
		$('#notification_error').show();
	}
}

function sendInfo(name,email,info,title)
{
	$('#info_send').hide();
	$('#info_saved').hide();
	$('#info_error').hide();
	$('#infoLoadIco').show();
	
	data = { name: name, email: email, info: info, contact_email: contact_email,title: title,lang: lang};
	$.post('/ajax/sendinfo',data,responseInfo, "json");
}

function responseInfo(data){

	$('#infoLoadIco').hide();
	if (data == 1) {
		$('#info_error').hide();
		$('#info_saved').show();
	}
	else {
		$('#info_saved').hide();
		$('#info_error').show();
	}
}

function add_notification_box()
{
	$.blockUI({ message:$('#notification_box'), css: { width: '600px' ,left: ($(window).width() - 600)/2/$(window).width()*100 + '%'} });
}

function add_contact_box()
{
	$.blockUI({ message:$('#contact_box'), css: { width: '600px' ,left: ($(window).width() - 600)/2/$(window).width()*100 + '%'} });
}

function close_box()
{
	$.unblockUI();
	return false;
}
