
// noConflict, just in case
var jQ = jQuery.noConflict();
//if(jQuery(someVariable).isEmpty()) { ... }
jQuery.fn.isEmpty = function() {
	return jQuery(this).val() == "" ? true : false;
};
/* function to get url parameters */
function urlParm(s) {
	var rx = /[\?&]([^=]+)=([^&]+)/g;
	var urlParams = new Array();
	var curParam = null;
	while(curParam=rx.exec(s)){ // **NOTE**: Assignment, not comparison...
		if((curParam[1] != null) && (curParam[2] != null)) {
			urlParams[curParam[1]] = curParam[2];
		}
	}
	return urlParams;
}
// this will strip the unnecessary cdata tags from the xml response
// i created this because the data was being read as html and anything
// in cdata tags was showing up blank
jQuery.fn.getCData = function() {
	var _this = jQuery(this).html();
	var rx = /(<!--\[cdata\[|\]\]-->)/ig;
	return _this.replace(rx,"");
}
var dataURLs = new Array();
//var adicio_widget = (function(){
function adicio_widget(config){
	// private variables and functions go here
	//alert(config);
	var init = false; // variable to store widget state (eg don't run a second time if true)
	var theData = null; // local object to store data returned from ajax call
	var dataURL = null; // data url for script call
	var adicioData = null; // ajax object
	var stringVar = null; // store the name of js variable containing the data
	var displayObj = null; // display object
	
	// public stuff goes in _main
	var _main = {
		/* configuration object, pull in variables from config, otherwise set to null */
		config:function(config){
			var prefix = config.secure ? "https://" : "http://";
			var clientID = config.clientID || null; 
			dataURL = prefix+clientID+config.dataURL || null; // set local form data url
			stringVar = config.stringVar || null; // set string local string variable
			displayObj = new Array(); // create local display object
			_main.run(); // done with config, let's begin...
		},
		run:function() {
			if(init) return; // if initialized, don't run again
			init = true; // if we're here, it's the first time running - initialized now true
			//console.log("dataURL: %s\n\nstringVar: %s",dataURL,stringVar); // debug
			if(!jQ(dataURL).isEmpty()) {
				adicioData = jQ.ajax({ 
				  type: "GET", 
				  url: dataURL, 
				  dataType: "script",
				  success: function(){ 
					var obj = window || obj; // gain proper object scope
					theData = parseXML( obj[stringVar] ); // set theData equal to value (turned into a XML DOC) of xml string variable
					_main.parseData();
				  } 
				});
			}
		},
		parseData:function() {				
			var city, state, zip, price, beds, baths, detail, img;
			var name, brokeragename, phone, address, description;
			
			if (dataURL.indexOf('searchTab=sell&searchType=featured') > 1 ){ // Featured Homes
				
				var widget_type = 'featured_homes';
				jQ('Property',theData).each(function(i){										   
					this_item = jQ(this);	
					city = this_item.find('City').text();
					state = this_item.find('State').text();
					zip = this_item.find('Zip').text();
					price = this_item.find('Price').text();
					beds = this_item.find('Bedrooms').text();
					baths = this_item.find('Bathrooms').text();
					detail = this_item.find('Url').text();
					img = this_item.find('Image1') ? this_item.find('Image1').text() : null;
					//create usable data display object
					displayObj[i] = { "city" : city, 
									  "state" : state, 
									  "zip" : zip, 
									  "price" : price, 
									  "beds" : beds, 
									  "baths" : baths, 
									  "detail" : detail,
									  "url" : img };
				});

			} else if (dataURL.indexOf('searchTab=rent&searchType=featured') > 1 ){ // Featured Rentals
				
				var widget_type = 'featured_rentals';
				jQ('Property',theData).each(function(i){										   
					this_item = jQ(this);	
					city = this_item.find('City').text();
					state = this_item.find('State').text();
					zip = this_item.find('Zip').text();
					price = this_item.find('Price').text();
					beds = this_item.find('Bedrooms').text();
					baths = this_item.find('Bathrooms').text();
					detail = this_item.find('Url').text();
					img = this_item.find('Image1') ? this_item.find('Image1').text() : null;
					//create usable data display object
					displayObj[i] = { "city" : city, 
									  "state" : state, 
									  "zip" : zip, 
									  "price" : price, 
									  "beds" : beds, 
									  "baths" : baths, 
									  "detail" : detail,
									  "url" : img };
				});
				
			} else if ( dataURL.indexOf('searchType=preferred') > 1 ){ // Preferred Agents
			
				var widget_type = 'preferred_agents';
				jQ('Agent',theData).each(function(i){
					this_item = jQ(this);	
					name = this_item.find('Name').text();
					brokeragename = this_item.find('BrokerageName').text();
					phone = this_item.find('Phone').text();
					detail = this_item.find('ProfileUrl').text();
					img = this_item.find('PortraitUrl') ? this_item.find('PortraitUrl').text() : null;
					//create usable data display object
					displayObj[i] = { "name" : name, 
									  "brokeragename" : brokeragename, 
									  "phone" : phone, 
									  "detail" : detail,
									  "url" : img };
				});
			
			} else if (dataURL.indexOf('searchType=openhouse') > 1 ){ // Open Houses
				
				var widget_type = 'open_houses';
				jQ('Property',theData).each(function(i){										   
					this_item = jQ(this);	
					address = this_item.find('Address').text();
					city = this_item.find('City').text();
					state = this_item.find('State').text();
					description = this_item.find('Description').text();
					detail = this_item.find('Url').text();
					img = this_item.find('Image1') ? this_item.find('Image1').text() : null;
					//create usable data display object
					displayObj[i] = { "address" : address, 
									  "city" : city, 
									  "state" : state, 
									  "description" : description, 
									  "detail" : detail,
									  "url" : img };
				});
				
			}
			
			_main.createWidget(displayObj, widget_type);
			
		},
		createWidget:function(displayObj, widget_type){
			//console.log(displayObj);
			if ( widget_type=='featured_homes' || widget_type=='featured_rentals' ){
				var prefix = (widget_type=='featured_homes') ? 'fh' : 'fr';
				
				f_items = '<li class="first_item"><img src="'+displayObj[0]['url']+'" alt="" rel="0" /></li>';
				for (var i=1; i<displayObj.length; i++){
					f_items = f_items + '<li><img src="'+displayObj[i]['url']+'" alt="" rel="'+i+'" /></li>';
				}
				jQ("#"+prefix+"_photos").html(f_items);
				
				f_info = '<span class="f_city" id="'+prefix+'_city">'+displayObj[0]['city']+'</span> <span class="f_state" id="'+prefix+'_state">'+displayObj[0]['state']+'</span> <span class="f_zip" id="'+prefix+'_zip">'+displayObj[0]['zip']+'</span> <span class="f_price" id="'+prefix+'_price"><a href="'+displayObj[0]['detail']+'">'+displayObj[0]['price']+'</a></span> <span class="f_pipe"> | </span> <span class="f_beds" id="'+prefix+'_beds">'+displayObj[0]['beds']+' Bed</span> <span class="f_pipe"> | </span> <span class="f_baths" id="'+prefix+'_baths">'+displayObj[0]['baths']+' Bath</span> <span class="f_pipe"> | </span><a class="f_detail" id="'+prefix+'_detail" href="'+displayObj[0]['detail']+'">more detail</a>';
				jQ("#"+prefix+"_info_contents").html(f_info);
				document.getElementById(prefix+"_slider").displayObj = displayObj;
				document.getElementById(prefix+"_slider").animation="stopped";
				document.getElementById(prefix+"_btn_left").mouse="off";
				document.getElementById(prefix+"_btn_right").mouse="off";
				document.getElementById(prefix+"_slider").relmatch=false;
				jQ("#"+prefix+"_btn_left").hover(function(){
					document.getElementById(prefix+"_btn_left").mouse="on";
					//animate_f(prefix,'left');
				}, function(){
					document.getElementById(prefix+"_btn_left").mouse="off";			  
				}).click(function(){ 
					animate_f(prefix,'left');
					return false
				});
				jQ("#"+prefix+"_btn_right").hover(function(){
					document.getElementById(prefix+"_btn_right").mouse="on";
					//animate_f(prefix,'right');
				}, function(){
					document.getElementById(prefix+"_btn_right").mouse="off";			  
				}).click(function(){ 
					animate_f(prefix,'right');
					return false
				});
				jQ("#"+prefix+"_photos img").live("click", function(){ // match all future imgs as well
					//if ( !jQ(this).is("#"+prefix+"_photos img:first") ){
					if ( jQ("#"+prefix+"_photos img:first").attr("rel") != jQ(this).attr("rel") ){ // not first img
						document.getElementById(prefix+"_btn_right").mouse="on";
						document.getElementById(prefix+"_slider").relmatch=false;
						animate_f(prefix,'right',jQ(this).attr("rel"));
					}
				});

			} else if (widget_type=='preferred_agents'){
				pa_header = '<div class="borderbox featuredagents clearfix"><h4>Featured Agents</h4><div class="slider" id="pa_slider"><ul class="clearfix">';
				pa_contents = '';
				for (var i=0; i<displayObj.length; i++){
					pa_contents = pa_contents + '<li><div class="item_photo"><a href="'+displayObj[i]['detail']+'"><img src="'+displayObj[i]['url']+'" alt="'+displayObj[i]['name']+'" /></a></div><div class="item_contents"><a class="item_headline" href="'+displayObj[i]['detail']+'">'+displayObj[i]['name']+'</a> '+displayObj[i]['brokeragename']+' <br /> '+displayObj[i]['phone']+' <br /><a href="'+displayObj[i]['detail']+'">more detail</a></div></li>';
				}
				pa_footer = '</ul></div></div><div class="borderbox_controls"><span>more Featured Agents</span><a id="pa_btn_down" href="#"><img src="http://cthousehunter.com/wp-content/blogs.dir/260/themes/ct_househunter/images/btn_arrow_down.gif" alt="Down" /></a><a id="pa_btn_up" href="#"><img src="http://cthousehunter.com/wp-content/blogs.dir/260/themes/ct_househunter/images/btn_arrow_up.gif" alt="Up" /></a></div>';
				jQ("#preferred_agents").html(pa_header+pa_contents+pa_footer);
				document.getElementById("pa_slider").animation="stopped";
				document.getElementById("pa_btn_down").mouse="off";
				document.getElementById("pa_btn_up").mouse="off";
				jQ("#pa_btn_down").hover(function(){
					document.getElementById("pa_btn_down").mouse="on";
					//animate('pa','down');
				}, function(){
					document.getElementById("pa_btn_down").mouse="off";			  
				}).click(function(){ 
					animate('pa','down');
					return false
				});
				jQ("#pa_btn_up").hover(function(){
					document.getElementById("pa_btn_up").mouse="on";
					//animate('pa','up');
				}, function(){
					document.getElementById("pa_btn_up").mouse="off";			  
				}).click(function(){ 
					animate('pa','up');
					return false
				});
				
			} else if (widget_type=='open_houses'){
				oh_header = '<div class="borderbox openhouses clearfix"><h4>Open Houses</h4><div class="slider" id="oh_slider"><ul class="clearfix">';
				oh_contents = '';
				for (var i=0; i<displayObj.length; i++){
					oh_contents = oh_contents + '<li><div class="item_photo"><a href="'+displayObj[i]['detail']+'"><img src="'+displayObj[i]['url']+'" alt="'+displayObj[i]['address']+'" /></a></div><div class="item_contents"><a class="item_headline" href="'+displayObj[i]['detail']+'">'+displayObj[i]['address']+', '+displayObj[i]['city']+' '+displayObj[i]['state']+'</a>'+displayObj[i]['description']+'<a class="item_details" href="'+displayObj[i]['detail']+'">... details</a></div></li>';
				}
				oh_footer = '</ul></div></div><div class="borderbox_controls"><span>more Open Houses</span><a id="oh_btn_down" href="#"><img src="http://cthousehunter.com/wp-content/blogs.dir/260/themes/ct_househunter/images/btn_arrow_down.gif" alt="Down" /></a><a id="oh_btn_up" href="#"><img src="http://cthousehunter.com/wp-content/blogs.dir/260/themes/ct_househunter/images/btn_arrow_up.gif" alt="Up" /></a></div>';					   
				jQ("#open_houses").html(oh_header+oh_contents+oh_footer);
				document.getElementById("oh_slider").animation="stopped";
				document.getElementById("oh_btn_down").mouse="off";
				document.getElementById("oh_btn_up").mouse="off";
				jQ("#oh_btn_down").hover(function(){
					document.getElementById("oh_btn_down").mouse="on";
					//animate('oh','down');
				}, function(){
					document.getElementById("oh_btn_down").mouse="off";			  
				}).click(function(){ 
					animate('oh','down');
					return false
				});
				jQ("#oh_btn_up").hover(function(){
					document.getElementById("oh_btn_up").mouse="on";
					//animate('oh','up');
				}, function(){
					document.getElementById("oh_btn_up").mouse="off";			  
				}).click(function(){ 
					animate('oh','up');
					return false
				});
			}
		}
	};
	return _main;              
}
// })();



