/*
   jQuery (character and word) counter
   Copyright (C) 2009  Wilkins Fernandez

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/(function(b) { b.fn.extend({ counter: function(a) { a = b.extend({}, { type: "char", count: "down", goal: 140 }, a); var d = false; return this.each(function() { function e(c) { if (typeof a.type === "string") switch (a.type) { case "char": if (a.count === "down") { g = " character(s) left"; return a.goal - c } else if (a.count === "up") { g = " characters (" + a.goal + " max)"; return c } break; case "word": if (a.count === "down") { g = " word(s) left"; return a.goal - c } else if (a.count === "up") { g = " words (" + a.goal + " max)"; return c } break; default: } } var g, f = b(this); b('<div id="' + this.id + '_counter"><span>' + e(b(f).val().length) + "</span>" + g + "</div>").insertAfter(f); var i = b("#" + this.id + "_counter span"); f.bind("keyup click blur focus change paste", function(c) { switch (a.type) { case "char": c = b(f).val().length; break; case "word": c = f.val() === "" ? 0 : b.trim(f.val()).replace(/\s+/g, " ").split(" ").length; break; default: } switch (a.count) { case "up": if (e(c) >= a.goal && a.type === "char") { b(this).val(b(this).val().substring(0, a.goal)); d = true; break } if (e(c) === a.goal && a.type === "word") { d = true; break } else if (e(c) > a.goal && a.type === "word") { b(this).val(""); i.text("0"); d = true; break } break; case "down": if (e(c) <= 0 && a.type === "char") { b(this).val(b(this).val().substring(0, a.goal)); d = true; break } if (e(c) === 0 && a.type === "word") d = true; else if (e(c) < 0 && a.type === "word") { b(this).val(""); d = true; break } break; default: } f.keydown(function(h) { if (d) { this.focus(); if (h.keyCode !== 46 && h.keyCode !== 8) if (b(this).val().length > a.goal && a.type === "char") { b(this).val(b(this).val().substring(0, a.goal)); return false } else return h.keyCode !== 32 && h.keyCode !== 8 && a.type === "word" ? true : false; else { d = false; return true } } }); i.text(e(c)) }) }) } }) })(jQuery);


/*
 * Url preview script 
 * powered by jQuery (http://www.jquery.com)
 * 
 * written by Alen Grakalic (http://cssglobe.com)
 * 
 * for more info visit http://cssglobe.com/post/1695/easiest-tooltip-and-image-preview-using-jquery
 *
 */
 
this.screenshotPreview = function(){	
	/* CONFIG */
		
		var xOffset = 10;
		var yOffset = 30;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
    });	
	$("a.screenshot").mousemove(function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


// starting the script on page load
$(document).ready(function(){
	screenshotPreview();
	
    $('#slideshow').cycle({
        delay: 2000,
        timeout: 8000,
        speed: 2000
    });
	
	$("#message").counter({
	count: 'down', 
	goal: 100
	});
	
	
// Quilt Memorial Form Send
$('#quilt').submit(function(){
    var data = {
    	firstname : $('#firstname').val(),
    	lastname : $('#lastname').val(),
    	birthyear : $('#birthyear').val(),
    	deathyear : $('#deathyear').val(),
    	message : $('#message').val(),
    	author_email : $('#author_email').val()
    };

	var email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
	var emailToVal = $("#author_email").val(); 
    
	if($('#firstname').val() == '' || $('#lastname').val() == ''){
		$('.errormsg').detach();
		$('#wadp').after('<p style="color:#b42323;" class="errormsg">Please fill out First and Last name.</p>');
		return false
		
    } else if ((emailToVal != 0) && (!email_regex.test(emailToVal))) {
			$('.errormsg').detach();
			$('#wadp').after('<p style="color:#b42323;" class="errormsg">Please enter a valid email address.</p>');
			return false
    
    } else {
        $('#quilt').replaceWith('<img src="/images/loading.gif"/>');
        var thank_you_message = "<div class='thank_you_message'><p>Thank you for contributing to the We Make The Change Virtual Memorial. We're currently reviewing your submission. Once your submission has been approved, you can view it by <a href='/virtual-memorial/'>clicking here</a>.</p></div>";
        $.ajax({
            url: "/process_memorial.php",
            type: 'POST',
            data: data,
            success: function(html){
                $('#wad').html(thank_you_message);
            }
        });
        return false;
    }
});

$(function() {
    // Add an event listener to the click event for each of your thumbnails
    $('.video-thumbnail').click(function(e) {

        // changes src of the iframe to the one we stored in the clicked thumbnail
        $('#video-iframe').get(0).src = this.getAttribute('video-uri');

        // stops default browser behaviour of loading a new page
        e.preventDefault(); 
    });
});

	
});




