/**
 * @author Yasar Bayar, yasar@navigatormm.com
 */

$(function(){
    //change domain based on city selection
    $('#domain_name option[value='+DOMAIN_NAME+']').attr('selected',true);
    $('#domain_name').change(function(){
        $.tii.GotoURL('http://www.'+$(this).val());
    });
    
    $.ajaxSetup({ cache: true }); 
    $.getJSON('/tiilib/ajax/get_weather.php',{w:DOMAIN_WOEID,u:'c',simple: true},function(data){
         $('#weather_condition').html(data.condition.text['0']);
         $('#weather_temp').html(data.condition.temp['0']);
    });
    
    switch(page_filename){
        case '': page_filename='index.php'; break;
        case 'listing.php': page_filename='search.php'; break;
    }
    $('a[href^=/'+page_filename+']').addClass('on');
    if(page_filename == 'index.php')$('a[href=/]').addClass('on');
});


$('.listing').live('click mouseover mouseout', function(event){
    switch(event.type){
        case 'click':
            if(event.liveFired.activeElement.name=='request') return;
            //window.location = $(this).find("h2:first a:first").attr("href");
            window.location = $(this).find("a:first").attr("href");
            break;

        case 'mouseover':
            $(this).addClass("listing-hover");
            break;

        case 'mouseout':
            $(this).removeClass("listing-hover");
            break;
    }
});



var RowRepeater_OnPageLoad = function($HOLDER){
    $HOLDER.find('.listing').each(function(i){
        if (i % 2 == 0) $(this).addClass("listing-bgcolor");
    });
    if($HOLDER.find('.tii-pager').html()==''){
        $HOLDER.find('.tii-pager').css('border','none');
    }
};

var RowRepeater_FixOhDate = function(data){
    if(data.oh_date=='0000-00-00' || data.oh_date=='1969-12-31')data.oh_date=data.oh_date2='';
    if(! data.oh_date==''){
        var rd = data.oh_date.split('-');
        var D=new Date(rd[0],rd[1]-1,rd[2]);
        data.oh_date2=D.getDayName()+' '+D.getMonthName()+', '+D.getDate();
    }
};

var RowRepeater_FixTime = function(data){
    if(data.attime.length < 11 || data.attime == ':00PM - :00PM')data.attime='';
};

var RowRepeater_ApplyRequestViewing = function($HOLDER, options){
    var $request_viewing = $('<a href="javascript:void(0);" class="request_viewing">Request Viewing</a>').click(function(e){
        e.stopImmediatePropagation();
        $.tii.GotoURL(
                'request_viewing.php?id=' + $(this).closest(options.item_template).attr('rel')
                ,true
                ,'width=640,height=450,resizable=yes,scrollbars=yes'
                );
    }).css({
        color: 'rgb(35, 83, 147)',
        cursor: 'pointer',
        fontSize: '11px'
    });
    $HOLDER.find(options.item_template).each(function(){
        var $oh_date = $(this).find('.oh_date');
        var $attime = $(this).find('.attime');
        var $oh = $(this).find('.oh');
        if($oh_date.html() == '' || $oh_date.html() == '<span title=""></span>' || $attime.html() == ''){
            $oh_date.html('').append($request_viewing.clone(true));
            $oh.remove();
            $attime.remove();
        }
    });
};
var RowRepeater_FormatMoney = function($HOLDER, options){
    $HOLDER.find('.money').formatCurrency({roundToDecimalPlace:0}).show();
};