function initFancies() {
	// Fancy beállítása
	$("a.fancy").fancybox({
        'titleFormat'		: formatTitle,
		'padding'           : 14,             //Space between FancyBox wrapper and content
        'overlayOpacity'    : 0.4,          //Opacity of the overlay (from 0 to 1; default - 0.3)
        'overlayColor'      : '#000',       //Color of the overlay
        'titlePosition'     : 'over',     //The position of title. Can be set to 'outside', 'inside' or 'over'
        'showCloseButton'   : false
	});

	// Fancy beállítása
	$("a.fancy_html").fancybox({
        'titleFormat'		: formatTitle,
		'padding'           : 14,             //Space between FancyBox wrapper and content
        'overlayOpacity'    : 0.4,          //Opacity of the overlay (from 0 to 1; default - 0.3)
        'overlayColor'      : '#000',       //Color of the overlay
        'titlePosition'     : 'over',     //The position of title. Can be set to 'outside', 'inside' or 'over'
        'showCloseButton'   : false,
		'type'				: 'inline'
	});

	$("a.fancy_orige").fancybox({
		'overlayColor'		: '#000',
		'overlayOpacity'	: '0.4',
		'titlePosition'     : 'over',
		'showCloseButton'   : true,
        'padding'           : 14
    });
}

$(document).ready(function() {

	initFancies();

	changeCss();	

	//$('#items_list').preloader();
	
	// Lábléc (menü) hover kezelése
	$('#footer-box').hoverIntent({
		timeout: 200,
		over: openFooter,
		out: checkHeight
	});

	// Menü automatikus kinyitása, ellenőrzés, hogy van e hely
	checkHeight();

	// Ablakméretezés figyelése
	$(window).resize(function() {		
		checkHeight();
	});

	// Láblécben a menüpontok szélességének beállítása - IE7 FIX, de minden böngészőben lefut
	/*$('.footer_box').not('.footer_box_none').each(function() {
		$(this).children().width($(this).width()-33);
		$(this).width($(this).width());
	});*/

	// labléc - hover/current kezelés
	$('.footer_box').mouseover(function(){
		$('.footer_right a.'+this.id).css('color', '#A1A1A1');
	});
	$('.footer_box').mouseleave(function(){
		$('.footer_right a.'+this.id).not('.footer_right a#current.'+this.id).css('color', '#AC8F1E');
	});
	$('.footer_right a').mouseover(function(){
		$(this).css('color', '#A1A1A1');
		$('#'+$(this).attr('class')+'.footer_box').addClass('current_hover');
	});
	$('.footer_right a').mouseleave(function(){
		if (this.id != 'current')
			$(this).css('color', '#AC8F1E');

		$('#'+$(this).attr('class')+'.footer_box').removeClass('current_hover');
	});

	// Főoldali szöveges box fade-elése
	$('.content_box_layer').fadeIn();

	// Főoldali termékajánló elrejtése
	$('#product-box-show').hide();

	// Főoldali termékajánló megjelenítése
	$('#product-box-show').click(function(){
        productBoxShow();
    });
	$('#product-box-close').click(function(){
        productBoxHide2();
    });

	// Jobb felső sarokban lévő ikonok kezelése
	$('.menus a').hover(function(){    
		$('.menus a').removeClass('current');
		var cName = this.className;
		$('#'+cName+'-box').show();
		$('.top_box').not('#'+cName+'-box').hide();
		$(this).addClass('current');
	});
	$('.top_box, .top_menu').mouseleave(function(){
		$('.top_box').hide();
		$('.menus a').removeClass('current');
	});
	//----

	/*// Friss - régi
	$jScroller.config.refresh = 60;
	$jScroller.add("#scroller-container", "#scroller", "left", 1 ,true);
	$jScroller.start();
	*/

	// Szavak első betűjének "uppercase"-elése
	$('h1.title span.title, .menu_layer h1.title span.title_sub2, h1.title span.title_sub, .link_container div.items_caps div a, div#footer-box a, .items_videos p.caps_text, .items_caps2 a span, div.links a').not('div.nocaps a').each(function(index) {
		var text = $(this).html();
		$(this).html(text.replace(/^([A-Za-záéíóúöőüűÁÉÍÓÚÖŐÜŰ])/g,'<span class="caps">$1</span>'));		
	});
	$('.only_image a span, .menu_layer a').each(function(index) {
		var text = $(this).html();
		$(this).html(text.replace(/(^| )([A-Za-záéíóúöőüűÁÉÍÓÚÖŐÜŰ])/g,'<span class="caps">$1$2</span>'));
	});

	// Bal felső almenük kezelése
	$('h1.title').hover(function(){
		$('.menu_layer').show();
    });
	$('.menu_layer').mouseleave(function(){
		$('.menu_layer').hide();
    });

	$('#container').bind('click', function(){
		$('.menu_layer').hide();
		$('.top_box').hide();
    });

	// Cufon replaces - hover események kezelése
	$('.box_left a').mouseover(function () {
		Cufon.replace($('span.title', this), { color: '#A1A1A1' });
	});
	$('.box_left a').mouseout(function () {
		Cufon.replace($('span.title', this), { color: '#ac8f1e' });
	});

	$('.list a.item').mouseover(function () {
		Cufon.replace($('span.title', this), { color: '#A1A1A1' });
	});
	$('.list a.item').mouseout(function () {
		Cufon.replace($('span.title', this), { color: '#ac8f1e' });
	});

	($('.list-count').text() == 0) ? $('#list-more').hide() : $('#list-more').show();
	($('.store-count').text() == 0) ? $('#basket-more').hide() : $('#basket-more').show();
});

