
/*
 * jQuery outside events - v1.1 - 3/16/2010
 * http://benalman.com/projects/jquery-outside-events-plugin/
 * 
 * Copyright (c) 2010 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,c,b){$.map("click dblclick mousemove mousedown mouseup mouseover mouseout change select submit keydown keypress keyup".split(" "),function(d){a(d)});a("focusin","focus"+b);a("focusout","blur"+b);$.addOutsideEvent=a;function a(g,e){e=e||g+b;var d=$(),h=g+"."+e+"-special-event";$.event.special[e]={setup:function(){d=d.add(this);if(d.length===1){$(c).bind(h,f)}},teardown:function(){d=d.not(this);if(d.length===0){$(c).unbind(h)}},add:function(i){var j=i.handler;i.handler=function(l,k){l.target=k;j.apply(this,arguments)}}};function f(i){$(d).each(function(){var j=$(this);if(this!==i.target&&!j.has(i.target).length){j.triggerHandler(e,[i.target])}})}}})(jQuery,document,"outside");

function flickerPin(){
	$('#pin-overlay').fadeOut(500,function(){
  	$('#pin-overlay').fadeIn(500);
  });
}

function isFullyLoaded(){
	
  $('#face-350 > img').load(function() {
  	if($('#faces-loaded').html()==0){
  		$('#faces-loaded').html(1);
    	$('#faces').fadeIn('2000',function(){
    		window.clearInterval(isLoadedInterval);
        window.clearInterval(pinFlicker);
    		flickerInterval = window.setInterval("flicker()", 50);
        $('#temp-map-background').hide();
  		});
    }
	});
  
}

$(document).ready(function () {

    if ($('#faces').length > 0) { //only run on home page!
        $.post("/Home/GetMapBackground", {

        }, function (response) {
            if (!isIe7()) {
                $('#faces').html(response);
                isLoadedInterval = window.setInterval("isFullyLoaded()", 500);
                pinFlicker = window.setInterval("flickerPin()", 1500);
            }
        });
    }

    $('.large-login').live('mouseenter', function () {
        $(this).parent().css('background-position', '0px -36px');
    });

    $('.large-login').live('mouseleave', function () {
        $(this).parent().css('background-position', '0px 0px');
    });

    $('.large-login').live('click', function () {
        /*
        if ($(this).attr('id') == 'password') {
        $(this).attr('type', 'password');
        }
        */

        $(this).parent().css('background-position', '0px -72px');
        $(this).css('color', '#333');
        $(this).val('');

    });

    $('#password, #emailAddress').live('focus', function () {

        $('#password-overlay').hide();

    });

    $('.large-login').live('focusout', function () {

        $(this).parent().css('background-position', '0px 0px');

        if ($(this).val() == '') {

            if ($(this).attr('id') == 'password') {
                $(this).css('color', '#CCC');
                $(this).val('Password');
            }

            if ($(this).attr('id') == 'emailAddress') {
                $(this).css('color', '#CCC');
                $(this).val('KILTR Username or email');
                $('#password-overlay').hide();
            }
        }

    });

    $('.small-reg-field').live('mouseenter', function () {
        $(this).parent().css('background-position', '0px -36px');
    });

    $('.small-reg-field').live('mouseleave', function () {
        $(this).parent().css('background-position', '0px 0px');
    });

    $('.small-reg-field').live('click', function () {
        $(this).parent().css('background-position', '0px -72px');
        $(this).css('color', '#333');
        $(this).val('');
    });

    $('.small-reg-field').live('blur', function () {
        $(this).parent().css('background-position', '0px 0px');
        if ($(this).val() == '') {
            if ($(this).attr('id') == 'firstName') {
                $(this).css('color', '#CCC');
                $(this).val('First name');
            }

            if ($(this).attr('id') == 'surname') {
                $(this).css('color', '#CCC');
                $(this).val('Surname');
            }

            if ($(this).attr('id') == 'organisation') {
                $(this).css('color', '#CCC');
                $(this).val('Organisation');
            }

            if ($(this).attr('id') == 'position') {
                $(this).css('color', '#CCC');
                $(this).val('Position');
            }

            if ($(this).attr('id') == 'password') {
                $(this).css('color', '#CCC');
                $(this).val('Create password');
            }

            if ($(this).attr('id') == 'email') {
                $(this).css('color', '#CCC');
                $(this).val('Email address');
            }
        }
    });

    $('.public-badge, .public-badge-last').live('mouseenter', function () {

        var badgeDescription = $('#description-' + $(this).attr('id')).find('.badgeDescription').html();
        var badgeTitle = $('#description-' + $(this).attr('id')).find('.badgeDescriptionTitle').html();

        var hoverCardHeight = $('#badgeBubble').height();
        var hoverCardWidth = $('#badgeBubble').width();
        var badgeHeight = $(this).height();

        var finalTopLocation = $(this).offset().top + badgeHeight;
        var finalPositionLeft = $(this).offset().left - 86;

        $('#badgeBubble').show();
        $('#badgeBubble').css('z-index', '100000');
        $('#badgeBubble').css('top', finalTopLocation + 'px');
        $('#badgeBubble').css('left', finalPositionLeft + 'px');
        $('#badgeBubble').html('<strong>' + badgeTitle + '</strong><br />' + badgeDescription);
    });

    $('.public-team, .public-team-last').live('mouseenter', function () {
        var badgeDescription = $('#description-' + $(this).attr('id')).find('.badgeDescription').html();
        var badgeTitle = $('#description-' + $(this).attr('id')).find('.badgeDescriptionTitle').html();

        var hoverCardHeight = $('#teamBubble').height();
        var hoverCardWidth = $('#teamBubble').width();
        var badgeHeight = $(this).height();

        var finalTopLocation = $(this).offset().top + badgeHeight;
        var finalPositionLeft = $(this).offset().left - 36;

        $('#teamBubble').show();
        $('#teamBubble').css('z-index', '100000');
        $('#teamBubble').css('top', finalTopLocation + 'px');
        $('#teamBubble').css('left', finalPositionLeft + 'px');
        $('#teamBubble').html('<strong>' + badgeTitle + '</strong><br />' + badgeDescription);
    });

    $('#teamBubble, .public-sect-left').live('mouseleave', function () {
        $('#teamBubble').hide();
    });


    $('.back-to-top').live('click', function () {
        $.scrollTo('#explore-header', 1500);
    });

    $('.back-to-top-about').live('click', function () {
        $.scrollTo('body', 1500);
    });

    $('.navDiscNewOff').live('click', function () {
        $('.navDiscNewOn').removeClass('navDiscNewOn').addClass('navDiscNewOff');
        $(this).removeClass('navDiscNewOff').addClass('navDiscNewOn');
        var slideInfo = $(this).attr('id');
        var slideInfoParts = slideInfo.split('-');
        var slidePosition = (parseInt(slideInfoParts[1]) * 880)
        $('#slideTrackNew').animate({
            marginLeft: "-" + slidePosition + "px"
        }, 2000);
    });

    $('#join-button').live('click', function () {
        /* $('#fadedOverlay,#registerOverlayContainer,#registerOverlay').show();
        var screenWidth = screen.width;
        var screenHeight = $('#innerContainer').height();
        $('#registerOverlay').css('margin-left', ((screenWidth - 737) / 2) + 'px');
        $('#fadedOverlay').css('height','4500px');*/
        return true;
    });

    $("#close-new-reg").live('click', function () {
        $('#fadedOverlay,#registerOverlayContainer,#registerOverlay').hide();
    });


    $('#logon-button').live('click', function () {
        $('#fadedOverlay,#loginOverlayContainer,#loginOverlay').show();
        var screenWidth = screen.width;
        $('#loginOverlay').css('margin-left', ((screenWidth - 737) / 2) + 'px');
        $('#fadedOverlay').css('height', '4500px');
        return false;
    });

    $("#close-new-login").live('click', function () {
        $('#fadedOverlay,#loginOverlayContainer,#loginOverlay').hide();
    });

    $('#login-button').live('click', function () {

        $('#fadedOverlay,#loggingInOverlayContainer,#loggingInOverlay').show();
        var screenWidth = screen.width;
        $('#fadedOverlay').css('height', '4500px');
        $('#loggingInOverlay').css('margin-left', ((screenWidth - 357) / 2) + 'px');

        if ($("#logonForm").valid()) {
            $.post('/Account/LogOnCheck',
				$('#logonForm').serialize(), function (response) {

				    var hasErrors = $('<div>' + response + '</div>').find('#logonFormErrorCount').attr('value') > 0;

				    if (hasErrors) {
				        $('#fadedOverlay,#loggingInOverlayContainer,#loggingInOverlay').hide();
				        notifyUser('Oops! We couldn\'t log you in, sure you had the right username (or email) and password?');
				        $('#logonFormContainer').html(response);
				        rebindValidators('#logonForm');
				        return false;
				    }

				    //$.post('/Account/LogOn', $('#logonForm').serialize());
				    $('#logonForm').submit();

				    return true;
				});
        }
        return false;
    });

});
