if(!DC){
	var DC = {};
}



DC = {

	init : function() {

		DC.CAROUSEL.init();
		DC.FORM.init();
		DC.ANCHORLINKS.init();
		DC.GALLERY.init();
		DC.SLIDER.init();

	}

}

DC.SLIDER = {

	containerId			: "slider",
	slideClass			: "slide",
	slidesNum			: null,
	positions			: null,
	autoPlay			: false,
	slideTimer			: false,
	slideTiming			: 6000,
	transitionTiming	: 800,
	activeSlide			: 1,


	init: function() {

		if (!$('#'+this.containerId).length)
			return ;

		if (!this.initSlides()) return ;
		this.setupNavigation();
		this.initMouse();

		this.play();

	},

	initMouse : function() {

		$('#'+this.containerId).hover(
			function () { DC.SLIDER.pause(); },
			function () { DC.SLIDER.play(); }
		);

		$('#slider td.navigation>a').each(function() {
			$(this).click(function(e) {
				DC.SLIDER.show($(this).attr('dcshow'));
				e.preventDefault();});
		});
	},

	initSlides : function() {

		var slides = $('#'+this.containerId+' .'+this.slideClass);

		if ( slides.length < 2 ) return false;

		// duplicate the first slide
		$('#'+this.containerId+' a.'+this.slideClass+':first').clone().appendTo('#'+this.containerId+'>div');

		slides = $('#'+this.containerId+' .'+this.slideClass);

		var slidesNum = 0;
		var positions = new Array();
		slides.each(
			function() {
				slidesNum++;
				$(this).attr('dcslide', slidesNum);
				var pos = $(this).position();
				positions[slidesNum] = pos.left;
			});

		this.slidesNum = slidesNum;
		this.positions = positions;

		return true;

	},

	setupNavigation : function() {

		i = 0;
		nav = '';
		while (i<(this.slidesNum-1)) {
			nav+='<a href="javascript:;" dcshow="'+(i+1)+'"></a>';
			i++;
		}

		$('#'+this.containerId+' td.navigation').html(nav);
		$('#slider a[dcshow="1"]').addClass('active');

	},

	play : function() {

		if (DC.SLIDER.slideTimer != false) clearTimeout(DC.SLIDER.slideTimer);
		DC.SLIDER.slideTimer = setTimeout('DC.SLIDER.show("next")', DC.SLIDER.slideTiming-DC.SLIDER.transitionTiming);
		DC.SLIDER.autoPlay = true;

	},

	pause : function() {

		if (DC.SLIDER.slideTimer != false) clearTimeout(DC.SLIDER.slideTimer);
		DC.SLIDER.autoPlay = false;

	},

	show : function(slide) {

		if (DC.SLIDER.slideTimer != false) clearTimeout(DC.SLIDER.slideTimer);
		if (DC.SLIDER.autoPlay) DC.SLIDER.slideTimer = setTimeout('DC.SLIDER.show("next")', DC.SLIDER.slideTiming);

		if (slide == "next") DC.SLIDER.activeSlide++;
		else if (slide == "prev") DC.SLIDER.activeSlide--;
		else DC.SLIDER.activeSlide = slide;

		if (DC.SLIDER.activeSlide == 0) {

//			console.log('yep');
			$('#slide_slice').css({'left': '-'+(DC.SLIDER.positions[DC.SLIDER.slidesNum]+'px')});
			DC.SLIDER.activeSlide = DC.SLIDER.slidesNum-1;
			$('#slide_slice').animate(
				{'left' : '-'+DC.SLIDER.positions[DC.SLIDER.activeSlide]+'px'},
				DC.SLIDER.transitionTiming);

		} else if (DC.SLIDER.activeSlide == DC.SLIDER.slidesNum) {

			DC.SLIDER.activeSlide = 1;
			$('#slide_slice').animate(
				{'left' : '-'+DC.SLIDER.positions[DC.SLIDER.slidesNum]+'px'},
				DC.SLIDER.transitionTiming,
				function() {$(this).css({'left': 0})});

		} else
			$('#slide_slice').animate(
				{'left' : '-'+DC.SLIDER.positions[DC.SLIDER.activeSlide]+'px'},
				DC.SLIDER.transitionTiming);

		$('#slider td.navigation>a').removeClass('active');
		$('#slider a[dcshow="'+DC.SLIDER.activeSlide+'"]').addClass('active');

//		console.log(DC.SLIDER.activeSlide);

	},

	_autoPlay : function() {}

}

