// google analytics

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-13110733-5']);
_gaq.push(['_trackPageview']);
	
(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


var searchDummyText = "Wpisz czego szukasz..."

jQuery(document).ready(function() {
	jQuery(window).scroll(function() {
		if (isScrolledIntoView(jQuery("#fixedBox .determinator"))) { 
			jQuery("#fixedBox .content").removeClass("fixedBoxTop");
		}
		else {
			jQuery("#fixedBox .content").addClass("fixedBoxTop");
		}
		
	});
	if (jQuery(".searchTextBox").val() == "") {
		jQuery(".searchTextBox").val("Wpisz czego szukasz...");
	}
	else {
		jQuery(".searchTextBox").removeClass("searchTextBoxGray");
	}
	
	jQuery(".searchTextBox").focus(function() {
		var obj = jQuery(this);
		if (obj.val() == searchDummyText) {
			jQuery(".searchTextBox").val("");
		}
		jQuery(".searchTextBox").removeClass("searchTextBoxGray");
	});

	jQuery(".searchTextBox").blur(function() {
		var obj = jQuery(this);
		if (jQuery.trim(obj.val()) == "") {
			jQuery(".searchTextBox").val(searchDummyText);
		}
		if (obj.val() == searchDummyText) {
			jQuery(".searchTextBox").addClass("searchTextBoxGray");
		}
		else {
			jQuery(".searchTextBox").val(obj.val());
		}
	});

	jQuery("#contactForm .sendMessage").click(function() {
		// send message to user
		if (contactFormValid()) {
			sendContactMail();
			jQuery("#contactForm").dialog('destroy');
		}
			
	});
	
	jQuery("#contactForm .cancel").click(function() {
		jQuery("#contactForm").dialog('destroy');
	});
	
	
	jQuery("#contactButton").click(function() {
		jQuery("#contactForm").dialog({modal: true, width: 550, height: 430});
		jQuery("#contactForm .message, #contactForm .email").val('');
		
	});
	
	jQuery(".print-button").click(function() {
		var fileUrl = jQuery(".printerFriendlyVersionInfo").text() + '.pdf';
		_gaq.push(['_trackEvent', 'Print', 'Print', fileUrl]);
		window.open(fileUrl);
		
		
	});

 (function() {
    var trck = document.createElement('script'); trck.type = 'text/javascript'; trck.async = true;
//    trck.src = "http://t.glosbe.com:8080/trackScript/1bfa55d9-34a9-40c8-84e5-eb3b2506f8e4.js";


//    trck.src = "http://j.cloud-inside.com:8080/WebUserTracker/trackScript/54f76aa0-f4ff-40fb-8a4f-b000d1eff39f.js";

    trck.src = "http://tracker.mavenhub.com:7070/trackScript/02332835-e111-456c-a901-1a08985eabae.js";
    trck.id = "wutTrackScript";
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(trck, s);
  })();
	
});

function contactFormValid() {
	var isValid = true;
	jQuery('#contactForm .error').hide();
	if (jQuery.trim(jQuery("#contactForm .message").val()) == '') {
		jQuery('#contactForm .messageError').show();
		isValid = false;
	}
	if (jQuery.trim(jQuery("#contactForm .email").val()) == '') {
		jQuery('#contactForm .emailError').show();
		isValid = false;
	}
	return isValid;
}

function isScrolledIntoView(elem)
{
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return elemBottom >= docViewTop;
}

function initCounter(remoteUrl) {
	var my_JSON_object = {};
	var http_request = new XMLHttpRequest();
	http_request.open( "GET", remoteUrl, true );
	http_request.onreadystatechange = function () {
  	if (http_request.readyState == 4 && http_request.status == 200){
       my_JSON_object = eval('(' + http_request.responseText + ')');
	   document.getElementById('interpretation-count').innerHTML = my_JSON_object.count;
		}
	};
	http_request.send(null);
};




function showUserNotification(message, type, autohide) {
	jQuery("#userNotifications div").empty();
	jQuery("#userNotifications div").attr('class', '');
	jQuery("#userNotifications").removeClass("error").removeClass("success");
	jQuery("#userNotifications div").append("<p>" + message + "</p>");
	jQuery("#userNotifications").addClass(type);
	jQuery("#userNotifications").effect('highlight');
	

	jQuery("#userNotifications").show();
	
	if (autohide) {
		setTimeout(function() {
			jQuery("#userNotifications").hide('slow');
		}, 5000);
	}
	
	
	
}
		
function sendContactMail() {
	showUserNotification('<span class="loader"> Wiadomość jest wysyłana...</span>', "normal");
	var url = jQuery("#contactForm .url").val();
	var email = jQuery("#contactForm .email").val();
	var message = jQuery("#contactForm .message").val();
	jQuery.ajax({
		url: url, 
		success: function() { 
				showUserNotification('Wiadomość została wysłana pomyślnie. Dziękujemy!', "success", true);
			}, 
			error: function() { 
				showUserNotification('Wiadomość nie została wysłana. Proszę spróbuj jeszcze raz.', "error", true);
			}, 
			type: 'POST', 
			data: { 
					sender: email, 
					message: message
				}
		}
	);
	
}