function callBasketAlert() {
	var alerttitle = {
		'hu' : '',
		'en' : '',
		'de' : ''
	};

	var alerttext = {
		'hu' : '<span>A termék bekerült a kosarába!</span>További termékeket tehet kosarába, vagy véglegesítheti<br />vásárlását, a felül található ikon segítéségével.',
		'en' : '<span>The item has been added to your cart.</span> You may add further items or finalise <br />your order by clicking on the <img src="/gfx/top_menu/basket.png" alt="" border="0" width="18" height="16" style="vertical-align: middle;" /> icon above.',
		'de' : ''
	};
	   
	jAlert(alerttext[_lang], alerttitle[_lang], 'alert');

	setTimeout(function(){
      $('#popup_ok').trigger('click');
	}, 5000);	

	return false;
}

function callListAlert() {
	var alerttitle = {
		'hu' : '',
		'en' : '',
		'de' : ''
	};

	var alerttext = {
		'hu' : '<span>A termék bekerült az ajánlatkérő listájába!</span>További termékeket tehet a listába, vagy elküldheti,<br /> a felül található <img src="/gfx/top_menu/offer.png" alt="" border="0" width="16" height="16" style="vertical-align: middle;" /> ikon segítéségével.',
		'en' : '<span>The item has been added to your cart. </span>You may add further items or finalise <br />your order by clicking on the <img src="/gfx/top_menu/offer.png" alt="" border="0" width="16" height="16" style="vertical-align: middle;" /> icon above.',
		'de' : ''
	};
	   
	jAlert(alerttext[_lang], alerttitle[_lang], 'alert');

	setTimeout(function(){
      $('#popup_ok').trigger('click');
	}, 5000);	

	return false;
}