DC.GALLERY = {

	init : function() {

		$('div.gallery a.image').fancybox({
			'overlayOpacity'		: 0.9,
			'speedIn'				: 500,
			'speedOut'				: 500,
			'centerOnScroll'		: true,
			'titleShow'				: false,
			'overlayColor'			: '#000',
			'transitionIn'			: 'elastic'
		});

	}

}

DC.ANCHORLINKS = {

	init : function() {

		$('a[href^="#"]:not(.disabled)').live('click', function(e) {

			var anchor = '';
			var pos = 1;

			// a link kezelésének megszakítása
			e.preventDefault();

			// a hivatkozásból kinyerjük az anchor nevét
			anchor = ($(this).attr('href'));

			if (anchor.length>1)
				anchor = $(anchor);
			else
				anchor = '';

			DC.ANCHORLINKS.scrollTo(anchor);

		});

		$('a[href^="#"].disabled').live('click', function(e) {e.preventDefault()});

	},

	scrollTo : function(e) {

		var pos = 1;

		if ($(e).length)
			pos = $(e).offset().top;

		if($.browser.opera)
			$('html').animate({scrollTop: pos}, 800);
		else
			$('html,body').animate({scrollTop: pos}, 800);

	}

}


DC.FORM = {

	submitText : '',

	init : function() {

		if (!$('#form').length)
			return ;

		this.submitText = $("#form a.submit span").text();

		DC.FORM._initHelp();
		DC.FORM._initErrors();
		DC.FORM._initMouse();

	},

	_initForm : function() {

	},

	_initHelp : function() {

		$('#form span.help').css({'display' : 'block', 'opacity' : 0});

		$('#form span.help').prev().focus(
			function() {
				$(this).next().animate({'right' : '-173px', 'opacity' : '1'}, 500);
			}
		);
		$('#form span.help').prev().blur(
			function() {
				$(this).next().animate({'right' : '-233px', 'opacity' : '0'}, 200);
			}
		);

	},

	_initErrors : function() {
		$('#form input.error').live('keypress', function(){
			$(this).removeClass('error');
			var field = $(this).attr('id');
			$('label[for="' + field + '"]').next().remove();
		});
		$('#form textarea.error').live('keypress', function(){
			$(this).removeClass('error');
			var field = $(this).attr('id');
			$('label[for="' + field + '"]').next().remove();
		});
	},

	_initMouse : function() {
		$('#form a.button.submit').click(DC.FORM.submit);
	},

	submit : function(e) {
		
		if ($(this).hasClass('operating'))
			return ;
		if (!DC.FORM.validate())
			return ;

		var dataString = $('#form>form').serialize();
		var dataUrl = $('#form>form').attr('action');

		DC.FORM.switchOnLoader();

		$('#form span.error').remove();
		$('#form p').remove();

		$.ajax({
			type: "POST",
			url: dataUrl,
			data: dataString,
			dataType: "json",
			success: DC.FORM.onSuccess,
			error: DC.FORM.onError,
			complete : DC.FORM.scrollTop
		});


	},

	switchOnLoader : function() {
		$('#form input').add('#form textarea').attr('disabled', 'disabled');
		$("#form a.submit span").text('Please wait...').addClass('operating');
	},

	switchOffLoader : function() {
		$("#form a.submit span").text(DC.FORM.submitText).removeClass('operating');
	},

	scrollTop : function() {
		DC.ANCHORLINKS.scrollTo($('a#form_top'));
	},

	onError : function() {
		console.log('on error...');
	},

	onSuccess : function(r) {

		if (r.success) {
			DC.FORM.showMessage(r.message);
			$('#form dl').slideUp(400, function(){$('#form input').add('#form textarea').removeAttr('disabled');});
		} else {
			$('#form input').add('#form textarea').removeAttr('disabled');
			DC.FORM.showError(r.errors);
		}
		DC.FORM.switchOffLoader();
	},

	validate : function() {
		return true;
	},

	showMessage : function(m) {
		$('#form>form').append(m.message);
		$('#form h3').text(m.title);
	},

	showError : function(error) {

		$('#form h3').after('<p>You have to fill out the highlighted fields to send the form!</p>')

		for (var i in error) {
//			console.log(error[i]);
			$('#'+error[i].field).addClass('error');
			$('label[for="'+error[i].field+'"]').after('<span class="error">' + error[i].text + '</span>');
		}
	}

}


