//global hangout funcs
var hquery = {
    path : null,
    loc : null,
    type : null,
    cats_in : null,
    cats : null,
    tags : null,
    tags_in : null,
    page : 1,
    favs : '',
    fillQueryVars : function (){
        hquery.loc = $('select#location').val();
		if(hquery.loc == null)
			hquery.loc = 0;
        hquery.type = $('input#hangout_type').val();
        hquery.cats_in = jQuery('#categorychecklist input:checkbox').map( function() {
            if(jQuery(this).is(' :checked')) return parseInt(jQuery(this).val(), 10); return null;
        } ).get().join(',');
        hquery.tags_in = jQuery('#tagchecklist input:checkbox').map( function() {
            if(jQuery(this).is(' :checked')) return parseInt(jQuery(this).val(), 10); return null;
        } ).get().join(',');
        hquery.cats = jQuery('#categorychecklist input:checkbox').map( function() {
            return parseInt(jQuery(this).val(), 10);
        } ).get().join(',');
        hquery.tags = jQuery('#tagchecklist input:checkbox').map( function() {
            return parseInt(jQuery(this).val(), 10);
        } ).get().join(',');
    },
    get : function (){
        $('.hangout-overlay').css({
            opacity:			0.8
        }).fadeIn('fast');
        $.post(hquery.path + 'ajax.php', {
            'type':hquery.type ,
            'location':hquery.loc ,
            'cats_in':hquery.cats_in ,
            'cats':hquery.cats,
            'tags_in':hquery.tags_in ,
            'tags':hquery.tags ,
            'paged':hquery.page,
            'action': 'resort-query'
        }, function(r) {
            $('.hangout-overlay').css({
                display:            'none'
            }).fadeOut('fast');
            $('#hangout-display').empty().html(r);
            $('.compare').css({
                display: 'block'
            });
			//alert(r);
            $('.compare input:checkbox').map(function() {
                if(jQuery(this).is(' :checked')) $(this).parent().toggleClass('compare-selected');
            } );
            //load Sifr To all Headings
            loadSifr();
        });
    },
    updateCatCount : function(){
        $.post(hquery.path + 'ajax.php', {
            'type':hquery.type ,
            'location':hquery.loc ,
            'cats_in':hquery.cats_in ,
            'cats':hquery.cats,
            'tags_in':hquery.tags_in ,
            'tags':hquery.tags ,
            'action': 'resort-query-count'
        }, function(r) {
            hquery.resetCats();
            hquery.resetTags();
            //alert(r);
            var holder = $('#response').html(r);
            $(".category",holder).each(function(id) {
                var cat = $(".category",holder).get(id);
                var val = $(".cat-val",cat).text();
                jQuery('span','#categorychecklist li#' + val ).html('('+$('#'+val,cat).text()+')');
            });
            $(".tag",holder).each(function(id) {
                var tag = $(".tag",holder).get(id);
                var val = $(".tag-val",tag).text();
                jQuery('span','#tagchecklist li#' + val ).html('('+$('#'+val,tag).text()+')');
            });
        });

    },
    resetCats : function (){
        jQuery('#categorychecklist li').each(function (){
            jQuery('span', this).html('(0)');
        });
    },
    resetTags : function (){
        jQuery('#tagchecklist li').each(function (){
            jQuery('span', this).html('(0)');
        });
    },
    defaultCatCount : function (){
        hquery.type = $('input#hangout_type').val();
        $.post(hquery.path + 'ajax.php', {
            'action': 'resort-query-count-default',
            'type':hquery.type
            }, function(r) {
            hquery.resetCats();
            hquery.resetTags();
            var holder = $('#response').empty().html(r);
            $(".category",holder).each(function(id) {
                var cat = $(".category",holder).get(id);
                var val = $(".cat-val",cat).text();
                jQuery('span','#categorychecklist li#' + val ).html('('+$('#'+val,cat).text()+')');
            });
            $(".tag",holder).each(function(id) {
                var tag = $(".tag",holder).get(id);
                var val = $(".tag-val",tag).text();
                jQuery('span','#tagchecklist li#' + val ).html('('+$('#'+val,tag).text()+')');
            });

        });
    },
    saveFavs : function (element){
        //alert(hquery.favs);
        $.post(hquery.path + 'ajax.php', {
            'type':hquery.type ,
            'favorites':hquery.favs,
            'action': 'save-favorites'
        }, function(r) {
            $('.load', $(element).parent()).hide();
            $(element).show();
            });
    },
    saveFavsCompare : function (element){
        //alert(hquery.favs);
        $.post(hquery.path + 'ajax.php', {
            'type':hquery.type ,
            'favorites':hquery.favs,
            'action': 'save-favorites'
        }, function(r) {
            $(element).hide();
            });
    },
    updateFavCount : function(){
    var count;
    var link = '';
        if((hquery.favs == '')||(hquery.favs == 0)){
            count = 0;
            hquery.favs = '';
        }
        else
            count = hquery.favs.split(',').length;
		if(hquery.type ==  'resort')
			 link  = '/resorts/compare-resorts/';
		else if (hquery.type ==  'liveaboard')
			 link = '/liveaboards/compare-liveaboards/';
		else if(hquery.type ==  'hotel')
			 link = '/hotels/compare-hotels/';
			
        if(count >= 1)
            $('.shoutout .inside #compareLink').html('<a href="'+ link +'" rel="nofollow">Compare Favorites</a>');
        else
            $('.shoutout .inside #compareLink').html('');
        $('.shoutout .count').html(count);
    },
    appendLoader : function (){
        //preload image based on path
        if(hquery.path != ''){
            var imgLoader = new Image();
            imgLoader.src = hquery.path + 'images/global/loader.gif';
        }
        $('#footer').after('<div class="hangout-overlay"><div><img src="'+imgLoader.src+'" /><br/>Loading...</div></div>');
    }
};

//helper functions

function new_fav_remove_fav(id) {
    var newtags = hquery.favs;
    newtags = newtags.replace( id, '' );
    // massage
    newtags = newtags.replace( /\s+,+\s*/g, ',' ).replace( /,+/g, ',' ).replace( /,+\s+,+/g, ',' ).replace( /,+\s*$/g, '' ).replace( /^\s*,+/g, '' );
    hquery.favs = newtags;
    return false;
}

function favs_flush_to_text(a) {
    a = a || false;
    var text = a ? a : '';
    var newtags = hquery.favs;
    var t = text.replace( /\s*([^,]+).*/, '$1,' );
    newtags += ','
    if ( newtags.indexOf(t) != -1 )
        return false;

    newtags += text;

    // massage
    newtags = newtags.replace( /\s+,+\s*/g, ',' ).replace( /,+/g, ',' ).replace( /,+\s+,+/g, ',' ).replace( /,+\s*$/g, '' ).replace( /^\s*,+/g, '' );
    hquery.favs = newtags;

    return false;
}