(function(jQuery){

    SearchRefine = function() { }
    SearchRefine.prototype.options = {};
    SearchRefine.prototype.div = null;
    SearchRefine.prototype.url = null;

    SearchRefine.prototype.start = function() {
        this.binds();
    }

    SearchRefine.prototype.setOptions = function(options) {
        this.options = jQuery.extend(this.options, options);
        this.div = this.options.div;
        this.url = this.options.url;

        // clean the url by removing query string
        if (this.url.indexOf("?") > 0) {
            this.url = this.url.split("?")[0];
        }
    }

    // Event Binds

    SearchRefine.prototype.binds = function() {

        var self = this;

        jQuery('#refineDateFrom', this.div).datepicker({
            showOn: "both",
            buttonImage: this.options.imageUrl + '/icons/silk/calendar.png',
            buttonImageOnly: true
        });
        jQuery('#refineDateTo', this.div).datepicker({
            showOn: "both",
            buttonImage: this.options.imageUrl + '/icons/silk/calendar.png',
            buttonImageOnly: true
        });

        showGoOnEdit = '#refinePriceFrom,#refinePriceTo,#refineDateFrom,#refineDateTo,.activity';
        jQuery(showGoOnEdit, this.div).keyup(function(event) {
            self.showGo(event);
        });
        jQuery(showGoOnEdit, this.div).change(function(event) {
            self.showGo(event);
        });

        $('.options .filter', this.div).click(function(event) {
            self.onFilter(event);
        });

        $('.options .remove', this.div).click(function(event) {
            self.onRemove(event);
        });

        $('.go a', this.div).click(function(event) {
            self.onGo(event);
        });

        $('.reset a', this.div).click(function(event) {
            self.onReset(event);
        })
    }


    // Event Functions

    SearchRefine.prototype.showGo = function(event) {
        box = jQuery(event.currentTarget);
        div = box.parent();
        goDiv = jQuery('div.go', div);

        //goDiv.show();
        goDiv.slideDown('fast');
    }

    SearchRefine.prototype.onFilter = function(event) {
        event.preventDefault();
        link = jQuery(event.currentTarget);

	this.addParamString(link.attr('value'));
        this.goToUrl();
    }

    SearchRefine.prototype.onRemove = function(event) {
        event.preventDefault();
        link = jQuery(event.currentTarget);

        this.deleteParamString(link.attr('value'))
        this.goToUrl();
    }

    SearchRefine.prototype.onGo = function(event) {

        event.preventDefault();

        params = {};

        params.datefrom = $('#refineDateFrom', this.div).val().replace(/\//g, '-');
        params.dateto = $('#refineDateTo', this.div).val().replace(/\//g, '-');
        params.pricefrom = $('#refinePriceFrom', this.div).val();
        params.priceto = $('#refinePriceTo', this.div).val();

        activities = '';
        jQuery.each(jQuery('.activity', this.div), function(i, checkbox) {
            if (checkbox.checked == true) {
                activities = activities + jQuery(checkbox).val() + '-';
            }
        });
        activities = activities.substring(0, activities.length - 1);
        params.activities = activities;

        this.addParamArray(params);
        this.goToUrl();
    }

    SearchRefine.prototype.onReset = function(event) {
        event.preventDefault();
        
        this.deleteParamString('bedroomsmin/bedroomsmax/sleepsmin/sleepsmax/datefrom/dateto/pricefrom/priceto/activities');
        this.goToUrl();
    }

    SearchRefine.prototype.resetLinks = function() {
        
    }


    
    // Object Functions

    SearchRefine.prototype.goToUrl = function() {
        window.location.href = this.url;
        //alert(this.url);
    }

    SearchRefine.prototype.addParam = function(param, value) {
        if (value) {
            this.url = this.url + '/' + escape(param) + '/' + escape(value);
        } 
    }

    SearchRefine.prototype.deleteParam = function(param) {
        regex = new RegExp('\/' + param + '\/[^\/]+', 'g');
        this.url = this.url.replace(regex, "");
    }

    SearchRefine.prototype.deleteParamString = function(string) {
        var self = this;

        array = string.split('/');
        jQuery.each(array, function(index, value) {
            self.deleteParam(value);
        });
    }

    SearchRefine.prototype.addParamString = function(string) {

        var self = this;

        array = string.split('/');
        jQuery.each(array, function(index, value) {
            if (index % 2 == 0) {
                self.deleteParam(value);
                self.addParam(array[index], array[index+1]);
            }
        });


    }

    SearchRefine.prototype.addParamArray = function(array) {

        var self = this;

        jQuery.each(array, function(field, value) {
            self.deleteParam(field);
            self.addParam(field, value);
        });
    }


})(jQuery);
