/**
 * 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;
    }
};

$(document).ready(function(){
	var is_open = '';
	var message = '';
	var is_success = false;

	if ($.cookie('quote[error]') != null)
	{
		if ($.cookie('quote[error]') == 1)
		{
			//message = '<div class="flyout-error">Please correct all invalid and empty fields.</div>';
			message = '<h2 class="flyout-error">Please correct all invalid and empty fields.</h2>';
			is_open = true;
		}
		else
		{
			//message = '<div class="flyout-success">Thank you for your enquiry. We will contact you shortly.</div>';
			message = '<h2 class="flyout-success">Thank you for your enquiry. We will contact you shortly.</h2>';
			is_open = true;
			is_success = true;
		}
	}

	flyout = '<div id="side-flyout"' + (is_open ? ' class="flyout-open"' : '') + '>'+
	'    <div class="side-flyout-container">'+
	'        <a class="side-flyout-trigger" href="#">Get a Free Quote</a>'+
	message+
	'        <div class="flyout-notice">To close, click the tab on the right.</div>';

	flyout += '        <form id="side-flyout-form" method="post" action="submit_quote.php">'+
	'            <label for="flyout-name" class="flyout-label">Full Name <em>*</em></label>'+
	'            <input type="text" id="flyout-name" name="name" class="text" value="' + ($.cookie('quote[name]') != null ? $.cookie('quote[name]') : '') + '" />'+
	'            <label for="flyout-company" class="flyout-label">Company <em>*</em></label>'+
	'            <input type="text" id="flyout-company" name="company" class="text" value="' + ($.cookie('quote[name]') != null ? $.cookie('quote[company]') : '') + '" />'+
	'            <label for="flyout-email" class="flyout-label">Email <em>*</em></label>'+
	'            <input type="text" id="flyout-email" name="email" class="text" value="' + ($.cookie('quote[email]') != null ? $.cookie('quote[email]') : '') + '" />'+
	'            <label for="flyout-phone" class="flyout-label">Phone Number <em>*</em></label>'+
	'            <input type="text" id="flyout-phone" name="phone" class="text" value="' + ($.cookie('quote[phone]') != null ? $.cookie('quote[phone]') : '') + '" />'+
	'            <label for="flyout-website" class="flyout-label">Website</label>'+
	'            <input type="text" id="flyout-website" name="website" class="text" value="' + ($.cookie('quote[website]') != null ? $.cookie('quote[website]') : '') + '" />'+
	'            <label class="flyout-label">Service you\'re interested in:</label>'+
	'            <div class="check-options">'+
	'                <label class="flyout-label"><input type="checkbox" name="services[]" value="Design"' + ($.cookie('quote[services][design]') != null ? ' checked="checked"' : '') + ' />Design</label>'+
	'                <label class="flyout-label"><input type="checkbox" name="services[]" value="Development"' + ($.cookie('quote[services][development]') != null ? 'checked="checked"' : '') + ' />Development</label>'+
	'                <label class="flyout-label"><input type="checkbox" name="services[]" value="Marketing"' + ($.cookie('quote[services][marketing]') != null ? 'checked="checked"' : '') + ' />Marketing</label>'+
	'                <div class="clear"></div>'+
	'            </div>'+
	/*
	'            <label class="flyout-label">What is your projected budget for this project? <em>*</em></label>'+
	'            <select name="budget">'+
	'                <option selected="selected" value="">Please Select...</option>'+
	'                <option>Unknown</option>'+
	'                <option>$3,000 - $4,000</option>'+
	'                <option>$4,000 - $6,000</option>'+
	'                <option>$6,000 - $10,000</option>'+
	'                <option>$10,000 - $15,000</option>'+
	'                <option>$15,000 - $25,000</option>'+
	'                <option>$25,000 - $50,000</option>'+
	'                <option>$50,000 - $100,000</option>'+
	'                <option>$100,000+</option>'+
	'            </select>'+
	*/
	'            <label for="flyout-comments" class="flyout-label">Your Ideas / Comments <em>*</em></label>'+
	'            <textarea cols="25" rows="10" id="flyout-comments" name="comments">' + ($.cookie('quote[comments]') != null ? $.cookie('quote[comments]') : '') + '</textarea>'+
	'            <input type="submit" class="submit" name="submit" value="Get Quote" />'+
	'        </form>';

	flyout += '    </div>'+
	'</div>';

	$('html body').prepend(flyout);

	var is_open_toggle = is_open;
	$('div#side-flyout').live('mouseenter', function(){
		$(this).stop().animate({left: 0}, 1500, function(){
			is_open_toggle = true;
			$(this).css({
				position: 'absolute',
				top: $(this).position().top + 'px'
			});
		});
	});

	$('div#side-flyout').live('mouseleave', function(){
		if (is_open_toggle == false)
		{
			$(this).stop().animate({left: -427}, 1500, function(){
				$(this).css({
					position: 'fixed',
					top: 'auto'
				});
			});
		}
	});

	$('div#side-flyout div.side-flyout-container a.side-flyout-trigger').live('click', function(){
		if (is_open_toggle == true)
		{
			$('div#side-flyout').stop().animate({left: -427}, 1500, function(){
				is_open_toggle = false;
				$(this).css({
					position: 'fixed',
					top: 'auto'
				});
			});
		}
		return false;
	});

	/*$('div#side-flyout div.side-flyout-container a.side-flyout-trigger').toggle(function(){
		$('div#side-flyout').animate({
			left: 0
		}, 1500);
		return false;
	}, function(){
		$('div#side-flyout').animate({
			left: -427
		}, 1500);
		return false;
	});*/

	/*if (is_success)
	{
		$('div#side-flyout').delay(5000).animate({left: -427}, 1500);
	}*/

	$.cookie('quote[error]', null);
	$.cookie('quote[name]', null);
	$.cookie('quote[email]', null);
	$.cookie('quote[company]', null);
	$.cookie('quote[phone]', null);
	$.cookie('quote[website]', null);
	$.cookie('quote[services]', null);
	$.cookie('quote[comments]', null);
	$.cookie('quote[services][design]', null);
	$.cookie('quote[services][development]', null);
	$.cookie('quote[services][marketing]', null);
});


