//-- Load on page ready
$(document).ready( function() {
	PEPS.rollover.init();
	
	//-- Popup ready
	/*
	$('#popOverlay input:eq(0)').click(function (e) {
		e.preventDefault();
		$('#popContainer').modal();
	});
	*/
	
	//when key is pressed in the textbox
	$("#dogAge").keypress(function (e) {
	  //if the letter is not digit then display error and don't type anything
	  if ( e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
	    //$("#errmsg").html("Digits Only").show().fadeOut("slow");
	    return false;
	  }
	});
	
	//-- Adds listener to file upload browse field
	$("input[@type=file]").change(function() {
		$("#uploadTxt").attr("value", $("#fileToUpload").attr("value"));
	});
});

//-- Ajax File Upload
function updatePage(results) {
	var xml = parseXML(results);
	
	var success = xml.getElementsByTagName("success")[0].childNodes[0].nodeValue;
	var msg = xml.getElementsByTagName("msg")[0].childNodes[0].nodeValue;
	var photo = xml.getElementsByTagName("photo")[0].childNodes[0].nodeValue;

	$("div#imgError").html(msg);
	$("#photoURL").attr("value", photo);
}

function parseXML(xmlResults) {
	try {
		//Internet Explorer
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.loadXML(xmlResults);
		return xmlDoc;
	}
	catch(e) {
		try {
			//Firefox, Mozilla, Opera, etc.
			parser = new DOMParser();
			xmlDoc = parser.parseFromString(xmlResults, "text/xml");
			return xmlDoc;
		}
		catch(e) {
	  		alert(e.message);
	  		return;
		}
	}
}


//-- Store
//---- Toggle Canvas
function toggleCanvas(item_id) {
	var item = $("#itemSelect").attr("value");

	if (item == "18x24") {
		$("#item_number").attr("value", item_id);
		$("#amount").attr("value", "80.00");
		$("#weight").attr("value", "2");
	} else if (item == "30x40") {
		$("#item_number").attr("value", item_id);
		$("#amount").attr("value", "150.00");
		$("#weight").attr("value", "3.5");
	}
}

//-- Submit Speak! Nominate Form
function submitSpeak() {
	desc = $("#description").val();
	desc = desc.replace(/\n/g, "<br />");
	desc = desc.replace(/\n\n+/g, '<br /><br />');

	$.ajax({
		url: "inc/post-speak.php",
		cache: false,
		data: 	"firstName=" + $("#firstName").val() + "&lastName=" + $("#lastName").val() + "&emailAddress=" + $("#emailAddress").val() + "&dogName=" + $("#dogName").val() + "&dogAge=" + $("#dogAge").val() + "&breed=" + $("#breed").val() + "&photoURL=" + $("#photoURL").val() + "&description=" + desc + "&termsCheck=" + $('#termsCheck').attr("checked"),
		success: function(response){
			if (response == "success") {
				$("div#formBody").html("<h1>Thank you for participating!</h1>").text();
			} else {
				$("div#submitError").html(response).text();
			}
		}
	});
}

//-- Subscribe to mailing list
function submitFunction(path) {
	$.ajax({
		url: path + "inc/subscribe.php",
		cache: false,
		data: "email=" + $("#email").val(),
		success: function(response) {
			$("#email").val(response);
		}
	});
}

//-- Clear text input field --//
var mail_default_txt = 'Enter Your Email Address';
function clearText(thefield) {
	if (thefield.defaultValue == thefield.value) {
		thefield.value = "";
	} else if (thefield.value == '') {
		thefield.value = thefield.defaultValue;
	}
}

//-- Div toggle visiblity
function divOn(divIn) {
	$(divIn).show();
}

function divOff(divIn) {
	$(divIn).hide();
}

//-- Rollover image swap --//
PEPS = {};
PEPS.rollover = {
   init: function() {
      this.preload();
      $('.ro').mouseover(this.over);
      $('.ro').mouseout(this.out);
   },

   preload: function() {
      $('.ro').each(function(key, elm) { $('<img>').attr('src', PEPS.rollover.newimage($(this).attr('src'))); });
   },
   
   over: function() {
      $(this).attr('src', PEPS.rollover.newimage($(this).attr('src')));
   },
   
   out: function() {
      $(this).attr('src', $(this).attr('src').replace(/_o/, ''));
   },
   
   newimage: function(src) {
      return src.substring(0, src.search(/(\.[a-z]+)/)) + '_o' + src.match(/(\.[a-z]+)/)[0];
   }
}

//-- Tracking
function tracking(param1, param2, param3)
{	
	var track = "/";
	
	if (param1)
	{
		track += param1 + '/';
	}
	
	if (param2)
	{
		track += param2 + '/';
	}
	
	if (param3)
	{
		track += param3;
	}

	pageTracker._trackPageview(track);
	//pageTracker._trackEvent(param1, param2, param3);
}