/* - Custom JS Scripts -----------------------------------
Functions Outlined Below
* printversion(template)
* reorder(orderBy)
* popUpBox(linkInfo, boxName)
* InspectBox(linkInfo, boxName)
* PictureBox(linkInfo, size)
* SizeDropdown - Split single size dropdown into 2 variables
---------------------------------------------------------- */

// Print Version Script used from OFI, Aution and Search Pages
function printversion(template){
	var newurl = location.href;
	//tmpl check and replace
	if(location.href.indexOf('tmpl=') > 0){//If OrderBy Exists					
		var getIndex = window.location.href.indexOf('&tmpl=');	//Find OrderBy		
		var preTmpl = window.location.href.substr(0, getIndex); //Get URL Before OrderBy
		var removeAmp = window.location.href.substring(getIndex+1); //Remove &amp; Before OrderBy
		var postTmpl = "";
		if(removeAmp.indexOf('&') > 0){
			var getIndexOfAmp = removeAmp.indexOf('&'); //Find &amp; After OrderBy
			var postTmpl = removeAmp.substring(getIndexOfAmp); //Get URL After &amp;					
		}
		var newurl = preTmpl + "&tmpl=" + template + "_printlist" + postTmpl; //Put It All Together						
	}else{
		var newurl = location.href+'&tmpl='+ template + "_printlist";
	}
	
	//rps check and replace
	if(newurl.indexOf('rps=') > 0){
		if(newurl.indexOf('&rps=&') > 0){
			newurl = newurl.replace('&rps=&', '&rps=250&');	
		}
		if(newurl.search(/&rps=\d{1,3}/) > 0){
			newurl = newurl.replace(/&rps=\d{1,3}/, '&rps=250');
		}
	}else{
		newurl = newurl+'&rps=250';
	}
	//p check and replace (page)
	if(newurl.indexOf('p=') > 0){
		if(newurl.search(/&p=\d{1,1}/) > 0){
			newurl = newurl.replace(/&p=\d{1,1}/, '&');
		}
	}
	var finalurl=newurl.replace('#','');
	window.open(finalurl,'','');
}

// Reorder the current search to what is needed from a link (<a>).
function reorder(orderBy){
	if(location.href.indexOf('f_OrderedBy=') > 0){//If OrderBy Exists					
		var getIndex = window.location.href.indexOf('&f_OrderedBy=');	//Find OrderBy		
		var preOrderBy = window.location.href.substr(0, getIndex); //Get URL Before OrderBy
		var removeAmp = window.location.href.substring(getIndex+1); //Remove &amp; Before OrderBy
		var postOrderBy = "";
		if(removeAmp.indexOf('&') > 0){
			var getIndexOfAmp = removeAmp.indexOf('&'); //Find &amp; After OrderBy
			var postOrderBy = removeAmp.substring(getIndexOfAmp); //Get URL After &amp;					
		}
		var newurl = preOrderBy + "&f_OrderedBy=" + orderBy + postOrderBy; //Put It All Together						
	}else{
		var newurl = location.href+'&f_OrderedBy='+orderBy;
	}
	
	var finalurl=newurl.replace('#',''); //Remove # from URL				
	location.href = finalurl;
}

