/**
 * Wysłanie formy
 * @param url string adres dla formy
 * @param form_id string id formy
 */
function submitForm(url, form_id){
	document.getElementById(form_id).action = url;
	document.getElementById(form_id).submit();
}

/**
 * Aktualizuje listę portów po wybraniu kraju
 * @param country_id int id kraju
 * @param url string adres dla Ajaxa
 * @param div_id string id diva dla Ajaxa
 * @param ajax_upload_gif string kręciołka
 */
function updatePortsList(country_id, url, div_id, ajax_upload_gif){
	$.ajax({
		type: 'POST',
		url: url + '/' + country_id,
		data: $('#AttributeEditForm').serialize(),
		beforeSend: function(){
			$("#" + div_id).html(ajax_upload_gif);
		},
		success: function(html) {
			$("#" + div_id).html(html);
			updatePortsWidth();
		}
	});
}

/**
 * Funkcja przelicza wartość walut
 */
function convertExchanges(){
	var euro = parseFloat($('#ExchangeEuro').val());	
	var from = $('#ExchangeFrom').val();
	from = from.replace(',', '.');
	from = parseFloat(from);
	
	$('#ExchangeTo').html(Math.round((from * euro)*Math.pow(10,2))/Math.pow(10,2));
}

/**
 * Usunięcie zdjęcia z galerii
 * @param photo_id int id zdjęcia
 */
function deletePhoto(photo_id){
	$('#photo_'+photo_id).hide();
	$('#photo_to_delete').append("<input type='hidden' name='data[Photo][delete][]' value='"+photo_id+"'/>");
}

function moveRegions(type){
	var current = current_region_content;
	current_region_content = current_region_content + type;
	$('#regions_' + current).fadeOut(500, function(){
		$('#regions_' + current_region_content).fadeIn(500);
	});
	
	if (current_region_content == 1){
		$('#RegionsPrevious').css('visibility', 'hidden');
	}else{
		$('#RegionsPrevious').css('visibility', 'visible');
	}
	
	if (current_region_content == regions_content_count){
		$('#RegionsNext').css('visibility', 'hidden');
	}else{
		$('#RegionsNext').css('visibility', 'visible');
	}
}

function showContactMoreInfo(){
	var id = 'ContactMoreInfoDiv';
	
	if ($('#' + id).css('display') == 'none'){
		$('#' + id).show();
	}else{
		$('#' + id).hide();
	}
}

function showFaqQuestion(id){
	if (current_selected_question > 0){
		$('#faq_question_' + current_selected_question).slideUp();
	}
	
	if ($('#faq_question_' + id).css('display') == 'none'){
		$('#faq_question_' + id).slideDown();
		current_selected_question = id;
	}else{
		$('#faq_question_' + id).slideUp();
		current_selected_question = 0;
	}
}

function hideSelectedDivs(){
	$('.select').each(function(){
		if (this.tagName == 'DIV'){
			if (this.id != lastShowedDiv){
				$('#' + this.id).slideUp(300);
			}
		}
	});
	
	lastShowedDiv = '';
}

function showSelect(options_div, main_div){
	var options = $('#'+options_div);
	
	if (options.css('display') == 'none'){
		var position = $('#' + main_div).position();
		
		options.css('top', position.top + 20);
		options.css('left', position.left);		
		options.slideDown(300);
		
		lastShowedDiv = options_div;
	}else{
		options.slideDown(300);
	}
}

function select(value, div_input_id, prefix, input_id, select_div_id){
	$('#' + input_id).val(value);
	$('#' + div_input_id).html($('#' + prefix+value).html());
	$('#' + select_div_id).slideUp(300);
}

function showPhoto(key){
	if (key != current_photo){
		current_photo = key;
		$('#gallery_container_slider').animate({right: 310 * (current_photo - 1)}, 1000);
		
		var current_square = (parseInt($('#gallery_container_list_slider').css('right')) - 13) / 91;
		
		if (key - current_square == 3){
			nextGalleryList();
		}
		
		if (key - current_square == 1){
			previousGalleryList();
		}
	}
}

function nextGalleryList(){
	if (current_list_photo < photo_count - 2){
		current_list_photo++;
		$('#gallery_container_list_slider').animate({right: 91 * (current_list_photo - 1) + 13}, 1000);
		$('#previous_photo').css('visibility', 'visible');
		
		if (current_list_photo == photo_count - 2){
			$('#next_photo').css('visibility', 'hidden');
		}
	}
}

