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

// TODO JsDoc

/**
 * Create a cookie with the given key 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 key The key 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 key.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String key The key 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 (key, value, options) {

    // key and value given, set cookie...
    if (arguments.length > 1 && (value === null || typeof value !== "object")) {
        options = jQuery.extend({}, options);

        if (value === null) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? String(value) : encodeURIComponent(String(value)),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};





var menu_timeout;
function mainmenu(){
$(" #nav ul ").css({display: "none"}); // Opera Fix
$(" #nav li").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(400);
		},function(){
		$(this).find('ul:first').css({visibility: "hidden"});
		});
}

 
 
 $(document).ready(function(){					
	mainmenu();
	
	

    //new nav
    $('#top_level .top_button').hover(function() {
        var hovered_button=$(this);
        clearTimeout($('#top_navigation').data('timeout_top'));
        $('#top_navigation').data('timeout_top',setTimeout(function(){
            $('#top_level').find('.over').removeClass('over');
            hovered_button.parent().addClass('over');
            $('#top_navigation').removeClass('not_over');
        },200));
        
        clear_menu_timeout();
    },timeout_menu);
    
    $('#top_level .second_level').hover(function() {
        clearTimeout($('#top_navigation').data('timeout_top'));
        $(this).closest('.main_nav_button').addClass('over');
        $('#top_navigation').removeClass('not_over');
        
        clear_menu_timeout();
    },timeout_menu);
    $('#nav_category_explore_projects li').hover(function(){
        $('#top_navigation .nav_category .over').removeClass('over');

        clearTimeout($('#top_navigation').data('timeout_top'));
        clear_menu_timeout();
    });

    $('#nav_category_explore_projects li').click(function(){
        $.cookie('explore_project',$.trim($(this).text()));
        $('#nav_category_explore_projects li.active').removeClass('active');
        $('#top_navigation .nav_category.active').removeClass('active');

        $(this).closest('#nav_category_explore_projects').next().children('ul.nav_category_'+$(this).attr('class')).addClass('active');
        $(this).addClass('active');

        clearTimeout($('#top_navigation').data('timeout_top'));
        clear_menu_timeout();
    });
    if ($.cookie('explore_project')) {
        $('#nav_category_explore_projects .'+$.trim($.cookie('explore_project'))).click();
    }
    $('#top_level .second_level_button').hover(function(){
        $('#top_level .second_level_button.over').removeClass('over');
        $(this).addClass('over');
        $('.third_level_large_preview').hide();
        $(this).children('.third_level_large_preview').show();
        //clear_menu_timeout();
    });

    $('.preview_mode').each(function(){
        $(this).data('old_html',$(this).html());
    })
    $('#top_level .third_level a').hover(function() {
        var preview_mode=$(this).closest('.second_level_button').find('.preview_mode');
        clearTimeout(preview_mode.data('timeout_outside'));
        preview_mode.find('.image').html($(this).attr('data-image')?$('<img />').attr('src',$(this).attr('data-image')):'');
        preview_mode.find('.status').text($(this).children('.status').text()?('Status: ' + $(this).children('.status').text()):'');
        preview_mode.find('.type').text($(this).children('.type').text()?('Type: ' + $(this).children('.type').text()):'');
        preview_mode.find('.title').text($(this).children('.title').text());
    },function(){
        var preview_mode=$(this).closest('.second_level_button').find('.preview_mode');
        preview_mode.data('timeout_outside',setTimeout(function(){
            preview_mode.html(preview_mode.data('old_html'));
        },300));
    });
	
	
    $('.search_holder input,#query').click(function() {
        if ($(this).val()=='Search the Newsroom...') {
            $(this).val('');
        }
		
    });
	
	
    $('.view_map').click(function(e){
        e.preventDefault();
        open_imap();
    })
});
function timeout_menu(e)
{
    menu_timeout=setTimeout(function(e){
       $('#top_navigation').find('.over').removeClass('over');
       $('.third_level_large_preview').hide();
        $('#top_navigation').addClass('not_over');
        
    },400);
}

function clear_menu_timeout(){
    if (menu_timeout) {
        clearTimeout(menu_timeout);
    }
}
function open_imap()
{

    if ($('#map_wrapper').hasClass('animating')) {
                return;
        }
        if (!$('#map_wrapper').hasClass('loading')) {
                $('#map_wrapper').addClass('loading');
                load_map();

        }
        $('#map_wrapper').addClass('animating');

       if ($('#map_wrapper').is(':visible')) {
            $('.view_map').removeClass('open');
            $('#map_wrapper').animate({top:-1000},function(){$(this).hide();$('#map_wrapper').removeClass('animating');});
            $('.MapThingy').css('background-position','0 0');
            $('.dropdown').hide();
       }else{
           $('.view_map').addClass('open');
            $('#map_wrapper').css('top','-1000px').show().animate({top:0},function(){$('#map_wrapper').removeClass('animating');});
			$('.MapThingy').css('background-position','0 -287px');
            $('.dropdown').hide();
        }
}
