var logindialog = {
    bgiframe: true,
    autoOpen: false,
    height: 200,
    width: 300,
    modal: true,
    buttons: {
        'Giriş': function (){
            $('#login_form').submit();
        }
    }
}

$(function(){
        $(document).pngFix();
        $("ul.sf-menu").superfish({
            speed: 'fast',
            animation: {height:'show'},   // slide-down effect without fade-in 
            delay:     1200,              // 1.2 second delay on mouseout
            dropShadows: false,
            autoArrows: false
        });
        $("ul li a").mouseover( function(e){
            $(this).parent().addClass('hover');
        });
        $("ul li a").mouseout( function(e){
            $(this).parent().removeClass('hover');
        });
        $("#login_modal").dialog(logindialog);
        var isCtrl;
        $(document).keyup(function (e) { 
                if(e.which == 18) isCtrl=false;
        }).keydown(function (e) {
                if(e.which == 18) isCtrl=true; 
                if(e.which == 76 && isCtrl) {
                    $("#login_modal").dialog('open');
                }
        });
        if ($('#user_message').length > 0){
            alert($('#user_message').text());
        }
        locationSelector();
        $('.product_search .product').change(function (e){
                $(this).parents('form').attr("action", $(this).val());
                $('.product_search .selective').css({'visibility':'hidden'});
                $('.product_search .selective').children('input, select').attr('disabled','disabled');
                var ty = $(this).val().substr(0,$(this).val().length - 4);
                $('.product_search .' + ty).css({'visibility':'visible'});
                $('.product_search .' + ty).children('input, select').removeAttr('disabled');
        });
        $('#otel_detail .headline .hfoto').hide();
        $('#otel_detail .headline .hfoto:first').show();
        $('#otel_detail .carousel .cfoto').click(function (e) {
                var hid = $(this).attr('id').replace(/_c_/,'_h_');
                $('#otel_detail .headline .hfoto').hide();
                $('#'+hid).show();
        });
        
        var moving = false;
        $('.carousel .next').click(function(e){
            if (!moving) {
                var pos = $('.carousel_canvas').css('top');
                pos = parseInt(pos.substr(0,pos.indexOf('px')));
                var hei = $('.carousel_canvas').height();
                if (pos - 273 > -1*hei) {
                    moving = true;
                    
                    $('.carousel_canvas').animate({top: (pos-133)+'px'},500,null,function(e){moving=false;});
                }
            }
            return false;
        });
        $('.carousel .prev').click(function(e){
            if (!moving) {
                var pos = $('.carousel_canvas').css('top');
                pos = parseInt(pos.substr(0,pos.indexOf('px')));
                if (pos < 0) {
                    moving = true;
                    
                    $('.carousel_canvas').animate({top: (pos+133)+'px'},500,null,function(e){moving=false;});
                }
            }
            return false;
        });

});

function validateMail(mail) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(mail) == false) {
      alert('Lütfen Geçerli Bir Mail Adresi Giriniz');
      return false;
   }
   return true;
}

function locationSelector (par) {
    if (!par) {
        par = document;
    }
    $(par).find('input.location').each(function(){
            // Create country/city selector
            var ct = $('<select id="'+$(this).attr('id')+'_ct" class="location_ct"><option value="">'+_ulke+'</option></select>');
            $(this).after(ct);
            var ci = $('<select id="'+$(this).attr('id')+'_ci" class="location_ci"><option value="">'+_sehir+'</option></select>')
            ct.after(ci);
            
            if ($(this).val() != "") {
                // Find parents of given location so that we can select it immediately
                $.getJSON('location.php?find='+$(this).val(), function(rt){
                        $(ct).data("preset",rt[0].id);
                        if (rt[1]){
                            $(ci).data("preset",rt[1].id);
                        }
                });
            }
    });
    $(par).find('.location_ct').change(function(e){
            // Populate city on country change
            // And set location as selected country
            var mid = "#"+$(this).attr('id');
            var lid = mid.substr(0,mid.length-3);
            var ciid = lid+'_ci';
            $(ciid).empty();
            $(ciid).append($('<option value="">'+sehir+'</option>'));
            $.getJSON('location.php?parent='+$(this).val(), function(rt){
                    $.each(rt, function(){
                            $(ciid).append($('<option value="'+this.id+'">'+this.name+'</option>'))
                    });
                    if ($(ciid).data("preset")) {
                        $(ciid).val($(ciid).data("preset"));
                        $(ciid).change();
                    }
            });
            $(lid).val($(this).val());
    });
    $(par).find('.location_ci').change(function(e){
            // Set location as selected city
            var mid = '#'+$(this).attr('id');
            var lid = mid.substr(0, mid.length - 3);
            if ($(this).val()) {
                $(lid).val($(this).val());
            } else { 
                var ctid = lid+"_ct";
                $(lid).val($(ctid).val());
            }
    });
    if ($(par).find('.location_ct').length > 0) {
        $.getJSON('location.php', function(rt) {
                // Popuate country selectors
                $.each(rt, function() {
                        $('.location_ct').append($('<option value="'+this.id+'">'+this.name+'</option>'))
                }); 
                $('.location_ct').each(function(e) {
                        if ($(this).data("preset")) {
                            $(this).val($(this).data("preset"));
                            $(this).change();
                        }
                });
        });
    }
}
