/*
The Skylark $ Extension Library
Copyright (c) 2011 Paul Wilson http://www.aintgoin2goa.com/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*Generated on: 14:38, 23rd February 2011

Scripts Included:

base.js
stickyfooter.js
form.js
*//*

Skylark Base 

The Base Code, used across all other skylark plugins

Part of the Skylark library.

Copyright (c) 2011 Paul Wilson http://www.aintgoin2goa.com/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version 1.3 - February 17th 2011

TESTED IN:
Chrome 9.0 (win)
Firefox 3.6 (win)
Safari 5.0 (win)
Opera 11.0 (win)
IE9
IE8
IE7
IE6
Android

HISTORY:
1.3 - Added tests for CSS3 properties
1.2.3 - Bug Fix - added ; at  end of object declarations
1.2.2 - Added multiple image Loader
1.2.1 - Added Single Image Loader
1.1 - Added XML Loader / Parser
1.0 - initial version

ISSUES:
Testing for column-count in Opera returns wrong value


CONTENTS:

setBoundInterval - when you need to call setInterval bound to a specific object (not the window)

	SKYLARK.setBoundInterval(function:Function, duration:Number, bind:Object):Number

testFor.placeholder - first of a number of tests.  This ones tests for support of placeholder attribute (used by form.js)

	SKYLARK.testFor.placeholder():Boolean
	
Load.xml - loads XML file via xmlhttprequest and parses it into a js object

	SKYLARK.Load.xml(url:String, callback:Function):Object
				



*/


var SKYLARK = {};


SKYLARK.setBoundInterval = function(func, dur, bind){
	return setInterval( function(){func.call(bind)}, dur );
};

SKYLARK.setBoundTimeout = function(func, dur, bind){
	return setTimeout( function(){ func.call(bind)}, dur);	
};

SKYLARK.testFor = {

	placeholder: function(){
		 var i = document.createElement('input');
  		return 'placeholder' in i;
	},
	
	_capitalizeProperty: function(prop){
		if(prop.indexOf('-') != -1 ){
			var arr = prop.split('-');
			arr[0] = arr[0].charAt(0).toUpperCase() + arr[0].slice(1);
			arr[1] = arr[1].charAt(0).toUpperCase() + arr[1].slice(1);
			var ret = arr[0]+arr[1];
		}else{
			var ret = prop.charAt(0).toUpperCase() + prop.slice(1);	
		}
		return ret;
	},
	
	_capitalizePropertyIE9: function(prop){
		if(prop.indexOf('-') != -1 ){
			var arr = prop.split('-');
			arr[1] = arr[1].charAt(0).toUpperCase() + arr[1].slice(1);
			var ret = arr[0]+arr[1];
		}else{
			var ret = prop;
		}
		return ret;
	},
	
	property: function(prop){
		var prop1 = SKYLARK.testFor._capitalizeProperty(prop);
		var prop2 = SKYLARK.testFor._capitalizePropertyIE9(prop);
		var tests = [prop1, 'Moz'+prop1, 'Webkit'+prop1, 'O'+prop1, prop2];
		var res = false;
		$.each( tests, function(i, v){
			if( document.body.style[v] !== undefined ) res = true;						
		});
		return res;
	}
};


SKYLARK.Load = {
	
		
	xml: function(url, callback){
		$.get(url, '', function(data){
			var p = new SKYLARK.XMLParser();
			var obj =  p.parseXML(data);
			callback(obj);
		});
	},
	
	image: function(url, callback){
		var img = new Image();
		img.onload = callback;
		img.src = url;
	},
	
	images: function(urls){
		var counter = 0;
		var max = urls.length;
		$.each(urls, function(i, url){
			var img = new Image();
			img.onload = function(){
				counter++;
				$('body').trigger('Load.imageLoaded', [this, counter-1]);
				if(counter == max) $('body').trigger('Load.allImagesLoaded');
			}
			img.src = url;
		});
	}
};


SKYLARK.XMLParser = function(){
	this.xmlDocument = '';
	this.xmlObject = {};
	this.xmlEscape = arguments.length == 1 ? arguments[0] : false;
	
	this.parseXML = function(xml){
		if( !xml.documentElement ){ return false; }
		var children = xml.documentElement.childNodes;
		for(var i = 0; i< children.length; i++){
		 if(children[i].nodeType == 1)	this.parseNode(children[i], this.xmlObject);
		}
		return this.xmlObject;
	};
	
	this.parseNode = function(node, parent){
		var nodeObj = {};
		var attrs = node.attributes;
		for(var i=0; i < attrs.length; i++){
			if( this.xmlEscape){
				nodeObj['@'+attrs[i].name] = attrs[i].value;						 
			}else{
				nodeObj[attrs[i].name] = attrs[i].value;	
			}	
		}
		for(var j=0; j < node.childNodes.length; j++){
			var v = node.childNodes[j];
			if( v.nodeType == 3){
				nodeObj.$ = $.trim(v.nodeValue);
			}else if( v.nodeType == 4){
				nodeObj.$ = $.trim(v.nodeValue);
			}else{
				this.parseNode(v, nodeObj);	
			}
		}
		if( parent[node.nodeName] ){
			if( parent[node.nodeName] instanceof Array ){
				parent[node.nodeName].push(nodeObj);	
			}else{
				var p = parent[node.nodeName];
				parent[node.nodeName] = [p, nodeObj];
			}
		}else{
			parent[node.nodeName] = nodeObj;	
		}
	};
};

