/*******

	***	Anchor Slider by Cedric Dugas   ***
	*** Http://www.position-absolute.com ***
	
	You can use and modify this script for any project you want, but please leave this comment as credit.
	
	Never have an anchor jumping your content, slide it.
	
	Just add the class anchor at your <a> tag and it will slide, user with no javascript will 
	still go to destination with the normal html anchor

	Don't forget to put an id to your anchor !
	
*****/

$(document).ready(function() {
$('.lang_icon').live('click', function() {
    lang = $(this).attr('id').substr(0,2);
    $('#locale_form').find('[name="language"]').val(lang);
    $('#locale_form').submit();
});
});

$(document).ready(function() {
	anchor.init();
	comments.init();
	send_to_friend.init();
	$("#search_button").click(function(){
	    if('' != $('#query').val()) {
	        $('#wyszukiwarka').submit();			
	    }
	});
});

send_to_friend = {
	init: function() {
		$('#send_to_friend_mesg').dialog({ title: 'Wyślij do przyjaciela', autoOpen: false, modal: true, draggable: false, resizable: false, buttons: { 'Ok': function() { $(this).dialog("close");} } });
		$('#dialog').dialog({ title: 'Wyślij do przyjaciela', autoOpen: false, modal: true, draggable: false, resizable: false,
			buttons: { 'Anuluj': function() { $(this).dialog("close"); },
		        'Wyslij': function(){
					send_to_friend.clean_error();
					var processed = send_to_friend.validate();
					var loc = window.location.href;
					if(!loc) {
					    loc = window.location;
					}
					if('-1' != $.inArray(false, processed)) {
					    send_to_friend.show_error(processed);    
					} else {
					    $('#dialog').dialog('close');
					    $.post(loc, $('#send_to_friend').serialize(), function(data){
					        if(data) {
						    $('#dialog').html(data.template);						     	 
						    if( !data.ok ) {
							$('#send_error_a').show();
							$('#dialog').dialog('open');
						    } else {
							$('#send_to_friend_mesg').dialog('open');
						    }
						}
					    })
					}
				} 
		} });

		$('#send_mail').click(function(e){
		    e.preventDefault();
		    send_to_friend.clean_error();		    
		    $('#dialog').dialog('open');					    		    
		});
	},
	validate: function() {
		var result = new Array();
		$(':input', '#send_to_friend').each(function(index){
		    var good = false;
		    if(''!=this.value) {	
			// sprawdźmy jeszcze maila, czy się waliduje
			if($(this).hasClass('email')) {
			    var reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			    if(reg.test(this.value)) {
				good = true;
			    }
			} else {
			    good = true;
			}
		    }
		    result.push(good);
		});
		return result;
	},
	show_error: function(processed) {
	    for(i=0; i < processed.length; i++) {
		if(!processed[i]) {
		    $('#send_error_' + i).show();
		}
	    }
	},
	clean_error: function() {
	    for(i=0; i < 6; i++) {
	        $('#send_error_' + i).hide();	
	    }
	    $('#send_error_a').hide();
	}
}

comments = {
	init: function() {
		$('#dodaj').live('click', function() {
			comments.form();
		});

		$('#send_comment').live('click', function(e) {
			e.preventDefault();
			var loc = window.location.href;
			if(!loc) {
				loc = window.location;
			}
			$.post(loc, $('#comments-add').serialize(), function(data) {
			    if(data) {	
		  	        $('#comments').html(data);				
			    }
			    comments.form();
			});
		});
	},

	form: function() {
	    if($('#comments-add').css('display') == 'none') {
	        $('#comments-add').slideDown(200);
	    } else {
		$('#comments-add').slideUp(200);
	    }
	}
}

anchor = {
	init : function()  {
		$("a.anchorLink").click(function () {	
			elementClick = $(this).attr("href")
			destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
		  	return false;
		})
	}
}

