var Dialog = new Dialog();

function ui_dialog(title, text){
	$("#message").attr('title', title);
	$("#message p").html(text);
	
            $("#message").dialog({
                minHeight: 60, 
                draggable: false,
                resizable: false, 
                show: 'slide', 
     			buttons: {
				    Zamknij: function(){
					   $(this).dialog("close");
                    }
				}
                });	 
}

function load_page(page){
      $.ajax({
        type: 'GET',
        url: strBaseUrl + 'ajax/load_page/'+page,
        beforeSend: function(){ Dialog.loading(); },
        success: function( html ){ 
            Dialog.open( '', html ); 
        }
      });
      
      return false;
}
    
$( document ).ready(function(){

$("#poll li").mouseover(function(){
	if($("#poll").hasClass('voted')) return false;

	$(this).addClass('hovered');
}).mouseout(function(){
	if($("#poll").hasClass('voted')) return false;

	$(this).removeClass('hovered');
});

var voted = 0;
$("#poll li").click(function(){
if($("#poll").hasClass('voted') || voted) return false;

poll_id = $(this).parent().attr('id').replace(/poll_/, "");
answer_id = $(this).attr('id').replace(/answer_/, "");
	      
    		$.ajax({
    			type: "GET",
    			url: strBaseUrl+"ajax/poll_vote/"+poll_id+"/"+answer_id,
    			dataType: "text",
    			success: function(data){
    				if(data == 0){
					ui_dialog("Błąd", "Wystąpił problem z zapisaniem głosu. Prawdopodobnie już głosowano z danego IP w tej ankiecie."); 				    
				} else {
					ui_dialog("Oddano głos", "Twój głos został poprawnie zapisany. Dziękujemy"); 
					new_poll = $(data).html();
					$("#poll").fadeOut('normal', function(){
						$("#poll").html(new_poll);
						$("#poll").fadeIn();
						voted = 1;
					});
					  
				}
			}
    		}); 			
});    
    
    /*
    function advert_stat(advert_id){
        alert(1);
    }
    
    
    var texas_ranger = 0; 
    $(".advert_stat").click(function(){
        return false;
        alert(' ');
        if(texas_ranger == 0){
            var advert_id = $(this).attr('id');
    		$.ajax({
    			type: "GET",
    			url: strBaseUrl+"ajax/advert_stat/"+advert_id,
    			dataType: "text"
    		}); 
        }    
            texas_ranger = 1;       
    });*/

    var short_description_limit = 300;
/*    $(".chars_limit").each(function(){
        textarea_chars = 0;
            
    });
    
    $(".chars_limit").onkeyup(function(){
        value = $(this).val().length;
        if(value > short_description_limit){
            value = value.substring(0, short_description_limit);
        }
        $(this).val(short_description_limit);    
    });*/


 	$('a.add_to_note').click(function(){	
    		var offer_id = $(this).attr('title');
            var index = $("a.add_to_note").index(this);
            $(this).fadeOut('normal', function(){
                $("a.remove_from_note:eq("+index+")").fadeIn();    
            });

    		$.ajax({
    			type: "GET",
    			url: strBaseUrl+"ajax/add_to_note/"+offer_id,
    			dataType: "script"
    		});
            count = parseInt($(".saved_offer_count").html()); 
            $(".saved_offer_count").fadeOut('normal', function(){   
                $(".saved_offer_count").html(count+1);
                $(".saved_offer_count").fadeIn('normal');
            });  
            
            if($(this).hasClass('offer_page')){
                $("a.add_to_note:visible").fadeOut('normal', function(){
                    $("a.remove_from_note").fadeIn();    
                });                
            }
      });
      
 	  $('a.remove_from_note').click(function(){	
    		var offer_id = $(this).attr('title');
            var index = $("a.remove_from_note").index(this);
            if($(this).hasClass('current_schowek')){
                $(this).parent().parent().fadeOut('normal');
            } else {
                $(this).fadeOut('normal', function(){
                    $("a.add_to_note:eq("+index+")").fadeIn();       
                });
            }
    		
            $.ajax({
    			type: "GET",
    			url: strBaseUrl+"ajax/remove_from_note/"+offer_id,
    			dataType: "script"
    		});
            count = parseInt($(".saved_offer_count").html());   
            $(".saved_offer_count").fadeOut('normal', function(){   
                $(".saved_offer_count").html(count-1);
                $(".saved_offer_count").fadeIn('normal');
            }); 
            
            if($(this).hasClass('offer_page')){
                $("a.remove_from_note:visible").fadeOut('normal', function(){
                    $("a.add_to_note").fadeIn();    
                });                
            }            
      }); 

                    $(".select_hover").mouseover(function(){
                        $(this).addClass('select_big_hovered');   
                    });
                    $(".select_hover").mouseout(function(){
                        $(this).removeClass('select_big_hovered'); 
                    });

		
    				var i = 0;
                    
    				var close_helper = 0;
                    
                    $("body").live('click', function(e){
                        //console.log("odpalam body: "+close_helper);
                        if(($("ol.select_objects_menu").is(":visible")) && close_helper == 1){
                            //console.log(1);
                            $("ol.select_objects_menu").hide(); 
                            //$("ol.select_cities_menu").hide(); 
                                                    
                        }  close_helper = 0;  
                                
                    });
                                				
    				$("ol.select_objects_menu li").live('mouseover', function(){
    					$(this).addClass('active');
    				}).live('mouseout', function(){
    					$(this).removeClass('active');
    				});	
    				$("ol.select_cities_menu li").live('mouseover', function(){
    					$(this).addClass('active');
    				}).live('mouseout', function(){
    					$(this).removeClass('active');
    				});	
                        				
    				$("div.select_obiekty").click( function(){	    
                                    close_helper = 0;
                                       $("ol.select_objects_menu").show();
                                       //console.log("odpalam menu: "+close_helper);
                                        setTimeout(function(){ close_helper = 1; }, 100);
                                    //}  				
    					//i = 0;
    				});	
            
                                                    								
    				$("ol.select_objects_menu li").live('click', function(){
    				      selected_index = $(this).index();
    				      selected_name = $(this).html();
    				      $("select[name=category] option").removeAttr('selected');
                                      
                                      $("select[name=category] option").each(function(){
                                        if($(this).html()==selected_name){
                                            slug_title = $(this).attr('selected', 'selected').val();
                                        }
                                      });
    				      //slug_title = $("select[name=category] option:eq("+selected_index+")").attr('selected', 'selected').val();
    				      $("ol.select_objects_menu").hide();
    				      //i = 1;
    				      $("div.select_obiekty span").html(selected_name);
             
              
    				});	
     
});

