$.noConflict();
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;
    }
};

var minutesBeforePopup = 2;

var secondsBeforePopup = minutesBeforePopup * 60;

jQuery(function (){

  jQuery("#callback-button, .launch-popup").attr("href", "javascript:void(0)").click(function(){
      showPopup();
  });

	if (jQuery.cookie("callback") == null){

		jQuery.cookie("callback", new Date().getTime());

	}else {

		if (jQuery.cookie("callback_dontshow") == null && jQuery.cookie("callback_dontshow_session") == null){

			var startTime = jQuery.cookie("callback");

			if (new Date().getTime() - parseInt(startTime) >  secondsBeforePopup * 1000){

				showPopup();

			}

		}

	}

});

var closeTimeout = null;

function showPopup (){

	var popup = jQuery(".popup");

	if (popup.length == 0){

		jQuery.get("./contact.callback.html", function (data){

			popup = jQuery(data);

			jQuery(document.body).append(popup);

			popup.find("#dont_show_again").click(function (){

				jQuery.cookie("callback_dontshow", true, { expires : jQuery(this).is(':checked') ? 365 : -1 });

			});

			popup.find(".close a").attr("href", "javascript:void(0);").click(function (){

				closePopup(popup);

			});

      popup.find("form").submit(function(e){
        e.preventDefault();
        jQuery.post("./contact.callback.json", jQuery(this).serialize(), function (data){
          jQuery(".redfieldtext").toggleClass("redfieldtext", false).toggleClass("fieldtext", true);
          jQuery(".popup").find("#error-msg").hide();

          //console.log(data);
          if (data.message == "OK"){
            jQuery.get("./contact.thankyou.html", function (data){
              jQuery('.popup').html(data);               
            });
          }else {
            jQuery(".popup").find("#error-msg").slideDown();
            jQuery.each(data, function(i, item) {
              jQuery('#label-'+item).toggleClass("fieldtext", false).toggleClass("redfieldtext", true);
            })
          }
        }, "json");
        return false;
      });

			displayPopup(popup);

		});

	}else {

		displayPopup(popup);

	}

}
function displayPopup (popup){

	var x = Math.floor((jQuery(document.body).width()/2) - 288);
  //alert(x);
	var y = (jQuery(window).height() / 2 - 150);

	popup.show().css({

		left : x,
		top : y

	});
  jQuery('html, body').animate({scrollTop:0}, 'slow');
}

function closePopup(popup){

	clearTimeout(closeTimeout);

	jQuery.cookie("callback", new Date().getTime());

	popup.fadeOut("slow", function (){

		popup.remove();

	});

}

