/*$(function() {
    $('#j-tipsy').tipsy({
        fade: true, 
        html : true
    });    
});*/



	

$(document).ready(function() {
    $("a.fancybox").fancybox({
        'overlayColor': '#C7E428',
        'overlayOpacity': 0.6,
        'hideOnContentClick': false
    });
    $("a.fancyboxfb").fancybox({
        'type'				: 'iframe',
        'width'				: 615, 
        'height'			: 390,		
        'overlayColor'		: '#C7E428',
        'overlayOpacity'	: 0.6,
        'hideOnContentClick': false,
        'hideOnOverlayClick': false

    });
    $("a.fancybox-invite").fancybox({
        'type'				: 'iframe',
        'width'				: 640, 
        'height'			: 440,		
        'overlayColor'		: '#C7E428',
        'overlayOpacity'	: 0.6,
        'hideOnContentClick': false,
        'hideOnOverlayClick': false

    });

});	



(function($) {
    $(document).ready(function(){
        $('.inner-city-select').ptMenu();
        $('.my-menu').ptMenu();
    });
})(jQuery);

$(function() {
    $('#password-top').focus(function(){
        $('.password-remind').delay(700).fadeIn();
    });
});

$(function() {
    $('#password-top').blur(function(){
        $('.password-remind').fadeOut();
    });
});

/*$(window).load(function() {
    $('#slider').nivoSlider({
        effect:'fade',
        animSpeed:200,
        pauseTime:3500,
        startSlide:0, 
        directionNav:true, 
        directionNavHide:true, 
        controlNav:false, 
        controlNavThumbs:false, 
        keyboardNav:true, 
        pauseOnHover:true, 
        manualAdvance:false, 
        captionOpacity:0.8, 
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){} 
    });
});*/


(function($) { 
    $.fn.animBg = function(command,options) { 
        if ( $('.loading-facebook').is(':visible') ) { 
            if(command && typeof command ==='string'){
                clearInterval($(this).data('anim'));
                return this;
            }
            var $this = $(this),
            o = {
                'interval':100
            },
            options = (typeof command === 'object' && typeof options === 'undefined')?command:options,
            o = jQuery.extend({}, o, options), anim;
            if(!o.step && !o.steps){
                return this;
            }
            anim = setInterval(function() { 
                var bgpos = parseInt( $this.css('background-position') );
                $this.css('background-position',(-(o.step*o.steps)>=(bgpos-o.step)?0:bgpos-o.step)+'px 0');
            }, o.interval);
            $this.data('anim',anim);
            return this; 
        }
    }
})(jQuery); 




jQuery(function( $ ){
    var placeholder = $( "#placeholder" );
    
    if (placeholder.length == 0)
        return;
    
    var message = $( "#sliding" );
    var message2 = $( "#sliding2" );
    var view = $( window );
    var partner = $('#partner-deals-start');
    var placeholder2 = $('#placeholder2');
    
    if (placeholder2.length == 0)
        return;
    
    var partnerTop = partner.offset().top;
    var placeholderTop = placeholder.offset().top;
    
    var filterVisible = false;

    var filterFixed = false;
    var toTopFixed = false;
    
    placeholder2.css('margin-top', partnerTop-placeholderTop);
    

    view.bind(
        "scroll resize",
        function(){
            
            var viewTop = view.scrollTop();
           
 
            if (partnerTop <= viewTop + view.height() && !filterVisible) {
                $('#filter-container').fadeIn();
                filterVisible = true;
            }
            else if (partnerTop > viewTop + view.height() && filterVisible) {
                $('#filter-container').fadeOut();
                filterVisible = false;
            }
            
            
            if (viewTop + 150 >= partnerTop && !filterFixed) {
                message2.addClass('fixed2');
                filterFixed = true;
            }
            else if (viewTop + 150 < partnerTop && filterFixed) {
                message2.removeClass('fixed2');
                filterFixed = false;
            }
 
 
            if (
                (viewTop > placeholderTop) &&
                !toTopFixed
                ) {
 
                placeholder.height(placeholder.height());
 
                // Make the message fixed.
                message.addClass( "fixed" );
                $('#to-top-container').fadeIn();
                toTopFixed = true;
            //message

 
            // Check to see if the view has scroll back up
            // above the message AND that the message is
            // currently fixed.
            } else if ((viewTop <= placeholderTop) && toTopFixed ) {
 
                // Make the placeholder height auto again.
                placeholder.css( "height", "auto" );
 
                // Remove the fixed position class on the
                // message. This will pop it back into its
                // static position.
                message.removeClass( "fixed" );
                $('#to-top-container').fadeOut();
                $('#filter-container').fadeOut();
                toTopFixed = false;
 
            }
        }
        );
            
    $('input[name="search-sliding"]').keydown(function(event){
        if (event.keyCode == '13') {
            event.preventDefault();
            return false;
        }
        
        var filter = $(this).val();
        if (filter=='Ko ieškote?')
            filter = '';
        
        if (filter.length < 2)
            return;
        
        if (event.keyCode == 8 || event.keyCode == 46) {
            logSearchQuery(filter);
        }
        //else
        //    newSearch = true;
    });
      
    $('input[name="search-sliding"]').focusin(function(){
        var value = $(this).val();
        if (value=='Ko ieškote?')
            $(this).val('');
    });

    $('input[name="search-sliding"]').focusout(function(){
        var value = $(this).val();
        if (value=='')
            $(this).val('Ko ieškote?');
        else
            logSearchQuery(value);
        
    });
    
    
    $(window).bind('onbeforeunload', function(){
        var filter = $(this).val();
        if (filter=='Ko ieškote?')
            filter = '';
        
        if (filter.length < 2)
            return;
        
        logSearchQuery(value);
    });
        
    $('input[name="search-sliding"]').keyup(function(event) {
        
        if (event.keyCode == '13') {
            event.preventDefault();
            return false;
        }      
        
        var filter = $(this).val();
        if (filter == 'Ko ieškote?')
            filter = '';
        
        if (filter.length < 2) {
            $('.box-main:not(.hide-old-deals):hidden').css('display', '');
            return;
        }
        
        
        //s_start = (new Date()).getTime();
        if (newSearch && filter.length < searchQuery.length){
            logSearchQuery(searchQuery);
            newSearch = true;
        }
        
        if (event.keyCode != 8 && event.keyCode != 46)
            newSearch = true;
            
            
        searchQuery = filter;
        filter = noLt(filter);
        //alert(filter);
        var filters = filter.split(' ');
        filter = '(?=.*' + filters.join(')(?=.*') + ')';
        filter = new RegExp(filter,'i');
        
        var notFound = true;
        
        
        $('.box-main[id]').each(function(index,e){

            var text = noLt($('a.partner-deal-title',e).text());

            if (filter.test(text)) {
                $(e).css('display', '');
                notFound = false;
            }
            else
                $(e).css('display','none');
            
        });
        
        //s_end = (new Date()).getTime();
        
        //$('#search-status').text('Užtruko: ' + (s_end-s_start) + 'ms');
        
        if (notFound)
            $('#deal-filter-not-found').show();
        else
            $('#deal-filter-not-found').hide();
    });
    
//$('.box-main:not([id])').bind('mouseenter', function(){
//    $(this).unbind('mouseenter');
//    FB.XFBML.parse($('.facebook-like', this).get(0));
//});
    
});

