
// ----------------------------------- //
// General JavaScript for Trophy Ridge //
// ----------------------------------- //

//calls current year in footer
var date = new Date();
var year = date.getYear();
if (year < 1000) year += 1900;

//see if this is a Microsoft browser
var isIE = navigator.appName.indexOf("Microsoft")!=-1;

// Load jQuery & jQuery UI, from Google APIs
google.load("jquery", "1.4.2");
google.load("jqueryui", "1.8");


//on page load
google.setOnLoadCallback(function () {

    /* run pageOnLoad function if it exists */
    if (typeof pageOnLoad == 'function') {
        pageOnLoad();
    }

    /* load ShareThis (if it is on the page -- SSL pages have it turned off */
    if ($("div.share").length > 0) {
        loadJavaScriptFile(ShareThisUrl, initShareThis, true, true);
    }

    //set rollovers for images and input images
    setImageRollovers();
	
	setCustomSelectors();
    setLinks();

    //load jQuery Plugins dynamically
    loadJavaScriptFile('/scripts/jquery/jquery-pngfix.js', fixPagePngs, true, true);
    loadJavaScriptFile('/scripts/jquery/jquery.bgiframe.min.js', jsnull, true, true);

});



//simplifiy object calls
var df = document.forms;
var di = document.images;


function changeImage(imgID,imgURL)
{
   if (document.images)
   {
	 document.images[imgID].src = imgURL;
   }else{
	document.getElementById( imgID).src = imgURL;
  }
}

/* **************************************
			jQuery Items
   ************************************** */
function loadJavaScriptFile(scriptName, callback, async_bool, cache_bool) {
    /* Loads and executes a JavaScript file */
    /* set async and cache to what's sent in */
    $.ajaxSetup({
        async: async_bool,
        cache: cache_bool
    });

    /* load script */
    $.getScript(scriptName, callback);

    /* reset async and cache to default */
    $.ajaxSetup({
        cache: false,
        async: true
    });
}

/* Auto Link Management */
function setCustomSelectors() {
    /* create custom jquery selectors to match external links and mailto links */
    jQuery.extend(jQuery.expr[":"], {
        external: function (obj) {
            return obj.href && !obj.href.match(/^mailto\:/) && !obj.href.match(/^javascript\:/) && (obj.hostname && (obj.hostname != location.hostname));
        },
        mailto: function (obj) {
            return obj.href && obj.href.match(/^mailto\:/);
        },
        pdf: function (obj) {
            return obj.href && obj.href.match(/\.pdf$/);
        },
        zip: function (obj) {
            return obj.href && obj.href.match(/\.zip$/);
        }
    });
}

function setLinks() {
    /* Add CSS class to all external or mailto links */
    $("a:external").addClass("external");
    $("a:mailto").addClass("mailto");
    $("a:pdf").addClass("pdf");
    $("a:zip").addClass("zip");

    /* Add Google Analytics tracking to them */
    setLinkAnalytics();
}

function fixPagePngs(){
	//fix for IE 5.5 and 6 PNG transparency issues
	//may cause problems with overlayed links or form items, doesn't work for tiled CSS backgrounds
	$("img[src$=.png]").pngfix();
}

function closeDialogsByOverlay(event) {
    /* find any active dialog and trigger it's close button when overlay is clicked */
    var closeButton = $(".ui-dialog:visible").find(".ui-dialog-titlebar-close");
    closeButton.trigger("click");
    $(".ui-widget-overlay").unbind("click", closeDialogsByOverlay);
}

function jsnull(){
	//do nothing
	//this function allows entry of href attribute without causing a page jump
	//<a href="javascript:jsnull();" onclick="jsDoSomething();">do something</a>
}


// ------------------------------- //
//       Window Popup Function     //
// ------------------------------- //

var w;
function popup(target, width, height, scrollbars, hidemenus){
 	var settings;
 	var menus;
 	var scrolls;
	
 	if (hidemenus) { menus = ",status=yes,toolbar=no,menubar=no,location=no"; }else{ menus = ",status=yes,toolbar=yes,menubar=yes,location=yes"; }
	
 	if (scrollbars) { scrolls = ",scrollbars=yes"; }else{ scrolls = ""; }
	
 	settings = 'width=' + width + ',height=' + height + ',resizable=yes' + scrolls + menus;
	
 	w = window.open(target,'bearpop',settings);
 	w.focus();
}


// ----------------------------------- //
//   Display and Visibility Functions  //
// ----------------------------------- //
function toggleDisplay(divID)
{
	if (document.layers)
		{
		var thisDiv = document.layers[divID];
		var newValue = (thisDiv.display != 'block') ? 'block' : 'none';
		thisDiv.display = newValue;
		}
	else if (document.all)
		{
		var thisDiv = document.all[divID];
		var newValue = (thisDiv.style.display != 'block') ? 'block' : 'none';
		thisDiv.style.display = newValue;
		}
	else if (document.getElementById)
		{
		var thisDiv = document.getElementById(divID);
		var newValue = (thisDiv.style.display != 'block') ? 'block' : 'none';
		thisDiv.style.display = newValue;
		}
}

