var allow_dl = 0;
function loadBlock( url, container ) {
	if ( !url || !container ) return;
	$( '#' + container ).load( url );
};
function submitForm( form_id, response_container ) {
	action = $( '#' + form_id ).attr( 'action' );
	fields = $( '#' + form_id ).serialize();
	$( '#' + response_container ).load( action, fields );
};
function showDhtmlPopup( id, load_block_url ) {
	if ( $( '#' + id ).css( 'display' ) != 'none' ) return;
	loadBlock( load_block_url, id );
	$( '.dhtml_popup' ).hide();
	$( '#' + id ).show();
};
function hideDhtmlPopup( id ) {
	$( '#' + id ).hide();
	$( '#' + id ).html( '' );
};
function toggleBlock( id, action, container ) {
	if ( $( '#' + id ).attr( 'class' ) == 'active' ) return;
	$( '#' + id ).parent().children().removeClass( 'active' );
	$( '#' + id ).addClass( 'active' );
	loadBlock( root_url + '?act=js/' + action, container );
};
function checkDl( name, it ) {
	$.ajax({
		async: false,
		type: 'post',
		url: root_url + '?act=js/allow_dl&name=' + name,
		success: function ( val ) { allow_dl = val; },
		error: function() { allow_dl = 0; }
	});
	if ( allow_dl == 1 ) {
		$( '.dl_error' ).hide();
		return true;
	}
	$( '#sys_prod_dl_err_cookie_' + it ).show();
	return false;
};
function toggleProdMenu( id, prod_id ) {
	if ( $( '#' + id ).attr( 'class' ) == 'active' ) return;
	$( '.sys_prod_menu a' ).removeClass( 'active' );
	$( '#' + id ).addClass( 'active' );
	loadBlock( root_url + '?act=js/sys_presentation/blocks/product&product_id=' + prod_id, 'system_product_container' );
};
var topPosition = 205;
function resetFont() {
	topPosition = 205;
	size = 1;
	setDataToCookie( 'fontSize', 1 );
	$('.content').attr( 'class', 'content' );
	setDataToCookie( 'sessionId', sessionId );

	
}
function changeFont( action ) {

	if ( action == 1 ) {
		if ( size < 4 ) {
			size++;
		}
	} else {
		if ( size > 1 ) {
			size--;
		}
	}
	if ( size == 1 ) {
		topPosition = 205;
	}
	else if ( size == 2 ) {
		topPosition = 209;
	}
	else if ( size == 3 ) {
		topPosition = 211;
	}
	else if ( size == 4 ) {
		topPosition = 211;
	}
	$('.content').attr( 'class', 'content' );
	$('.content').addClass( 'size_'+size );
	
	
	setDataToCookie( 'sessionId', sessionId );
	setDataToCookie( 'fontSize', size );
}

function getPopupContainer(id) {
	id = id ? id : 'popup_container';
	
	var popupContainer = $('#' + id);
	
	if (!popupContainer.length) {
		popupContainer = $('<div id="' + id + '" class="js_popup" style="position: relative;"><div style="position: absolute;"></div></div>');
		$('body').append(popupContainer);
	}
	
	return popupContainer.find('div');
}

function expand_favorite( obj_button, subtype ) {

	var button = $( obj_button );
	var hide_button = $( '#hide_button_' + subtype );
	var listItems = $( '#items_' + subtype ).children( '.collapsable' );

	button.hide();
	hide_button.show();
	listItems.show();
}
	
function collapse_favorite( obj_button, subtype ) {

	var button = $( obj_button );
	var show_button = $( '#show_button_' + subtype );
	var listItems = $( '#items_' + subtype ).children( '.collapsable' );

	button.hide();
	show_button.show();
	listItems.hide();
}

function updateCollapsables( ) {

	// jeigu rodomas sarasas favorite item'u, tada update'ina, kurios is ju yra collapsable, kurios ne.
	// tam, kad tai veiktu, turi buti uzsetintas kintamasis visible_favorites - kiek item'u turi buti matoma is karto.
	if ( visible_favorites ) {

		$( '.favorite_list_items' ).map( function () {
		
			var index = 0;
			$(this).children().map( function() {
			
				if ( index++ > visible_favorites ) {
				
					$(this).addClass( 'collapsable' );
				} else {
				
					$(this).removeClass( 'collapsable' );
				}
			});
		});
	}
}