// Reorder the current search to what is needed from a link (<a>).
function disType(tmpl){
	if(location.href.indexOf('tmpl=') > 0){//If OrderBy Exists					
		var getIndex = window.location.href.indexOf('&tmpl=');	//Find OrderBy		
		var preTmpl = window.location.href.substr(0, getIndex); //Get URL Before OrderBy
		var removeAmp = window.location.href.substring(getIndex+1); //Remove &amp; Before OrderBy
		var postTmpl = "";
		if(removeAmp.indexOf('&') > 0){
			var getIndexOfAmp = removeAmp.indexOf('&'); //Find &amp; After OrderBy
			var postTmpl = removeAmp.substring(getIndexOfAmp); //Get URL After &amp;					
		}
		var newurl = preTmpl + "&tmpl=" + tmpl + postTmpl; //Put It All Together	
	
		//rps check and replace
		if(tmpl != "search_display") {
			if(newurl.indexOf('rps=') > 0){
				if(newurl.indexOf('&rps=&') > 0){
					newurl = newurl.replace('&rps=&', '&rps=12&');	
				}
				if(newurl.search(/&rps=\d{1,3}/) > 0){
					newurl = newurl.replace(/&rps=\d{1,3}/, '&rps=12');
				}
			}else{
				newurl = newurl+'&rps=12';
			}
		}
							
	}else{
		var newurl = location.href+'&tmpl='+tmpl;
	}
	
	//rps check and replace
	if(tmpl == "search_display") {
		if(newurl.indexOf('rps=') > 0){
			if(newurl.indexOf('&rps=&') > 0){
				newurl = newurl.replace('&rps=&', '&rps=250&');	
			}
			if(newurl.search(/&rps=\d{1,3}/) > 0){
				newurl = newurl.replace(/&rps=\d{1,3}/, '&rps=250');
			}
		}else{
			newurl = newurl+'&rps=250';
		}
	}
	//p check and replace (page)
	if(newurl.indexOf('p=') > 0){
		if(newurl.search(/&p=\d{1,1}/) > 0){
			newurl = newurl.replace(/&p=\d{1,1}/, '&');
		}
	}
	var finalurl=newurl.replace('#',''); //Remove # from URL				
	location.href = finalurl;
}

//Popups for Start Sites
function popUpBox(linkInfo, boxName) {
	winOptions = "alwaysRaised=yes,location=no,status=no,titlebar=no,"+
	"toolbar=no, scrollbars=no,height=560,width=620,left=0,top=0,resizable=yes";
	ordWin = window.open(linkInfo, boxName, winOptions);
	}
function InspectBox(linkInfo, boxName) {
	winOptions = "alwaysRaised=yes,location=no,status=no,titlebar=no,"+
	"toolbar=no, scrollbars=yes,height=500,width=650,left=0,top=0,resizable=no";
	ordWin = window.open(linkInfo, boxName, winOptions);
	}
function PictureBox(linkInfo, size, url) {
	size = size.replace(',', ',height=')
	linkOptions = "alwaysRaised=yes,location=no,status=no,titlebar=no,toolbar=no,scrollbars=yes,width=" + size + ",left=0,top=0,resizable=yes";
	linkURL = url + "/pol/Property/search.asp?" + linkInfo
	window.open(linkURL, 'pic', linkOptions);
	}
function MapBox(linkInfo, size, url) {
	size = size.replace(',', ',height=')
	linkOptions = "alwaysRaised=yes,location=no,status=no,titlebar=no,toolbar=no,scrollbars=no,width=" + size + ",left=0,top=0,resizable=yes";
	linkURL = url + "/pol/Property/search.asp?" + linkInfo
	window.open(linkURL, 'pic', linkOptions);
	}
	