function previousGalleryList(){
	if (current_list_photo > 1){
		current_list_photo--;
		$('#gallery_container_list_slider').animate({right: 91 * (current_list_photo - 1) + 13}, 1000);
		$('#next_photo').css('visibility', 'visible');
		
		if (current_list_photo == 1){
			$('#previous_photo').css('visibility', 'hidden');
		}
	}
}

function showAddNoteForm(){
	if ($('#add_note').css('display') == 'none'){
		$('#add_note').show();
	}else{
		$('#add_note').hide();
	}
}

function showAddCommentForm(){
	if ($('#add_comment').css('display') == 'none'){
		$('#add_comment').show();
	}else{
		$('#add_comment').hide();
	}
}

function showNewTripForm(){
	if ($('#new_trip_form').css('display') == 'none'){
		$('#new_trip_form').show();
	}else{
		$('#new_trip_form').hide();
	}
}

function editTrip(id){
	$.ajax({
		url: url_trips_edit + '/' + id,
		beforeSend: function(){
			$('#edit_trip_form').hide();
			$('#edit_trip_form_ajax').show();
			$('#new_trip_form').hide();
		},
		success: function(html) {
			$('#edit_trip_form').html(html);
			$('#edit_trip_form').show();
			$('#edit_trip_form_ajax').hide();
			runTinyMCE();
		}
	});
}

function showNewReferenceForm(){
	if ($('#new_reference_form').css('display') == 'none'){
		$('#new_reference_form').show();
	}else{
		$('#new_reference_form').hide();
	}
}

function editReference(id){
	$.ajax({
		url: url_references_edit + '/' + id,
		beforeSend: function(){
			$('#edit_reference_form').hide();
			$('#edit_reference_form_ajax').show();
		},
		success: function(html) {
			$('#edit_reference_form').html(html);
			$('#edit_reference_form').show();
			$('#edit_reference_form_ajax').hide();
			runTinyMCE();
		}
	});
}

function showNewAnnouncementForm(){
	if ($('#new_announcement_form').css('display') == 'none'){
		$('#new_announcement_form').show();
	}else{
		$('#new_announcement_form').hide();
	}
}

function editAnnouncement(id){
	$.ajax({
		url: url_announcements_edit + '/' + id,
		beforeSend: function(){
			$('#edit_announcement_form').hide();
			$('#edit_announcement_form_ajax').show();
		},
		success: function(html) {
			$('#edit_announcement_form').html(html);
			$('#edit_announcement_form').show();
			$('#edit_announcement_form_ajax').hide();
			document.location.href = url_announcements_my + '#edit_announcement_div';
		}
	});
}

function editRide(id){
	$.ajax({
		url: url_rides_edit + '/' + id,
		beforeSend: function(){
			$('#edit_ride_form').hide();
			$('#edit_ride_form_ajax').show();
		},
		success: function(html) {
			$('#edit_ride_form').html(html);
			$('#edit_ride_form').show();
			$('#edit_ride_form_ajax').hide();
			$('#edit_trip_form_ajax').hide();
			runTinyMCE();
		}
	});
}

function editPhotoAlbum(id){
	$.ajax({
		url: url_photo_albums_edit + '/' + id,
		beforeSend: function(){
			$('#edit_photo_album_form').hide();
			$('#edit_photo_album_form_ajax').show();
		},
		success: function(html) {
			$('#edit_photo_album_form').html(html);
			$('#edit_photo_album_form').show();
			$('#edit_photo_album_form_ajax').hide();
		}
	});
}

function previousPhoto(){
	if (current_photo > 1){
		var current = current_photo;		
		current_photo--;
		
		$('#gallery_container_photo_album_slider').animate({right: 623 * (current_photo - 1)}, 1000);		
		
		$('#photo_description_' + (description_key_to_hide)).fadeOut(500, function(){
			$('#photo_description_' + (current - 1)).fadeIn(500);
		});
		
		if (current_photo == 1){
			$('#previous_photo').css('visibility', 'hidden');
		}
		
		$('#next_photo').css('visibility', 'visible');
		
		if (current_photo < current_photo_list + photo_list_limit - 1){
			previousPhotoList();
		}
		
		description_key_to_hide = current_photo;
	}
}

