/**
 * @author Yasar Bayar, yasar@navigatormm.com
 * edited by Jonathan Whiting (April 10, 2010)
 */


$(function(){
    var oh_page_nr, listing_page_nr;
	
	$('.tab1').click(function(){
		$('.tab1').each(function(){
			$(this).removeClass('active');
		});
		$(this).addClass('active');
	});
	
    $('#listings').rowRepeater(DATASOURCE,{
        dataschema: ['id','cat_name','prop_address','oh_date','attime','price','iduser','status'],
        per_page: 8,
        paging_location: 'both',
        external_data:{DOMAIN_ID:DOMAIN_ID},
        remember_last_page: true,
        soft_paging: false,
        parse_url_hash: true,

        messages:{
            nodata: '<span class="red bold">No result found. Please change your search criterias and try again.</span>'
        },
        onNextPageClick: function($HOLDER){
            if(window.location.hash){
                this.url = $.tii.SUP('tab',location.href.substr(location.href.indexOf("#")+1));
            }
        },
        onPrevPageClick: function($HOLDER){
            if(window.location.hash){
                this.url = $.tii.SUP('tab',location.href.substr(location.href.indexOf("#")+1));
            }
        },
        onBeforeRowLoad: function(data){
            RowRepeater_FixOhDate(data);
            RowRepeater_FixTime(data);
            return data;
        },
        onPageLoad: function($HOLDER){
            RowRepeater_ApplyRequestViewing($HOLDER, this);
            RowRepeater_FormatMoney($HOLDER,this);
            RowRepeater_OnPageLoad($HOLDER);
            $HOLDER.show();
            $.post('/app/ajax/handle.php',{
                a: 'set-viewed',
                ids: this.ids
            }, function(){});
        },
        events: [
            {id:'btnListings',type:'click',handler:function(event){
                window.location.href='#';

                var RR=event.data;

                RR.internal.RemoveFilter();
                RR.internal.SetPage(1);
                RR.internal.Update();
                RR.internal.SetState('all');
            }},
            {id:'btnUpcomingOH',type:'click',handler:function(event){
                window.location.href='#';
                
                var RR=event.data;
                
                var D=new Date();
                var y=D.getFullYear();
                var m=D.getMonth()+1; m=m<10?'0'+m:m;
                var d=D.getDate();d=d<10?'0'+d:d;
                var today = y+'-'+m+'-'+d;
                
                RR.internal.Filter(function(item,dataschema){
                    var d=item.oh_date;
                    if (d==undefined) d=item[$.inArray('oh_date',dataschema)];

                    var t=item.attime;
                    if (t==undefined) t=item[$.inArray('attime',dataschema)];

                    if(d>=today && t.length>=11) return item;
                    return false;
                });
                RR.internal.Sort('oh_date','asc','date');
                RR.internal.SetPage(1);
                RR.internal.Update();

                RR.internal.SetState('oh');
            }},
            {id:'btnSortByPriceDesc',type:'click',handler: function(event){
                window.location.href='#most';
                var RR=event.data;
				RR.internal.RemoveFilter();
                RR.internal.SetPage(1);
                RR.internal.Sort('price','DESC','numeric');
                //RR.internal.SetPage(1);
                RR.internal.Update();
            }},
            {id:'btnSortByPriceAsc',type:'click',handler: function(event){
                window.location.href='#least';
                var RR=event.data;
				RR.internal.RemoveFilter();
                RR.internal.SetPage(1);
                RR.internal.Sort('price','ASC','numeric');
                //RR.internal.SetPage(1);
                RR.internal.Update();
            }},

			{id:'btnSolds',type:'click',handler:function(event){
                window.location.href='#';
                
                var RR=event.data;
			
                RR.internal.Filter(function(item,dataschema){
                    var d=item.status;
                    if (d==undefined) d=item[$.inArray('status',dataschema)];

                    if(d==3) return item;
                    return false;
                });
                RR.internal.Sort('price','asc','date');
                RR.internal.SetPage(1);
                RR.internal.Update();

               
            }}
        ]
    });

    var tab=$.tii.GUP('tab');
    switch(tab){
        case 'least': $('#btnSortByPriceAsc').click();break;
        case 'most': $('#btnSortByPriceDesc').click();break;
    }
});