$(document).ready(function() {
	
	//Remove right border from table
	$("td:last-child, th:last-child").addClass('lastCell');
		   
	//Language select
	$('#selectedLanguage span, #availableLanguages').live('mouseover', function() {
		$('#availableLanguages').show();
		return false;
	});
	
	$('#selectedLanguage span, #availableLanguages').live('mouseout', function() {
		$('#availableLanguages').hide();
		return false;
	});
	
	//Default input value
	var active_color = '#474747'; // Colour of user provided text
	var inactive_color = '#bbb'; // Colour of default text	
	$("input.default-value").css("color", inactive_color);
	var default_values = new Array();
	$("input.default-value").focus(function() {

		if (!default_values[this.id]) {
			default_values[this.id] = this.value;
		}
		if (this.value == default_values[this.id]) {
			this.value = '';
			this.style.color = active_color;
		}
		$(this).blur(function() {
			if (this.value == '') {
				this.style.color = inactive_color;
				this.value = default_values[this.id];
			}
		});
	});
	
	
	//Dodatkowe linki
	$('.sub_cat_link').mouseover(function(){	
		$(this).css({color: '#07319D'});
	});
	
	$('.sub_cat_link').mouseout(function(){	
		$(this).css({color: ''});
	});	
	
	$('.sub_cat_link').click(function(e){
		e.preventDefault(); 	
		var url = $(this).parents('a').attr("href");
		var id = $(this).attr("id");
		window.location = url+'#'+id;
	});	
	
	hashes = document.location.hash.substring(1);
	if (hashes != undefined)
	{
		slideTo(hashes);
		if ($(window).scrollTop() > 180){
			$('#sideMenu').animate({top:$(window).scrollTop()-180+"px" },{queue: false, duration: 350});
		}		
	}
	
	//Przesówanie okna menu
	$(window).scroll(function()
	{
		if ($(window).scrollTop() > 180){
			$('#sideMenu').animate({top:$(window).scrollTop()-180+"px" },{queue: false, duration: 350});
		}
	
		if ($(window).scrollTop() <= 0){
			$('#sideMenu').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350});
		}
	}); 	
	
	//Scroll
	$('.scroll-pane').jScrollPane({showArrows: true});
	
	//Change image
	$('#productGallery a.product_photo_thumb').click(function(e) {
		e.preventDefault(); 	
		
		$('#productGallery .selected').next().show();
		
		$('#productGallery a.product_photo_thumb').removeClass('selected');
		$(this).addClass('selected');

		var image_medium = $('#productGallery .selected').attr('rel');
		var image_big = $('#productGallery .selected').attr('href');
		$('#productGallery .product_photo_main img').attr('src', image_medium);
		$('#productGallery .product_photo_main a').hide();
		$('#productGallery .product_photo_main a[href="'+image_big+'"]').show();
		
		var description = $('#productGallery .selected img').attr('alt');
		var title = $('#productGallery .selected img').attr('rel');
		$('#productGallery .product_photo_desc').html('<strong>'+title+'</strong><br />'+description);	
		$('#productGallery .product_photo_main a').attr('title', title);
			
	});
	
    //Tooltips
	$('.product_certificate').tipsy({gravity: 'e', opacity: '1.0', offset: 3, fade: true});
	$('.product_icon').tipsy({gravity: 'se', opacity: '1.0', offset: 3, fade: true});
	
	var tip;
	$(".tip_trigger").hover(function(){

		//Caching the tooltip and removing it from container; then appending it to the body
		tip = $(this).find('.tip').remove();
		$('body').append(tip);

		tip.show(); //Show tooltip

	}, function() {

		tip.hide().remove(); //Hide and remove tooltip appended to the body
		$(this).append(tip); //Return the tooltip to its original position

	}).mousemove(function(e) {
	//console.log(e.pageX)
		  var mousex = e.pageX + 15; //Get X coodrinates
		  var mousey = e.pageY + 15; //Get Y coordinates
		  var tipWidth = tip.width(); //Find width of tooltip
		  var tipHeight = tip.height(); //Find height of tooltip

		 //Distance of element from the right edge of viewport
		  var tipVisX = $(window).width() - (mousex + tipWidth);
		  var tipVisY = $(window).height() - (mousey + tipHeight);

		if ( tipVisX < 15 ) { //If tooltip exceeds the X coordinate of viewport
			mousex = e.pageX - tipWidth - 15;
			$(this).find('.tip').css({  top: mousey, left: mousex });
		} if ( tipVisY < 15 ) { //If tooltip exceeds the Y coordinate of viewport
			mousey = e.pageY - tipHeight - 15;
			tip.css({  top: mousey, left: mousex });
		} else {
			tip.css({  top: mousey, left: mousex });
		}
	});
	
	//jCarousel
	$('#mycarouse').jcarousel({
		scroll: 1,
		auto: 2,
        wrap: 'circular',
        initCallback: mycarousel_initCallback
		
    });
	
	//colorbox 
	$("a.colorbox").colorbox({scalePhotos: true, maxWidth: '90%', maxHeight: '90%', initialWidth: '50px', initialHeight: '50px', rel: 'gallery'});
	
	$(".questionbox").colorbox({width:"345px", height:"420px", initialWidth: '50px', initialHeight: '50px', inline:true, href:"#modalQuickQuestion"});
	
	//Jump to right place
	$('a[href^=#]').slideto({
		speed  : 500
	});	
	
	//wywolanie formularza zapytania
	question_form();	
	
	//wywolanie formularza kontaktowego
	contact_form();
	
	//wywoalanie formularza newsletter
	newsletter_form();
	
	$(window).bind('load', function()
	{
	
		introbox();
	
	});
	
	 // facebook widget
	 $(".facebook").hover(function(){
	    $(".facebook").stop(true, false).animate({right:"0"},"medium");
	 },function(){
	    $(".facebook").stop(true, false).animate({right:"-260"},"slow");
	 },500);
	

});