function nextPhoto(){
	if (current_photo < photos_count){
		$('#gallery_container_photo_album_slider').animate({right: 623 * current_photo}, 1000);
		
		var current = current_photo;
		$('#photo_description_' + (description_key_to_hide)).fadeOut(500, function(){
			$('#photo_description_' + (current + 1)).fadeIn(500);
		});
		
		current_photo++;
		
		if (current_photo == photos_count){
			$('#next_photo').css('visibility', 'hidden');
		}
		
		$('#previous_photo').css('visibility', 'visible');
		
		if (current_photo > current_photo_list + photo_list_limit - 1){
			nextPhotoList();
		}
		
		description_key_to_hide = current_photo;
	}
}

function previousPhotoList(){
	if (current_photo_list > 1){
		current_photo_list = Math.max(current_photo_list - Math.min(photo_list_limit, photos_count - photo_list_limit), 1);
		
		$('#gallery_container_photo_album_list_slider').animate({right: 91 * (current_photo_list - 1) + 13}, 1000);		
		
		if (current_photo_list == 1){
			$('#previous_list_photo').css('visibility', 'hidden');
		}
		
		$('#next_list_photo').css('visibility', 'visible');
	}
}

function nextPhotoList(){
	if (current_photo_list < photos_count - photo_list_limit + 1){
		if (photos_count - current_photo_list - photo_list_limit >= photo_list_limit){
			current_photo_list = current_photo_list + Math.min(photo_list_limit, photos_count - current_photo_list - photo_list_limit) - 1;
		}else{
			current_photo_list = current_photo_list + photos_count - current_photo_list - photo_list_limit;
		}
		
		$('#gallery_container_photo_album_list_slider').animate({right: 91 * current_photo_list + 13}, 1000);
		current_photo_list++;
		
		if (current_photo_list == photos_count - photo_list_limit + 1){
			$('#next_list_photo').css('visibility', 'hidden');
		}
		
		$('#previous_list_photo').css('visibility', 'visible');
	}
}

function showPhotoAlbumPhoto(key){
	if (key != current_photo){
		description_key_to_hide - current_photo;
		if (key > current_photo){
			current_photo = key - 1;
			nextPhoto();
		}else{
			current_photo = key + 1;
			previousPhoto();
		}
	}
}

function showUserContactForm(){
	var position = $('#user_email').position();
	
	var contact_form = $('#user_contact_form');
	
	contact_form.css('top', position.top + $('#user_email').height());
	contact_form.css('left', position.left + 35);
	contact_form.show();
}

/**
 * Uruchomienie tinyMCE
 * @author TB
 */
function runTinyMCE(){
	tinyMCE.init({
		theme : "advanced",
		mode : "textareas",
		language : "pl",
		convert_urls : false,
		theme_advanced_toolbar_location : "top",
		theme_advanced_buttons1: "bold,italic,underline,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,bullist,numlist,outdent,indent,separator,link,unlink,image,forecolor,separator,code",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_align : "left",
		plugins : "paste, advimage",
		theme_advanced_buttons3_add : "pastetext",
		paste_auto_cleanup_on_paste : true,
		width: tiny_mce_width,
		height: tiny_mce_height,
		file_browser_callback: 'fileBrowserCallBack',
		doctype: '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
		extended_valid_elements : 'a[name|href|target|title|class=content_link]',
		content_css : tiny_content_css
    });
}

/**
 * Wycentrowanie strony
 * @author TB
 */
function centerPage(){
	var window_width = $(window).width();
	var document_width = $(document).width();
	if (document_width > window_width){
		var scroll = Math.round(((document_width - window_width) / 2)*Math.pow(10,0))/Math.pow(10,0);
		$('html').attr('scrollLeft', scroll);
	}	
}

/**
 * Ustawia wszystkim checkboxom z portami taka sama szerokosc
 * @author TB
 */
function updatePortsWidth(){
	var max_width = 0;
	$('#ports_select div.checkbox').each(function(){
		if (this.style.width == '0px'){
			this.style.width = '';
		}
		max_width = Math.max($(this).width(), max_width);
	});
	
	$('#ports_select div.checkbox').css('width', max_width);
}


/**
 * Ustawia wszystkim checkboxom z armatorami taka sama szerokosc
 * @author TB
 */
function updateOwnersWidth(){
	var max_width = 0;
	$('#owners_select div.checkbox').each(function(){
		if (this.style.width == '0px'){
			this.style.width = '';
		}
		max_width = Math.max($(this).width(), max_width);
	});
	
	$('#owners_select div.checkbox').css('width', max_width);
}

