var drb = {
	param:{
		edit_cancel: function(a){
			var param_value = a.parents('.param-value');
			
			jQuery.ajax({
				type: 'GET',
				dataType: 'html',
				success: function(resp){					
					param_value.html(resp);
				},
				beforeSend: function(){
					a.after('<img src="/i/ajax-loader-comments.gif" alt="loading..." title="loading..." />');
				},
				url: a.attr('href')
			});
			return false;
		},
		edit: function(a){
			var param_value = a.parents('.param-value');
			
			jQuery.ajax({
				type: 'GET',
				dataType: 'html',
				success: function(resp){
					param_value.html(resp);
				},
				beforeSend: function(){
					param_value.html('<img src="/i/ajax-loader-comments.gif" alt="loading..." title="loading..." />');
				},
				url: a.attr('href')
			});
			return false;
		},
		save: function(form){
			var param_value = form.parents('.param-value');
			
			dataString = form.serialize();

			jQuery.ajax({
	            type: "POST",
	            url: form.attr('action'),
	            data: dataString,
	            beforeSend: function(){
	            	form.find('input[type="submit"]').after('<img src="/i/ajax-loader-comments.gif" alt="loading..." title="loading..." />');
				},
	            success: function(resp) {
	            	param_value.html(resp);
	            }
	        });
			return false;
		}
	}	
};

