/*
 * http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf
 */
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

/**
 * @see http://ejohn.org/blog/javascript-array-remove/
 */
Array.prototype.remove = function(from, to) {
	this.splice(from, !to || 1 + to - from + (!(to < 0 ^ from >= 0) && (to < 0 || -1) * this.length));
	return this.length;
};


/*
 * http://mozilla.org/editor/key-event-spec.html
 * http://developer.mozilla.org/en/DOM/Event/UIEvent/KeyEvent
 */
if (typeof KeyEvent == 'undefined') {
	KeyEvent = {
		DOM_VK_CANCEL : 3,
		DOM_VK_HELP : 6,
		DOM_VK_BACK_SPACE : 8,
		DOM_VK_TAB : 9,
		DOM_VK_CLEAR : 12,
		DOM_VK_RETURN : 13,
		DOM_VK_ENTER : 14,
		DOM_VK_SHIFT : 16,
		DOM_VK_CONTROL : 17,
		DOM_VK_ALT : 18,
		DOM_VK_PAUSE : 19,
		DOM_VK_CAPS_LOCK : 20,
		DOM_VK_ESCAPE : 27,
		DOM_VK_SPACE : 32,
		DOM_VK_PAGE_UP : 33,
		DOM_VK_PAGE_DOWN : 34,
		DOM_VK_END : 35,
		DOM_VK_HOME : 36,
		DOM_VK_LEFT : 37,
		DOM_VK_UP : 38,
		DOM_VK_RIGHT : 39,
		DOM_VK_DOWN : 40,
		DOM_VK_PRINTSCREEN : 44,
		DOM_VK_INSERT : 45,
		DOM_VK_DELETE : 46,
		DOM_VK_0 : 48,
		DOM_VK_1 : 49,
		DOM_VK_2 : 50,
		DOM_VK_3 : 51,
		DOM_VK_4 : 52,
		DOM_VK_5 : 53,
		DOM_VK_6 : 54,
		DOM_VK_7 : 55,
		DOM_VK_8 : 56,
		DOM_VK_9 : 57,
		DOM_VK_SEMICOLON : 59,
		DOM_VK_EQUALS : 61,
		DOM_VK_A : 65,
		DOM_VK_B : 66,
		DOM_VK_C : 67,
		DOM_VK_D : 68,
		DOM_VK_E : 69,
		DOM_VK_F : 70,
		DOM_VK_G : 71,
		DOM_VK_H : 72,
		DOM_VK_I : 73,
		DOM_VK_J : 74,
		DOM_VK_K : 75,
		DOM_VK_L : 76,
		DOM_VK_M : 77,
		DOM_VK_N : 78,
		DOM_VK_O : 79,
		DOM_VK_P : 80,
		DOM_VK_Q : 81,
		DOM_VK_R : 82,
		DOM_VK_S : 83,
		DOM_VK_T : 84,
		DOM_VK_U : 85,
		DOM_VK_V : 86,
		DOM_VK_W : 87,
		DOM_VK_X : 88,
		DOM_VK_Y : 89,
		DOM_VK_Z : 90,
		DOM_VK_CONTEXT_MENU : 93,
		DOM_VK_NUMPAD0 : 96,
		DOM_VK_NUMPAD1 : 97,
		DOM_VK_NUMPAD2 : 98,
		DOM_VK_NUMPAD3 : 99,
		DOM_VK_NUMPAD4 : 100,
		DOM_VK_NUMPAD5 : 101,
		DOM_VK_NUMPAD6 : 102,
		DOM_VK_NUMPAD7 : 103,
		DOM_VK_NUMPAD8 : 104,
		DOM_VK_NUMPAD9 : 105,
		DOM_VK_MULTIPLY : 106,
		DOM_VK_ADD : 107,
		DOM_VK_SEPARATOR : 108,
		DOM_VK_SUBTRACT : 109,
		DOM_VK_DECIMAL : 110,
		DOM_VK_DIVIDE : 111,
		DOM_VK_F1 : 112,
		DOM_VK_F2 : 113,
		DOM_VK_F3 : 114,
		DOM_VK_F4 : 115,
		DOM_VK_F5 : 116,
		DOM_VK_F6 : 117,
		DOM_VK_F7 : 118,
		DOM_VK_F8 : 119,
		DOM_VK_F9 : 120,
		DOM_VK_F10 : 121,
		DOM_VK_F11 : 122,
		DOM_VK_F12 : 123,
		DOM_VK_F13 : 124,
		DOM_VK_F14 : 125,
		DOM_VK_F15 : 126,
		DOM_VK_F16 : 127,
		DOM_VK_F17 : 128,
		DOM_VK_F18 : 129,
		DOM_VK_F19 : 130,
		DOM_VK_F20 : 131,
		DOM_VK_F21 : 132,
		DOM_VK_F22 : 133,
		DOM_VK_F23 : 134,
		DOM_VK_F24 : 135,
		DOM_VK_NUM_LOCK : 144,
		DOM_VK_SCROLL_LOCK : 145,
		DOM_VK_COMMA : 188,
		DOM_VK_PERIOD : 190,
		DOM_VK_SLASH : 191,
		DOM_VK_BACK_QUOTE : 192,
		DOM_VK_OPEN_BRACKET : 219,
		DOM_VK_BACK_SLASH : 220,
		DOM_VK_CLOSE_BRACKET : 221,
		DOM_VK_QUOTE : 222,
		DOM_VK_META : 224
	}
}

