/* Inheritance plugin */
(function($) {
var
	hasIntrospection = (function(){_}).toString().indexOf('_') > -1,
	emptyBase = function() {}
	;

$.inherit = function() {

	var
		hasBase = $.isFunction(arguments[0]),
		base = hasBase? arguments[0] : emptyBase,
		props = arguments[hasBase? 1 : 0] || {},
		staticProps = arguments[hasBase? 2 : 1],
		result = props.__constructor || base.prototype.__constructor?
			function() {
				this.__constructor.apply(this, arguments);
			} : function() {},
		inheritance = function() {}
		;

	$.extend(result, base, staticProps);

	inheritance.prototype = base.prototype;
	result.prototype = new inheritance();
	result.prototype.self = result.prototype.constructor = result;

	var propList = [];
	$.each(props, function(i) {
		if(props.hasOwnProperty(i)) {
			propList.push(i);
		}
	});
	$.each(['toString', 'valueOf'], function() {
		if(props.hasOwnProperty(this) && $.inArray(this, propList) == -1) {
			propList.push(this);
		}
	});

	$.each(propList, function() {
		if(hasBase
			&& $.isFunction(base.prototype[this]) && $.isFunction(props[this])
			&& (!hasIntrospection || props[this].toString().indexOf('.__base') > -1)) {

			(function(methodName) {
				var
					baseMethod = base.prototype[methodName],
					overrideMethod = props[methodName]
					;
				result.prototype[methodName] = function() {
					var baseSaved = this.__base;
					this.__base = baseMethod;
					var result = overrideMethod.apply(this, arguments);
					this.__base = baseSaved;
					return result;
				};
			})(this);

		}
		else {
			result.prototype[this] = props[this];
		}
	});

	return result;

};

})(jQuery);

/* $.uid plugin */
jQuery.fn.id = function()
{  
	if ((jQuery.trim(this.eq(0).attr('id'))).length==0)
	{
		this.eq(0).attr('id', ('uid'+Math.random()).replace('.','') );
	}
	return this.eq(0).attr('id');
};


/* Favorite products */
var favorite_products = function ()
{
	var that = {};
	
	that.init = function(options)
	{
		that.icons = $('div.main_content div.add-2-fav');
		that.init_fav_list();
		that.icons.each(function(){
			$(this).show();
			if ( jQuery.inArray($(this).attr('pid'), that.fav_list) >=0 )
			{
				$(this).addClass('active');
				$(this).attr('title', options.rem_label);
				$(this).find('img').get(0).alt = options.rem_label;
			}
			else
			{
				$(this).attr('title', options.add_label);
				$(this).find('img').get(0).alt = options.add_label;
			}
			$(this).click(function(){
				var pid = $(this).attr('pid');
				if ($(this).hasClass('active'))
				{
					var pos = -1;
					for (var i=0,c=that.fav_list.length; i<c; i++)
					{
						if (that.fav_list[i] == pid) pos = i;
					}
					if (pos>=0) that.fav_list.splice(pos, 1);
					$(this).attr('title', options.add_label);
					$(this).find('img').get(0).alt = options.add_label;
				}
				else
				{
					that.fav_list.push(pid);
					$(this).attr('title', options.rem_label);
					$(this).find('img').get(0).alt = options.rem_label;
				}
				
				Cookie.createCookie('fav', that.fav_list.join(','), 365);
				
				$(this).toggleClass('active');
			});
		});
	};
	
	that.init_fav_list = function()
	{
		var fav_string = Cookie.readCookie('fav');
		if (fav_string != null)
		{
			that.fav_list = fav_string.split(',');
		}
		else
		{
			that.fav_list = [];
			Cookie.createCookie('fav', '', null);
		}
	};
	
	return that;
};