DC.CAROUSEL = {

	// a tároló elem
	containerId : "carousel",

	// a slideok száma
	slidesNum : null,

	// alap állapotban a slideok szélessége: (hely-margó)/slideokszáma
	normalWidth : null,

	// a nyitott állapotú méret: slideméret-(2*margó)
	openedWidth : null,

	// összezárt (másik van nyitva) méret: (hely-openedWidth)/(slideokszáma-1)
	closedWidth : null,

	init : function() {

		if (!$('#'+this.containerId).length)
			return false;

		this.slidesNum = $('#'+this.containerId+' a').length;
		this.container = $('#'+this.containerId);
		this.initPositions();
		this.initMouse();

	},

	initPositions : function() {

		var panels = $('#'+this.containerId+' a');

		panels.each(function(index) {
			$(this).attr('fdid', index+1);
		});

		var containerWidth = $('#'+this.containerId).innerWidth()-320;
		this.normalWidth = Math.floor(containerWidth/panels.length);
		this.openedWidth = $('#'+this.containerId+' a[fdid="1"]').innerWidth()-10;
		this.closedWidth = Math.floor((containerWidth-this.openedWidth)/(panels.length-1));

		if (Math.abs(this.openedWidth) <100)
			setTimeout('DC.CAROUSEL.initPositions()', 100);
		else
			this.change(0);

	},

	initMouse : function() {

		$('#'+this.containerId+' a').hover(
			function() { DC.CAROUSEL.change($(this).attr('fdid')); },
			function() { DC.CAROUSEL.change(0); });

		$('#'+this.containerId+' div.slide_hider').hover(
			function() { DC.CAROUSEL.change(DC.CAROUSEL.slidesNum); },
			function() { DC.CAROUSEL.change(0); });

	},

	change : function(slide) {

		var posX = 0;
		var position = null;
		var speed = 500;

//		console.log('change: '+slide);

		for (var i=1;i<=DC.CAROUSEL.slidesNum;i++) {
			position = $('a[fdid="'+i+'"]').position();
			if (posX != 0) speed = Math.abs(position.left - posX - 320)*5;
			$('a[fdid='+i+']').stop().animate({'left': posX+320+'px'}, speed);
			if (slide==0) posX += DC.CAROUSEL.normalWidth;
			else if (i==slide) posX += DC.CAROUSEL.openedWidth;
			else posX += DC.CAROUSEL.closedWidth;
		}

	}

}



$(document).ready(function() {
  $('.button').bind({
    mousedown	: function() { $(this).addClass('mousedown'); },
    blur		: function() { $(this).removeClass('mousedown'); },
    mouseup		: function() { $(this).removeClass('mousedown'); },
	mouseover	: function() { $(this).addClass('hover'); },
	mouseout	: function() { $(this).removeClass('hover');
	}
   });
  
  $('#display.products div.product_category').bind({
	mouseover	: function() { $(this).addClass('hover'); },
	mouseout	: function() { $(this).removeClass('hover'); }
  });

   DC.init();
   
});
