/**
 * Cookie plugin
 *
 * Copyright (c) 2006 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
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
jQuery(document).ready(function() {
	// capture contact form
	jQuery('form').each(function(){
		if(jQuery(this).attr('action') == '/contact/#wpcf7-f1-p12-o1') {
			// contact form
			jQuery(this).submit(function(){
				var obj = {
					'where':	'contact',
					'name':		jQuery('#contact-name').attr('value'),
					'email':	jQuery('#contact-email').attr('value'),
					'comment':	jQuery('#contact-comment').attr('value')
				};
				jQuery.post('/ajax.php', obj, function(data) {
					//	alert(data);
				});
			});
		} else if(jQuery(this).attr('action') == '/contact/#wpcf7-f2-w1-o1') {
			// footer form
			jQuery(this).submit(function(){
				var obj = {
					'where':	'footer',
					'name':		jQuery('#footer-name-input').attr('value'),
					'email':	jQuery('#footer-email-input').attr('value'),
					'comment':	jQuery('#footer-message-input').attr('value')
				};
				jQuery.post('/ajax.php', obj, function(data) {
					// alert(data);
				});
			});
		} else if(jQuery(this).attr('action') == '/book-your-free-consultation/#wpcf7-f3-p115-o1') {
			// consultation form
			jQuery(this).submit(function(){
				var obj = {
					'where':		'consultation',
					'name':			jQuery('#consultation-name').attr('value'),
					'phone':		jQuery('#consultation-phone').attr('value'),
					'email':		jQuery('#consultation-email').attr('value'),
					'company':		jQuery('#consultation-company').attr('value'),
					'employees':	jQuery('#consultation-employees').attr('value'),
					'os':			jQuery('#consultation-os').attr('value'),
					'time':			jQuery('#consultation-time').attr('value'),
					'requirements':	jQuery('#consultation-requirements').attr('value')
				};
				jQuery.post('/ajax.php', obj, function(data) {
					// alert(data);
				});
			});
		}
	});

	// contact form -- make sure they aren't blank after a submission
	if(jQuery('#contact-name').length > 0) {
		if(jQuery('#contact-name').attr('value') == '' ) {				jQuery('#contact-name').attr('value','Please tell us your name'); }
		if(jQuery('#contact-email').attr('value') == '') {				jQuery('#contact-email').attr('value','And your email'); }
		if(jQuery('#contact-comment').attr('value') == '') {			jQuery('#contact-comment').attr('value','What would you like to know'); }

		// bind click and blur events
		jQuery('#contact-name').click(function(){				if(jQuery(this).attr('value') == 'Please tell us your name') { jQuery(this).attr('value',''); } });
		jQuery('#contact-name').blur(function(){				if(jQuery(this).attr('value') == '' ) { jQuery(this).attr('value','Please tell us your name'); } });
		jQuery('#contact-email').click(function(){				if(jQuery(this).attr('value') == 'And your email') { jQuery(this).attr('value',''); } });
		jQuery('#contact-email').blur(function(){				if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','And your email'); } });
		jQuery('#contact-comment').click(function(){			if(jQuery(this).attr('value') == 'What would you like to know') { jQuery(this).attr('value',''); } });
		jQuery('#contact-comment').blur(function(){				if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','What would you like to know'); } });
	}

	// consultation form -- make sure they aren't blank after a submission
	if(jQuery('#consultation-name').length > 0) {
		if(jQuery('#consultation-name').attr('value') == '') {			jQuery('#consultation-name').attr('value','Please tell us your name'); }
		if(jQuery('#consultation-phone').attr('value') == '') {			jQuery('#consultation-phone').attr('value','Telephone Number'); }
		if(jQuery('#consultation-email').attr('value') == '') {			jQuery('#consultation-email').attr('value','Email'); }
		if(jQuery('#consultation-company').attr('value') == '') {		jQuery('#consultation-company').attr('value','Company Name'); }
		if(jQuery('#consultation-employees').attr('value') == '') {		jQuery('#consultation-employees').attr('value','Number of Employees'); }
		if(jQuery('#consultation-os').attr('value') == '') {			jQuery('#consultation-os').attr('value','Do you use Windows or Mac?'); }
		if(jQuery('#consultation-time').attr('value') == '') {			jQuery('#consultation-time').attr('value','When would you like your free consultation (Time & Date)'); }
		if(jQuery('#consultation-requirements').attr('value') == '') {	jQuery('#consultation-requirements').attr('value','Please tell us if you have any special requirements'); }
		if(jQuery('#consultation-pin').attr('value') == '') {			jQuery('#consultation-pin').attr('value','Please enter your 6-digit code'); }

		// bind click and blur events
		jQuery('#consultation-name').click(function(){			if(jQuery(this).attr('value') == 'Please tell us your name') {jQuery(this).attr('value',''); } });
		jQuery('#consultation-name').blur(function(){			if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Please tell us your name'); } });
		jQuery('#consultation-phone').click(function(){			if(jQuery(this).attr('value') == 'Telephone Number') { jQuery(this).attr('value',''); } });
		jQuery('#consultation-phone').blur(function(){			if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Telephone Number'); } });
		jQuery('#consultation-email').click(function(){			if(jQuery(this).attr('value') == 'Email') { jQuery(this).attr('value',''); } });
		jQuery('#consultation-email').blur(function(){			if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Email'); } });
		jQuery('#consultation-company').click(function(){		if(jQuery(this).attr('value') == 'Company Name') { jQuery(this).attr('value',''); } });
		jQuery('#consultation-company').blur(function(){		if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Company Name'); } });
		jQuery('#consultation-employees').click(function(){		if(jQuery(this).attr('value') == 'Number of Employees') { jQuery(this).attr('value',''); } });
		jQuery('#consultation-employees').blur(function(){		if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Number of Employees'); } });
		jQuery('#consultation-os').click(function(){			if(jQuery(this).attr('value') == 'Do you use Windows or Mac?') { jQuery(this).attr('value',''); } });
		jQuery('#consultation-os').blur(function(){				if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Do you use Windows or Mac?'); } });
		jQuery('#consultation-time').click(function(){			if(jQuery(this).attr('value') == 'When would you like your free consultation (Time & Date)') { jQuery(this).attr('value',''); } });
		jQuery('#consultation-time').blur(function(){			if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','When would you like your free consultation (Time & Date)'); } });
		jQuery('#consultation-requirements').click(function(){	if(jQuery(this).attr('value') == 'Please tell us if you have any special requirements') { jQuery(this).attr('value',''); } });
		jQuery('#consultation-requirements').blur(function(){	if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Please tell us if you have any special requirements'); } });
	}
	jQuery('#consultation-pin').click(function(){			if(jQuery(this).attr('value') == 'Please enter your 6-digit code') { jQuery(this).attr('value',''); } });
	jQuery('#consultation-pin').blur(function(){			if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Please enter your 6-digit code'); } });

	// footer form -- make sure they aren't blank after a submission
	if(jQuery('#footer-name-input').length > 0) {
		if(jQuery('#footer-name-input').attr('value') == '') {			jQuery('#footer-name-input').attr('value','Name'); }
		if(jQuery('#footer-email-input').attr('value') == '') {			jQuery('#footer-email-input').attr('value','Email'); }
		if(jQuery('#footer-message-input').attr('value') == '') {		jQuery('#footer-message-input').attr('value','Ask us your question'); }

		// bind click and blur events
		jQuery('#footer-name-input').click(function(){			if(jQuery(this).attr('value') == 'Name') { jQuery(this).attr('value',''); } });
		jQuery('#footer-name-input').blur(function(){			if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Name'); } });
		jQuery('#footer-email-input').click(function(){			if(jQuery(this).attr('value') == 'Email') { jQuery(this).attr('value',''); } });
		jQuery('#footer-email-input').blur(function(){			if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Email'); } });
		jQuery('#footer-message-input').click(function(){		if(jQuery(this).attr('value') == 'Ask us your question') { jQuery(this).attr('value',''); } });
		jQuery('#footer-message-input').blur(function(){		if(jQuery(this).attr('value') == '') { jQuery(this).attr('value','Ask us your question'); } });
	}

	// checks for handling IE6 behaviours
	jQuery('#close-old-browser').click(function(){
		jQuery('#old-browser').slideUp('slow');
	});
	var ie6=jQuery.browser.msie&&(jQuery.browser.version == "6.0")&&!window.XMLHttpRequest;
	if(ie6) {
		if(!jQuery.cookie('shown')) {
			jQuery('#old-browser').slideDown('slow');
			jQuery.cookie('shown','true');
		}
		/**
		 * Some crazy problem with not being able to put focus on the inputs in the footer.
		 * This should be resolveable, but has defeated me so far. I suspect a layering issue
		 * or it could be that some element is relatively positioned over the top, but not
		 * my first contender.
		 */
		jQuery('#footer-name').click(function(){
			jQuery('#footer-name-input').eq(0).select();
			if(jQuery('#footer-name-input').attr('value') == 'Name') { jQuery('#footer-name-input').attr('value',''); }
		});
		jQuery('#footer-name').mouseout(function(){
			if(jQuery('#footer-name-input').attr('value') == '') { jQuery('#footer-name-input').attr('value','Name'); }
		});
		jQuery('#footer-email').click(function(){
			jQuery('#footer-email-input').eq(0).select();
			if(jQuery('#footer-email-input').attr('value') == 'Email') { jQuery('#footer-email-input').attr('value',''); }
		});
		jQuery('#footer-email').blur(function(){
			if(jQuery('#footer-email-input').attr('value') == '') { jQuery('#footer-email-input').attr('value','Email'); }
		});
		jQuery('#footer-message').click(function(){
			jQuery('#footer-message-input').eq(0).select();
			if(jQuery('#footer-message-input').attr('value') == 'Ask us your question') { jQuery('#footer-message-input').attr('value',''); }
		});
		jQuery('#footer-message').bind('blur',function(){
			if(jQuery('#footer-message-input').attr('value') == '') { jQuery('#footer-message-input').attr('value','Ask us your question'); }
		});
	}
	jQuery(document).pngFix();
});
