var charactersLimit = 600;
var formSent;
var product_id;
var RecaptchaOptions = {
	theme : 'clean',
	custom_translations : {
		instructions_visual : "Przepisz oba widoczne słowa:",
		instructions_audio : "Wpisz usłyszane słowa:",
		play_again : "Odtwórz słowa ponownie",
		cant_hear_this : "Pobierz dźwięk w formacie MP3",
		visual_challenge : "Zadanie tekstowe",
		audio_challenge : "Zadanie dźwiękowe(EN)",
		refresh_btn : "Przeładuj wyrazy",
		help_btn : "Pomoc(EN)",
		incorrect_try_again : "Niepoprawnie. Spróbuj ponownie"
	}
};


/* utils */
$(function() {
$('#facebook').hover(function(){
				$(this).animate({right:'0px'},{queue:false,duration:500});
			}, function(){
				$(this).animate({right:'-292px'},{queue:false,duration:500});
			});

	sA();
	window.onresize = sA;
	
	$('#ProductSearchForm').submit(function() {
		$('#ProductSearchForm').attr('action', $('#ProductSearchForm').attr('action') + '/' + $('#ProductSearch').val());
		return true;
	});
	
	if ($('#trigger-of-lightbox').length != 0) {
		armTheLB($('#trigger-of-lightbox').attr('rel'));
	}
	
	$('.thumbnails > img').each(function(i,val) {
		//if (i == 0) return;	
		img = jQuery(val);
		img.click(clickFoto);
	});
	
	$('#orbitvu-thumbnail').click(clickOrbit);
	
	product_id = $('#productId').val();
	
	//tabulation
	$(".tabs ul.tab-header").tabs("div.tab-content > div", { tabs: 'li' }); 
	
	//Comments
	formSent = false;
	setCharsLeft();
	$('#commentText').keyup(setCharsLeft);
	$('#commentSend').click(saveClick);
	$('#CommentAddForm').submit(saveClick);
	$('#add-comment').click(showOptions);
	
	$('#allProductPageComments').load($('#commentsGetterUrl').html(), null, onLoadProductComments);
	
	$('#nextPreviousComments .ajax-loader').hide();
	
	$("#commentOperation .login").click(function() {
		$("#login-box").css('display') == 'none' ? $("#login-box").slideDown('fast') : setTimeout(blink, 100);
		$.scrollTo(0, 'slow');
	});
});

function clickFoto() {
	$('#orbitvu').hide();
	$('.photo').show();
	
	$('.thumbnails > img').each(function(i,val) {
		img = jQuery(val);
		img.unbind('click');
	});
	$(".photo .preloader").show();
	jQuery.getJSON($('#ajaxPhotoSwitcher').attr('href') + '/' + $(this).attr('id'), prepareImage_callback);
}

function clickOrbit() {
	$('.photo').hide();
	$('#orbitvu').fadeIn('fast');
	$('object, embed').css('visibility', 'visible');
}

lightbox_image = false;
function prepareImage_callback(data,txt) {
	lightbox_image = data.source;
	$('.thumbnails > img').each(function(i,val) {	
		img = jQuery(val);
		//if (img.attr('id') == data.source) return;
		img.click(clickFoto);
	});
	jQuery('<img src="'+data.path+'" />').load(loadedImage_callback);
}

function loadedImage_callback(eventObj) {
	$('.photo #trigger-of-lightbox img').replaceWith(this);
	$('.photo #trigger-of-lightbox').attr('href', $('#getPhotoBig').attr('href') + '/' + lightbox_image);
	
	armTheLB(lightbox_image);
	
	$('.photo .preloader').hide();
}

function armTheLB(lightbox_image) {
	var startIndexLB = 0;
	var images = new Array();
	$('#meta-group a').each(function(i, element) {
		if ($(element).attr('id') == 'for-lb-' + lightbox_image) {
			startIndexLB = i;
		}
		
		var tmp = Array();
		tmp.push($(element).attr('href'));
		images.push(tmp);
	});
	$('.photo a').click(function() {
		jQuery.slimbox(images, startIndexLB,{counterText: 'Zdjęcie {x} z {y}'});
		return false;
	});
}

function showOptions() {
	$('#commentOperation').slideDown();
}

function setCharsLeft() {
	var comment, comment_length;
	if ($('#commentText').size() == 0) {
		return;
	}
	comment = $('#commentText').val();
	comment_length = comment.length;
	if (comment_length > charactersLimit) {
		$('#commentText').val(comment.substr(0, charactersLimit));
		$('#charsLeft').html('0');
	} else {
		$('#charsLeft').html(charactersLimit - comment_length );
	}
}

function saveClick() {
	if (formSent) {
		return false;
	}
	commentLen = $('#commentText').val().length;
	
	if (commentLen == 0 || commentLen > charactersLimit) {
		return false;
	}
	
	$('#commentSend').unbind('click');
	formSent = true;
	$('#commentOperation .ajax-loader').show();
	$.post(getRoot('/dodaj-komentarz'),{
			productId: $('#productId').val(),
			comment: $('#commentText').val(),
			recaptcha_response_field: $('#recaptcha_response_field').val(),
			recaptcha_challenge_field: $('#recaptcha_challenge_field').val()
		},saveClickCallback,'text');
	return false;
}

function saveClickCallback(data,txtStatus) {
	$('#commentOperation .ajax-loader').hide();
	
	if (data == 'ok') {
		$('#commentOperation').html('Twój komentarz został zapisany i zostanie wkrótce opublikowany.');
		$('#commentOperation').removeClass();
		$('#add-comment').hide();
		$('#commentOperation').addClass('info');
	} else {
		$('#commentSend').click(saveClick);
		formSent = false;
		if (data === 'incorrect-captcha-sol') {
			$('#recaptcha-explanation').addClass('recaptcha-error')
			$('#recaptcha-explanation').html('Błędnie wpisane słowa. Spróbuj jeszcze raz.');
		}
		Recaptcha.reload();
	}
}

function onLoadProductComments() {
	$('#comments-ajax-loader').hide();
	$('#nextPreviousComments a').each(function () {
			$(this).click(function () {
					$('#comments-ajax-loader').show();
					$('#allProductPageComments').load($(this).attr('href'), null, onLoadProductComments);
					return false;
				});

		});
}

function sA() {
 var sM = $("#view");
 var offset = $(window).height() - $("#top").height() - $("#footer").height();
 if (offset > 0) sM.css("min-height", offset);
}

