$(document).ready(function(){
    $('.wp-caption a').fancybox();
    $('.blog_entry p a img').parent().fancybox();
    $('.termsAndConditions').fancybox({autoDimensions:false,width:900, height:500});

    $('#sign-up-email').validateEmail('sign-up-submit');

    $('#sign-up-form').bind('submit',function(){				// event listener for the form submit
	$.post('/ajax/sign-up/',{e:$('#sign-up-email').val(),s:sid},function(){	// send the email to the database
	    $('#sign-up #initial').fadeOut('slow',function(){
		$('#sign-up #thank-you').fadeIn('slow',function(){
		    $(location).attr('href','/newsletter/thank-you/');
		});
	    });
	});
	return false;
    });

    //trace(getPropertyValue('translate3d','translate3d(10px,15em,11pt) rotate(12deg) translate(12em, 14px)'));
    //trace(getPropertyValue('rotate','translate3d(10px,15em,11pt) rotate(12deg) translate(12em, 14px)'));
    //trace(getPropertyValue('translate','translate3d(10px,15em,11pt) rotate(12deg) translate(12em, 14px)'));
});


/**
 * Return all the values inside () for a specified property in a string
 * Will return an array of numeric matches, or false on failure
 * @param property
 * @param styles
 * @return mixed
 */
/*
function getPropertyValue(property, styles) {
    //var pattern = new RegExp('/translate3d\(([0-9a-z\,]+)\)/');
    var pattern = new RegExp(property+'\\(([^\\)]+)');
    var matches = styles.match(pattern);

    if(matches != undefined && matches[1] != undefined) {
	pattern = new RegExp('([0-9]+)','g');
	return matches[1].match(pattern);
    }

    return false;
}*/

$(window).load(function(){
   
});

jQuery.fn.extend({
    validateEmail: function(submitButton) {

	var dst = this;								// reference to the email field for later manipulation
	var fieldTimeout = false;						// timeout var so that we don't validate at every keystroke, only when they pause

	$(this).bind('click',function(){					// first click clears the text hint
	    if($(this).val() == 'enter email') {
		$(this).val('');
	    }
	});

	$(this).bind('keyup',function() {
	    $(this).removeClass();
	    $(this).addClass('validating');

	    // alter the submit button if applicable
	    if (submitButton) {
		$(submitButton).addClass('pending');
	    }
	    clearTimeout(fieldTimeout);
	    fieldTimeout = setTimeout(function(){
		    $.post('/ajax/validate-email/',{ data: $(dst).val() },function(data){
			
			$(submitButton).removeClass();
			$(dst).removeClass();

			if(data == 1) {
			    $(dst).addClass('ok');
			} else {
			    $(submitButton).addClass('disabled');
			    $(dst).addClass('error');
			}
		    });
		},
		500
	    );
	});
    }
});

/**
 * jQuery Cookie plugin
 *
 * Copyright (c) 2010 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

/**
 * Fuck-up prevention; checks to see if the console exists before posting to it, so that console.log messages that are accidentally left in do not break a page when run live
 * Called "trace" as I have been doing a lot of AS3 today and I keep typing it anyway
 * @param message
 * @return void
 */
function trace(message) {
    if(typeof(console) != 'undefined' && typeof(console.log) == 'function') {
	console.log(message);
    }
}