function introbox() {
	$("#introbox").colorbox({inline:true, href:"#introbox", transition:"elastic", speed:500, initialWidth: 0, initialHeight: 0, maxHeight: "70%", maxWidth: "70%", opacity: 0, open: "true"});		

}

function slideTo(divid){
        var targetOffset=0;
        if ( divid != 0)
                targetOffset = $("#"+divid).offset().top;
        $('html,body').animate({scrollTop: targetOffset}, 500);
}

function mycarousel_initCallback(carousel)
{
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

//Formularz kontaktowy
function contact_form() {
	
	$("form #contact_form_button").click(function(){

		var contact_success = $('.contact_success').text();
		var contact_error_send = $('.contact_error_send').text();
		var contact_error_email = $('.contact_error_email').text();
		var contact_error_all = $('.contact_error_all').text();
		var contact_wait = $('.contact_wait').text();

		$('#notice').fadeIn("normal").html('<div class="wait">'+contact_wait+'</div>');
		var parametry = $("form").serialize();
	
		$.ajax({
			type: "POST",
			url: "/classes/contact-process.php",
			data: parametry,
			cache: false,			
			success: function(html) {
				if (html == 'success')
				{
					$('#notice').fadeIn("normal").html('<div class="success">'+contact_success+'</div>');
					$('#contact_form_company, #contact_form_name, #contact_form_phone, #contact_form_email, #contact_form_message').val("");
				}
				else if (html == 'error_mail')
				{
					$('#notice').fadeIn("normal").html('<div class="error">'+contact_error_email+'</div>');
				}					
				else if (html == 'error_send')
				{
					$('#notice').fadeIn("normal").html('<div class="error">'+contact_error_send+'</div>');
				}				
				else
					$('#notice').fadeIn("normal").html('<div class="error">'+contact_error_all+'</div>');
			}
		});
		
		return false; 
	});
	
}

//Formularz zapytania
function question_form() {
	
	$("form #question_form_button").click(function(){

		var question_success = $('.question_success').text();
		var question_error_send = $('.question_error_send').text();
		var question_error_email = $('.question_error_email').text();
		var question_error_all = $('.question_error_all').text();
		var question_wait = $('.question_wait').text();

		$('#notice_question').fadeIn("normal").html('<div class="wait">'+question_wait+'</div>');
		var parametry = $("form").serialize();
	
		$.ajax({
			type: "POST",
			url: "/classes/question-process.php",
			data: parametry,
			cache: false,			
			success: function(html) {
				if (html == 'success')
				{
					$('#notice_question').fadeIn("normal").html('<div class="success">'+question_success+'</div>');
					$('#question_form_company, #question_form_name, #question_form_phone, #question_form_email, #question_form_message').val("");
				}
				else if (html == 'error_mail')
				{
					$('#notice_question').fadeIn("normal").html('<div class="error">'+question_error_email+'</div>');
				}					
				else if (html == 'error_send')
				{
					$('#notice_question').fadeIn("normal").html('<div class="error">'+question_error_send+'</div>');
				}				
				else
					$('#notice_question').fadeIn("normal").html('<div class="error">'+question_error_all+'</div>');
			}
		});
		
		return false; 
	});
	
}

//Formularz newsletter
function newsletter_form() {

	$("form #newsletter_form_button").click(function(){

		var newsletter_success = $('.newsletter_success').text();
		var newsletter_error_send = $('.newsletter_error_send').text();
		var newsletter_error_email = $('.newsletter_error_email').text();
		var newsletter_error_all = $('.newsletter_error_all').text();
		var newsletter_wait = $('.newsletter_wait').text();

		$('#notice_newsletter').fadeIn("normal").html('<div class="wait">'+newsletter_wait+'</div>');
		var parametry = $("form").serialize();
	
		$.ajax({
			type: "POST",
			url: "/classes/newsletter-process.php",
			data: parametry,
			cache: false,			
			success: function(html) {
				if (html == 'success')
				{
					$('#notice_newsletter').fadeIn("normal").html('<div class="success">'+newsletter_success+'</div>');
					$('#newsletter_form_email').val("Wpisz adres e-mail");
					$('#newsletter_form_email').css('color', '#aaa');
					setTimeout(function() {
						$('#notice_newsletter').fadeOut('slow');
					}, 8000);
				}
				else if (html == 'error_mail')
				{
					$('#notice_newsletter').fadeIn("normal").html('<div class="error">'+newsletter_error_email+'</div>');
					setTimeout(function() {
						$('#notice_newsletter').fadeOut('slow');
					}, 5000);					
				}					
				else if (html == 'error_exist')
				{
					$('#notice_newsletter').fadeIn("normal").html('<div class="error">'+newsletter_error_send+'</div>');
					$('#newsletter_form_email').val("Wpisz adres e-mail");
					$('#newsletter_form_email').css('color', '#aaa');

					setTimeout(function() {
						$('#notice_newsletter').fadeOut('slow');
					}, 5000);						
				}				
				else
					$('#notice_newsletter').fadeIn("normal").html('<div class="error">'+newsletter_error_all+'</div>');
					setTimeout(function() {
						$('#notice_newsletter').fadeOut('slow');
					}, 5000);						
					
			}
		});
		
		return false; 
	});
	
};
