//alex scripts

// reason css, display=none is used cos safari has bugs with hide()

//my vars

debugMode=false; //this must be off when we go live!!


function initDOMHinterNoosa() {
	 
     
     $('.anti-spam').css({ display: 'none' }).find('input').val('hinternoosa');

	
	$('#houseTickerBox').css('display','block');
	
	$('#quickSearch label').css('display','none');
	$('#submitQuickSearch').css('display','none');
	$('#linkQuickSearch').css('display','block').bind('click', function(){
  $('#quickSearch').submit();
});
	$('#quickSearch').submit(checkSearchQuery); //event handler for contact form
	
	
	$('#smsMeForm label').css('display','none');
	$('#smsMeForm form').submit(checkSmsForm);
	
	$('#appraisalForm').submit(checkAppraisalForm);
	
	
		$('#magazineForm').submit(checkMagazineForm);

		$('#mailing-list-form').submit(checkMailingListForm);

	
	$('#smsMeForm form input[type=text]').each(function() {
											 
											 
											 $(this).bind('blur',{originalText: $(this).attr('title') }, switchInputText).bind('focus',{originalText: $(this).attr('title') }, switchInputText).attr('value',$(this).attr('title'));
	
	
		
											 
											 });
	
	
	
	
	
	$('#searchQueryInput').bind('blur',{originalText: $('#searchQueryInput').attr('title') }, switchInputText);
	$('#searchQueryInput').bind('focus',{originalText: $('#searchQueryInput').attr('title') }, switchInputText).attr('value',$('#searchQueryInput').attr('title'));	
	
	scrollWeeklyHotlist();
	
	createLinkActions();
	
	
	updateTime();
	setInterval(updateTime,1000);
	
	$('#more_photos a:last').after('<p class="print">print &#9658;</p>');
	
	$('.print').bind('click', function() { window.print(); });
	
	
	//preload info box images
	
	$('body').append('<div style="display:none" id="preloadImages"><img src="images/tooltip/arrow-left.png" alt="" width="1" height="1" /><img src="images/tooltip/arrow-right.png" alt="" width="1" height="1" /><img src="images/tooltip/loader.gif" alt="" width="1" height="1" /><img src="images/prettyPhoto/bl.gif" alt="" width="1" height="1" /><img src="images/prettyPhoto/br.gif" alt="" width="1" height="1" /><img src="images/prettyPhoto/btnClose.gif" alt="" width="1" height="1" /><img src="images/prettyPhoto/btnNext.gif" alt="" width="1" height="1" /><img src="images/prettyPhoto/btnPrevious.gif" alt="" width="1" height="1" /><img src="images/prettyPhoto/loader.gif" alt="" width="1" height="1" /><img src="images/prettyPhoto/tl.gif" alt="" width="1" height="1" /><img src="images/prettyPhoto/tr.gif" alt="" width="1" height="1" /></div>');


    if ($('#more_info p:first').length) {

        var info = $('#more_info p:first').text();
    
        info = info.split(/[\s\.\?]+/);
        
        var normalWordCount = info.length;
        
        info = info.slice(0,50);
        
        var newWordCount = info.length;
        
        info = info.join('  ');
        
       // console.log(normalWordCount + '   ' + newWordCount);
        
        if (normalWordCount > newWordCount) {
        
            info += '...';
        }
    
       // console.log(info);
    
        $('#more_info').prepend('<div id="print-text">' + info + '</div>');
    
        
    
    }
    
    
    // add 'add to bookmarks button'
    
    $('#header').append('<span id="add-to-bookmarks">Add to Bookmarks</span>');
    
    $('#add-to-bookmarks').click(function() {
       createBookmark('HinterNoosa Real Estate', 'http://www.hinternoosa.com.au'); 
    });
}

 function createBookmark(title, url) 
 {
      if ($.browser.mozilla == true) {
            window.sidebar.addPanel(title, url, '');
    } else if($.browser.msie == true) {  
            window.external.AddFavorite( url, title);
    } else {
            alert('Please use Ctrl + D (or Cmd + D if using a Mac) to bookmark this website.');
    }

 }



function initWindowHinterNoosa() {
	

	
	var pageTracker = _gat._getTracker("UA-693909-23");
	pageTracker._trackPageview();
}





function createLinkActions() {
	

	

	$('#content a.jsBack').bind('click','',function(){
										  
										  history.back();
										  return false;
										  
										  });
	
	
	$('a[rel=external]').each(function() { 
												
  		$(this).attr('title','Link opens in a new window.').bind("click", {href: $(this).attr('href') }, newWindow);
  
  
	}); //make external links in new window!
	
		
	
	
	
}




