function updateTips( t ) {
    $( ".validateTips" ).text( t ).addClass( "ui-state-highlight" );
    setTimeout(function() {
        $( ".validateTips" ).removeClass( "ui-state-highlight", 1500 );
    }, 500 );
}

function checkLength( o, n, min, max ) {
    if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        updateTips( "D\u0142ugość " + n + " musi si\u0119 zawierać pomiędzy " + min + " and " + max + "." );
        return false;
    } else {
        return true;
    }
}

function checkRegexp( o, regexp, n ) {
    if ( !( regexp.test( o.val() ) ) ) {
        o.addClass( "ui-state-error" );
        updateTips( n );
        return false;
    } else {
        return true;
    }
}

function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        //i = jQuery(this).index() + 1;
        i = jQuery(this).index()/2+1;
        carousel.scroll(jQuery.jcarousel.intval(i));
        return false;
    });
    mycarousel_stopCallback(carousel);
}

function mycarousel_stopCallback(carousel)
{
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}

function custom_confirm(title, prompt, action) {
    $('#dialog .prompt').text(prompt);
    $("#dialog").dialog({
        resizable: false,
        height:140,
        modal: true,
        autoOpen : true,
        title: title,
        buttons : {
            'Tak' : function() {
                $(this).dialog("close");
                action();
            },
            'Nie': function() {
                $(this).dialog("close");
            }
        }
    });
}

$(function() {
    $('#pytanie').load('/zapytanie.php');
   
    $('input[name="checkall"]').click(function() {
        $( this ).is( ':checked' ) ? 
        $(".list :checkbox").attr('checked', 'checked') : 
        $(".list :checkbox").removeAttr('checked');
    });
    


    $("a.fb").fancybox({
        'opacity' : true,
        'autoScale' : false,
        'transitionIn' : 'elastic',
        'transitionOut' : 'elastic',
        'titlePosition' : 'over'
    });
    
    
    $("a.pup").fancybox({
        'width': '640',
        'height': '480',
        'autoScale': true,
        'transitionIn': 'fade',
        'transitionOut': 'fade',
        'type': 'iframe',
        'href': $(this).attr('href')

    });
    
    // rozwijane menu
    qm_create(0,false,0,2000,false,true,true,true,false);
    
    // hover
    $('#upload, input[type=submit], input[type=button], button').hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    // przycisk wroc
    $('#btnback').click(function() {
        window.history.back();
    });

    $("#roll").hide();

    $('.toggle').click(function() {
        $('#roll').toggle('slow', function() {});
    });

    $("#toggle").click(function() {
        $("#brandlist").toggle(800);
    });

    // potwierdzenia dla akcji ogloszen wl/wy/del/ref
    $("a.ico_del, a.ico_on, a.ico_off, a.ico_ref, a.confirm").click(function(e) {
        e.preventDefault();
        var href = this.href;
        var title = this.title;
        var promt = this.text;
        custom_confirm(title, promt, function() {
            location.href = href;
        });
    });
    
    //    if ($("#specmp").length>0) {
    //        $("#specmp").jcarousel({
    //            auto: 8,
    //            wrap: 'circular',
    //            scroll: 1,
    //            initCallback: mycarousel_initCallback,
    //            buttonNextHTML: null,
    //            buttonPrevHTML: null
    //        });
    //    }
    //    if ($(".slideshow").length>0) {
    //        $(".slideshow").jcarousel({
    //            auto: 8,
    //            wrap: 'circular',
    //            scroll: 1,
    //            buttonNextHTML: null,
    //            buttonPrevHTML: null,
    //            initCallback: mycarousel_stopCallback
    //        });
    //        
    //    }
    //    if ($("#partners").length>0) {
    //        $('#partners').jcarousel({
    //            scroll : 6
    //        });
    //    }
       
    $("table.zebra tr:even").addClass("even");
    $("table.zebra tr:odd").addClass("odd");


    $(".jmp").change(function () {
        $(".jmp option:selected").each(function () {
            window.location.assign($(this).val());
        });
    });

    $('#contact').each(
        function() {
            $( "#dialog:ui-dialog" ).dialog( "destroy" );
            $("#dialog").dialog("destroy");
            $(".validateTips").dialog("hide");

            var $link = $(this);
            var u = $link.attr('rel');
            var $dialog = $('<div></div>').load($link.attr('href')).dialog(
            {
                bgiframe : true,
                resizable : false,
                modal : true,
                autoOpen : false,
                title : $link.attr('title'),
                width : 540,
                height : 400,
                buttons : {
                    'Wyślij' : function() {
                        var name = $('#name');
                        var email = $('#email');
                        var usr = $('#user');
                        var url = $('#url');
                        var query = $('#query');
                        var allFields = $( [] ).add( name ).add( email ).add( query ),
                        tips = $( ".validateTips" );
						
                        var bValid = true;
                        allFields.removeClass( "ui-state-error" );
    
                        bValid = bValid && checkLength( name, "Od", 3, 16 );
                        bValid = bValid && checkLength( email, "e-mail", 6, 80 );
                        bValid = bValid && checkLength( query, "zapytanie", 5, 250 );
    
                        bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Nazwa użytkownika może zawierać znaki a-z, 0-9 i zaczynać się od litery." );
                        bValid = bValid && checkRegexp( email, /^(.+?)@(([a-z0-9\.-]+?)\.[a-z]{2,5})$/i, "Podaj prawdziwy adres e-mail np. kontakt@gieldarow.pl" );

                        if ( bValid ) {
                            $.ajax({
                                type : "POST",
                                url : "/sendmsg.php",
                                data : "name="+ name.val()+ "&email="+ email.val()+"&query="+query.val()+"&usr="+usr.val()+"&url="+url.val(),
                                success : function() {
                                    $("#dialog").dialog({
                                        bgiframe : true,
                                        modal : true,
                                        title : $link.attr('title'), 
                                        resizable : false,
                                        buttons : {
                                            Ok : function() {
                                                $(this).dialog('close');
                                            }
                                        }
                                    });
                                    $dialog.dialog('close');
                                }
                            });
                        }
                        return false;
                    },
                    'Anuluj' : function() {
                        $dialog.dialog('close');
                    }
                },
                close : function() {
                    $('#name').val('');
                    $('#email').val('');
                    $('#query').val('');
                }
            });
            $link.click(function() {
                $dialog.dialog('open');
                $('#usr').val(u);
                return false;
            });
        });
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    
});
