var MENU_TIMER = null;
var TOP_COUNTER = null;

function lookup_price(url, a_week, a_weeks) {
    if(a_week != 'ajax') {
        var week = a_week;
        var weeks = a_weeks;
    } else {
        var week = $('select[name=cweek] :selected').val();
        var weeks = $('input[name=cweeks]:checked').val();
    }
    
    $('#house_booker').fadeTo(500, 0.10, function() {
        $.get(url+'?booking='+week.split('-')[1]+'|'+week.split('-')[0]+'|'+weeks, {}, function(data) {
            $('#house_booker').html(data);
            $('#house_booker').fadeTo(500, 1.0, function() {
                $('#house_booker').fixPNG();
            });
        });
    });
}

function switch_top() {
    TOP_COUNTER += 1;
    if(TOP_COUNTER == TOP_IMAGES.length) {
        TOP_COUNTER = 0;
    }
    $('#top_img_0 img').attr('src', $('#top_img_1 img').attr('src'));
    
    setTimeout(function() {
        $('#top_img_1 img').hide(0);
        setTimeout(function() {
            $('#top_img_1 img').attr('src', TOP_IMAGES[TOP_COUNTER]);
            $('#top_img_1 img').fadeIn(2000, function() {
                if(TOP_COUNTER < TOP_IMAGES.length - 1) {
                    $('#top_img_0 img').attr('src', TOP_IMAGES[TOP_COUNTER+1]);
                }
            });
        }, 500);
    }, 500);
    
    setTimeout(switch_top, 5000);
}

function skaven_search(url) {
    $.fancybox("<p style=\"text-align:center;padding: 20px 0 0 0;\"><img src=\""+search_loader+"\" alt=\"\"><br /><br />"+search_text+"</p>", {
                        'autoDimensions'    : false,
                        'width'             : 350,
                        'height'            : 100,
                        'transitionIn'      : 'none',
                        'transitionOut'     : 'none',
                        'showCloseButton'   : false
                });
    var m_url = '/'+url+'/';
    m_url += $('select[name=week] :selected').val();
    m_url += ','+$('select[name=weeks] :selected').val();
    m_url += ','+$('select[name=beds] :selected').val();
    m_url += ','+$('select[name=bedrooms] :selected').val();
    m_url += ','+$('select[name=bathrooms] :selected').val();
    m_url += ','+($('input[name=pets]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=smoking]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=spa]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=sauna]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=dishwasher]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=washer]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=internet]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=fireplace]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=tv]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=terrace]').is(':checked')?'1':'0');
    m_url += ','+($('input[name=dryer]').is(':checked')?'1':'0');
    m_url += '/0/';
    
    setTimeout(function() {
        window.location.href = m_url;
    }, 750);
    return false;
}

function skaven_lookup() {
    var data = $('input[name=house_id]').val();
    if(data != '') {
        window.location.href = "/lookup/"+encodeURIComponent(data)+'/';
    }
    return false;
}

$(function(){
    $.fn.extend({
        fixPNG: function(sizingMethod, forceBG) {
            if (!($.browser.msie)) return this;
            var emptyimg = "/1.4/graphics/empty.gif"; //Path to empty 1x1px GIF goes here
            sizingMethod = sizingMethod || "scale"; //sizingMethod, defaults to scale (matches image dimensions)
            this.each(function() {
                var isImg = (forceBG) ? false : jQuery.nodeName(this, "img"),
                    imgname = (isImg) ? this.src : this.currentStyle.backgroundImage,
                    src = (isImg) ? imgname : imgname.substring(5,imgname.length-2);
                this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + sizingMethod + "')";
                if (isImg) this.src = emptyimg;
                else this.style.backgroundImage = "url(" + emptyimg + ")";
            });
            return this;
        }
    });
    
    $("#house_id").DefaultValue($.catalog_lookup_string);
    
    /*No email bot spams*/
    $('a.email_check').nospam({ replaceText: true, filterLevel: 'low' });   
    
    // CMS Dropdown Menu
    $('#menu ul li').mouseover(function() {
    
        if(MENU_TIMER) {
            window.clearTimeout(MENU_TIMER);
            MENU_TIMER = null;
        }

        $('#menu ul li').find('li').css('display', 'none');
        $('#menu ul li').find('ul').css('display', 'none');
        $(this).find('li').css('display', 'block');
        $(this).find('ul').css('display','block');
    });

        $('#menu ul li').mouseout(function() {
            var this_ref = this;
            MENU_TIMER = window.setTimeout(function() {
                $(this_ref).find('ul').css('display', 'none');
            }, 500);
        });
    $("#menu ul li ul").css('display','none');
    
    // Top Slideshow
    if(TOP_IMAGES.length > 1) {
        TOP_COUNTER = 0;
        $('#top_img_0 img').attr('src', TOP_IMAGES[1]);
        setTimeout(switch_top, 5000);
    }
    
    $("a[rel=lightbox]").fancybox();
});