/* Cookie wrapper */
var Cookie = {
	createCookie: function (name, value, days)
	{
		var date, expires;
		if (days) {
			date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			expires = "; expires=" + date.toGMTString();
		}
		else {
			expires = "";
		}
		document.cookie = name + "=" + value + expires + "; path=/";
	},

	readCookie: function (name)
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		var i, c;
		for (i = 0; i < ca.length; i += 1) {
			c = ca[i];
			while (c.charAt(0) === ' ') {
				c = c.substring(1, c.length);
			}
			if (c.indexOf(nameEQ) === 0) {
				return c.substring(nameEQ.length, c.length);
			}
		}
		return null;
	},

	eraseCookie: function (name)
	{
		createCookie(name, "", -1);
	}
};

/* Placeholder */
var Placeholder = {

	make: function (elem, class_empty)
	{
		class_empty = (typeof class_empty === 'string') ? class_empty : 'empty';

		$(elem).focus(function () {
			if (this.value === $(this).attr('placeholder')) {
				this.value = '';
			}
			$(this).removeClass(class_empty);
		});

		$(elem).blur(function () {
			if (!this.value.length) {
				this.value = $(this).attr('placeholder');
				$(this).addClass(class_empty);
			}
		});

		if (!elem.value.length) {
			$(elem).blur();
		}
	}
};

/* Search form */
var search_form = function ()
{
	var that = {};
	
	that.init = function(root_elem)
	{
		that.text_elem = root_elem.find('input[name=text]');

		root_elem.find('input[placeholder]').each(function () {
			Placeholder.make(this);
		});
		
		root_elem.find('form').submit(function() {
			var text = ''+that.text_elem.val();
			return !that.text_elem.hasClass('empty') && (text.replace(/\s+/,'')).length > 0;
		});
		
		root_elem.find('#sample_query').click(function() {
			that.text_elem.val($(this).text());
			that.text_elem.focus();
		});
	};
	
	return that;
};
$(document).ready(function(){
	search_form().init($('#search_form'));
});

var Bg = {
	version: '0.1a',
	author: 'Anton Lysenko', 
	copyright: 'Anton Lysenko <mail@antonlysenko.com>'
};

/*
Bg.Modal
@require Bg
@require jquery.ingerit.js plugin
*/
Bg.Modal = $.inherit(
{
	/*
	 * hParam
	 * ------
	 * container
	 * fader
	 * open
	 * close
	 */
	__constructor: function(hParam)
	{
		this.jContainer=hParam.container;
		this.jFader=hParam.fader;
		this.jOpen=hParam.open;
		this.jClose=hParam.close;
		
		this.bKeep=false;
		
		this.jContainer.click(function(self){return function(event)
		{
			self.bKeep=true;
		}}(this));
		
		/*
		this.cbDocumentClick = function(self){return function(event)
		{
			self.hide(event);
		}}(this);
		*/
		/*
		this.cbDocumentKeyDown = function(self){return function(event)
		{
			self.cancel(event);
		}}(this);
		*/

		this.jOpen.click(function(self){return function(event)
		{
			self.toggle(event);
		}}(this));

		this.jClose.click(function(self){return function(event)
		{
			self.toggle(event);
		}}(this));
	},
	
	toggle: function(event)
	{
		this.stopEvent(event);

		if(this.jContainer.hasClass("hidden"))
			this.show(event);
		else
			this.hide(event);
	},

	hide: function(event)
	{
		if(this.bKeep)
		{
			this.bKeep=false;
			return;
		}
		this._hide();
	},
	
	_hide: function()
	{
		this.jFader.addClass("hidden");
		this.jContainer.addClass("hidden");

		//$(document).unbind("click", this.cbDocumentClick);
		$(document).unbind("keydown", this.cbDocumentKeyDown);
	},

	cancel: function(event)
	{
		var code = event.keyCode ? event.keyCode : event.which ? event.which : null;
		if (code === 27) // Esc
			this.hide(event);
	},

	show: function(event)
	{
		this.jFader.removeClass("hidden");
		this.jContainer.removeClass("hidden");

		//$(document).click(this.cbDocumentClick);
		$(document).keydown(this.cbDocumentKeyDown);
	},
	
	stopEvent: function(event)
	{
		event.preventDefault();
		event.stopPropagation();
	}
});

