// JavaScript Document
var currFeatured = 1;
var featuredInterval = false;
var featuredDelay = 5;		//Seconds

$(document).ready( function() {

	if ($.browser.msie) {

		$('body').addClass('ie');
		$('body').addClass('ie'+$.browser.version.substr(0,1));

	}

	$('.shadowedBox').each(function(i) {

		//Add shadow around the box
		$(this).prepend('<div class="shadowedBoxTop" />' +
						'<div class="shadowedBoxLeft" />' +
						'<div class="shadowedBoxRight" />' +
						'<div class="shadowedBoxBottom" />' +
						'<div class="shadowedBoxTL" />' +
						'<div class="shadowedBoxTR" />' +
						'<div class="shadowedBoxBL" />' +
						'<div class="shadowedBoxBR" />');

	});

	$('.redHead').each(function(i) {

		$(this).prepend('<div class="redHeadLeft" />' +
						'<div class="redHeadRight" />' +
						'<div class="redHeadShadowLeft" />' +
						'<div class="redHeadShadowRight" />' +
						'<div class="shadowedBoxBottom" />' +
						'<div class="shadowedBoxBL" />' +
						'<div class="shadowedBoxBR" />');

	});

	$('.redHeadShadowLeft, .redHeadShadowRight').each(function(i, e) {

		$(this).css('height', ($(this).parent().height() - 40));
		
	});

	$('.round-corners').corners("5px transparent");

	$('div.redFadeButton, ' +
	  'div.redFadeButton_selected, ' +
	  'div.button, ' +
	  '.propBigLink, ' +
	  '#show_adv_search, ' +
	  '.littleWhiteBox, ' +
	  '.pageNavPrev, ' +
	  '.pageNavNext').each(function(i, e) {

		if ($(this).find('a:first-child').length == 1 && $(this).find('a:first-child').attr('href') != '') {

			$(this).css({'cursor' : 'pointer'})
				   .bind('click', function(el) {

				if (el.target.tagName != 'A') {

					if ($(this).find('a[href]').attr('target') == '_blank') {
						
						window.open($(this).find('a[href]').attr('href'));
						
					} else {
						
						window.location = $(this).find('a[href]').attr('href');
						
					}
	
				}
				
			});
			
		}

	});

	$('.propListing .smallImages img').each(function(i, e) {

		if ($(this).attr('src') == 'images/noPropImage.jpg') {
		
			$(this).css({'cursor' : 'auto'});
			
		} else {

			$(this).bind('click', function(e) {
	
				$(this).closest('.propListing').find('.mainImage img').attr({"src"   : $(this).attr('src'),
																			 "alt"   : $(this).attr('alt'),
																			 "title" : $(this).attr('title')
																			});
				
			});
			
		}
		
	});

	$('#chkSelectAll').bind('click', function(e) {

		checked = ($(this).attr("checked") == true ? "checked" : "");

		$(this).closest('table').find('input:checkbox:not(#chkSelectAll)').attr("checked", checked);
		
	});

	$('#checkboxCluster input:checkbox:not(#chkSelectAll)').bind('click', function(e) {

		var bAllChecked = true;
		var set = $(this).closest('#checkboxCluster').find('input:checkbox:not(#chkSelectAll)');

		$(set).each(function(i, e) {
		
			if ($(this).attr("checked") == false) { bAllChecked = false; }
		
		});
		
		$('#chkSelectAll').attr("checked", (bAllChecked ? "checked" : ""));
		
	});

	$('.littleWhiteBox').bind('click', function(e) {

		$('#largeCommentPic').attr('src', $(this).attr('src'));
		
	});

	$('.helpImage').bind('mouseover', function(e) { $(this).siblings('.helpText').show(); });
	$('.helpImage').bind('mouseout',  function(e) { $(this).siblings('.helpText').hide(); });
	$('.helpText').bind('mouseover', function(e) { $(this).show(); });
	$('.helpText').bind('mouseout', function(e) { $(this).hide(); });

	$('#featProps').bind('mouseover', function(e) { clearInterval(featuredInterval); });
	$('#featProps').bind('mouseout', function(e) { featuredInterval = setInterval("updateFeatured()", 2000); });

	$('#property .featPropSmall').bind('click', function(e) {

		$('#property #featPropLarge').attr({'src'   : $(this).attr('src'),
										 	'alt'   : $(this).attr('alt'),
											'title' : $(this).attr('title')});

	});

	$('#propImagesContainer .smallPic').bind('click', function(e) {

		$('#propImagesContainer #largePic').attr({'src'   : $(this).attr('src'),
												  'alt'   : $(this).attr('alt'),
												  'title' : $(this).attr('title')});

	});

	$('.smallCommentPic').bind('click', function(e) {

		$('#largeCommentPic').attr({'src'   : $(this).attr('src'),
									'alt'   : $(this).attr('alt'),
									'title' : $(this).attr('title')});

	});

	$('#prevComment').bind('click', function(e) { getImage('comments', $('#largeCommentPic'), -1, "jpg|gif") });
	$('#nextComment').bind('click', function(e) { getImage('comments', $('#largeCommentPic'), 1, "jpg|gif") });

	$('#aboutUsImgScrollerPrev').bind('click', function(e) { getImage('aboutUs', $('#aboutUsImgScrollerImage'), -1, "jpg|gif") });
	$('#aboutUsImgScrollerNext').bind('click', function(e) { getImage('aboutUs', $('#aboutUsImgScrollerImage'), 1, "jpg|gif") });

	//Add dividing grey line between left and right parts of site
	var lHeight = $('#main_body_left').height();
	var rHeight = $('#main_body_right').height();

	if (lHeight > rHeight) { $('#main_body_left').css('border-right', '1px dotted #999'); }
	else 				   { $('#main_body_right').css('border-left', '1px dotted #999'); }

});

