;
jQuery(function() {
	var baseHref = $('#ProjectPath').val();

	function subcategoryToggle(E){
		var level = $(E.target).closest('a').attr('className').match(/level\-(\d+)/)[1];
		$(E.target).closest('li').children('ul').toggleClass('unfolded').toggleClass('unfolded_level-' + level);
		$(E.target).closest('li').children('a').children('span').toggleClass('open');
		return false;
	}

	function basketUpdate(e) {
		$(e.target).closest('form').submit();
	}

	function reloadPage(E) {
		document.location = $(E.target).val();
	}

	function dropmenuClick(e) {
		e.preventDefault();
		var T = $(e.target).closest('.dropmenu').toggleClass('dropmenu-on');
		if (!T.attr('id')) T.attr('id', 'printker-id-' + new Date().getTime());
		if (T.hasClass('dropmenu-on')) {
			$(document).bind('click.dropmenu', function(E2){
				var T2 = $(E2.target).closest('.dropmenu');
				if (!T2.length || (T2.attr('id') != T.attr('id'))) {
					T.removeClass('dropmenu-on');
					$(document).unbind('click.dropmenu');
				}
			});
		}
		if (window.map) {
			map.checkResize();
			map.setCenter(point, 15);
		}
		return false;
	}

	/*
	 * Browsing Product Images
	 */
	function productImageSwap(e) {
		e.preventDefault();
		$('#ProductImage').css(
				'background-image',
				$('#ProductImage').css('background-image').replace(/\/[\d]+\//,
						'/' + e.target.href.fragment() + '/'));
	}

	/*function changeTab(event) {
		alert('Ha ezt látod, szólj, hogy melyik oldalon bukkantál rá!');
		event.preventDefault();
		var
			id = $(event.target).closest('a').attr('href').fragment(),
			c = $(event.target).closest('.tab-area').children('.tab-container'),
			tabs = c.find('.tab-target'),
			active = tabs.filter('.tab-active'),
			next = tabs.filter('#' + id);
		if (!active.length)
			active = tabs.filter(':first');
		if (active.attr('id') == id)
			return;
		$(event.target).closest('.tab-area')
				.find('.tab-button').removeClass('active');
		$(event.target).closest('.tab-button').addClass('active');
		active.removeClass('tab-active').css( {
			position : 'absolute',
			left : '1000px'
		});
		next.addClass('tab-active').css( {
			position : 'relative',
			left : '0px'
		});
	}*/

	/*
	 * Event bindings follow
	 */

	// Login & Search
	$('.hint').addClass('inu-inputhint-field').inputHint({
		hintClass : 'inu-inputhint-hint'
	});
	// Dropmenus (Login etc.)
	$('.dropmenu-button').click(dropmenuClick);

	// Tabbing
	//$('.tab-button').click(changeTab);

	// Updating Basket
	$('#Basket .basket-product label input').change(basketUpdate);

	// Browsing Product Images
	$('#ProductLeft ul li').click(productImageSwap);

	// Loading category subtree
	$('#CategoriesTree.extended .level-1.has-children > .cat-control').click(function(E){
		if (!$(E.target).parent().parent().find('ul').length) {
			jQuery.get(
				baseHref + 'ajax/tree/' + $(E.target).parent().attr('id').replace('category-', ''),
				function(d){
					$(E.target).parent().parent().append(d);
				}
			);
		}
	});

	// CategoryTree
	$('#CategoriesTree.extended .has-children span.cat-control').live('click', subcategoryToggle);

	// ProductsListOrder
	$('#ProductsListOrder').change(reloadPage);

	// Change Language
	$('#Language > select').change(function(E){
		$('#Language').submit();
	});

	// Form submitting anchors
	$('.form-submit-trigger').click(function(E){
		E.preventDefault();
		$(E.target).closest('form').submit();
		return false;
	});

	if (typeof customInit == 'function') customInit();

	$(/*'.error-close'*/'#Error').click(function(){
		$('#Blank').fadeOut();
		$('#Error').fadeOut();
	});



	/*
	 * Discount checkbox
	 */	
	$('#ProductsDiscounted :checkbox').click(function(e){
		$(this).closest('label').toggleClass('on');
	});
	
	/*
	 * Tiptip
	 */
		$(".help-icon").tipTip({defaultPosition:'top'});

	/*
	 * compare function
	 */	
	$('.product-box :checkbox').click(function(e){
		if (this.checked == true) {$(e.target).closest(".compare-ch").addClass("on");}
		else {$(e.target).closest(".compare-ch").removeClass("on");}
	});
	var checkboxes = $('.product-box :checkbox');
	var checkboxesfunc = function(E) {
		if (checkboxes.filter(':checked').length < 2) {
			$('#ProductCompareButton input').each(function(){
				this.disabled = true;
				if ($('#ProductCompareButton .on')) {
					$('#ProductCompareButton').removeClass('on');
				}
			});
		} else {
			$('#ProductCompareButton input').each(function(){
				$('#ProductCompareButton').addClass('on');
				this.disabled = false;
			});
		}
		if (checkboxes.filter(':checked').length >= 2) {
			checkboxes.each(function(){
				if (!this.checked)
					this.disabled = true;
			});
		} else {
			checkboxes.each(function(){
				this.disabled = false;
			});
		}
	}
	checkboxes.click(checkboxesfunc);
	$('#ProductCompareButton input').click(function(E){
		var ids = [];
		checkboxes.filter(':checked').each(function(){
			ids.push($(this).val());
		});
		location.href = $('#ProjectPath').val() + 'osszehasonlitas/' + ids.join(';');
	});

	$('.switch').click(function(){
		$('.switch-tab').hide().children(':input').each(function(){
			this.disabled = true;
		});
		$('#' + $(this).attr('id') + '_tab').show().children(':input').each(function(){
			this.disabled = false;
		});
	});
});
String.prototype.fragment = function() {
	return this.substr(this.indexOf('#') + 1);
};
try {
    document.execCommand( "BackgroundImageCache", false, true );
} catch( e ) { };