function toggleVisible(divID)
{
	if (document.layers)
		{
		var thisDiv = document.layers[divID];
		var newValue = (thisDiv.visibility != 'show') ? 'show' : 'hide'
		thisDiv.visibility = newValue;
		}
	else if (document.all)
		{
		var thisDiv = document.all[divID];
		var newValue = (thisDiv.style.visibility != 'visible') ? 'visible'	: 'hidden';
		thisDiv.style.visibility = newValue;
		}
	else if (document.getElementById)
		{
		var thisDiv = document.getElementById(divID);
		var newValue = (thisDiv.style.visibility != 'visible') ? 'visible' : 'hidden';
		thisDiv.style.visibility = newValue;
		}
}

function setDisplay(divID,setType){
	if (document.layers){
		document.layers[divID].display = setType;
		}
	else if (document.all){
		document.all[divID].style.display = setType;
		}
	else if (document.getElementById){
		document.getElementById(divID).style.display = setType;
		}
}

// ------------------------------- //
//   Email Addy Hiding Functions   //
// ------------------------------- //
at = '@';
mailer = 'mailto:';

function getmail(name, domain, suffix, text, titletext){ //user supplied email parts and alt text
	hiddenMail=(name + at + domain + '.' + suffix);
	if (! text || text == ''){ //if no text then use full email address
		text = hiddenMail;
	}
	if (! titletext || titletext == ''){
		email = '<a href="' + mailer + hiddenMail + '" >' + text + '</a>';
	}else{
		email = '<a href="' + mailer + hiddenMail + '" title="' + titletext + '" >' + text + '</a>';
	}
	document.write(email);
}


/* **************************************
			Image Rollovers
   ************************************** */
function setImageRollovers(){
	$("img[src*=_nm.]").each(function(index, domElement) { 
		setRolloverEvents($(domElement));
		setRolloverPreloads($(domElement))
	});	
	$("input[type=image][src*=_nm.]").each(function(index, domElement) {	
		setRolloverEvents($(domElement));
		setRolloverPreloads($(domElement));
	});
}

function setRolloverEvents(jqueryobj){
	var imgsrc = jqueryobj.attr("src");
	var imgsrcON = imgsrc.replace(/_nm/ig,"_hv");
	
	jqueryobj.mouseover(function(){
		if(jqueryobj.attr("src").indexOf("-av.") < 0) {jqueryobj.attr("src", imgsrcON)};
	});
	jqueryobj.mouseout(function(){
		if(jqueryobj.attr("src").indexOf("-av.") < 0) {jqueryobj.attr("src", imgsrc)};
	});
}

function setRolloverPreloads(jqueryobj){
	rollsrc = jqueryobj.attr("src");
	rollON = rollsrc.replace(/_nm/ig,"_hv");
	$("<img>").attr("src", rollON);
}




function hideAllSelectBoxes(){
	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {selects[i].style.visibility = "hidden";}
}

function showAllSelectBoxes(){
	// reveal select boxes that were hidden from IE	
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {selects[i].style.visibility = "visible";}	
}

/* **************************************
        ShareThis
************************************** */

var ShareThisUrl = "http://w.sharethis.com/button/sharethis.js#publisher=134e3a85-e321-4195-bdb1-36073f59d148&amp;type=website&amp;buttonText=&amp;post_services=email%2Cfacebook%2Ctwitter%2Cmyspace%2Cdigg%2Csms%2Cwindows_live%2Cdelicious%2Cstumbleupon%2Creddit%2Cgoogle_bmarks%2Clinkedin%2Cbebo%2Cybuzz%2Cblogger%2Cyahoo_bmarks%2Cmixx%2Ctechnorati%2Cfriendfeed%2Cpropeller%2Cwordpress%2Cnewsvine%2Cxanga%2Cblinklist%2Ctwine%2Ctwackle%2Cdiigo%2Cfark%2Cfaves%2Cmister_wong%2Ccurrent%2Clivejournal%2Ckirtsy%2Cslashdot%2Coknotizie%2Ccare2%2Caim%2Cmeneame%2Csimpy%2Cblogmarks%2Cn4g%2Cbus_exchange%2Cfunp%2Csphinn%2Cfresqui%2Cdealsplus%2Ctypepad%2Cyigg&amp;button=false&amp;onmouseover=false&amp;tracking=google";


function initShareThis(idprefix, title, url) {
    //if no parameters are sent in, this defauts to loading a primary page share
   //if parameters are sent in, it allows for multiple shares on a given page via ID prefixes
   try {
       var shared_object;
       var thistitle = document.title;
       var thisurl = document.location.href;
       var thisIDPrefix = "ck";

       if (title) {thistitle = title;}
       if (url) { thisurl = url; }
       if (idprefix) { thisIDPrefix = idprefix; }

       shared_object = SHARETHIS.addEntry({
           title: thistitle,
           url: thisurl
       });


       shared_object.attachButton($("#" + thisIDPrefix + "_sharethis").get(0));
       shared_object.attachChicklet("email", $("#" + thisIDPrefix + "_email").get(0));
       shared_object.attachChicklet("facebook", $("#" + thisIDPrefix + "_facebook").get(0));
       shared_object.attachChicklet("twitter", $("#" + thisIDPrefix + "_twitter").get(0));

        /*force ShareThis to fire it's document ready method*/
        SHARETHIS.onReady();
        
    } catch (e) {
        $(".share").html("<span class=\"white\">sharing tools did not load</span>");
    }
}
