

jQuery.noConflict();
jQuery(document).ready(function($) {

    var getTextNodesIn = (function() {
        function textNodeFilter() {
            return this.nodeType == 3;
        }

        return function(el) {
            var $el = $(el);
            return $el
                .contents()
                .filter(textNodeFilter)
                .add(
                    $el
                       .find("*")
                       .contents()
                       .filter(textNodeFilter)
                );
        };
    })();

    var url = location.href;
    if (url.search("propertydetails-business-transfer.aspx") > -1) {
        // Wrap all text nodes in <p> tags and remove <br /> tags
        var $propertyDetails = $('.propertyDetailsDescription .propertyDetails'),
        txt = getTextNodesIn($propertyDetails);

        txt.each(function(i, e) {
            var $e = $(e);
            if (this.nodeType == 3) {
                var p = $('<p></p>');
                p.text($e.text()).appendTo($propertyDetails);
            }
            $e.remove();
        });
        $propertyDetails.children('br').remove();

        // Set up read more after third paragraph tag
        $propertyDetails.children('p:nth-child(3)').nextAll().wrapAll('<div class="dropContent"></div>');
        $propertyDetails.children('p:nth-child(3)').after('<p class="right"><a class="lnkDropContent">Read More</a></p>');
        $('.lnkDropContent').click(function(e) {
            var $e = $(this);
            if ($e.hasClass('open')) {
                $('.dropContent').slideUp();
                $e.removeClass('open').text('Read More');
            } else {
                $('.dropContent').slideDown();
                $e.addClass('open').text('Show Less');
            }

        });
    }
});

function validateSearch(obj) {
	if(obj.value == "") {
		alert("Please enter a search phrase");
		return false;
	} else {
		location.href = '/search-results.aspx?search=' + URLEncode(obj.value);
		return false;
	}
}

function flashWrite(flashfile,x,y,flashvar) {
	document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='"+ x +"' height='"+ y +"'>");
	document.write("<param name='movie' value='"+ flashfile +"' />");
	document.write("<param name='quality' value='high' />");
	document.write("<param name='flashvars' value='"+ flashvar +"' />");
	document.write("<param name='wmode' value='transparent' />");
	document.write("<embed src='"+ flashfile +"' wmode='transparent' ");
	document.write("flashvars='"+ flashvar +"'  ");
	document.write("quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+ x +"' height='"+ y +"'></embed>");
	document.write("</object>");
}

function printYear() {
	var today = new Date();
	document.write(today.getFullYear());
}

function writeEmail(email) {
	document.write('<a href="mailto:'+email+'">'+email+'</a>');
}

function changeTextSize(textSize) {
	document.body.className = 'site ' + textSize;
	return false;
}

function printPage() {
	window.print();
	return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

window.onunload = function(e) {
	var textSize = document.body.className.split(' ')[1];
	createCookie('style', textSize, 365);
}