//Used to split commercial size from one dropdown to 2 hidden inputs
$(document).ready(function(){
	$("select[name=sizeRange]").change(function(){
		var findSplit = $(this).val().indexOf('-');
		var sizeMin = $(this).val().substr(0, findSplit);
		var sizeMax = $(this).val().substr(findSplit+1);
		$("input[name=f_minimum_size]").attr("value", sizeMin);
		$("input[name=f_maximum_size]").attr("value", sizeMax);
	});
	
	
// Main Menu Active States
	//get body ID
	var bodyID = $("body").attr("id");	
	//sets the main menu items as active
	$("ul.menu a").each(function(){
		var linkID = $(this).attr("id");
		if(linkID.indexOf(bodyID) > 0) {
			$(this).addClass("active");
		}
	});
	
//Set active state to dropdown hover
	$(".menu ul").hover(
		function(){$(this).parent().children('a:first').addClass('active');},
		function(){
			//check if current active link need to remain active on mouseout
			var linkID = $(this).parent().children('a:first').attr("id");
			if(linkID.indexOf(bodyID) < 1) {
				$(this).parent().children('a:first').removeClass('active');
			}
		}
	);
	
	
	//All elements that are being loaded from external files.
	//$("DIV TO LOAD INTO IT").load("FILE TO LOAD");
	
	//Priority Preview Element
	$("#priorityPeview").load("/elements/priority-preview.asp");
	$("#suburbProfiles").load("/elements/suburb-profiles.asp");
	$("#testimonialsElement").load("/elements/testimonials.asp");
	$("#nowShowingElement").load("/elements/now-showing.asp");
	$("#eMagElement").load("/elements/e-mag.asp");
	$("body#renting #eMagElement").load("/elements/e-mag-renting.asp");
	$("#stayInTouchElement").load("/elements/stay-in-touch.asp");
	$("#marketReviewRentElement").append("<iframe frameborder='0' hspace='0' src='http://www.chnews.com.au/rental-market-review/' width='365' height='432' name='marketReview'> </iframe>");	
	$("#marketReviewBuyElement").append("<iframe frameborder='0' hspace='0' src='http://www.chnews.com.au/sales-market-review/' width='365' height='432' name='marketReview'> </iframe>");	
	$("#marketReviewElement").append("<iframe frameborder='0' hspace='0' src='http://www.chnews.com.au/market-review/' width='365' height='432' name='marketReview'> </iframe>");	
	$("#marketReviewMainElement").append("<iframe frameborder='0' hspace='0' src='http://www.chnews.com.au/market-news/' width='850' height='210' name='marketReviewMain'> </iframe>");	
	$("#theresMoreElement").load("/elements/theres-more.asp");
	$("#appraisalElement").load("/elements/appraisal.asp");	
	$("#searchCurrentElement.current").load("/pol/property/search.asp?f_AgentID=2041&xsl=2041&f_st=1&f_ps=2&rps=18&f_Orderedby=lmd&tmpl=search_element");
	$("#searchCurrentElement.sold").load("/pol/property/search.asp?f_AgentID=2041&xsl=2041&f_st=1&f_ps=6&rps=18&f_Orderedby=lmd&tmpl=search_element");
	$("#meetTheTeamElement.fullTeam").load("/pol/property/staff.asp?f_AgentID=2041&xsl=2041&tmpl=staff_element");
	$("#meetTheTeamElement.salesTeam").load("/pol/property/staff.asp?f_AgentID=2041&xsl=2041&cat_id=115&tmpl=staff_element");
	$("#meetTheTeamElement.rentalTeam").load("/pol/property/staff.asp?f_AgentID=2041&xsl=2041&cat_id=116&tmpl=staff_element");
	
	
	$(".videoBox").fancybox({
		'width'				: 1059,
		'height'			: 560,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe',
		'scrolling'			: 'no'
	});
	
	//Details Page Slideshow
	$('.imagesWrap #images').cycle({ 
	    fx:     'fade', 
	    prev:   '#prev', 
	    next:   '#next', 
	    after:   onAfter, 
	    timeout: 0,
	    speed: 500
	});
	function onAfter(curr, next, opts) {
		var index = opts.currSlide;
		$('#prev')[index == 0 ? 'fadeOut' : 'fadeIn']();
		$('#next')[index == opts.slideCount - 1 ? 'fadeOut' : 'fadeIn']();
	}
});
	function StaffListings(staffID){
		$("#searchCurrentElement.staffCurrent").load("/pol/property/search.asp?f_AgentID=2041&xsl=2041&f_st=1&f_ps=2&rps=18&f_Orderedby=lmd&tmpl=search_element&f_StaffID="+staffID);
	}
	function StaffListingsSold(staffID){
		$("#searchCurrentElement.staffSold").load("/pol/property/search.asp?f_AgentID=2041&xsl=2041&f_st=1&f_ps=6&rps=18&f_Orderedby=lmd&tmpl=search_element&f_StaffID="+staffID);
	}