function selectCountryPriceListSearch(){
	updatePortsList($('#SearchCountryId').val(), url_ports_update_list, 'ports_select', ajax_upload_gif);
	updateOwnersList($('#SearchCountryId').val(), url_owners_update_list, 'owners_select', ajax_upload_gif);
}

function updateOwnersList(country_id, url, div_id, ajax_upload_gif){
	$.ajax({
		url: url + '/' + country_id,
		beforeSend: function(){
			$("#" + div_id).html(ajax_upload_gif);
		},
		success: function(html) {
			$("#" + div_id).html(html);
			updateOwnersWidth();
		}
	});
}

/**
 * Wyszukiwarka
 * @param search_type int po czym szukać
 */
function searchByType(search_type){
	$('#SearchType').val(search_type);
	$('#SearchIndexForm').submit();
}

function addPhotoAlbum(){
	if ($('#PhotoAlbumName').val() == ''){
		alert('Proszę podać nazwę galerii');
		return false;
	}
	$.ajax({
		url: $('#PhotoAlbumCreate').attr('action'),
		type: 'POST',
		data: $('#PhotoAlbumCreate').serialize(),
		beforeSend: function(){
			$('#PhotoAlbumCreate').hide();
			$('#edit_photo_album_form').hide();
			$('#edit_photo_album_form_ajax').show();
		},
		success: function(html) {
			$('#edit_photo_album_form').html(html);
			$('#edit_photo_album_form').show();
			$('#edit_photo_album_form_ajax').hide();
		}
	});
}

function selectQuestionnaireProduct(){
	if ($('#QuestionnaireProductId').val() == 0){
		$('#product_additional').show();
	}else{
		$('#product_additional').hide();
	}
}

function selectQuestionnaireOwner(){
	if ($('#QuestionnaireOwnerId').val() == 0){
		$('#owner_additional').show();
	}else{
		$('#owner_additional').hide();
	}
}

function selectQuestionnairePort(){
	if ($('#QuestionnairePortId').val() == 0){
		$('#port_additional').show();
	}else{
		$('#port_additional').hide();
	}
}

function clearProductIndexForm(){
	$('#ProductName').val('Nazwa jachtu');
	$('#ProductBathroomsFrom').val('_od');
	$('#ProductBathroomsTo').val('do_');
	$('#ProductLengthFrom').val('_od');
	$('#ProductLengthTo').val('do_');
	$('#ProductCabinsFrom').val('_od');
	$('#ProductCabinsTo').val('do_');
	$('#ProductBunksFrom').val('_od');
	$('#ProductBunksTo').val('do_');
	$('#select_product_type_id').html('Wybierz typ jachtu');
	$('#ProductProductTypeId').val('');
}

function checkDownloadSendOfferForm(){
	if ($('#DownloadUsername').val() == '' || $('#DownloadDealNumber').val() == '' || $('#DownloadEmail').val() == ''){
		alert('Proszę uzupełnić wszystkie pola');
		return false;
	}
	return true;
}

function selectCountryQuestionnaire(){
	$.ajax({
		url: url_owners_update_select + '/' + $('#QuestionnaireCountryId').val(),
		success: function(html) {
			$('#owners_list').html(html);
		}
	});
	
	$.ajax({
		url: url_ports_update_select + '/' + $('#QuestionnaireCountryId').val(),
		success: function(html) {
			$('#ports_list').html(html);
		}
	});
}

function showPriceListProductsLoader(){
	/*$('#price_list_products').hide();
	$('#price_list_search_loader').show();
	document.location.href = document.location.href + '#price_list_search_loader';*/
	$('#PriceListProductsSearchWait').dialog('open');
}

function showCruisePriceListDetails(key){
	if ($('#cruise_price_list_details_' + key).css('display') == 'none'){
		$('#cruise_price_list_details_' + key).show('blind');
	}else{
		$('#cruise_price_list_details_' + key).hide('blind');
	}
}

function checkNewPhotoAlbum(){
	if ($('#PhotoAlbumName').val() == ''){
		alert('Proszę podać nazwę galerii');
		return false;
	}
	return true;
}

function showCruiseTerms(span){
	var table = $(span).parents('table');
	
	$('tr', table).show();
	$('tr.more', table).hide();
}
