/**
 * @author Yasar Bayar, yasar@navigatormm.com
 * Changelog:
 * 2011.03.01 - Jon Hulka
 * Modified functionality of 'upcomming', 'newlistings', and 'featured' tabs:
 * The tabs are only displayed if they contain data.
 */

$(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();
    }
    
    //2011.03.05
    //to prevent showing multiple tabs at once
    var isShown=false;
	//2011.03.01
    //Hide upcoming tab - the callback will re-show it if there are any
	$('#upcomming').hide();
	$('#t_upcomming').hide();
    var u_el=$('#upcomming').rowRepeater(js_dir+'/app/ajax/handle.php?a=get-upcoming-houses',{
		dataschema: ['id','type','oh_date','attime','price','agency','realtor','mls','price','prop_address'],
		external_data: {domain_id: DOMAIN_ID},
		remember_last_page: false,
		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);
			//2011.03.01
			if(u_el.length>0&&u_el.data!==null)
			{
				if(!isShown)$('#upcomming').show();
				$('#t_upcomming').show();
				isShown=true;
			}
		}
	});

	//2011.03.01
    //Hide new listings tab - the callback will re-show it if there are any
	$("#newlistings").hide();
	$("#t_newlistings").hide();
	var n_el=$('#newlistings').rowRepeater(js_dir+'/app/ajax/handle.php?a=get-new-listings',{
			dataschema: ['id','prop_address','type','price','cat_name','mls'],
			external_data: {domain_id: DOMAIN_ID},
			remember_last_page: false,
			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);
				//2011.03.01
				if(n_el.length>0&&n_el.data!==null)
				{
					if(!isShown)$("#newlistings").show();
					$("#t_newlistings").show();
					isShown=true;
				}
			}
		});

	//2011.03.01
    //Hide featured tab - the callback will re-show it if there are any
	$("#featured").hide();
	$("#t_featured").hide();
	var f_el=$('#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);
				//2011.03.01
				if(f_el.length>0&&f_el.data!==null)
				{
					if(!isShown)$("#featured").show();
					$("#t_featured").show();
					isShown=true;
				}
			}
		});
	
	$('#similiar').rowRepeater(js_dir+'/app/ajax/handle.php?a=get-similiar-listings&id=4679',{
			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);
			}
		});
	
	$('#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;