function countryAlert() {
	var alerttitle = {
		'hu' : '',
		'en' : '',
		'de' : ''
	};

/*
	var alerttext = {
		'hu' : 'Felhívjuk figyelmét, hogy a szállítási költség az Ön országába jelentős, adott eesetben több is lehet, mint a rendelni kívánt termékek együttes értéke! Kérjük csak akkor folytassa a tranzakciót, ha ezt tudomásul veszi és vállalja!',
		'en' : '',
		'de' : ''
	};
*/

	var alerttext = {
		'hu' : '<strong style="line-height: 35px;">Minimális rendelési érték 300.000 Ft!</strong><br />Felhívjuk figyelmét, hogy webshopon keresztül történő vásárlás esetében ÁFA visszatérítésre nincs lehetőség!',
		'en' : '<strong style="line-height: 35px;">Minimum order value: HUF 300,000.</strong><br /> Please note that VAT cannot be reclaimed on purchases made through our webshop.',
		'de' : ''
	};

	jAlert(alerttext[_lang], alerttitle[_lang], 'alert', function(r) {
		/*if (r) {
			location.href='/hu/vasarlas/penztar/';
		}*/
	});
	   
	//jAlert(alerttext[_lang],  alerttitle[_lang], 'alert');

	return false;
}

function changeCss() {
	var color = cookieControl.get('color');

    if (color) {
		/*var link = $('<link>');
		link.attr({
			type: 'text/css',
			rel: 'stylesheet',
			href: '/css/styles_'+color+'.css'
		});
		$("head").append(link);*/

		$("head").append("<link>");
		var css = $("head").children(":last");
		css.attr({
			rel:  "stylesheet",
			type: "text/css",
			href: '/css/styles_'+color+'.css'
		});

        //$('<link href="/css/styles_'+color+'.css" rel="stylesheet" type="text/css" media="screen, print" />').appendTo('head');

        $('#text-box a').removeClass('current');

        if (color == 'dark') {
            $('#text-box a.text1').addClass('current');

        } else if (color == 'light') {
            $('#text-box a.text2').addClass('current');

        } else if (color == 'white') {
            $('#text-box a.text3').addClass('current');
        }
    }		
}

// Bal felső almenü elrejtése
function hideMenuLayer() {
	$('.menu_layer').animate({top: '-180px'}, 300, function(){});
}

// Termékajánló box megjelenítése/elrejtése
function productBoxShow() {
	$('#product-box-show').hide();
	$('#product-box-close').show();
	$('#product-box').animate({left: '0'}, 300, function(){});
}
function productBoxHide2() {
	$('#product-box-close').hide();
	$('#product-box-show').show();
	$('#product-box').animate({left:'-180px'}, 200, function(){});
}
function productBoxHide() {
	$('#product-box-close').hide();
	//$('#product-box-show').show();
	$('#product-box').animate({left:'-180px'}, 200, function(){});	
}

// Lábléc menü kinyitása
function openFooter() {
	cookieControl.set('menuState', 'zarva','1','','','');

	$('#footer-box').animate({bottom: '0'}, 100, function(){});
	$('.footer_header .menu').addClass('open');

	if (openDisabled()) {
		$('.footer_logo').animate({bottom: '-120px'}, 100, function(){});
	}	
}

// Lábléc menü bezárása
function closeFooter() {
	$('#footer-box').animate({bottom :'-205px'}, 100, function(){});
	$('.footer_header .menu').removeClass('open');

	if (openDisabled()) {
		$('.footer_logo').animate({bottom: '10px'}, 100, function(){});
	}	
}

// Fancy title felülbírálása
function formatTitle(title, currentArray, currentIndex, currentOpts) {
	var pagerLeft = '';
	var pagerRight = '';

	if (currentIndex > 0){
		pagerLeft = '<a href="javascript:;" onclick="$.fancybox.prev();" class="fancy_prev">&nbsp;</a>';
	}
	if (currentIndex+1 < currentArray.length){
		pagerRight = '<a href="javascript:;" onclick="$.fancybox.next();" class="fancy_next">&nbsp;</a>';
	}

    return '<div id="fancy-title">'+pagerRight+pagerLeft+'<a href="javascript:;" onclick="$.fancybox.close();"><img src="/gfx/fancybox/fancy_close.png" border="0" width="15" height="15" class="fancy_close" /></a>' + (title && title.length ? '<span class="title">' + title + '</span>' : '' ) + '</div>';
}

function openDisabled() {
	var contentHeight = 450 + 285;
	var browserHeight = $(window).height();

	return (contentHeight > browserHeight);
}

