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

$(function(){
	
	$("#usual1 ul").idTabs();
    $('#usual1 ul a').click(function(){
        $.cookie('usual1.tab', $(this).attr('href'));
    });
    
    $('.btn-search-all').click(function(){
        $.cookie('tii.rowRepeater-listings.page','');
    });
    
    if($.cookie('usual1.tab')){
        $('a[href='+$.cookie('usual1.tab')+']').click();
    }
    
	$('#upcomming').rowRepeater(js_dir+'/app/ajax/handle.php?a=get-upcoming-houses',{
        dataschema: ['id','type','oh_date','attime','price'],
		external_data: {domain_id: DOMAIN_ID},
		per_page: 3,
        onBeforeRowLoad: function(data){
            RowRepeater_FixOhDate(data);
            RowRepeater_FixTime(data);
            return data;
        },
        onPageLoad : function($HOLDER, options){
            if (typeof(options) == 'undefined') options=this;
            RowRepeater_ApplyRequestViewing($HOLDER, options);
            RowRepeater_FormatMoney($HOLDER,options);
            RowRepeater_OnPageLoad($HOLDER);
        }
	});

	$('#newlistings').rowRepeater(js_dir+'/app/ajax/handle.php?a=get-new-listings',{
        dataschema: ['id','prop_address','type','price','cat_name'],
		external_data: {domain_id: DOMAIN_ID},
		per_page: 3,
        onBeforeRowLoad: function(data){
            RowRepeater_FixOhDate(data);
            RowRepeater_FixTime(data);
            return data;
        },
        onPageLoad : function($HOLDER, options){
            if (typeof(options) == 'undefined') options=this;
            RowRepeater_ApplyRequestViewing($HOLDER, options);
            RowRepeater_FormatMoney($HOLDER,options);
            RowRepeater_OnPageLoad($HOLDER);
        }
	});
    
	$('#featured').rowRepeater(js_dir+'/app/ajax/handle.php?a=get-featured-listings',{
        dataschema: ['id','oh_date','attime','price','agency','cat_name'],
		external_data: {domain_id: DOMAIN_ID},
        per_page: 1,
        remember_last_page: false,
        onBeforeRowLoad: function(data){
            RowRepeater_FixOhDate(data);
            RowRepeater_FixTime(data);
            return data;
        },
        onPageLoad : function($HOLDER, options){
            if (typeof(options) == 'undefined') options=this;
            RowRepeater_ApplyRequestViewing($HOLDER, options);
            RowRepeater_FormatMoney($HOLDER,options);
            RowRepeater_OnPageLoad($HOLDER);
		}
	});

    $('#favlistings').rowRepeater(js_dir+'/app/ajax/handle.php?a=get-fav-listings',{
        external_data: {domain_id: DOMAIN_ID},
        messages: {
            nodata: 'Click "Add to Favorites" on any listing to save it here.'
        },
        tunnel: $('<div id="favlistings-tunnel"></div>').appendTo('body'),
        onPageLoad : function($HOLDER){
            $('.remove').hide().click(function(e){
                e.stopImmediatePropagation();
                $Remove = $(this);
                $.getJSON(
                        js_dir+'/app/ajax/handle.php'
                        ,{'a': 'remove-fav-listing','id':$Remove.attr('rel')}
                        ,function(AR){
                            if(AR.success){
                            $Remove.closest('.listing').remove();
                        }
                });
            });
            
            $HOLDER.find('.listing').hover(
                function(){$(this).find('.remove').show()},
                function(){$(this).find('.remove').hide()}
            );
            
            RowRepeater_OnPageLoad($HOLDER);
            
            switch(window.location.pathname){
                case '/detail.php':
                    if($.inArray($.tii.GUP('id'),this.ids)>=0){
                        $('#btnAddToFavorites').addClass('disabled').unbind('click').click(function(){return false;});
                    }
                    break;
            }
        }
    });
});

/*
	Standards Compliant Rollover Script
	Author : Jonathan Whiting
	http://www.crayonsandwalls.com
*/

function initRollovers() {
	if (!document.getElementById) return

	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_o'+ftype);

			aImages[i].setAttribute('hsrc', hsrc);

			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;

			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}

			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_o'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

function toggleRegionSelect(id){
    var ul = document.getElementById("group_"+id);
    var li = null;
    var li_obj = null;
    for(var i=0; i<ul.childNodes.length; i++){
        li = ul.childNodes[i];
        li_obj = li.firstChild;
        if(li_obj.nodeName.toLowerCase() == 'input'){
            //if(li_obj.checked){
            //    li_obj.checked = false;
            //}else{
            //    li_obj.checked = true;
            //}
            li_obj.checked = document.getElementById("id_city_"+id).checked;
        }
    }
}

window.onload = initRollovers;