// String to XML DOM function
function parseXML( xml ) {
	if( window.ActiveXObject && window.GetObject ) {
		var dom = new ActiveXObject( 'Microsoft.XMLDOM' );
		dom.loadXML( xml );
		return dom;
	}
	if( window.DOMParser )
		return new DOMParser().parseFromString( xml, 'text/xml' );
	throw new Error( 'No XML parser available' );
}

// Make it a 'plugin'
jQuery.parseXML = function( xml ) {
	return jQuery( parseXML(xml) );
};



function animate(prefix,direction){
	if ( direction=='down' ){
		var btn_id = prefix+'_btn_down';
		var j_remove = "#"+prefix+"_slider li:first";
		var ani_offset = '-=84px';
		var ani_fix = 84;
	} else if ( direction=='up' ){
		var btn_id = prefix+'_btn_up';
		var j_remove = "#"+prefix+"_slider li:last";
		var ani_offset = '+=84px';
		var ani_fix = -84;
	}
	if ( document.getElementById(btn_id).mouse=="on" ){
		if ( document.getElementById(prefix+"_slider").animation=="stopped" ){
			if (direction=='down'){
				jQ("#"+prefix+"_slider li:first").clone().appendTo("#"+prefix+"_slider ul");
			} if (direction=='up'){
				jQ("#"+prefix+"_slider li:last").clone().prependTo("#"+prefix+"_slider ul");
				jQ("#"+prefix+"_slider ul").css("top", ((jQ("#"+prefix+"_slider ul").css("top").slice(0, -2)*1)+ani_fix) + 'px' );
			}
			jQ("#"+prefix+"_slider ul").animate({ 
				top: ani_offset
			  }, 800, "linear", function(){ // was 500
				  jQ(j_remove).remove();
				  if (direction=='down'){
				  	jQ(this).css("top", ((jQ(this).css("top").slice(0, -2)*1)+ani_fix) + 'px' );
				  }
				  document.getElementById(prefix+"_slider").animation="stopped";
				  //animate(prefix,direction); // recursive call
			} );
		}				 
		document.getElementById(prefix+"_slider").animation="running";	
	}
}



