// Modal Window that will be displaying the results.
var modalWindow = {
	parent:"body",
	windowId:null,
	content:null,
	width:null,
	height:null,
	close:function()
	{
		$(".modal-window").remove();
		$(".modal-overlay").remove();
	},
	open:function()
	{
		var modal = "";
		modal += "<div class=\"modal-overlay\"></div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
		modal += this.content;
		modal += "</div>";	

		$(this.parent).append(modal);

		$(".modal-window").append("<a class=\"close-window\"></a>");
		$(".close-window").click(function(){modalWindow.close();});
		$(".modal-overlay").click(function(){modalWindow.close();});
	}
};

function displayModalWindow(content) {
    modalWindow.windowId = "myModal";
	modalWindow.width = 380;
	modalWindow.height = 400;
	modalWindow.content = "	<div>";
	modalWindow.content += content;
	modalWindow.content += "<div class=\"modal-button \"><a href=\"#\" class=\"modal-button\">close window</a></div>";    	
	modalWindow.content += "</div>";
	modalWindow.open();
	$(".modal-button").click(function () {
    	modalWindow.close();
	    return false
        });
	return false;
}
/* TURN ON OR OFF AS NEEDED */

/*

==========================
HOMEPAGE SLIDESHOW USING CYCLE PLUGIN - http://malsup.com/jquery/cycle/download.html
==========================

*/
 
$(document).ready(function() {
    $('.animation').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout: 7000 // milliseconds between slide transitions (0 to disable auto advance)
	});
	// bind the submit handler to the submit of the tool class
	$('.tool form').submit(function() {
	    var score = 0 
	    // get the list of names so we can tell that all of them have been checked:
        var names = {};
        $('.tool form input:radio').each(function() { // find unique names
              names[$(this).attr('name')] = true;
        });
        var count = 0;
        $.each(names, function() { // then count them
              count++;
        });
        if($('.tool form input:radio:checked').length != count) {
            displayModalWindow("<p>Please answer all questions before submitting.</p>");
            return false;
        }

	    // on the submission, get the results added up
	    for( var i=0; i < this.elements.length; i++) {
	        if ($(this.elements[i]).is(":checked")) {
	            score += parseInt($(this.elements[i]).val());
            }
        }
        var showscore = $(this.elements['showscore']).val();
        //alert("Score:" + score);
        $('.toolresult').each(function() {
            //figure out the score of each div
            var limit_string = $(this).attr('id').split("_")[1];
            var top = parseInt(limit_string.split("-")[0]);
            var bottom = parseInt(limit_string.split("-")[1]);
            if ((score >= parseInt(limit_string.split("-")[0])) && (score <= parseInt(limit_string.split("-")[1]))) {
                var displaycontent = '';
                if (showscore > 0) {
                    displaycontent = "<p class='score'>Your score is: " + score + "<p>";
                }
                displayModalWindow(displaycontent + $(this).html());
            }
        });
        return false;
	});
});

/*

==========================
IMAGE SWAPPING ON ROLLOVER
==========================

*/

/*

if (document.images) {
	ph_upstairs = new Image
	ph_upstairs.src = "i/ph_upstairs.jpg"
}
else { 
	ph_upstairs = ""

	document.photo = "" 
}

REQURIES THIS CODE IN XHTML

1. ASSIGN NAME VARIABLE TO IMAGE TO SWAP OUT (PHOTO HERE)
<img src="i/ph_river.jpg" width="470" height="351" border="0" name="photo" alt="the samoset retreat photo gallery" />

2. ASSIGN MOUSEOVER AND SOURCE TO THE THUMBNAIL IMAGE
<img src="i/ph_upstairs_th.jpg" width="63" height="47" border="0" alt="upstairs" class="th" onmouseover="document.photo.src=ph_upstairs.src" />

*/


/*
========================
SHIPPING SAME AS BILLING
========================

function FillShipping(f) {
if(f.same.checked == true) {
    f.sNm.value = f.bNm.value;
    f.sCn.value = f.bCn.value;
	f.sAd.value = f.bAd.value;
	f.sCt.value = f.bCt.value;
	f.sSt.value = f.bSt.value;
	f.sZc.value = f.bZc.value;
	f.sCy.value = f.bCy.value;
	f.sEm.value = f.bEm.value;
	f.sPh.value = f.bPh.value;
	f.sFn.value = f.bFn.value; 
    }
}

REQUIRES THIS CODE IN THE XHTML

<input type="checkbox" name="same" id="same_address" onclick="FillShipping(this.form)" /> Check if shipping address is the same as billing address

The values above sNm / bNm correspond to the name value of the input form element.  They can be changes as needed.

*/


/*
============================
EXTERNAL LINKS IN NEW WINDOW
============================

// this script adds class="external" to external links so you can style it to include the external icon



this.blankwin = function(){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	var a = document.getElementsByTagName("a");	
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return (href.indexOf("http://")!=-1 && href.indexOf(hostname)==-1) ? true : false;				
	};
	this.set = function(obj){
		obj.target = "_blank";
		obj.className = "external";
	};	
	for (var i=0;i<a.length;i++){
		if(check(a[i])) set(a[i]);
	};		
};

// script initiates on page load. 

this.addEvent = function(obj,type,fn){
	if(obj.attachEvent){
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn](window.event );}
		obj.attachEvent('on'+type, obj[type+fn]);
	} else {obj.addEventListener(type,fn,false);};
};
addEvent(window,"load",blankwin);

*/


/*
===================
DROPDOWN NAVIGATION
===================


function getNewPage(form) {
	var f = form.urlList;
	var selectedUrl = f.options[f.selectedIndex].value;
	return location.href = selectedUrl;
}

*/