function newWindow(event) {
	href = event.data.href;
	
	
	
	var blankWindow = window.open(href, '_blank');
	blankWindow.focus();	
	return false;
	
}









var email_re =/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i ;

var phone_re =/[0-9-\s\(\)]{6,}/i ;



function resetClass(args) {
	
	if (args.data) {
			$element='#'+(args.data.element);

	
	} else {
		$element=args;
		
	}
	
	$($element).removeClass();
	$('#errorText').text('').hide(100);
		
}


function highlightError(element) {

	$('#'+element).addClass('inputError').focus().select().one('keyup',{element: element},resetClass);;

}


function showError(text) {
	$('#errorText').text(text).show(300);
}


function checkSmsForm() {
	
	form = new Array();
	
	
	$('#smsMeForm input[type=text], textarea').each(function() {
																   
																   form[$(this).attr('id')]=$(this).attr('value');
																   
		//debug($(this).attr('id'));
		
																   });
	
		
	
	

	

	
	var name_re =/^[a-z-'\s]{2,}$/i;
	if (!form['contactName'].match(name_re) || form['contactName']=='Name') {
		showError("Please enter your full name.");
		
		highlightError('contactName');
		return false;
  	}
		
	/*
	if (!$form['email'].match(email_re) ) {
		showError("Please enter a valid email address."); 
		highlightError('email');
		return false;
  	}
		*/
		
	if (!form['contactPhone'].match(phone_re) || form['contactPhone']=='Number' ) {
		showError("Please enter your number."); 
		highlightError('contactPhone');
		return false;
  	}
	return true; // passed all regexs, now its server side time!!

	
}




function checkAppraisalForm() {
	
	form = new Array();
	
	
	$('#appraisalForm input[type=text], textarea').each(function() {
																   
																   form[$(this).attr('id').replace('input','').toLowerCase()]=$(this).attr('value');
																   
		debug($(this).attr('id').replace('input','').toLowerCase());
		
																   });
	
		
	
	

	

	
	var name_re =/^[a-z-'\s]{2,}$/i;
	if (!form['name'].match(name_re)) {
		showError("Please enter your full name.");
		
		highlightError('inputName');
		return false;
  	}
		
	
	if (!form['email'].match(email_re) ) {
		showError("Please enter a valid email address."); 
		highlightError('inputEmail');
		return false;
  	}
		
	/*	
	if (!form['contactPhone'].match(phone_re) || form['contactPhone']=='Number' ) {
		showError("Please enter your number."); 
		highlightError('contactPhone');
		return false;
  	}
	
	*/
	
	return true; // passed all regexs, now its server side time!!

	
}


function checkMailingListForm() {
	
	form = new Array();
	

	$('#mailing-list-form input[type=text]').each(function() {
																   
																   form[$(this).attr('id').replace('input','').toLowerCase()]=$(this).attr('value');
																   
		debug($(this).attr('id').replace('input','').toLowerCase());
		
																   });
	
		
	
	

	

	
	
	if (!form['email'].match(email_re) ) {
		showError("Please enter a valid email address."); 
		highlightError('inputEmail');
		return false;
  	}
		
	
	var name_re =/^[a-z-'\s]{2,}$/i; // '
	if (!form['name'].match(name_re)) {
		showError("Please enter your full name.");
		
		highlightError('inputName');
		return false;
  	}
		
		
	// if wanting noosa magazine, must have an address!
	
	if ($('#ncsm').is(':checked') && $('#inputAddress').val() == '') {
	
	   showError("You must enter your address if you want the magazine.");
		
		highlightError('inputAddress');
		return false;
	   
	}	
	
	
	// now we want priced to/from and like to receive must be entered
	
	var receivedChecked = $('#like-to-receive input[type=checkbox]:checked').length;
	
		
	if (receivedChecked == 0) {
	
	   showError("You must select something you'd like to receive.");
		
		
		return false;
	
	}
	
	if ($('#input-priced-from').val() == '' || $('#input-priced-to').val() == '') {
	
	 showError("You must enter your price range.");
		
		
		return false;
	
	}
	
	
	

	
	return true; // passed all regexs, now its server side time!!

	
}




function checkMagazineForm() {
	
	form = new Array();
	
	
	$('#magazineForm input[type=text], textarea').each(function() {
																   
																   form[$(this).attr('id').replace('input','').toLowerCase()]=$(this).attr('value');
																   
		debug($(this).attr('id').replace('input','').toLowerCase());
		
																   });
	
		
	
	

	

	
	var name_re =/^[a-z-'\s]{2,}$/i;
	if (!form['name'].match(name_re)) {
		showError("Please enter your full name.");
		
		highlightError('inputName');
		return false;
  	}
		
	
	if (!form['email'].match(email_re) ) {
		showError("Please enter a valid email address."); 
		highlightError('inputEmail');
		return false;
  	}
		
	/*	
	if (!form['contactPhone'].match(phone_re) || form['contactPhone']=='Number' ) {
		showError("Please enter your number."); 
		highlightError('contactPhone');
		return false;
  	}
	
	*/
	
	return true; // passed all regexs, now its server side time!!

	
}






var shiftPixels=3;
var scrollSpeed=100;
var movePixels=shiftPixels;
function scrollWeeklyHotlist() {
	
 
 	
	
	setInterval(scrollTicker,scrollSpeed);
	
	secondList = '<ul class="houseTicker" id="theHouseTicker2">' + $('#theHouseTicker1').html() + '</ul>';
	
	
	
	tickerWidth=0;
	$('.houseTicker li').each(function(i){
					tickerWidth += jQuery(this).width() +30;
				});
	
	//tickerWidth-=150;
	
	//debug(tickerWidth);
	
	
	
	
	$('#theHouseTicker1').after(secondList);
	$('#theHouseTicker1').css('left','10px');
	$('#theHouseTicker2').css('left',(tickerWidth));
	$('.houseTicker').width(tickerWidth);
	
	
	$('#houseTickerBox').hover(function(){
  				movePixels=Math.round(shiftPixels/2);
		},function(){
		  		movePixels=shiftPixels;
});
	
	
}

var scrollCount=false;

function scrollTicker() {

	


	for(i=1;i<3;i++) {
	

		currentLeft = $('#theHouseTicker' + i).css('left').replace('px','');
		
		//debug(currentLeft);
		
		newLeft = currentLeft-movePixels;
	
		$('#theHouseTicker' +i).css('left',newLeft + 'px');
	
	
	
		//debug($('#theHouseTicker1').css('left'));
	//debug('>'+tickerWidth);
	
		if ($('#theHouseTicker' +i).css('left').replace('px','') < (tickerWidth*-1)) {
		
					

		
			if (scrollCount==false) {
				
				newPosition=tickerWidth-20;
				scrollCount=true;
				//debug('SCROLL:'+scrollCount);
				
			} else {
				newPosition=(tickerWidth-20);
				scrollCount=false;
				//debug('SCROLL:'+scrollCount);

				
			}
			
			//newPosition = $('.houseTicker:last li:last').css('left').replace('px','');
		
			//debug('left:'+newPosition);
		
			$('#theHouseTicker' +i).css('left',(newPosition))
		
			
		}
	
	
	
	}
	
	

	
}

function testScroll() { // delete when browsers work
	 $('.houseTicker:last li:last').css('border','1px solid blue');
	 
		//debug($('#theHouseTicker1').css('left'));
	
}








function switchInputText(event) {
	//debug(event.data);
	originalText = event.data.originalText;
		
		//debug('['+originalText+']');
		
	if (this.value=='') {
		
		
		this.value=originalText;
		resetClass('#'+$(this).attr('id'));
		//debug('i want this id: '+$(this).attr('id'));
	}
	
	else if (this.value==originalText) {
	
		this.value='';
		
	} 
}





function checkSearchQuery() {
//	alert('ff');
	value = $('#searchQueryInput').attr('value');
	
	if (value==$('#searchQueryInput').attr('title') || value=='') {
		return false;	

	} else {
		return true;
		
	}
	
	
	
}


function debug(text) {
	
	if (debugMode==true) {
		try{
			console.log(text);
		}
		catch(error){
			alert(text);
		}
			
	}
	
}

function updateTime() {


	offset="+10";

  
    d = new Date();
   
    
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
	
	
    nd = new Date(utc + (3600000*offset));
   
   
	today = nd;
	
	
	 var minute = today.getMinutes();
	 
	 
	 
	 
	 var hour   = ((today.getHours()));
	 var hour24 = ((today.getHours()));
	 
	  
	 var ampm   = "";
	 
	 var day    = today.getDay();
	 
	 if(minute<10)
	  {
	   	minute = "0"+minute;
	  }
	 
	
	 if((hour24)<12)
	  {
	   	ampm = "AM";
	  }
	 else
	  {
	   	ampm = "PM"
	  }
	 if(hour24>=12)
	  {
	  	hour= hour-12;
	  }
	 if(hour24==0)
	  {
	  	hour=12;
	  }
	  
	  
	  if (hour==0) { hour=12 };
		
	
	$('#iPhoneTime').html((hour+':'+minute+' '+ampm));
	

}








$(document).ready(function(){
   initDOMHinterNoosa();
  });

window.load = initWindowHinterNoosa();