function animate_f(prefix,direction,rel){
	
	if ( rel===undefined ){
		document.getElementById(prefix+"_slider").relmatch=false;
		var rel_set = false;
	} else {
		var rel_set = true;
	}
	
	//alert( document.getElementById(prefix+"_slider").relmatch +' '+ document.getElementById('fh_btn_right').mouse +' '+ document.getElementById(prefix+"_slider").animation );
	
	if ( direction=='left' ){
		var btn_id = prefix+'_btn_left';
		var j_remove = "#"+prefix+"_slider li:last";
		var ani_offset = '+=130px';
		var ani_fix = -130;
	} else if ( direction=='right' ){
		var btn_id = prefix+'_btn_right';
		var j_remove = "#"+prefix+"_slider li:first";
		var ani_offset = '-=130px';
		var ani_fix = +130;
	}
	if ( document.getElementById(btn_id).mouse=="on" ){
		if ( document.getElementById(prefix+"_slider").animation=="stopped" ){
			if ( document.getElementById(prefix+"_slider").relmatch==false ){
					
				var displayObj = document.getElementById(prefix+"_slider").displayObj;
				
				if (direction=='right'){
					jQ("#"+prefix+"_slider li:first").clone().appendTo("#"+prefix+"_slider ul");
					if ( jQ("#"+prefix+"_slider li:last").hasClass("first_item") ){
						jQ("#"+prefix+"_slider li:last").removeClass("first_item");
					}
					jQ("#"+prefix+"_slider li:last").css({'padding-left' : '10px','padding-right' : '0px'}); // make the new element small
					jQ("#"+prefix+"_slider li:last img").css({'height' : '85px','width' : '113px'});
					
					// animate next photo getting bigger
					jQ("#"+prefix+"_slider li:first+li").animate({ 
						paddingLeft: "14px",
						paddingRight: "14px"
					}, 700 ); // was 500
					jQ("#"+prefix+"_slider li:first+li img").animate({ 
						height: "120px",
						width: "160px"
					}, 700 ); // was 500
					
					// animate previous photo getting smaller
					jQ("#"+prefix+"_slider li:first").animate({ 
						paddingLeft: "10px",
						paddingRight: "0px"
					}, 850 ); // was 650
					jQ("#"+prefix+"_slider li:first img").animate({ 
						height: "85px",
						width: "112px"
					}, 850 ); // was 650
					
					// get big photo's propery info
					var item_rel = jQ("#"+prefix+"_slider li:first+li img").attr("rel");
				} 
				
				if (direction=='left'){
					jQ("#"+prefix+"_slider li:last").clone().prependTo("#"+prefix+"_slider ul");
					jQ("#"+prefix+"_slider ul").css("left", ((jQ("#"+prefix+"_slider ul").css("left").slice(0, -2)*1)+ani_fix) + 'px' );
					
					// animate previous photo getting bigger
					jQ("#"+prefix+"_slider li:first").animate({ 
						paddingLeft: "14px",
						paddingRight: "14px"
					}, 700 ); // was 500
					jQ("#"+prefix+"_slider li:first img").animate({ 
						height: "120px",
						width: "160px"
					}, 700 ); // was 500
					
					// animate next photo getting smaller
					jQ("#"+prefix+"_slider li:first+li").animate({ 
						paddingLeft: "10px",
						paddingRight: "0px"
					}, 700 ); // was 500
					jQ("#"+prefix+"_slider li:first+li img").animate({ 
						height: "85px",
						width: "112px"
					}, 700 ); // was 500
					
					// get big photo's propery info
					var item_rel = jQ("#"+prefix+"_slider li:first img").attr("rel");
				}
				
				// Property Info fade-in and fade-out
				jQ("#"+prefix+"_info_contents").fadeOut(100, function(){
					jQ("#"+prefix+"_city").html(displayObj[item_rel]['city']+',');
					jQ("#"+prefix+"_state").html(displayObj[item_rel]['state']);
					jQ("#"+prefix+"_zip").html(displayObj[item_rel]['zip']);
					jQ("#"+prefix+"_price").html('<a href="'+displayObj[item_rel]['detail']+'">'+displayObj[item_rel]['price']+'</a>');
					jQ("#"+prefix+"_beds").html(displayObj[item_rel]['beds']+' Bed');
					jQ("#"+prefix+"_baths").html(displayObj[item_rel]['baths']+' Bath');
					jQ("#"+prefix+"_detail").attr("href",displayObj[item_rel]['detail']);
					setTimeout(function() { jQ("#"+prefix+"_info_contents").fadeIn(100); }, 450); // was 300
				});
				
				//Sliding photos animation
				jQ("#"+prefix+"_slider ul").animate({ 
					left: ani_offset
				  }, 700, "linear", function(){ // was 500
					  jQ(j_remove).remove();
					  if (direction=='right'){
						jQ(this).css("left", ((jQ(this).css("left").slice(0, -2)*1)+ani_fix) + 'px' );
					  }
					  document.getElementById(prefix+"_slider").animation="stopped";
					  if ( rel==item_rel ){
						  document.getElementById(prefix+"_slider").relmatch=true;
					  }
					  if (rel_set) {animate_f(prefix,direction,rel); }// recursive call // only on photo click
				} );
				document.getElementById(prefix+"_slider").animation="running";	
			}
		}
		
	}
}



