/************************************************************************************************************************
/
/	Author: 	Jonathan Martin
/				www.martin-creative.com
/	Client:		RateRexburgHousing.com
/
/***********************************************************************************************************************/

/************************************************************************************************************************
/
/	Page:			Listing page
/	Function: 		setOpacity and 2 jQuery override functions
/	Descritpion: 	Overrides the jQuery fadeIn and out functions so the fade will work with IE 7.
/ 					The override functions were found at: http://malsup.com/jquery/fadetest.html.
/					The setOpacity function was needed in conjunction with the two override functions to 
/					make the fading blackbox div work correctly.
/
/***********************************************************************************************************************/
function setOpacity() {
	var blackBox = document.getElementById( 'blackBox' );

	blackBox.style.opacity = .5;
	blackBox.style.KhtmlOpacity = .5;
	blackBox.style.filter = "alpha(opacity=50)";
}
jQuery.fn.fadeIn = function( speed, callback ) { 
	return this.animate( {opacity: 'show'}, speed, function() { 
		if ( jQuery.browser.msie )  
			this.style.removeAttribute('filter');  
		if ( jQuery.isFunction( callback ) ) 
			callback();  
	}); 
}; 
jQuery.fn.fadeOut = function( speed, callback ) { 
	return this.animate( {opacity: 'hide'}, speed, function() { 
		if ( jQuery.browser.msie )  
			this.style.removeAttribute('filter');  
		if ( jQuery.isFunction( callback ) ) 
			callback();  
	}); 
}; 

/************************************************************************************************************************
/
/	Page:			Listing page
/	Function: 		showPnl, hidePnl
/	Descritpion: 	Shows or hides the black box div and slides a panel open or closed
/
/***********************************************************************************************************************/
function showPnl( id ) {
	
	setOpacity();
	
	var blackBox = document.getElementById( 'blackBox' );
	blackBox.style.diplay = 'block';
	$("div#blackBox").fadeIn(200);
	$("div#" + id + ":hidden").slideDown("medium");
}
function hidePnl( id ) {
	
	setOpacity();
	
	var blackBox = document.getElementById( 'blackBox' );
	$("div#blackBox").fadeOut(200, function () { 
		blackBox.style.display = 'none';
	});
	$("div#" + id + ":visible").slideUp("medium");
}

/************************************************************************************************************************
/
/	Page:			Listing page
/	Function: 		togglePnl
/	Descritpion: 	Determines if the "blackBox" div should be faded in or out and if the "Choose Rating" panel should
/ 					be up or down.
/
/***********************************************************************************************************************/
function togglePnl() { 
	var blackBox	= document.getElementById( 'blackBox' );
	var allTabPnl	= document.getElementById( 'allComplexPnl' );
	var typeTabPnl	= document.getElementById( 'complexTypePnl' );
	
	// if the all tab panel is displayed, check to see if the sort panel for that tab is displayed.
	// If it is, hide it.  Otherwise, display it.
	if ( typeTabPnl.style.display == 'none' ) {
		if (blackBox.style.display == 'none') {
			showPnl( 'chooseRatingAll', blackBox );
		}
		else {
			hidePnl( 'chooseRatingAll', blackBox );
		}		
	}
	else {
		if ( blackBox.style.display == 'none' ) {
			showPnl( 'chooseRatingType', blackBox );
		}
		else {
			hidePnl( 'chooseRatingType', blackBox );
		}	
	}
}

/************************************************************************************************************************
/
/	Page:			Listing page, Compare Page, Individual Page
/	Function: 		toggleFolder
/	Descritpion: 	Changes the background of a folder heading and slides it's related folder open or closed.
/
/***********************************************************************************************************************/
function toggleFolder( folder ) {
	if ( folder.className == 'folder' ) {
		folder.setAttribute( 'class', 'folderOpen' );
		folder.className = 'folderOpen';
		
		$("div#" + folder.id + "Folder:hidden").slideDown("medium");
	}
	else {
		folder.setAttribute( 'class', 'folder' );
		folder.className = 'folder';
		
		$("div#" + folder.id + "Folder:visible").slideUp("medium");
	}	
}

/************************************************************************************************************************
/
/	Page:			Individual page
/	Function: 		initCycle
/	Descritpion: 	Slides children of a parent div left or right, uses jQuery plugin called cycle: 
/					http://www.malsup.com/jquery/cycle/.  Also note that the cycle function changes the 
/					position attribue of each child element to absolute which cause the toggleSubFolder( folder )
/					function to display improperly in IE (still works, just not as smooth).
/
/***********************************************************************************************************************/
function initCycle() {
	// populate an array with the names of the parent divs
// 	var pnls = new Array (6);
// 	pnls [0] = "quality";
// 	pnls [1] = "proximity";
// 	pnls [2] = "parking";
// 	pnls [3] = "wardSocial";
// 	pnls [4] = "management";
// 	pnls [5] = "value";
	
	// build the cycle function(s) for all the elements in the array
	var output = "$(function() {  ";
	for (i = 0; i < pnls.length; i++) {
		output +=	"$('#" + pnls[i] + "Parent').cycle({";
		output +=	"fx: 'scrollHorz',";
		output +=	"speed: 1000,";
		output +=	"next: '#" + pnls[i] + "Next',";
		output +=	"prev: '#" + pnls[i] + "Prev',";
		output +=	"easing: 'backout',";
		output +=	"nowrap: 1,";
		output +=	"timeout: 	0";
		output +=	"});";
	}
	output += "});";
	
	// write the functions so they can be used
	eval(output);
}

function initImgCycle() {
    $(function() {
        $('.arrangImgs').cycle({
            fx: 'fade',
            speed: 1000
        });
    });
}

// execute the function
//initCycle();
//initImgCycle();

/************************************************************************************************************************
/
/	Page:			Compare Page
/	Function: 		toggleSubFolder
/	Descritpion: 	Changes the "+" or "-" operator of a folder heading and slides it's related folder open or clossed.
/
/***********************************************************************************************************************/
function toggleSubFolder( folder ) {
	// if the operator in front of the folder name is "+" then 
	// change it to "-" and slide the folder open
	var children = folder.childNodes;	
	var operator = '+';
	
	if( folder.innerHTML.indexOf( '-' ) == -1 ) {
		operator = '-';
		$("div#" + folder.id + "Folder:hidden").slideDown("medium");
	}
	// otherwise, change the "-" to plus and slide the folder closed
	else {
		operator = '+';
		$("div#" + folder.id + "Folder:visible").slideUp("medium");
	}
	
	// had to do it this way for IE and Firefox to work
	// IE recognizes [0] and Firefox recognizes [1]
	// it still works without the if statement in IE 
	// but causes an error
	
	// Firefox
	if( window.XMLHttpRequest && !( window.ActiveXObject ) ) {
		children[1].innerHTML = operator;
	}
	// Other
	else {
		children[0].innerHTML = operator;
	}
}