// Magasság figyelése
function checkHeight() {	
	(openDisabled() || cookieControl.get('menuState') == 'nyitva') ?  closeFooter() : openFooter();
}

// Lábléc menü bezárás figyelése + Cookie kezelés
function checkClose() {
	var menuState = 'zarva';

	if (cookieControl.get('menuState') == 'zarva') {
		menuState = 'nyitva';

	} else {
		menuState = 'zarva';
	}

	cookieControl.set('menuState', menuState,'1','','','');

	checkHeight();
}

// Webshop support functions
function addList(id, alertmsg, pretag, count) {
	
	if (alertmsg == null)	{
		alertmsg = true;
	}

	if (pretag == null)	{
		pretag = '';
	}

	if (count == null) {
		if ( !$('#'+pretag+'count_'+id).val() || $('#'+pretag+'count_'+id).val() == 0 ) return false;
		count = $('#'+pretag+'count_'+id).val()
	}
	
	if (alertmsg && (count > 0)) callListAlert();

	$.ajax({
        'url': '/' + _lang + '/system/store/list/push/' + id + '/' + count + '/',
        'success': function (msg) {
            setBasketInfo(0);
			//$('#'+pretag+'count_'+id).val(1);
        }
    });
}

function addBasket(id, alertmsg, pretag, count) {

	if (alertmsg == null)	{
		alertmsg = true;
	}

	if (pretag == null)	{
		pretag = '';
	}

	if (count == null) {
		if ( !$('#'+pretag+'count_'+id).val() || $('#'+pretag+'count_'+id).val() == 0 ) return false;
		count = $('#'+pretag+'count_'+id).val()
	}
	
	if (alertmsg && (count > 0)) callBasketAlert();

	$.ajax({
        'url': '/' + _lang + '/system/store/basket/push/' + id + '/' + count + '/',
        'success': function (msg) {
            setBasketInfo(0);
			//$('#'+pretag+'count_'+id).val(1);
			//alert('#'+pretag+'sumprice_'+id); 
			$('#'+pretag+'sumprice_'+id).html(msg);
		}
    });
}

function addBasketAndLogInScr(id, alertmsg, pretag, count) {

	if (alertmsg == null)	{
		alertmsg = true;
	}

	if (pretag == null)	{
		pretag = '';
	}

	if (count == null) {
		if ( !$('#'+pretag+'count_'+id).val() || $('#'+pretag+'count_'+id).val() == 0 ) return false;
		count = $('#'+pretag+'count_'+id).val()
	}
	
	if (alertmsg && (count > 0)) callBasketAlert();

	$.ajax({
        'url': '/' + _lang + '/system/store/basket/push/' + id + '/' + count + '/',
        'success': function (msg) {
			window.location = '/' + _lang + '/felhasznalok/regisztracio/webshop/';
		}
    });
}

function setBasketInfo() {
    $.ajax({
        'url': '/'+_lang+'/system/store/info/',
        'dataType': 'script',
        'success': function () {
            $('.list-count').text(listcount);
			$('.store-count').text(count);
            $('.store-price').text(price);
//            $('.store-weight').text(weight);

			(listcount > 0) ? $('#list-more').show() : $('#list-more').hide();
			(count > 0) ? $('#basket-more').show() : $('#basket-more').hide();

			// Üres kosár esetén
			var pathArray = window.location.pathname.split( '/' );
			if ( count == 0 && pathArray[2] == 'vasarlas') {
                window.location.reload();
			}
        }
    });
}

/*
** jquery alerts parameters
*/

if (_lang == 'en'){
	$.alerts.okButton = 'OK';
	$.alerts.cancelButton = 'Cancel';

} else if (_lang == 'de') {
	$.alerts.okButton = 'OK';
	$.alerts.cancelButton = 'Cancel';

} else {
	$.alerts.okButton = 'OK';
	$.alerts.cancelButton = 'Mégsem';
}

$.alerts.draggable = false;
$.alerts.overlayOpacity = .5;
$.alerts.overlayColor = '#000000';

/**/

