﻿/// <reference path="../jquery-1.3.2.js"/>

var Impulse = {
	init: function() {
		/*
			GLOBAL EVENTS
		*/
		$('a[rel="external"]').click(function() {
			window.open($(this).attr('href'));
			return false;
		});

		$('img.mevents')
			.mouseenter(function() {
				$(this).attr('src', $(this).attr('src').replace('off', 'over'));
			})
			.mouseleave(function() {
				$(this).attr('src', $(this).attr('src').replace('over', 'off'));
			});

		$('img.mfade')
			.mouseenter(function() { $(this).css('opacity', .9); })
			.mouseleave(function() { $(this).css('opacity', 1); });
		/*
			END GLOBAL EVENTS
		*/





		/*
			SEARCH BOX
		*/
		$('input.search-submit').click(function() {
			$('#frmSearch').triggerHandler('submit');
		});

		$('input.search').focus(function() {
			if (Impulse.Search.length == 0)
			{
				$.getJSON('/search', function(data) {
					Impulse.Search = data;
					$('input.search').autocomplete(Impulse.Search, {
						formatItem: function(item) {
							return item.text;
						},
						width: 175,
						selectFirst: false,
						matchContains: true
					}).result(function(event, item) {
						location.href = item.url;
					});
					$('input.search').blur().focus();
				});
			}
		});

		$('#frmSearch').submit(function() {
			window.location = '/explore/search/' + $('input.search').val().replace(/[^a-zA-Z 0-9]+/g, '').replace(/ /g, '+');
			return false;
		});
		/*
			END SEARCH BOX
		*/





		/*
			LOGIN FORM
		*/
		$('#login_form').ajaxForm({
			dataType: 'json',
			beforeSubmit: function(formData, jqForm, options) {
				$('#login_form .ajax_loader').css('visibility', 'visible');
			},
			success: function(data, textStatus) {
				if (data.result)
				{
					$('#result').html();
					window.location.href = window.location.href;
				}
				else
				{
					$('#result').html('Invalid e-mail or password provided.');
					$('input[name=userName]').
					$('#login_form .ajax_loader').css('visibility', 'hidden');
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown)
			{
				$('#result').html(textStatus + ' : ' + errorThrown);
				$('#login_form .ajax_loader').css('visibility', 'hidden');
			}
		});

		$('#login_link').click(function() {
			$.blockUI({
				message: $('#login_window'),
				css: {
					width: '561px',
					height: '204px',
					cursor: 'arrow',
					border: '0',
					paddingLeft: '4px',
					paddingTop: '4px',
					backgroundColor: 'transparent',
					backgroundImage: 'url(\'/content/themes/default/images/logon_bg.png\')'
				},
				overlayCSS:  { 
					backgroundColor: '#000',
					opacity: 0.6
				},
				focusInput: true,
				fadeIn: 0,
				fadeOut: 0
			});
			return false;
		});
		$('#login_form .close').click(function() {
			$.unblockUI({fadeOut: 0});
			return false;
		});

		$('#login_form .submit').click(function() {
			$('#login_form').triggerHandler('submit');
			return false;
		});
		$('#login_form input').keypress(function(eventObject) {
			if (eventObject.which == 13)
				$('#login_form').triggerHandler('submit');
			else if ($('#result').html() != '')
				$('#result').empty();
		});
		$('#login_form').submit(function() { 
			$(this).ajaxSubmit();
			return false; 
		});
		/*
			END LOGIN FORM
		*/





		/*
			DROP DOWN MENUS
		*/
		$('.menu_explore').after($('#menu_explore_body'));

		$('.menu_explore').parent().hover(function() {
			$('#menu_explore_body').show();
		}, function() {
			$('#menu_explore_body').hide();
		});

		$('.menu_developer').after($('#menu_developer_body'));

		$('.menu_developer').parent().hover(function() {
			$('#menu_developer_body').show();
		}, function() {
			$('#menu_developer_body').hide();
		});
		/*
			END DROP DOWN MENUS
		*/
	},
	Search: []
};

$(document).ready(function() {
	Impulse.init();
});