/**
 * Object inheritance
 *
 * @param parent -- Function or Object
 */
Function.prototype.inherit = function(parent) {
	if (parent.constructor == Function) { // Normal inheritance
		this.prototype = new parent;
		this.prototype.constructor = this;
		this.prototype.parent = parent.prototype;
	}
	else { // Pure virtual inheritance
		this.prototype = parent;
		this.prototype.constructor = this;
		this.prototype.parent = parent;
	}

	return this;
}

/* Form validation */
// google.load('jquery', '1.3');

$.require.include([
	'jquery.validate',
	'jquery.validate-ru'
]);

$.validator.setDefaults({
	errorClass: 'error',

	highlight: function(element, errorClass) {
		$(element).addClass(errorClass);
	},

	unhighlight: function(element, errorClass) {
		$(element).removeClass(errorClass);
	}
});


/**
 * Yandex.Direct default values
 */
ya_class = 1;
ya_cid = 48061;
yandex_partner_id = 48061;
yandex_site_bg_color = 'FFFFFF';
yandex_site_charset = 'utf-8';
yandex_ad_format = 'direct';
yandex_font_size = 1;
yandex_direct_title_color = '996600';
yandex_direct_url_color = '996600';
yandex_direct_all_color = '000000';
yandex_direct_text_color = '000000';
yandex_direct_hover_color = 'CC9966';


$(document).keydown(function(event) {
	if (event.ctrlKey) {
		var link;

		switch (event.keyCode ? event.keyCode : event.which ? event.which : null) {
			case KeyEvent.DOM_VK_HOME:
				link = document.getElementById('HomeLink');
				break;
			case KeyEvent.DOM_VK_LEFT:
				link = document.getElementById('PrevLink');
				break;
			case KeyEvent.DOM_VK_UP:
				link = document.getElementById('UpLink');
				break;
			case KeyEvent.DOM_VK_RIGHT:
				link = document.getElementById('NextLink');
				break;
			case KeyEvent.DOM_VK_DOWN:
				link = document.getElementById('DownLink');
				break;
		}

		if (link && link.href) document.location = link.href;
	}
});


$(document).ready(function() {
	// Login float form
	$('#signin').click(function(event) {
		$('#auth-float').toggle();
		$('#username').focus();
		return false;
	});

	// Hide form on any outside click
	$(document).click(function(event) {
		$('#auth-float').hide();
	});

	/* Form should not hide on clicks inside,
	 * therefore disable onclick event bubbling.
	 */
	$('#auth-float').click(function(event) {
		event.stopPropagation();
	});

	// Add validation handler
	$('#auth-float').validate();

	$('#random-artwork > .thumbnail').hover(
		function() {
			$(this).find('.flyout').removeClass('hidden');
		},
		function() {
			$(this).find('.flyout').addClass('hidden');
		}
	);

	$('a.flyout.sendToMobile').click(function() {
		Toolkit.openPopup(this.href, 'sms' + parseInt(Math.random() * 10000), 780, 560);
		return false;
	});

	// Input placeholder
	if (typeof InputPlaceholder != 'undefined') {
		var inputPlaceholder = new InputPlaceholder();
		inputPlaceholder.init();
	}

	// Even and odd rows for listing tables
	$('table.list tbody tr:even').addClass('even');
	$('table.list tbody tr:odd').addClass('odd');
});


/**
 * Toolkit
 */
Toolkit = {};

Toolkit.isInt = function(value) {
	var testValue = parseInt(value);
	return (!isNaN(value) && value == testValue && value.toString() == testValue.toString());
};

Toolkit.selectLocation = function(select, target) {
	var target = target || top;
	if (select.options[select.selectedIndex].value)
		target.location.href = select.options[select.selectedIndex].value;
};

/**
 * Opens popup window
 *
 * @param string location -- URL
 * @param string name -- name of the popup window
 * @param int width -- optional width in pixels
 * @param int height -- optional height in pixels
 * @param int left -- optional left in pixels
 * @param int right -- optional top in pixels
 * @return Object window
 */
Toolkit.openPopup = function(location, name, width, height, left, top) {
	var width = width || 640;
	var height = height || 480;
	var left = left || (screen.width - width) / 2;
	var top = top || (screen.height - height) / 2;

	var features = "width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",alwaysRaised=yes,dependent=yes";

	return window.open(location, name, features);
}