function noLt(value) {
    return value.replace(/[ą]/gi,'a')
    .replace(/[č]/gi,'c')
    .replace(/[ęė]/gi,'e')
    .replace(/[į]/gi,'i')
    .replace(/[š]/gi,'s')
    .replace(/[ųū]/gi,'u')
    .replace(/[ž]/gi,'z');
    
}

var s_start = null;
var s_end = null;
var newSearch = false;
var searchQuery = '';

function logSearchQuery(value) {
    if (newSearch) {
        newSearch = false;

        $.ajax({
            url:'/ajax/search.php?q=' + escape(value), 
            cache:false
        });
    }
}

function hideOld(e){
    //alert(this);
    $(e).siblings().removeClass('selected');
    $(e).addClass('selected');
    $('.deal-old').addClass('hide-old-deals');
}
    
function showAll(e){
    //alert(this);
    $(e).siblings().removeClass('selected');
    $(e).addClass('selected');
    $('.deal-old').removeClass('hide-old-deals');
}

function showByPrice(e, price) {
    $(e).siblings().removeClass('selected');
    $(e).addClass('selected');
    
    if (price == '') {
        $('.hideByPrice').removeClass('hideByPrice');
        return;
    }
        
    
    showHideByPrice (price, 20);
    showHideByPrice (price, 50);
    showHideByPrice (price, 100);
    showHideByPrice (price, 200);
    $('.ikimax').addClass('hideByPrice');
}

function showHideByPrice(price, limit) {
    if (price >= limit)
        $('.iki' + limit).removeClass('hideByPrice');
    else
        $('.iki' + limit).addClass('hideByPrice');
}



jQuery.cookie=function(a,b,c){
    if(typeof b!='undefined'){
        c=c||{};
        
        if(b===null){
            b='';
            c.expires=-1
        }
        var d='';
        if(c.expires&&(typeof c.expires=='number'||c.expires.toUTCString)){
            var e;
            if(typeof c.expires=='number'){
                e=new Date();
                e.setTime(e.getTime()+(c.expires*24*60*60*1000))
            }else{
                e=c.expires
            }
            d='; expires='+e.toUTCString()
        }
        var f=c.path?'; path='+(c.path):'';
        var g=c.domain?'; domain='+(c.domain):'';
        var h=c.secure?'; secure':'';
        document.cookie=[a,'=',encodeURIComponent(b),d,f,g,h].join('')
    }else{
        var j=null;
        if(document.cookie&&document.cookie!=''){
            var k=document.cookie.split(';');
            for(var i=0;i<k.length;i++){
                var l=jQuery.trim(k[i]);
                if(l.substring(0,a.length+1)==(a+'=')){
                    j=decodeURIComponent(l.substring(a.length+1));
                    break
                }
            }
        }
        return j
    }
};