function getImage(imageSet, imgObj, offset, filetypes) {

	var script  = '../scripts/getImage.php';
	var currImg = $(imgObj).attr('src').substr($(imgObj).attr('src').lastIndexOf('/') + 1);

	args = { "set"       : imageSet,
			 "currImg"   : currImg,
			 "offset"    : offset,
			 "filetypes" : filetypes
		   }

	$.post(script, args, function(data) { $(imgObj).attr('src', data) });
	
}

function updateFeatured() {

	if (array_count(aFeatProps) == 0 || array_count(aFeatProps[currFeatured]) == 0) {

		clearInterval(featuredInterval);
		$('#noProps').show();
		return false;

	} else {

		clearInterval(featuredInterval);
		featuredInterval = setInterval("updateFeatured()", featuredDelay * 1000);

		var script  = 'scripts/getFeatured.php';

		args = { "set" : aFeatProps[currFeatured]['type'],
				 "id"  : aFeatProps[currFeatured]['id']
			   }

		$.post(script, args, function(data) {

			if (data.type == 'advert') {

				$('#advert a').attr({'href'   : data.link,
									 'target' : data.target,
									 'title'  : data.alt
									});

				$('#advert a img').attr({'src'   : data.img,
										 'alt'   : data.alt,
										 'title' : data.alt
										});

				$('#noJS').hide();
				$('#noProps').hide();
				$('#advert').fadeIn('slow');
				$('#property').hide();
				
			} else if (data.type == 'property') {

				var aImages = new Array();
				aImages[0] = data.image1;
				aImages[1] = data.image2;
				aImages[2] = data.image3;

				$('#featPropLarge').attr('src', aImages[0]);
				$('.featPropSmall').each(function(i, e) {

					$(e).attr('src', aImages[i]);

				});

				$('#featPropLoc').html(data.location);
				$('#featPropDesc').html(data.details);
				$('#featPropPrice').html(data.price);
				$('#featPropRef').html(data.agent_ref);

				$('#property a').attr('href', 'property-detail.php?p='+data.propID);

				$('#property .redHeadShadowLeft, #property .redHeadShadowRight').each(function(i, e) {
			
					$(this).css('height', ($(this).parent().height() - 40));
					
				});

				$('#noJS').hide();
				$('#noProps').hide();
				$('#advert').hide();
				$('#property').fadeIn('fast');
				
			}

		}, "json");

	}

	if (++currFeatured >= aFeatProps.length) {

		currFeatured = 1;

	}

}

function array_count(arr) {

	var count = 0;

	for (var i in arr)
		count++;

	return count;

}