/*

Sticky Footer

Makes the footer stick to the bottom of the page (unless content is longer than screen height)

Part of the Skylark library.

Copyright (c) 2011 Paul Wilson http://www.aintgoin2goa.com/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version 1.0.1 - February 14th 2011

Tested in:
Chrome 8/.0
Firefox 3.6
IE 9
IE 6/7/8 (using IETester) 
Safari 5.0 (windows)
Opera 10.63

HISTORY:
1.0.1 - BUGFIX, added ; to end of all objects
1.0 - initial version

USAGE
 (after DOM is ready)
	$(footer_selector).stickyFooter(element_to_expand);
	
	footer_selector = the footer element (usually ('#footer')
	element_to_expand - the main content container
	

*/

$.stickyFooterFunction = function(footer, selector){
	$(selector).css('height', 'auto');
	$(footer).attr( 'footerElement', 'yes' );
	var footerHeight, docHeight, windowHeight;
	footerHeight = docHeight = 0;
	windowHeight = $(window).height();
	$('body').children().each( function(){
		if( $(this).attr('footerElement') )  footerHeight = $(this).height();
		docHeight = docHeight + $(this).height();
	});
	var diff = windowHeight - docHeight;
	
	if( diff > 0 ) $(selector).height($(selector).height() + diff);
};


$.fn.stickyFooter = function(selector){
	var el = this;
	$.stickyFooterFunction(el, selector);							
	$(window).resize( function(){
		$.stickyFooterFunction(el, selector);						   
	});
};
/*

Skylark Form Validator

Automatic Form Validation and placeholder functionality for those browsers without it

Part of the Skylark library.

Copyright (c) 2011 Paul Wilson http://www.aintgoin2goa.com/

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version 1.1.1 - February 14th 2011 (added placeholder stuff and renamed to form.js

Tested in:
Chrome 8/.0
Firefox 3.6
IE 9
IE 6/7/8 (using IETester) 
Safari 5.0 (windows)
Opera 10.63

HISTORY:
1.1.1 - BUGFIX: removed erranr console.log call
1.1 - Added placeholder stuff and renamed to form.js
1.0 - Initial Version


USAGE

1) Add the attribute validate="yes" to the FORM tag
2) Add the attribute validation="[type_of_validation]" to the form field tag
3) To send the form you must add a listener to the form's 'validationPassed' event, eg..

$('form').bind('validationPassed', function(){
	this.send();											
});

4) to set a custom error message and the attribute errorMessage="your_message_here" to the form field tag

Validation Types:

required
email
url
matches (matches to regexp)

*/

(function($){
$.Validate= {
	
	options: {
		errorClass : 'fieldWithErrors'
	},
	
	messages: {
		required : 'This field is required',
		email : 'This is not a valid email',
		matches : 'Value does not match',
		url : 'This is not a valid url'
		
	},
	
	required : function(val){
		return $.trim(val) == "" ? false : true;
	},
	
	email: function(val){
		var regexp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		return regexp.test(val);
	},
	
	matches: function(val, test){
		return val == test ? true : false;	
	},
	
	url: function(val){
		var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
		return regexp.test(val);
	}
}


$.fn.validate = function(){
	
	$(this).submit( function(e){
		e.preventDefault();
		$(this).attr('validationpassed', 'yes');
		var form = $(this);
		var val, msg;
		$(this).find('input[validation], select[validation], textarea[validation]').each( function(){
			$($(this).parent()).removeClass($.Validate.options.errorClass);		
			$($(this).parent()).find('.errorMessage').remove();
			if( $(this).attr('placeholder') == this.value ) this.value = '';
			val = this.value;
			var str = $(this).attr('validation').split(' ');
			var func = str[0];
			var arg = str.length > 1 ? str[1] : '';
			if( arg.indexOf('$') == 0 ){
				arg = arg.replace('$', '');
				arg = $(arg).attr('value');
			}
			if( $.Validate[func](val, arg) === false ){
				form.attr('validationpassed', 'no');
				$($(this).parent()).addClass($.Validate.options.errorClass);
				if( $(this).attr('errorMessage') == undefined ){
					msg = $.Validate.messages[func];
				}else{
					msg = 	$(this).attr('errorMessage')
				}
				$($(this).parent()).prepend('<p class="errorMessage">'+msg+'</p>');
			}
		});
		if( $(this).attr('validationpassed') == 'yes' ){
			$(this).trigger('validationPassed');	
		}
		
	});
}

$( function(){ 
	if( !SKYLARK.testFor.placeholder() ){	
	 
		$('input[placeholder]').each( function(){
			$(this).focus( function(){
				if( this.value == $(this).attr('placeholder') ) this.value = '';						
			});
			$(this).blur( function(){
				if( $.trim(this.value) == '' ) this.value = $(this).attr('placeholder');						
			});
			if( $.trim(this.value) == '' ){
				this.value = $(this).attr('placeholder');			
			}
		});
		$('textarea[placeholder]').each( function(){
			$(this).focus( function(){
				if( this.value == $(this).attr('placeholder') ) this.value = '';						
			});
			$(this).blur( function(){
				if( $.trim(this.value) == '' ) this.value = $(this).attr('placeholder');						
			});
			if( $.trim(this.value) == '' ){
				this.value = $(this).attr('placeholder');			
			}
		});
	}
	$('form[validate]').validate(); 
			
			
});


		  })($);





