$(document).ready(function(){
	$('.toolhtml').tooltip({
			delay: 0,
			showURL: false,
			bodyHandler: function() {
				return $("<img/>").attr("src", this.rel);
			}
	});
	$('#epapersSubscription li').click(function(){
		$('#epapersSubscription li').removeClass('active');
		$(this).addClass('active');
		obInput = $(this).find('input:radio');
		obInput.attr('checked','checked');
		$('#epapersTitle').html($(this).find('input:hidden').attr('value'));
		ohidden = $(this).find('input:hidden');
		if($(this).find('option:selected').size() > 0){
			$('#epapersSubscriptionUrl').attr('href',ohidden[0].name+$(this).find('option:selected').attr('value'));
		} else {
			$('#epapersSubscriptionUrl').attr('href',ohidden[0].name);
		}
		msg = 'zł';
		if(ohidden[1].value > 1){
			msg = ' zł (' + Math.round(($(this).find('span').html() / ohidden[1].value)*Math.pow(10,2))/Math.pow(10,2) + ' zł za wydanie) ';
			$('#smsprice').hide();
			$('.epapersSubscriptionType').show();
		} else {
			$('#smsprice').show();
			$('.epapersSubscriptionType').hide();
		}

		$('#productprice').html($(this).find('span').html()+ msg);
		$('#productprice1').html($(this).find('span').html() + ' zł');
		$('#epapersSubscriptionType').html($(this).find('option:selected').html());
	});

	/*$('.hover-star').rating({
	  focus: function(value, link){
	    var tip = $('#hover-test');
	    tip[0].data = tip[0].data || tip.html();
	    tip.html(link.title || 'value: '+value);
	  },
	  blur: function(value, link){
	    var tip = $('#hover-test');
	    $('#hover-test').html(tip[0].data || '');
	  }
	});*/
});



function navi(url)
{
	if (url != 0) {
		document.location.href = url;
	}
}
function addToNewsletter(url,mailid,check,id){
	var mail = $(mailid).attr('value');
	if(!$(check).attr('checked')){
		msgShow(id,'Musisz zaakceptować regulamin!.',true);
		return false;
	}
	if(validateEmail(mail)){
		$.ajax({
			type: "POST",
			url: url,
			data: "action=addToNewsletter&email="+mail+"&product="+$('#urlSendFriend').attr('value') ,
			dataType: "text",
			beforeSend: function() {
				$(id).html('Wysyłanie wiadomości email');
			},
			error: function() {
				msgShow(id,'Bład',true);
			},
			success: function(data) {
				msgShow(id,data,false);
			}
		});
	} else {
		$(id).html();
		msgShow(id,'Podany adres email jest niepoprawny.',true);
		return false;
	}
}
function sendToFriend(url,ifriend,isender,id){

	var friendmail = $(ifriend).attr('value');
	var sendermail = $(isender).attr('value');
	var error = false;

	if(!validateEmail(friendmail)){
		msgShow(ifriend+'_msg','Podany adres email jest niepoprawny.',true);
		error=true;
	} else msgShow(ifriend+'_msg','',true);

	if(!validateEmail(sendermail)){
		msgShow(isender+'_msg','Podany adres email jest niepoprawny.',true);
		error=true;
	} else msgShow(isender+'_msg','',true);

	if(!error){
		$.ajax({
			type: "POST",
			url: url,
			data: "action=sendToFriend&fr_email="+friendmail+"&fr_sender_email="+sendermail+"&url="+$('#urlSendFriend').attr('value'),
			dataType: "text",
			beforeSend: function() {
				$(id).html('Wysyłanie wiadomości email');
			},
			error: function() {
				msgShow(id,'Bład',true);
			},
			success: function(data) {
				msgShow(id,data,false);
				$(ifriend).attr('value','');
				$(isender).attr('value','');
			}
		});
	} else {
		//return false;
	}
}
function msgShow(id,msg,error){
	$(id).html(msg);
	if(error){
		$(id).css('color','red');
	} else {
		$(id).css('color','green');
	}
}
function validateEmail(address){
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(address) == false) {
      return false;
   } return true;
}

function open_win(link){
	window.open(link,'problem','width=700px,height=400px,menubar=no,status=yes,location=no,toolbar=no,scrollbars=yes');
}
function show(name) {
 	$(name).toggle();
}


function log_register()
{
	document.getElementById('login').style.display = 'block';
	document.getElementById('register').style.display = 'none';
	document.getElementById('login_img').src= 'public/images/cart_icons/radio_on.gif';
	document.getElementById('register_img').src= 'public/images/cart_icons/radio_off.gif';
}

function register_log()
{
	document.getElementById('login').style.display = 'none';
	document.getElementById('register').style.display = 'block';
	document.getElementById('login_img').src= 'public/images/cart_icons/radio_off.gif';
	document.getElementById('register_img').src= 'public/images/cart_icons/radio_on.gif';

}
