var arr=new Array(1, 2, 3);
var i=1 ;
var length=3;
var currSlide=3;
var slideIntervalId=0;

$().ready(function() {
if($(".slide_box").length>0) {
	slideIntervalId=setInterval("navigate('next', true)", 12000);
}
});

$("a.js_ajaxLoad").live("click", function(){
    var rel=$(this).attr('rel');
        var container=rel ? $("#"+rel) : $();
            $.ajax({
                type: $(this).hasClass("js_ajaxPost") ? "POST" : "GET",
                url: $(this).attr("href"),
                success: function(response) {
                        if(container.size()>0) {
                            container.html(response);
                            container.show();
                        }
                }
            });
    return false;
});


  function navigate(slide, interval) {
	  if($("#slide"+currSlide).length>0) { 
		  if(!interval) clearInterval(slideIntervalId);
		  $("#slide"+currSlide).hide();
		  if(slide=='next') currSlide++;
		  else currSlide--;
		  
		  if(currSlide<arr[0]) currSlide=arr[arr.length-1];
		  else if(currSlide>arr[arr.length-1]) currSlide=arr[0];
		  if($("#slide"+currSlide).length>0) $("#slide"+currSlide).show("slow");
	  }
  }
	function Pager(object) {
		this.pager=object.find(".pager");
		this.container=object.find(".container");
		
		if(this.pager.length>0 && this.container.length>0) {
			this.overlay=$('<div class="overlay"></div>').prependTo(this.container).hide();
			var ov=this.overlay;
			this.container.ajaxStart(function(){
    			ov.show();
    		});
			this.container.ajaxComplete(function(){
				ov.hide();
			})
			var pager=this;
			this.pager.find("a:not(.next):not(.prev)").click(function(){
				pager.load($(this));
				return false;
			});
			this.pager.find("a.next").click(function(){
				var curr=$(this).siblings(".bold");
				var next=curr.nextAll("a");
				if(next.size()>0 && $(next.get(0)).index(this)==-1) {
					var firstNext=$(next.get(0));
					pager.load(firstNext);
					curr.removeClass("bold");
					firstNext.addClass("bold");
				}
				return false;
			})
			
			this.pager.find("a.prev").click(function(){
				var curr=$(this).siblings(".bold");
				var prev=curr.prevAll("a");
				if(prev.size()>0 && $(prev.get(0)).index(this)==-1) {
					var firstPrev=$(prev.get(0));
					pager.load(firstPrev);
					curr.removeClass("bold");
					firstPrev.addClass("bold");
				}
				return false;
			})
			
			
		}
	}
	
	Pager.prototype={
			load: function(a) {
	    		this.container.load(a.attr("href"), function(){
	    			a.addClass("bold");
	    			a.siblings().removeClass("bold");
				});
			}
	}
	

    function galleryImageOnLoad() {
    	$(document).ready(function(){
    		
    	 	$('#voiceConfirm').dialog({ 
    	 		title: "Dodaj głos",
    	 		
    			modal: true, 
    	        draggable: false,
    	        resizable: false,
    	        autoOpen: false
    		});
    		
    		$("a.star_voice").click(function(){
    			var href=$(this).attr("href");
    			var note=$(this).text();
    			
    			$('#voiceConfirm').html("Czy napewno chcesz dać ocenę: "+ note +"?");
    			$('#voiceConfirm').dialog("option", "buttons", {			
    	            "Dodaj głos": function() {	 
    	            	ajaxAddVoice(href);
    	            	$('#voiceConfirm').dialog("close");
    	       		 }
    			});

    			$('#voiceConfirm').dialog("open");
    			return false;
    		})
    	})
    	
    }	
  function ajaxNewsletterAdd() {
  
		$.ajax( {
					url : "/newsletters/create",
					type : "POST",
					dataType : "json",
					data : $("#newsletter").serialize(),
					
					beforeSend : function() {
						$("#completed_route_preloader").show();
					},
					complete : function(response) {
						eval("var response="+response.responseText);

						if (response.msg == "add_oki") {
							
							$("#msg").html(
									"Na email wysłano link aktywacyjny.");
						    $("#msg_err").hide();
							$("#msg").show();
							
						}
						else if (response.msg == "add_exist") {
							$("#msg_err").html(
							               "Podany email istnieje już w bazie.");							
					         $("#msg_err").show();
						}
						else if (response.msg == "add_empty") {
							$("#msg_err").html(
							               "Podaj adres email.");
					         $("#msg_err").show();
						}
						else if (response.msg == "add_invalid") {
							$("#msg_err").html(
							               "Podany email jest nieprawidłowy.");
					         $("#msg_err").show();
						}
						else if (response.msg == "update_newsletter") {
							$("#msg").html(
							               "Email został zapisany.");
							 $("#msg_err").hide();
					         $("#msg").show();
						}
						
						
					}
				});
	
	  return false;
   }
  
  function ajaxInvitationAdd() {
	  
		$.ajax( {
					url : "/invitations/create",
					type : "POST",
					dataType : "json",
					data : $("#invitation").serialize(),
					
					beforeSend : function() {
						$("#completed_route_preloader").show();
					},
					complete : function(response) {
						eval("var response="+response.responseText);
						if (response.msg == "add_oki") {
							
							$("#info").html(
									"Zaproszenie zostało wysłane.");
							$("#invitation_email").attr('value', '');
							$("#msg").hide();
							$("#info").show();
							
						}
						else if (response.msg == "add_exist") {
							$("#msg").html(
							               "Użytkownik należy do społeczności iTur.");
							$("#info").hide();
					         $("#msg").show();
						}
						else if(response.msg == "invite_exist"){
							$("#msg").html(
				               "Zaproszenie zostało już wysłane.");		
							$("#info").hide();
		                     $("#msg").show();
						}
						else if (response.msg == "add_empty") {
							$("#msg").html(
							               "Podaj adres email.");
							$("#info").hide();
					         $("#msg").show();
						}
						else if (response.msg == "add_invalid") {
							$("#msg").html(
							               "Podany email jest nieprawidłowy.");
							$("#info").hide();
					        $("#msg").show();
						}

						
						
					}
				});
	
	  return false;
 }
  
  function ajaxAddVoice(href){
  	$.ajax( {
			url : href,
			type : "POST",
			dataType : "json",
			
			complete : function(response) {
				
				eval("var response="+response.responseText);
				
				if (response.msg == "voice_ok") {
					
					$("#amount").html(response.amount_voices);
					$("#avg").html(response.avg_voices);	
					$("#voice_form").hide();
					$("#voice_added_info").show(); 
				}else if (response.msg = "voice_error") {
					$("#msg_voice_err").html(
					"Zdjęcie zostało ocenione wcześniej przez Ciebie.");
					$("#msg_voice_err").show();
				}
			}
		});
  }
  
  function ajaxAddComment() {
	  
		$.ajax( {
					url : "/gallery/addComment",
					type : "POST",
					dataType : "json",
					data : $("#comment").serialize(),
					
					beforeSend : function() {
						$("#completed_route_preloader").show();
					},
					complete : function(response) {
				 		eval("var response="+response.responseText);
				      
						if (response.msg == "add_ok") {
							
							$("#msg_comm").html(
									"Komentarz został dodany.");
						    $("#msg_err").hide();
							$("#msg_comm").show();
							$("#comment_form").hide();
							
						}
						else if (response.msg == "add_empty") {
						
							$("#msg_err").html(
							               "Podaj treść komentarza.");
							 $("#msg_comm").hide();
					         $("#msg_err").show();
					     }
					}
				});
 }
  
  function videoPresentation(key){

	 	$('#videoPresentation').dialog({ 
	 		title: "Filmy - grupa iTur.pl",	 		
			modal: true, 
	        draggable: false,
	        width: 500,
	        resizable: false,
	        autoOpen: false	       	      
		});
	 	$('#videoPresentation').html('<div style="display: block";><embed src="http:\/\/www.youtube.com\/v\/'+key+'" type="application\/x-shockwave-flash" allowfullscreen="true" height="344" width="425"><\/div>');
		$('#videoPresentation').dialog("open");

    return false;
  }
  
  function showActiveBookmark(classname){
	    $("ul#bookmarks .active").removeClass("active");
	    $("#li_"+classname).addClass("active");
	    $('#galleryAlbumContent').hide();
	    $("#albumContent").hide();
	    $("#placesWzContent").hide();
	    $("#nopContent").hide();
	    $("#about_me").hide();
	    $('#contactContent').hide();
	    $("#friendsContent").hide();
	    $("#visitedContent").hide();
	    $("#planningContent").hide();
	    $("#addedContent").hide();
	    $("#"+classname).show();  
	        
	    return false;
  }
  function imagesInAlbum(album_id, user_id, object_id){
//	    $("#galleryRouteContent").hide();
//	    $('#galleryAlbumContent').hide();
	   
	    $("#albumContent").hide();
	    $("#placesWzContent").hide();
	    $("#nopContent").hide();
	    $("#about_me").hide();
	    $("#friendsContent").hide();

 	   if (album_id > 0){
     	 	$('#galleryAlbumContent').html('<div id="user_album" style="display: none";>   <img id="u_album_preloader" src="/images/preloader.gif" /> Trwa ładowanie galerii zdjęć użytkownika ...<\/div>');
      	 	$('#galleryAlbumContent').load('/gallery/userAlbumImages/?user_id='+user_id+'&album_id='+album_id , function() {
      	 		$("#u_album_preloader").hide();
      	 	});
      	
      	    $('#user_album').show();  
      	    $('#galleryAlbumContent').show();
   	 
 	   }else if (object_id > 0){
// 		   $('#galleryRouteContent').load('/album-atrakcji/'+user_id+'/'+object_id+'/');
// 		   $('#galleryRouteContent').show();  
 	   }
 	   else {
//   		  $('#galleryRouteContent').html('<div id="albumRoutes" style="display: none";><\/div>');
//   		  $('#albumRoutes').load('/album-tras/'+user_id+'/'+album_id+'/');
//   		  $('#albumRoutes').show();	  
// 		  $('#galleryRouteContent').show();  
 	   }  
 	  
 	   return false;  
    }
 
  
  function log(s){
	    if (typeof(console) == 'object' && typeof(console.log) == "function") {
	        console.log(s);
	    }
  }