var activeform = 0;
function ccb() {
    $('#socialFixed').animate({ width: '34px' }, 1000)
    $('#contact-wrapper').animate({ width: '0', marginLeft: "34px" }, 1000)
    $('#contact-form').animate({ left: '-510px' }, 1000);
    $('#socialFixed').removeClass("contact-hover");
    $('#contact').show();
    $('.sentForm').hide();
    $('#contact input, #contact textarea').val('').removeClass("ierror");
    $('#contact #isubject').removeClass("ierror");
    $('#contact .error').hide();
    $('#contact #isubject option[value="0"]').attr("selected", "selected")
    $('#glightbox').hide();
    activeform = 0;
}


$(document).ready(function () {


    $('#jumpContact').live("click", function (e) {
        e.preventDefault();
        $('#socialFixed').addClass("contact-hover");
        $('#socialFixed, #contact-wrapper').animate({ width: '510px' }, 900);
        $('#contact-form').animate({ left: '0px', marginLeft: "0" }, 1000);
        $('#glightbox').show();
        activeform = 1;
    });


    $('.close-contact').click(function (e) {
        e.preventDefault();
        ccb();
    });

    $("#glightbox").live('click', function () {
        if ($('#socialFixed').hasClass("contact-hover"))
            ccb();
    });

    $(document).keydown(function (e) {
        if (e.keyCode == 27 && $('#socialFixed').hasClass("contact-hover")) {
            ccb();
        } else if (e.keyCode == 13 && activeform == 1) {
            $("#btnContactSend").click();
        }
    });



    $("#jumpContact").hover(
            function () {
                $('#socialFixed').css("background-position", "-505px -737px")
            },
            function () {
                $('#socialFixed').css("background-position", "-914px -6px")
            });


//            $("#sigIcon").hover(
//            function () {
//                $('#login .soon').show()
//            },
//            function () {
//                $('#login .soon').hide()
//            }
//        );

//    $("#signtxt").hover(
//            function () {
//                $(this).parent().parent().find(".soon").show()
//            },
//            function () {
//                $(this).parent().parent().find(".soon").hide()
//            }
//        );

//    $("#signUp2").hover(
//            function () {
//                $('#signUp2 .soon').show()
//            },
//            function () {
//                $('#signUp2 .soon').hide()
//            }
//        );

    $('body').mousewheel(function (event, delta) {
        var browser = $.browser.msie + $.browser.version;
        if (browser != "true8.0") {
            var top = $(window).scrollTop() + 220;
            var features = $("#individual").offset().top;
            var business = $("#corporate").offset().top;
            var about = $("#meet").offset().top;

            if (top < features) {
                $('#headermenu li a').removeClass("selectedlink");
            }
            else if (top > features && top < business) {
                $('#headermenu li a').removeClass("selectedlink");
                $('.features-link').addClass("selectedlink");
            } else if (top > business && top < about) {
                $('#headermenu li a').removeClass("selectedlink");
                $('.solutions-link').addClass("selectedlink");
            } else if (top > about) {
                $('#headermenu li a').removeClass("selectedlink");
                $('.about-link').addClass("selectedlink");
            }
        }
    });


    $('.toTop a').click(function (e) {
        e.preventDefault();
        $('#headermenu li a').removeClass("selectedlink");
        $('html, body').animate({ scrollTop: 0 }, 1000);
        return false;
    });



    $('.features-link').click(function (e) {
        e.preventDefault();
        $('#headermenu li a').removeClass("selectedlink");
        $(this).addClass("selectedlink");
        $('html, body').animate({
            scrollTop: $("#individual").offset().top - 120
        }, 2000);
    });


    $('.solutions-link').click(function (e) {
        e.preventDefault();
        $('#headermenu li a').removeClass("selectedlink");
        $(this).addClass("selectedlink");
        $('html, body').animate({
            scrollTop: $("#corporate").offset().top - 120
        }, 2000);
    });


    $('.about-link').click(function (e) {
        e.preventDefault();
        $('#headermenu li a').removeClass("selectedlink");
        $(this).addClass("selectedlink");
        $('html, body').animate({
            scrollTop: $("#meet").offset().top-120
        }, 2000);
    });

    $("#refreshimage").live('click', function (e) {
        e.preventDefault();
        $image = $("#secureimage");
        $image.attr('src', '/Captcha?' + (new Date()).getTime());
    });


    $("#btnContactSend").live('click', function (e) {
        e.preventDefault();
        var hasError = false;
        if ($('#iname').val() == "") { $("#iname").addClass("ierror"); hasError = true; }
        if ($('#iemail').val() == "") { $("#iemail").addClass("ierror"); hasError = true; }
        if ($('#iemail').val() != "" && !mailfilter.test($('#iemail').val())) { $("#erroriemail").hide(); $('#erroriemailcontrol').show(); hasError = true; }
        if ($('#iphone').val() == "") { $("#iphone").addClass("ierror"); hasError = true; }
        if ($('#isubject').val() == "0") { $("#isubject").addClass("ierror"); hasError = true; }
        if ($('#imessage').val() == "") { $("#imessage").addClass("ierror"); hasError = true; }
        if ($('#icaptcha').val() == "") { $("#icaptcha").addClass("ierror"); hasError = true; }
        if (hasError == false) {
            $.ajax({
                type: "POST",
                url: "/Contact.aspx",
                dataType: "html",
                data: $('#contact-form').serialize(),
                beforeSend: function () { $('.loading').show(); },
                success: function (template) {
                    if (template == "true") {
                        $('#contact').hide();
                        $('.loading').hide();
                        $('.sentForm').show();
                        $("#refreshimage").trigger("click");
                    }
                    else {
                        $('.errorOccured').show();
                       
                    }
                }
            });
        }
    });

    $('#contact input, #contact textarea').live('keypress', function () {
        $('#error' + $(this).attr("id")).hide();
        $(this).removeClass("ierror");
    });

    $('#contact select').live('change', function () {
        $('#error' + $(this).attr("id")).hide();
        $(this).removeClass("ierror");
    });

   // $('#tweetOnAir').tweets({ username: "infoflight" });

    url = 'http://api.twitter.com/1/statuses/user_timeline/infoflight.json?callback=?';
    $.getJSON(url, function (tweet) {
        $("#tweetOnAir").html(tweet[0].text);
    });

});

var mailfilter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

function isNumberKey(evt) {
    var e = evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

(function($){
	$.fn.tweets = function(options) {
		$.ajaxSetup({ cache: true });
		var defaults = {
			tweets: 1
		};
		var options = $.extend(defaults, options);
		return this.each(function() {
			var obj = $(this);
			$.getJSON('http://search.twitter.com/search.json?callback=?&rpp='+options.tweets+'&q=from:'+options.username,
		        function(data) {
		            $.each(data.results, function(i, tweet) {
		                if(tweet.text !== undefined) {
		                    $(obj).append("<a href='http://twitter.com/infoflight/status/"+tweet.id_str+ "' target='_blank'>" + tweet.text + "</a>");
							
		                }
		            });
		        }
		    );
		});
	};
})(jQuery);


