$(document).ready(function() {
    $("a.fancy").fancybox({
        'padding'               : 7,
        'overlayOpacity'        : 0.7,
        'overlayColor'          : '#000',
        'titlePosition'         : 'over'
    });
    $("a.fancy").each( function() { this.href = this.href.replace( /\/(hu|de|en)\/(.*)$/, '/$2'); } );


    //category list
    if ( $.cookie('openedtab') ) {
        $('#productslist div').hide();
        $('#'+$.cookie('opened')).show();
        $('#tab'+$.cookie('openedtab')).addClass('current');
    }else {
        $('#productslist div').hide();
        $('#tab3').show();
        $('#tab3').addClass('current');
        $('#'+$.cookie('opened')).show();
        $('#marks').show();
    }

    if ( $.cookie('openedcat') ) {
        $('#productslist ul li ul').hide();
        $('#'+$.cookie('openedprefix')+'_catlist'+$.cookie('openedcat')).show();
        $('#'+$.cookie('openedprefix')+'_cat'+$.cookie('openedcat')).addClass('opened');
    }else {
        $('#productslist ul li ul').hide();
        $('#s_catlist1').show();
        $('#m_catlist1').show();
        $('#s_cat1').addClass('opened');
        $('#m_cat1').addClass('opened');
    }

    if ( $.cookie('openedsubcat') ) {
        $('#productslist ul li ul li ul').hide();
        $('#'+$.cookie('openedsubprefix')+'_catlist'+$.cookie('openedsubcat')).show();
        $('#'+$.cookie('openedsubprefix')+'_cat'+$.cookie('openedsubcat')).addClass('opened');
    }else {
        $('#productslist ul li ul li ul').hide();
        $('#s_catlist1').show();
        $('#m_catlist1').show();
        $('#s_cat1').addClass('opened');
        $('#m_cat1').addClass('opened');
    }

});

function opencat(prefix, id) {
    if ( $('#'+prefix+'_cat'+id).hasClass('opened') ) {
        $('#'+prefix+'_catlist'+id).wsVhide('fast');
        $('#'+prefix+'_cat'+id).removeClass('opened');
    } else {
        $('#'+prefix+'_catlist'+id).wsVshow('fast');
        $('#'+prefix+'_cat'+id).addClass('opened');

		if (prefix == 'c') {
			$.cookie('openedsubcat', id, { path: '/' });
			$.cookie('openedsubprefix', prefix, { path: '/' });
		}
		else {
			$.cookie('openedcat', id, { path: '/' });
			$.cookie('openedprefix', prefix, { path: '/' });
		}
    }
}
function opentab(thisid, id) {
    $('#productslist div').hide();
    $('#tabs a').removeClass('current');
    $('#'+id).show();
    $('#tab'+thisid).addClass('current');
    $.cookie('openedtab', thisid);
    $.cookie('opened', id);
}

function quickShop(item, count) {
	if (item == '') {
        if ( _lang == 'en') {
    		alert('Enter the item code!');
        } else {
    		alert('Kérjük adja meg a cikkszámot!');
        }
		return false;
	}
	if (count < 1 ) {
        if ( _lang == 'en') {
    		alert('Enter the quantity!');
        } else {
    		alert('Kérjük adja meg a kívánt mennyiséget!');
        }
		return false;
	}

	$.ajax({
		url: '/'+_lang+'/store/quickshop/'+item+'/'+count,
		success: function(msg) {
			if (msg) {
				if (msg == 'OK') {
					updateBasket('add');
                    if ( _lang == 'en') {
    					alert('The product is in the basket!');
                    } else {
    					alert('A termék bekerült a kosárba!');
                    }
					location.href = '/'+_lang+'/rendeles/';
				}
				else if(msg == 'UPDATE'){
					updateBasket('update');
                    if ( _lang == 'en') {
    					alert('The product is in the basket!');
                    } else {
    					alert('A termék bekerült a kosárba!');
                    }
					location.href = '/'+_lang+'/rendeles/';
				}
				else if(msg == 'ERR') {
                    if ( _lang == 'en') {
    					alert('The requested product has not been found!');
                    } else {
    					alert('A keresett termék nem található!');
                    }
                }
			}
		}
	});
	return false;
}

function toBasket(id, dist, valt) {
	$.ajax({
		url: '/'+_lang+'/store/set/'+id+'/'+dist,
		success: function(msg){
			if (msg){
				if (msg == 'OK') {
					updateBasket('add');
                }
				else if(msg == 'UPDATE') {
					updateBasket('update');
                }
				else if (msg == 'ERR') {
                    if ( _lang == 'en') {
    					return alert('Attention. The ordered quantity is '+ valt + ' or its multiple.');
                    } else {
    					return alert('Figyelem! A megrendelhető mennyiség '+ valt + ' vagy annak többszöröse.');
                    }
                }
                if ( _lang == 'en') {
                    alert('The product is in the basket!');
                } else {
                    alert('A termék bekerült a kosárba!');
                }
			}
		}
	});
	return false;
}

function delBasket(id) {
	$.ajax({
		url: '/'+_lang+'/store/del/'+id,
		success: function(msg){
			if (msg == 'OK')
				updateBasket('del', id);
		}
	});
	return false;
}


function updateBasket(type) {
	$.ajax({
		url: '/'+_lang+'/store/getinfo/',
		success: function(msg){
			$("#basket").html(msg);
			$("#basket").fadeIn(300);
		}
	});
}

