var comments_bool=false;
var comments_list= new Array();
$(document).ready(function () {
    var nextAddr=1;

    if (document.getElementById('new_offer_current_promos')!=null){
        $('.new_offer_warning').hide();
//        $('#new_offer_current_promos').load('/ajax_offers_company/'+$('.new_uo_input').val());
    }

    $('.newo_radio').click(
    function (){
        if ($(this).val()==1){
            $('.new_offer_warning').hide();
            $('#new_offer_cities').hide();
            $('#new_offer_addr').hide();
        } else {
            $('.new_offer_warning').show();
        }
    });

    $('#newo_want_cities').click(function(){
        $('#new_offer_cities').show();
        $('#new_offer_addr').hide();
        $('.new_uo_addr_selected option').remove().appendTo('.new_uo_addr_all');
        $('.new_uo_addr_all option').each(function(){
            $(this).removeAttr('Selected');
            if ($(this).html()==''){
                $(this).remove();
            }
        });
        return false;
    })

    $('#newo_want_addr').click(function(){
        $('#new_offer_cities').hide();
        $('#new_offer_addr').show();
        $('.new_uo_cities_selected option').remove().appendTo('.new_uo_cities_all');
        $('.new_uo_cities_all option').each(function(){
            $(this).removeAttr('Selected');
            if ($(this).html()==''){
                $(this).remove();
            };
        });
        return false;
    })


    $('#newo_city_to_list').click(function() {
        if ($('.new_uo_cities_selected').html()=='<OPTION></OPTION>'){
            $('.new_uo_cities_selected').html('');
        };
        $('.new_uo_cities_all option:selected').remove().appendTo('.new_uo_cities_selected');
        return false;
    });
    $('.new_uo_cities_all').dblclick(function() {
        if ($('.new_uo_cities_selected').html()=='<OPTION></OPTION>'){
            $('.new_uo_cities_selected').html('');
        };
        $('.new_uo_cities_all option:selected').remove().appendTo('.new_uo_cities_selected');
        return false;
    });


    $('#newo_city_del').click(function() {
        $('.new_uo_cities_selected option:selected').remove().appendTo('.new_uo_cities_all');
        return false;
    });
    $('.new_uo_cities_selected').dblclick(function() {
        $('.new_uo_cities_selected option:selected').remove().appendTo('.new_uo_cities_all');
        return false;
    });

    $('#newo_city_del_all').click(function() {
        $('.new_uo_cities_selected option').remove().appendTo('.new_uo_cities_all');
        return false;
    });

    $('#newo_addr_to_list').click(function() {
        if ($('.new_uo_addr_selected').html()=='<OPTION></OPTION>'){
            $('.new_uo_addr_selected').html('');
        };
        $('.new_uo_addr_all option:selected').remove().appendTo('.new_uo_addr_selected');
        return false;
    });
    $('.new_uo_addr_all').dblclick(function() {
        if ($('.new_uo_addr_selected').html()=='<OPTION></OPTION>'){
            $('.new_uo_addr_selected').html('');
        };
        $('.new_uo_addr_all option:selected').remove().appendTo('.new_uo_addr_selected');
        return false;
    });


    $('#newo_addr_del').click(function() {
        $('.new_uo_addr_selected option:selected').remove().appendTo('.new_uo_addr_all');
        return false;
    });
    $('.new_uo_addr_selected').dblclick(function() {
        $('.new_uo_addr_selected option:selected').remove().appendTo('.new_uo_addr_all');
        return false;
    });

    $('#newo_addr_del_all').click(function() {
        $('.new_uo_addr_selected option').remove().appendTo('.new_uo_addr_all');
        return false;
    });





    $('.new_uo_input').change(function(){
//       alert('xxx');
       if ($(this).val()!=0){
           $('.newo_user_cbx').show();
           $('.new_offer_warning').show();
           $('#new_offer_current_promos').load('/ajax_offers_company/'+$(this).val());
           $('#new_offer_current_promos').show();
        } else {
           $('.newo_user_cbx').hide();
           $('.new_offer_warning').hide();
           $('#new_offer_current_promos').hide();
        }
    });

    $('#newo_submit_s2').click(function(){
        //alert('xx');
        $('.new_uo_cities_selected option').attr('selected','selected');
            //$('.new_uo_cities_selected;
    });



    $('#add_address').click(function(){
        $.get('/ajax_addr_to_ins/'+(nextAddr++),{},function(data, stat){
            $('#new_addresses').append(data);
        });
        return false;
    });

    $('.voiv_header').click(function(){              
       $('.voiv_c_list').hide(200);
       $(this).parent().children().show(200);
       return false;
    });

    $('.coords').click(function(e){
        var v_id= $(this).attr("id");
        v_id=v_id.substring(2);
        var height = $('#map_img').height();
        var width = $('#map_img').width();
        var boxheight = $('#tm'+v_id).height();
        var boxwidth = $('#tm'+v_id).width();
        var bodywidth = $('body').width();
        var contwidth = $('#container').width();
        var margin = bodywidth-contwidth;
        margin < 0 ? margin = 0 : margin/=2;
        leftVal=e.pageX-margin-(boxwidth/2)+20+"px";
        topVal=e.pageY-(height/2)-boxheight-130+"px";
        $('.tooltip_map').hide();
        $('#tm'+v_id).css({left:leftVal,top:topVal}).slideDown('fast');
        return false;
    });

//
//
//    $('.coords').click(function(e){
//        var v_id= $(this).attr("id");
//        v_id=v_id.substring(2);
//        var map=document.getElementById("map_img");
//        var oX=oY=0;
//        oX=map.offsetLeft;
//        oY=map.offsetTop;
//        while (map=map.offsetParent){
//            oX+=map.offsetLeft;
//            oY+=map.offsetTop;
//        }
//        oX=e.pageX-oX;
//        oY=e.pageY-oY;
//        $('.tooltip_map').hide();
//        $('#tm'+v_id).css('bottom',(976-oY));
//        $('#tm'+v_id).css('left',(oX-49));
//        $('#tm'+v_id).slideDown('fast');
//        return false;
//    });
    $('.tooltip_map').hover(
			function() {},
			function() { $(this).slideUp('fast'); });


    $('.dropdown_menu').hover(
			function() {
                var menu_id= $(this).attr("id");
                $('#'+menu_id+' .dropdown_menu_items').slideDown('fast');
            },
			function() {
                var menu_id= $(this).attr("id");
                $('#'+menu_id+' .dropdown_menu_items').slideUp('fast');
            });


    $('.cities_menu').hover(
			function() { $('.cities_list').slideDown('fast'); },
			function() { $('.cities_list').slideUp('fast'); });

    $('.sc_links').hover(
        function (){
            $('.sc_links').css('background-image','none');
            $(this).css('background-image','url(/styles/2/img/panel_hover.png)');
            $(this).css('color','#388ab9');
            //$(this).css('background-position','bottom center');
            $(this).css('background-repeat','repeat-x');
            var cat_id= $(this).attr("id");
            cat_id=cat_id.substring(7);
            $('#subcat_menu_div').html($('#subcat'+cat_id).html());
            return false;
        },function() {
            $(this).css('color','white');
        }
        );

    $('textarea[name=offer_text]').keyup(function(){
        var words=$('textarea[name=offer_text]').val().split(' ');
        if (words.length>100){
            alert("Przekroczono ilość dopuszczalnych wyrazów!");
        }
        var words_left=100-words.length;
        if (words_left<0) words_left=0;
        $('#offer_form_words').html('Pozostało słów: <b>'+words_left+'</b>')
    })

    $('input[name=offer_tags]').keyup(function(){
        $('#tags_list').html('');
        var words=$('input[name=offer_tags]').val().split(';');
        var last_word=words[words.length-1];
        var pattern=new RegExp('^'+last_word,'i');
        if (last_word!=''){
            $.each(tags,function(id,val){
                //alert(pattern.test(last_word));
                if (pattern.test(val) && val!=null){
                    $('#tags_list').append('<li><a href="" class="tag_tip">'+val+'</a></li>');

                }
            });
        } else {
            $('#tags_list').html('<li>Tu ukażą się podpowiedzi do tagów</li>');
        }
    });

    $('#tags_list li a').livequery('click', function(){
        var words=$('input[name=offer_tags]').val().split(';');
        words[words.length-1]=$(this).html()+';';
        $('input[name=offer_tags]').val(words.join(';'));
        $('input[name=offer_tags]').focus();
        //$('input[name=offer_tags]').val($('input[name=offer_tags]').val()+$(this).val());
        //alert($(this).html());
        return false;
        //$('input[name=offer_tags]').val($('input[name=offer_tags]').val()+$(this).val());
    });


    $('input[name=offer_title]').keyup(function(){
        var words=$('input[name=offer_title]').val().split(' ');
        if (words.length>10){
            alert("Przekroczono ilość dopuszczalnych wyrazów!");
        }
        var words_left=10-words.length;
        if (words_left<0) words_left=0;
        $('#offer_title_words').html('Pozostało słów: <b>'+words_left+'</b>')
    })





    $('.dynamic_box_category').click(
        function (){
            var c_id= $(this).attr("id");
            c_id=c_id.substring(1);
            $('#mainpage_offers_content').load('/ajax_offers/'+c_id+'/'+usercity);
            return false;
        })

    $('#a_new_company').click(function(){
        $('#new_offer_content').slideUp('fast');
        $('#new_company_content').show();
        return false;
    });

    $('#most_popular_offers_toggle').click(
    function (){
        if ($(this).text()=='Schowaj'){
            $(this).text('Pokaż');
        } else {
            $(this).text('Schowaj');
        }
        $('#most_popular_offers_content').toggle(500);
        return false;
    })

    $('#newest_offers_toggle').click(
    function (){
        if ($(this).text()=='Schowaj'){
            $(this).text('Pokaż');
        } else {
            $(this).text('Schowaj');   
        }
        $('#newest_offers_content').toggle(500);
        return false;
    })

    $('#similar_offers_toggle').click(
    function (){
        if ($(this).text()=='Schowaj'){
            $(this).text('Pokaż');
        } else {
            $(this).text('Schowaj');
        }
        $('#similar_offers_content').toggle(500);
        return false;
    })

     $('#suggested_offers_toggle').click(
    function (){
        if ($(this).text()=='Schowaj'){
            $(this).text('Pokaż');
        } else {
            $(this).text('Schowaj');
        }
        $('#suggested_offers_content').toggle(500);
        return false;
    })

    $('#map_hide').click(
    function (){
        $('#address_map_box').hide(500);
        return false;
    })

    $('.specific_comment_toggle').click(
        function (){
            var c_id= $(this).attr("id");
            c_id=c_id.substring(1);
            if (comments_list[c_id]){
                //$('#t'+c_id).html('<img src="/styles/2/img/c_show.png" alt="" />');
                $('#hidden_post'+c_id).hide();
                $('#header_p'+c_id).show();
                comments_list[c_id]=false;
            } else {
                //$('#t'+c_id).html('<img src="/styles/2/img/c_hide.png" alt="" />');
                $('#header_p'+c_id).hide();
                $('#hidden_post'+c_id).show();
                comments_list[c_id]=true;
            }
            return false;
        });
    $('input[name=company_type]').click(function(){
        if ($(this).val()==0){
            $('#new_addresses').show();
            $('#add_address').show();
        } else {
            $('#new_addresses').hide();
            $('#add_address').hide();
        }

    });

    $('#hidden_comments_toggle').click(
    function (){
        var time=0;
        if (comments_bool){
            $('.comment_hidden').hide(time);
            for (key in comments_list){
                comments_list[key]=false;
                $('#t'+key).html('<img src="/styles/2/img/c_show.png" alt="" />');
            }
            comments_bool=false;
            $(this).text('Pokaż wszystkie ukryte komentarze');
        } else {
            $('.comment_hidden').show(time);
            for (key in comments_list){
                comments_list[key]=true;
                $('#t'+key).html('<img src="/styles/2/img/c_hide.png" alt="" />');
            }
            comments_bool=true;
            $(this).text('Schowaj wszystkie ukryte komentarze');
        }

        return false;
    })
    $('.addr_links').click(
    function (){
        var city_id= $(this).attr("id");
        city_id=city_id.substring(4);
        //var tmp=$('#addr'+city_id).html();
        $('.addr_hidden').hide();
        $('#addr'+city_id).slideDown();
//        //alert(city_id);
//        $('#offer_address_container_addr').html(tmp);
//        
        return false;
    })


    $('#f_insert_link').click(function(){
        //var url = prompt("Podaj URL", "Podaj URL");
        $('#url_p').show();
        $('#f_insert_link').hide();
        return false;
    });

    $('#post_url_add').click(function(){
        var url = $('#post_url').val();
//        $('#post_url').show();
//        $('#f_insert_link').hide();
        $('.reply_textarea').val($('.reply_textarea').val()+'[url]'+url+'[/url]');
        $('#post_url').val('http://');
        $('#url_p').hide();
        $('#f_insert_link').show();
        return false;
    });

    $('#f_insert_bold').click(function(){
        $('.reply_textarea').val($('.reply_textarea').val()+'[b][/b]');
        return false;
    });

    $('#f_insert_ital').click(function(){
        $('.reply_textarea').val($('.reply_textarea').val()+'[i][/i]');
        return false;
    });

    $('.city_maincats_links').click(function(){
        $('.city_subcats').slideUp('fast');
        var cat_id= $(this).attr("id");
        cat_id=cat_id.substring(7);
        $('#subcats'+cat_id).slideDown('fast');
        return false;
    });



    $('.addr_links').click(
    function (){
        var city_id= $(this).attr("id");
        city_id=city_id.substring(4);
        //var tmp=$('#addr'+city_id).html();
        $('.addr_hidden').hide();
        $('#addr'+city_id).slideDown();
//        //alert(city_id);
//        $('#offer_address_container_addr').html(tmp);
//
        return false;
    })


    $('.map_toggle').click(
    function (){
        var a_id= $(this).attr("id");
        a_id=a_id.substring(1);
        

        //$('#address_map').html(addr[a_id]);
        if(GBrowserIsCompatible())
        {
            //alert(a_id);
            //var map = new GMap2(document.getElementById("address_map"));
            var addr_geo = new GClientGeocoder();
            //var mapsize = new GSize(383,383);
            var map = new GMap2(document.getElementById("address_map"),{size:new GSize(383,383)});
            addr_geo.getLatLng(addr[a_id],function(a_pt)
            {
                //alert(a_id);
                if (!a_pt)
                {
                    alert("Niestety, adres nie został odnaleziony!");
                }
                else
                {
                    //alert(addr[a_id]);
                 //   $('#address_map_box').show(500);
                    $('#address_map_box').show(500, function(){
                        
                        map.clearOverlays();
                        map.setCenter(a_pt,16);
                        map.addControl(new GSmallMapControl());
                        var icon = new GIcon();
                        icon.image='http://maps.google.com/mapfiles/kml/pal5/icon14.png';
                        icon.shadow='http://maps.google.com/mapfiles/kml/pal5/icon14s.png';
                        icon.iconSize=new GSize(32,32);
                        icon.iconAnchor=new GPoint(16,16);
                        icon.infoWindowAnchor=new GPoint(16,16);
                        var addr_marker = new GMarker(a_pt,{icon: icon, title: addr[a_id]});
                        map.addOverlay(addr_marker);
                        addr_marker.openInfoWindowHtml('<strong>'+company+'</strong><br />'+addr[a_id]);
                        var l=location;
                        l.hash='map_box_list'
                        window.location=l;
                    });

                    
                    
                }
            });
        }
        return false;
    })
});