// browser detection
var browser_name = navigator.appName;
var b_version = navigator.appVersion;
var browser_version = parseFloat(b_version);
if (browser_name == 'Microsoft Internet Explorer') {
	browser_name = 'ie';
}

(function(){
	/*Use Object Detection to detect IE6*/
	var  m = document.uniqueID /*IE*/
	&& document.compatMode  /*>=IE6*/
	&& !window.XMLHttpRequest /*<=IE6*/
	&& document.execCommand ;
	
	try{
		if(!!m){
			m("BackgroundImageCache", false, true) /* = IE6 only */ 
		}
		
	}catch(oh){};

})();


window.onload = function(){
	// for home page marketing
	if ($("#marketing_area1").length) {
		$('#marketing_area1').cycle({ fx:'fade', speed:1000, timeout:10000 });
		$('#marketing_area1').show();
	}
	if ($("#marketing_area2").length) {
		$('#marketing_area2').cycle({ fx:'fade', speed:1000, timeout:10000 });
		$('#marketing_area2').show();
	}
	if ($("#marketing_area3").length) {
		$('#marketing_area3').cycle({ fx:'fade', speed:1000, timeout:10000 });
		$('#marketing_area3').show();
	}
	if ($("#marketing_area4").length) {
		$('#marketing_area4').cycle({ fx:'fade', speed:1000, timeout:10000 });
		$('#marketing_area4').show();
	}

	// for cinematic viwer
	if ($("#myshadow").length) {
		Shadowbox.init();
	};

	// login/register 
	$().ready(function() {
		var show = $('#ex5show');
		var hide = $('#ex5hide');
		
		$('div.square')
			.jqm({overlay: 50, trigger: false})
			.jqmAddTrigger(show)
			.jqmAddClose(hide)

		$('#loginit').click(function() {
			document.getElementById('loginit_iframe').src = "/html/loginit.php";
			$('div.square.loginit').jqmShow();
			return false;
		});

		$('#myaccountit').click(function() {
			document.getElementById('loginit_iframe').src = "/html/loginit.php?formtype=register&formaction=edit";
			$('div.square.loginit').jqmShow();
			return false;
		});

		$('#logoutit').click(function() {
			location.href = "/html/loginit.php?formtype=logout&random=" + Math.random();
			return false;
		});
	});

	// for subfeature pagination
	if ($("table#paginateid1").length) {
		$("table#paginateid1").show();
	}
	if ($("table#subImagesDiv1").length) {
		$("table#subImagesDiv1").show();
		$("table#subListingInfo1").show();
	}


	// for large photos
	if ($("div#featureImage1").length) {
		$("div#featureImage1").css("visibility","visible");
	}
	if ($("div#featureText1").length) {
		$("div#featureText1").css("visibility","visible");
	}

	// for agent page
	if ($("div#subPhotoContainer1").length) {
		$("div#subPhotoContainer1").show();
	}

	// for mapping
	if (typeof mapInit=="function") {
		mapInit();
	}
};


function customFadeIn(node, speed) {
	// use this if fading text
	if (browser_name == 'ie') {
		$(node).fadeIn(1000, function() {
			this.style.removeAttribute('filter');
		});
	} else {
		$(node).fadeIn(1000);
	}
}


function fadeImg(direction) {
	if (direction == 'n') {
		if (nextFeature < totalFeatures) {
			nextFeature = currFeature + 1;
		} else {
			nextFeature = 1;
		}
	} else {
		if (nextFeature < 2) {
			nextFeature = totalFeatures;
		} else {
			nextFeature = currFeature - 1;
		}
	}

	$("div#featureImage"+currFeature).fadeOut(speed);
	$("div#featureImage"+nextFeature).fadeIn(speed);
	$("div#featureImage"+nextFeature).css("visibility","visible");

	$("div#featureText"+currFeature).css("visibility","hidden");
	$("div#featureText"+nextFeature).css("visibility","visible");
	
	currFeature = nextFeature;
}