var diverclub = {
	invite_friend_more: function(a){
		a.parent().next('div.field').show(100); 
		a.remove(); 
		return false;
	},	
	smile: function(a){
		$.ajax({
			type: 'get',
			url: a.attr('href'),
			dataType: 'html',			
			success: function(data){
				a.addClass('button-smile-ok');
				a.after(data);
			}
		});
		return false;
	},	
	vote: function(a){
		var vote_box = a.parent('.vote-box');
		vote_box.find('.result-vote').html('<img src="/i/ajax-loader-comments.gif" alt="loading..." />');
		$.ajax({
			type: 'get',
			url: a.attr('href'),
			dataType: 'json',			
			success: function(data){
				vote_box.find('.result-vote').html(data.msg);
				if(data.status == 'success'){
					vote_box.find('.info-vote').replaceWith(data.info);
				}
			}
		});
		return false;
	},
	
	forum:{
		notify:{
			markAsRead: function(a, id){
				$.ajax({
					type: 'get',
					url: a.attr('href'),
					dataType: 'html',			
					success: function(data){
						id.slideUp(300, function(){
							$(this).remove();							
						});
					}
				});
				return false;
			}
		}
	},
	
	dropdown: function() {
		$('#nav .dropdown').hover(
			function(){ $(this).css('height', 'auto'); },
			function(){ $(this).css('height', '2.5em'); }
		);			
	},
	
	messages:{
		remove: function(a, id){
			if(confirm){
				$.ajax({
					type: 'GET',
					dataType: 'html',
					success: function(i){
						id.slideUp(300, function(){
							$(this).remove();							
						});									
					},
					url: a.attr('href')
				});
			}
			return false;
		},
		reply: function(button){
			/* Formularz odpowiedzi wiadomości */
			button.remove();
			$('#reply').show(100, function(){
				$('#message_body').focus();
			});
			$('#message_body').animate({scrollTop: $('#message_body').height()}, 300);
		}
	},
	
	comments:{
		comment_reply: function(id, text, a){
			$('#comment_idcomment_reply').val(id);
			$('html, body').animate({scrollTop: $('#list-comments').offset().top}, 550);
			$('#comment_text').focus().text(text);			
			$('form.comments').attr('action', a.attr('href'));
			return false;
		},
		getMore: function(a){
			var list = a.closest('.activity'),
				offset = a.offset().top;								
			a.addClass('loading');
			$.ajax({
				type:'get',
				dataType:'html',
				success:function(i){
					$(i).appendTo(list);
					a.parent().remove();
					$('html, body').animate({scrollTop: offset}, 750);
				},
				url: a.attr('href')
			}); 
			return false;
		},
		remove: function(a, id){
			if(confirm){
				$.ajax({
					type: 'GET',
					dataType: 'html',
					success: function(i){
						id.slideUp(300, function(){
							$(this).remove();							
						});									
					},
					url: a.attr('href')
				});
			}
			return false;
		}
	},
	
	selectLanguage: function() {
		var dropdown = $('#nav .lang');
		
		dropdown.find('a.current').parent().prependTo(dropdown);
	},
	
	selectPartner: function() {
		$('#select-partner .item').click(function(){
			var item = $(this);
			$.ajax({
				type: 'GET',
				dataType: 'json',
				beforeSend: function(){
					item.addClass('loader');
				},
				success: function(resp){
					item.removeClass('loader');
					if(resp.saved){
						item.addClass('selected');
					}else{
						item.removeClass('selected');
					}	
				},
				url: item.find('.check').attr('href')
			});
			return false;
		});
	},
	
	gallery: function() {
		var photo = $('#photo'),
			photoWidth = photo.width();
		
		if($('a', photo).length) {
			var img = $('img', photo),
				imgHeight = img.height(),
				linkWidth = 400;
				
			if($('.prev', photo).length) {
				var prev = $('<a />', {
						id: 'prev-photo',
						href: $('.prev', photo).attr('href'),
						css: {
							left: 0,
							height: imgHeight,
							width: linkWidth
						}
					})
					.hover(
						function(){ $('.prev', photo).addClass('hover'); },
						function(){ $('.prev', photo).removeClass('hover'); }
					)
					.appendTo(photo);
			}

			if($('.next', photo).length) {
				var next = $('<a />', {
					id: 'next-photo',
					href: $('.next', photo).attr('href'),
					css: {
						right: 0,
						height: imgHeight,
						width: linkWidth
					}
				})
				.hover(
					function(){ $('.next', photo).addClass('hover'); },
					function(){ $('.next', photo).removeClass('hover'); }
				)
				.appendTo(photo);
			}
		}
	},
	
//	tableRowLink: function() {
//		if(!$('div.table-a table').length) {
//			return;
//		}
//		
//		$('div.table-a table').each(function(){
//			$('tr', this).each(function(){
//				$(this).click(function(e){
//					window.location = $('a:first', this)[0].href;
//				});
//			});
//		});
//	},
	
	disabledButton: function() {
		$('span.disabled input').live('click', function() {
			return false;
		});
	},
	
	fixes: {
		misc: function() {
			$('#nav .inbox').css('min-width', $('#nav .dropdown').width() - parseInt($('#nav .inbox').css('paddingLeft')))
		},
		nthChild: function() {
			$('#photos > a:nth-child(7n), #videos > a:nth-child(7n)').addClass('nth7n');
		},
		chromeAutofillBug : function() {
			if (is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
			    $('#content input').attr('autocomplete', 'off');
			}
		}
	},

	gmaps: {
		load: function() {
			if (GBrowserIsCompatible()) {			
				map = new GMap2(document.getElementById("map_wrap"));
				geocoder = new GClientGeocoder();							
				map.enableScrollWheelZoom();
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				if(pos_x && pos_y){
					map.setCenter(new GLatLng(pos_x, pos_y), 7);					
					var marker = new GMarker(new GLatLng(pos_x, pos_y));
					map.addOverlay(marker);
				}	
			}
		},
		loadExpedition: function() {
			if (GBrowserIsCompatible()) {			
				map = new GMap2(document.getElementById("map_wrap"));
				geocoder = new GClientGeocoder();							
				if(pos_x && pos_y){
					map.setCenter(new GLatLng(pos_x, pos_y), 3);					
					var marker = new GMarker(new GLatLng(pos_x, pos_y));
					map.addOverlay(marker);
				}	
			}
		}
	},
	
	ellipsis: function() {	
		$('.user-profile td.limit').each(function(){
			var s = $('> span', this);
			if($('span', s).text().length > 21) {
				var ss = $('span', s);
				
				s.addClass('ellipsis').find('.email').removeClass('email');
				ss.after('<span>' + ss.text() + '</span>');
				ss.text(ss.text().substr(0, 19) + '…');
			}
		});
		
		$('.side .excerpt li em').each(function(){
			if($(this).text().match(/\s/)) {
				var t = $(this).text();
				t = t.substr(0, t.indexOf(' '));
				$(this).text(t);
			}
			
			if($(this).text().length > 10) {
				var name = $(this).text(),
					short = $(this).text().substring(0, 7) + '…';

				$(this).text(short);
				/*
				$(this).closest('a').hover(
					function(){ $('em', this).text(name); },
					function(){ $('em', this).text(short); }
				);
				*/
			}
		});
	},
	
	equalHeights : function() {

		var items = $('.diverbase-list .item > a'),
			rows = Math.ceil(items.size()/2);

		for (i = 0; i < rows; i++){
			items.slice((i*2),(i*2+2)).equalHeights();
		}
		
		var gallery = $('.main .excerpt:not(.list-a) li.item > a'),
			gRows = Math.ceil(gallery.size()/2);
		
		for (i = 0; i < gRows; i++){
			gallery.slice((i*2),(i*2+2)).equalHeights();
		}

		
		var friends = $('#content .friends-list > li > a'),
			fRows = Math.ceil(friends.size()/2);
		
		for (i = 0; i < fRows; i++){
			friends.slice((i*3),(i*3+3)).equalHeights();
		}		
	},
	
	diving: {
		load_coords: function(a) {
			navigator.geolocation.getCurrentPosition(function(position){

				$.ajax({
					type: 'POST',
					data: {lat: position.coords.latitude, lng: position.coords.longitude},
					dataType: 'json',
					success: function(resp){
						$('#diving_gps_n_0').val(resp.n[0]);
						$('#diving_gps_n_1').val(resp.n[1]);
						$('#diving_gps_n_2').val(resp.n[2]);
						$('#diving_gps_n_3').val(resp.n[3]);
						
						$('#diving_gps_e_0').val(resp.e[0]);
						$('#diving_gps_e_1').val(resp.e[1]);
						$('#diving_gps_e_2').val(resp.e[2]);
						$('#diving_gps_e_3').val(resp.e[3]);
					},
					url: a.attr('href')
				});
				
			});
			return false;
		}
	}
};


