
var WebShop = function() {

	var instance = this;

	this.catid = 0;
	this.file = "";

	this.loadHTML = "";

	this.GET = new Array();
	this.PHPGET = new Array();


	instance.initLightBox = function ()
	{
		var dir = 'js/jquery-lightbox-0.5/images/';
		$('a.lightbox').lightBox({
				imageLoading:			dir + 'lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
				imageBtnPrev:			dir + 'lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
				imageBtnNext:			dir + 'lightbox-btn-next.gif',			// (string) Path and the name of the next button image
				imageBtnClose:			dir + 'lightbox-btn-close.gif',		// (string) Path and the name of the close btn
				imageBlank:				dir + 'lightbox-blank.gif',
				activeImage: 1});
	}

	instance.addFavorite = function (title, url)
	{
		if (document.all)
		{
			window.external.AddFavorite(url, title);
		}
		else if (window.sidebar)
		{
			window.sidebar.addPanel(title, url, "");
		}
	}


	instance.getProperty = function(obj,property)
	{
		return typeof obj[property] != 'undefined' ? obj[property] : '';
	}

	instance.parseQuery = function(query)
	{
		var _GET = new Array();
		
		if (query)
		{
			query = query.split('&');
			for(var i in query)
			{
				var tmp = query[i].split('=');
				_GET[tmp[0]] = tmp.slice(1).join('=');
			}
		}
		return _GET;
	}

	instance.setUrl = function()
	{
		var a = window.location.href.split('#');
		var f = a[0].substr(a[0].lastIndexOf('/')+1);

		var qsa = f.split('?');

		instance.file = qsa[0];

		var query = (a.length > 1) ? a[1] : '';

		instance.GET = instance.parseQuery(query);

		instance.PHPGET = instance.parseQuery(qsa.slice(1).join('?'));
	}
	instance.setUrl();
	instance.catid = (parseInt(this.GET.cat).toString() != 'NaN') ? parseInt(this.GET.cat) : 0


	instance.orderProducts = function ()
	{
		$('error').html('');
		$.post('includes/orderProducts.php',{
				order: 1
			},
			function(data,iStatus) {
				$('#kosar').html(data);
			}
		);
	};

	/**
	* Kosár újratöltése
	*
	* Argumentum nélkül csak betölti
	* 2 argumentumal hívva az első a termékid, a második, hogy
	* hozzáadni, vagy visszarakni, vagy törölni akarunk. 
	* Hozzáadni: 'add', visszarakni: 'sub', törölni: 'del'
	* 3 argumentum esetén a 3. a felvevendő, vagy törlendő darabszám
	*/
	instance.reloadBasket = function ()
	{ 
		var params = (arguments.length < 2) ? {} : {
			termekid: arguments[0],
			darab: arguments[1] != 'del' ? $('#kosar'+arguments[1]+'db_'+arguments[0]).val() : 0,
			action: arguments[1]
		}
		if (arguments.length > 2)
		{
			params.darab = arguments[2];
		}
		$.post('includes/smallBasket.php',params,
			function(data,sStatus) {
				$('#kosar').html(data);
			}
		);
		var bigBasket = $('#bigBasket');
		if (bigBasket.length)
		{
			$.post('includes/bigBasket.php',params,
				function(data,sStatus) {
					bigBasket.html(data);
				}
			);
		}
	};

	instance.setPage = function (page)
	{
		instance.setUrl();
		instance.GET.page = page; 
		instance.reloadProductList();
	}

	/**
	 * Termékek újratöltése
	 * 
	 */
	instance.reloadProductList = function ()
	{
		var arg = (arguments.length > 0) ? arguments[0] : new Array();
		//instance.setUrl();
		$('#productList').html(instance.loadHTML);
		var catid = (typeof arg.catid != 'undefined') ? arg.catid : instance.catid;
		var _page = instance.GET.page ? instance.GET.page : 1;
		
		instance.catid = catid;

		if (typeof arg.file != 'undefined' && instance.file != arg.file)
		{
			window.location.href = arg.file+'#cat='+instance.catid;
			return;
		}
		if (typeof instance.PHPGET.search != 'undefined')
		{
			$.get('includes/productList.php',{
					cat: catid,
					page: _page,
					search: instance.PHPGET.search
				},
				function(data,sStatus) {
					$('#productList').html(data);
					instance.initLightBox();
				}
			);
		}
		else
		{
			$.get('includes/productList.php',{
					cat: catid,
					page: _page
				},
				function(data,sStatus) {
					$('#productList').html(data);
					instance.initLightBox();
				}
			);
		}
	}

	instance.setJSCategorymenu = function(id)
	{
		var links = document.getElementById(id).getElementsByTagName('a');
		for(var i=0; i<links.length;i++)
		{
			var href = "#";
			var index = links[i].href.indexOf('?')
			if (index)
			{
				href = '#'+links[i].href.substr(index+1);
			}
			links[i].href = href;
		}
	}

	instance.init = function()
	{

		$(window).load(function() {
			instance.initLightBox();
			instance.reloadBasket();

			var e = $('#productList');
			if (!e.length) return;
			instance.loadHTML = $('#ajaxload').html();//e.html();

			instance.reloadProductList();

			var category = document.getElementById('categorymenu_'+instance.catid);
			if (category)
			{
				instance.openCategory(category);
			}

		});
	}

	instance.show = function (obj)
	{
		obj.style.display = (obj.style.display != 'block') ? 'block' : 'none';
	}

	instance.dropdown = function (link)
	{
		var ul = link.parentNode.getElementsByTagName('ul')[0];
		if (ul)
		{
			instance.show(ul);
		}
	}

	instance.openCategory = function(category)
	{
		category.style.display = 'block';
		var pn = category.parentNode.parentNode;
		if (pn && pn.id != 'categorymenu')
		{
			instance.openCategory(pn)
		}

	}
};

var WS = new WebShop();
WS.init();