function fadeSubImg(imgid, direction, folio, maxphotos, imgurl) {
	var myimgdiv_a = 'div#subimgdiv' + imgid + 'a';
	var myimgdiv_b = 'div#subimgdiv' + imgid + 'b';
	var myimgsrc_a = 'img#subimgsrc' + imgid + 'a';
	var myimgsrc_b = 'img#subimgsrc' + imgid + 'b';
	var myimgcnt = 'div#subimgcnt' + imgid;
	var current_photoid = $(myimgdiv_a).attr('photoid');
	var next_photoid;
	var prev_photoid;
	var suffix = "";
	var next_suffixid;
	var prev_suffixid;
	

	if (maxphotos > 1) {
		// calculate suffix
		if (direction == 'n') {
			if (current_photoid == maxphotos) {
				next_suffixid = "";
				next_photoid = 1;
			} else {
				next_suffixid = current_photoid;
				next_photoid = parseInt(current_photoid) + 1;
			}

			if (next_photoid > 1) {
				suffix = '_' + next_suffixid;
			}

			$(myimgdiv_a).attr('photoid',next_photoid);
			$(myimgcnt).html(next_photoid + '/' + maxphotos);
		} else {
			if (current_photoid == 1) {
				prev_photoid = maxphotos;
				prev_suffixid = parseInt(maxphotos) -1 ;
			} else if (current_photoid == 2) {
				prev_photoid = parseInt(current_photoid) - 1;
				prev_suffixid = "";
			} else {
				prev_photoid = parseInt(current_photoid) - 1;
				prev_suffixid = parseInt(current_photoid) - 2;
			}

			if (prev_photoid > 1) {
				suffix = '_' + prev_suffixid;
			}

			$(myimgdiv_a).attr('photoid',prev_photoid);
			$(myimgcnt).html(prev_photoid + '/' + maxphotos);
		}
	}

	// check to see which div has power on and power off
	var divToTurnOn;
	var divToTurnOff;
	var imgToTurnOn;
	var imgToTurnOff;
	if ($(myimgdiv_a).attr('power') == 'on') {
		divToTurnOn = myimgdiv_b;
		divToTurnOff = myimgdiv_a;
		imgToTurnOn = myimgsrc_b;
		imgToTurnOff = myimgsrc_a;
		$(myimgdiv_b).attr('power','on');
		$(myimgdiv_a).attr('power','off');
	} else {
		divToTurnOn = myimgdiv_a;
		divToTurnOff = myimgdiv_b;
		imgToTurnOn = myimgsrc_a;
		imgToTurnOff = myimgsrc_b;
		$(myimgdiv_a).attr('power','on');
		$(myimgdiv_b).attr('power','off');
	}
	
	var photoUrl = imgurl + '/' + folio + suffix + '.jpg';
	$(divToTurnOff).fadeOut(1000);
	$(imgToTurnOn).attr('src', photoUrl);
	$(divToTurnOn).fadeIn(1000);
}


function pageSubFeature(toPage) {
	var currentPage = $('div#paginateContainer').attr('currentPage');

	var divtohide = 'table#paginateid' + currentPage;
	var divtoshow = 'table#paginateid' + toPage;
	var imgtohide = 'table#subImagesDiv' + currentPage;
	var imgtoshow = 'table#subImagesDiv' + toPage;
	var infotohide = 'table#subListingInfo' + currentPage;
	var infotoshow = 'table#subListingInfo' + toPage;

	$(divtohide).hide();
	$(divtoshow).show();
	$(imgtohide).fadeOut(1000);
	customFadeIn(imgtoshow, 1000);
	/*
	$(imgtoshow).fadeIn(1000);
	if (browser_name == ie) {
		$(imgtoshow).fadeIn(1000, function() {
			this.style.removeAttribute('filter');
		});
	} else {
		$(imgtoshow).fadeIn(1000);
	}
	*/
	$(infotohide).hide();
	$(infotoshow).show();
	
	$('div#paginateContainer').attr('currentPage',toPage);
}