function onDomReady(event) {
	// "Show all favorites / events registrations" button
	$('.js_show_all_button', $('.js_favorites.js_list, .js_events.js_list')).bind('click', function(event) {
		var button = $(this);
		var list = button.parents('.js_list');
		var listItem = $('.js_list_item');
		
		event.preventDefault();
		
		button.hide();
		listItem.show();
	});
	
	
	// "Add to favorite" / "Register event" buttons
	$('.js_add_fer_button').bind('click', function(event) {
		var button = $(this);
		var popupContainer = button.parents('.js_fer_button_container:first').find('.js_fer_popup_container');
		
		$('.cnt_block_popup:visible .close').trigger('click');
		
		event.preventDefault();
		
		var requestParameters = null;
		if (button.hasClass('js_favorite')) {
			requestParameters = {
				url		: root_url + 'index.php?act=user_zone/favorites/add',
				data	: {content_item_id : button.attr('rel')}
			}
		} else if (button.hasClass('js_event_registration')) {
			requestParameters = {
				url		: root_url + 'index.php?act=user_zone/events/registrations/add',
				data	: {event_id : button.attr('rel')}
			}
		} else if ( button.hasClass( 'js_unregister' ) ) {
			
			requestParameters = {
				
				url: root_url + 'index.php?act=js/user_zone/unregister'
			}
		} else if ( button.hasClass( 'js_newsletter_subscribe' ) ) {
			
			requestParameters = {
				
				url: root_url + 'index.php?act=js/user_zone/newsletter/subscribe'
			}
		} else {
			return false;
		}
		
		if (!popupContainer.html().length) {
			$('.js_fer_popup_container').hide();
			$.ajax( {
				type: 'get',
				url: requestParameters.url,
				data: requestParameters.data,
				success: function(response){
					if (popupContainer) {
						popupContainer.html( response );
						popupContainer.find('.js_popup_close_button').bind('click', function(event) {event.preventDefault(); popupContainer.hide();});
						popupContainer.find('form.js_ajax').bind('submit', onAjaxFormSubmit);
						popupContainer.show();
						
						popupContainer.find( '.js_popup_close_button_2' ).bind( 'click', function( event ) {
							
							event.preventDefault();
							popupContainer.hide();
						} );
					}
				}
			} );
		} else {
			if ( popupContainer.css('display') == 'none' ) {
				$('.js_fer_popup_container').hide();
				popupContainer.show();
			} else {
				popupContainer.hide();
			}
		}
	});
	
	// "Remove favorite / event registration" button
	$('.js_remove_button', $('.js_favorites.js_list, .js_events.js_list')).bind('click', function(event) {
		var button = $(this);
		var list = button.parents('.js_list');
		var showAllButton = $('.js_show_all_button', list);
		var listItem = button.parents('.js_list_item');
		
		event.preventDefault();
		button.unbind('click');
		button.bind('click', function(event) {event.preventDefault();});
		
		var requestParameters = null;
		if (listItem.hasClass('js_favorite')) {
			requestParameters = {
				url		: root_url + 'index.php?act=js/user_zone/favorites/remove',
				data	: {content_item_id : button.attr('rel')}
			}
		} else if (listItem.hasClass('js_registration')) {
			requestParameters = {
				url		: root_url + 'index.php?act=js/user_zone/events/registrations/remove',
				data	: {event_id : button.attr('rel')}
			}
		} else {
			return false;
		}
		
		$.ajax( {
			type: 'post',
			url: requestParameters.url,
			data: requestParameters.data,
			dataType: 'json',
			success: function( response ){
				if (response.success) {
					$('.js_list_item:hidden:first', list).slideDown('fast');
					
					var listItemsCount = $('.js_list_item', list).length;
					
					if ( listItemsCount == 1 ) {
						list.slideUp('fast', function(event) {
							$(this).remove();
							if (!$('.js_favorites.js_list, .js_events.js_list').length) $('#user_zone_empty_favorites_list_text').show();
						});
					} else {
						if ( listItemsCount <= 6 ) {
							showAllButton.hide();
						}
						
						listItem.slideUp('fast', function(event) {$(this).remove();});
					}
					
					updateCollapsables( );
				}
			}
		} );
	});
	
	// "Navigation block" change button
	$('.js_navigation_block .js_navigation_container_change_button').live('click', function(event) {
		var buttonElement = $(this);
		var activeContainerElement = $('#' + buttonElement.attr('rel'));
		var containersElement = activeContainerElement.parents('.js_navigation_block');
		
		event.preventDefault();
		
		$('.js_navigation_container', containersElement).hide();
		activeContainerElement.show();
	});
	
	// Ajax forms submit event
	var onAjaxFormSubmit = function(event) {
		var form = $(this);
		var container = form.parents('.js_ajax_response_placeholder:first');
		var preloadHelper = form.parents('.js_preload_helper:first');
		
		event.preventDefault();
		
		if ( !container.length ) {
			container = form.parent();
		}
		if ( !preloadHelper.length ) {
			preloadHelper = container;
		}
		
		preloadHelper.css('visibility', 'hidden');
		
		$.ajax( {
			type: form.attr( 'method' ),
			url: form.attr( 'action' ),
			data: form.serialize(),
			success: function( response ){
				if (response.indexOf('[JS_REDIRECT_HELPER]') != -1) {
					$('body').append(response);
				} else {
					container.html(response);
					$('form.js_ajax', container).bind('submit', onAjaxFormSubmit);
					preloadHelper.css('visibility', 'visible');
				}
			}
		} );
	}
	
	$('form.js_ajax').bind('submit', onAjaxFormSubmit);
	
}

function updateCitiesList( country_id, selected_city ) {
	
	if ( typeof country_id != 'undefined' ) {
		
		$.ajax( {
			
			url: root_url + '?act=js/cities',
			data: { 'country_id' : country_id, 'selected_city' : selected_city },
			success: function( result ) {
				
				if ( typeof $( '#registration_form_city' ).get( 0 ) != 'undefined' ) {
					
					$( '#registration_form_city' ).html( '' ).html( result );
				}
			}
		} );
	}
}