$(function(){


	$('body').addClass('js');
	
	$('.item.gallery a, .excerpt .item a').each(function(){
        text = $(this).text();
        text = jQuery.trim(text);
       
        if(text == '')
            $(this).remove();
       
    });
    /* poprawka błednego wyświetlania ekspedycji */
	$('.item.gallery').each(function(){

        a = $(this).find('a:empty');
        a.addClass('empty');

        if(a.is('.empty')){
            url = a.attr('href');
            a.remove();

            $(this).find('div dl dt').unwrap('a');
            $(this).children('div').wrap('<a href="'+url+'"></a>');
        }
        else if($(this).children().is('div')){
            url = $(this).find('div dl a').attr('href');

            $(this).find('dl dt').unwrap('a');
            $(this).children('div').wrap('<a href="'+url+'"></a>');
        }
    }); 
    /* poprawka błednego wyświetlania galerii i spolecznosci */
	$('.excerpt .item>dl').each(function(){

        url = $(this).find('a').attr('href');

		$(this).find('dt').unwrap('a');
		$(this).wrap('<a href="'+url+'" style="min-height: 102px;"></a>');
	
    }); 
    
	
	/*if(typeof user !== 'undefined'){		
		var checkTime = setInterval('check_time()', 30000);
	}*/
	
	$(document).keyup(function(event){
		if($(event.target).is('html') || $(event.target).is('body')){
		    if (event.keyCode == 37){
		    	if($('#prev-photo', '#photo').attr('href'))
		    		window.location.href = $('#prev-photo').attr('href');
		    }
		    else if (event.keyCode == 39){
		    	if($('#next-photo', '#photo').attr('href'))
		    		window.location.href = $('#next-photo').attr('href');
		    }
		}
	});
	if($.browser.msie){
		$('.releted-trigger-input').click(function(){	
			checkHiddenInputs()
		});
		$('.releted-trigger-input').prev('label').click(function(){	
			checkHiddenInputs()
		});
	
		function checkHiddenInputs(){
			$('.releted-trigger-input').each(function(){
				
				return $(this).is(':checked') ? $(this).siblings('.releted-hidden-elem').show() : $(this).siblings('.releted-hidden-elem').hide();
				
			});		
		}	
	}
	
	diverclub.dropdown();
	
	if($('form .field').length) {
		$('form .field').filter(':has(:checkbox)').addClass('checkbox');
	}
	
	if($('.main form').length == 1) { 
		$('.main form .field:first').find('input, select').focus(); 
	}
	
	if($('#select-partner').length) {
		diverclub.selectPartner();
	}
	
	if($('#rotator').length) {		
		$.getScript('/js/jq/jquery.cycle.lite.1.0.min.js', function() {
		
		$('#rotator')
			.cycle({
				timeout: 5000,
				speed: 1000,
				pause: 1
			});
		});		
	}
	
	diverclub.fixes.misc();
	diverclub.fixes.nthChild();
	diverclub.fixes.chromeAutofillBug();
	
	diverclub.gallery();
	//diverclub.tableRowLink();
	diverclub.disabledButton();
	diverclub.ellipsis();
	diverclub.equalHeights();
	diverclub.selectLanguage();
	
	$('#get-more-expeditions-my').live('click', function(){
		var a = $(this);
		$.ajax({
			type: 'GET',
			dataType: 'html',
			success: function(i){
				$(i).appendTo('tbody.my-exp');
				$('tbody.my-exp').find('.pagination').remove();
			},
			url: a.attr('href')
		});
		return false;
	});
	$('#get-more-expeditions-friends').live('click', function(){
		var a = $(this);
		$.ajax({
			type: 'GET',
			dataType: 'html',
			success: function(i){
				$(i).appendTo('tbody.friends-exp');
				$('tbody.friends-exp').find('.pagination').remove();
			},
			url: a.attr('href')
		});
		return false; 
	});
	

	if($.browser.msie){
		
		if($.browser.version <= 7){
			$('.th-colon th').append(':');
			$('#select-partner .item').append('<span class="after" />')
			$('#select-partner .item.nth-5n').after('<li class="clear">-</li>');
		} // ie7
			
		
	} // ie
	
});
function check_time(){
	$.ajax({
		data: user,
		type: 'GET',
		dataType: 'json',
		success: function(i){
		},
		url: '/check_time.php'
	});
}
function myCustomQueueCompleteHandler(){
	swfu_widget.fire(swfu_widget.handlers, 'queue_complete');
	$('#add_few_next_button').show();
	window.onbeforeunload = true;
	return true;
}
function myCustomFileDialogStartHandler(){
	swfu_widget.handlers.onFileDialogStart();
	window.onbeforeunload = function(){
		return 'Upload in progress...';
	};
}

/*jslint browser: true */ /*global jQuery: true */
/* equal heights */
(function($){$.fn.equalHeights=function(){var max=0;$(this).each(function(){if($(this).height()>max){max=$(this).height();}}).css({'min-height':max});if($.browser.msie&&parseInt($.browser.version)==6)$(this).height(max);return this;};})(jQuery);