function agentFeatureListing(listingnum) {
	if (listingnum != prevnum) {
		//alert($("div#subPhotoContainer1").length);
		//$("div#subPhotoContainer2").fadeIn(speed);
		//$("div#subPhotoContainer1").hide();
		$("div#subPhotoContainer"+prevnum).fadeOut(speed);
		$("div#subPhotoContainer"+listingnum).fadeIn(speed);
		//$("div#subPhotoContainer"+listingnum).css("visibility","visible");
		$("div#featureText"+prevnum).css("visibility","hidden");
		$("div#featureText"+listingnum).css("visibility","visible");
		prevnum = listingnum;
	}
}

function checkPropType(formname, ptype, checkedflg) {
	if (formname == 'sale') {
		if (ptype == 'home') {
			document.searchform.type[0].checked = true;
			document.searchform.type[1].checked = false;
			document.searchform.type[2].checked = false;			
		} else if (ptype == 'land') {
			document.searchform.type[0].checked = false;
			document.searchform.type[1].checked = true;
			document.searchform.type[2].checked = false;
		} else if (ptype == 'comm') {
			document.searchform.type[0].checked = false;
			document.searchform.type[1].checked = false;
			document.searchform.type[2].checked = true;
		}
	} else {
		if (ptype == 'rental') {
			document.searchform.type[0].checked = true;
			document.searchform.type[1].checked = false;
		} else if (ptype == 'comm') {
			document.searchform.type[0].checked = false;
			document.searchform.type[1].checked = true;
		}
	}
}

function setStatus() {
	if (document.searchform.dummyStatus[0].checked && !document.searchform.dummyStatus[1].checked) {
		document.searchform.status.value = '1';
	} else if (!document.searchform.dummyStatus[0].checked && document.searchform.dummyStatus[1].checked) {
		document.searchform.status.value = '2';
	} else {
		document.searchform.status.value = '1,2';
	}
}


function setCommPtype(ptype) {
	if (ptype == 's') {
		document.searchform.ptype[0].checked = true;
		document.searchform.ptype[1].checked = false;
		document.searchform.ptype[2].checked = false;			
	} else if (ptype == 'l') {
		document.searchform.ptype[0].checked = false;
		document.searchform.ptype[1].checked = true;
		document.searchform.ptype[2].checked = false;
	} else if (ptype == 'r') {
		document.searchform.ptype[0].checked = false;
		document.searchform.ptype[1].checked = false;
		document.searchform.ptype[2].checked = true;
	}
}


function setRecentlyAdded() {
	if (document.searchform.recentlyaddedcheck.checked) {
		document.searchform.addedsince.value = document.searchform.sevendays.value;
	} else {
		document.searchform.addedsince.value = '';
	}
}

function setTowns() {
	for(i=0; i<document.searchform.elements.length; i++) {
		if (document.searchform.elements[i].name.indexOf('town') > -1) {
			document.searchform.elements[i].checked = document.searchform.townall.checked;
		}
	}
}


function popWin(winname, myurl) {
	var width = 680;
	var height = 520;

	if (winname == '_global') {
		width = 750;
	} else if (winname == '_about6') {
		width = 800;
	} else if (winname == '_about1') {
		width = 1100;
		height = 800;
	} else if (winname.indexOf("_marketing") > -1) {
		width = 955;
		height = 700;
	}

	var popup = window.open(myurl, winname, 'directories=no,location=yes,menubar=yes,toolbar=yes,resizable=yes,scrollbars=yes,width='+width+',height='+height);
	// popup.resizeTo(667, 520);
	// popup.location.href = url;
	popup.focus();
	return false;
}


function saveLogin(ptype, innum) {
	document.getElementById('saveit_iframe').src = "/html/loginit.php?type=" + ptype + "&innum=" + innum;
	$('div.square.saveit').jqmShow();
	return false;
}

function chooseRentalSimilar() {
	$('div#rentalSimilar').toggle(1000);
}



