$(document).ready(function() {
    //$('div#product-gallery div#actual-picture img').resizeElem(500);
    //$('div#product-gallery div#actual-picture img').show();

    $('div#open-company').css('opacity', 0.85);
    $('div#open-form').css('opacity', 1.5);
    
    $('div.action-menu div.action-menu-list').hide();
    
    $('table.standard-list td').click(function() {
    	$('table.standard-list td').removeClass('marked');
    	$('table.standard-list td').removeClass('marker-bg');    	
    	$(this).parent().children().addClass('marked');
    	$(this).parent().children('.marker').addClass('marker-bg');

    	$(this).parent().find('div.action-menu-list').slideToggle('fast');
    	$('div.action-menu div.action-menu-list').hide();
    });
    
    $('div.action-menu div.action-menu-list div.action-menu-item').click(function() {

    	elem = $(this).children('a');
    	input = $(this).find('.del-link');
    	no = $('table.standard-list td.marked div.no').text();
    	input.each(function() {
    		$(this).attr('value', no);
    	});
    	if (elem.hasClass('param'))
    	{
    		if (no == '')
    		{
	        	alert('Please select a line!');
	    		return false;
	    	}
	    	else
	    	{
	        	href = elem.attr('href');
	    		elem.attr('href', href + '/no/' + no);
	    	}
    	}
    });
    
    $('div#lookup-form').hide();
    $('#lookup-table').click(function () {
    	$(this).markRow();
    });
    
    $('#cms-cat-select').change(function () {
    	var url = document.location.href;
    	url = url.replace(/\/category\/[0-9]+/, '');
    	document.location.href = url + '/category/' + $(this).val();;
    });
     
    $('div#tabs div.tab-caption:first').addClass('tab-active');
    $('form.tabbed-form fieldset').parent().hide();
    $('form.tabbed-form fieldset').parent().prev().hide();
    $('form.tabbed-form fieldset#fieldset-general').parent().show();
    $('form.tabbed-form fieldset#fieldset-save').parent().show();
    $('form.line-sub-form .element-label').hide();
    
    $('#categories').find('.cat-link-folder').siblings('ul').hide();
    $('#categories').find('.cat-link-folder').click(function () {
    	$(this).siblings('ul').toggle();
        $.cookie('opencat', $(this).find('a').attr('id').replace('cat-', ''), {path: '/'});
    });
    
    $('.cat-link a').click(function () {
        $.cookie('opencat', $(this).attr('id').replace('cat-', ''), {path: '/'});
    });
    
    //var cats = $('#categories').find('#cat-'+$.cookie('opencat')).parents('ul').show();
    var cats = $('#categories').find('#cat-'+$.cookie('opencat'));
    cats.css('font-weight', 'bold');
    cats.parents('ul').show();
    cats.parent('span').next().next().show();
    
    jQuery.fn.setActiveStep(0);
    
    $(':text, select, input[type=password], textarea').focus(function() {
    	$(':text, select, input[type=password], textarea').removeClass('highlight');
    	$(this).addClass('highlight');
    });
    
    $('fieldset#fieldset-newaddress').hide();
    
    $('div#product-gallery div#pictures div.picture').click(function() {
    	link = $(this).find('img').next('div').text();
    	$('div#product-gallery div#actual-picture').find('img').attr('src', link);
    });
    
    $('.photobox').each(function (i) {
    	id = $(this).attr('id').substring(9);
    	$(this).refreshPhotobox(id); 
    });
    
    $('div#header').everyTime(4000, function(i) {
    	set = $('.photobox');
    	set.each(function (i) {
    		id = $(this).attr('id').substring(9);
    		$(this).refreshPhotobox(id);
    	});
    });
    
    $('div#header').everyTime(4000, function(i) {
        $.fn.refreshItembox('new');
    });    
    
    
    $('div#header').everyTime(4000, function(i) {
        $.fn.refreshItembox('offer');
    });       
        
    currDate = new Date();
    var currYear = currDate.getFullYear() + 10;
    
    $.datepicker.setDefaults($.extend({
    		showMonthAfterYear: false, 
    		changeMonth: true, 
    		changeYear: true,
    		constrainInput: true,
    		yearRange: '1920:' + currYear,
    		showOtherMonths: true
    		//minDate: ''
    	}, $.datepicker.regional[''])
	);
	
	$('.product-picture-fk').hover(
	    function () {
    	    var jq = $(this).find('.product-info-bubble');
    	    if (jq.css('display') == 'none') {
    	       jq.fadeIn('slow');
    	    }
    	    $().mousemove(function (e) {
                jq.css('top', e.pageY + 'px').css('left', e.pageX + 'px');
            });
        },
        function () {
            $(this).find('.product-info-bubble').fadeOut('fast');
        }
    );    
    
    $('#language-change-hu').click(function () {
        $.cookie('language', 'hu', { path: '/'});
        document.location.reload(true);
    });

    $('#language-change-en').click(function () {
        $.cookie('language', 'en', { path: '/'});
        document.location.reload(true);
    });    
    
    $('#language-change-de').click(function () {
        $.cookie('language', 'de', { path: '/'});
        document.location.reload(true);
    });   
    
    $('#blind-change').click(function () {
        if ($.cookie('blind') == null) {
            $.cookie('blind', '1', { path: '/'});
            document.location.reload(true);
        } else {
            $.cookie('blind', null, { path: '/'});
            document.location.reload(true);
        }
    });     
});

jQuery.fn.extend({ 
	ajaxSendForm: function() {
		$.ajax({
			type: "POST",
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				msg = jQuery.trim(msg);
				alert(msg);
			}
		});
		return false;
	},
	ajaxSendRegisterForm: function() {
		$('.ajax-loader').show();
		$.ajax({
			type: "POST",
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				msg = jQuery.trim(msg);
				$('.ajax-loader').hide();
				if (msg == 'Success') {
					//jQuery.fn.setActiveStep(3);
					document.location.reload();
				} else {
					$('#registerForm').html(msg);
					$('#registerForm').show();
				}
			}
		});
		return false;
	},
	ajaxSendBillingAddressForm: function() {
		$('.ajax-loader').show();
		$.ajax({
			type: "POST",
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				msg = jQuery.trim(msg);
				$('.ajax-loader').hide();
				if (msg == 'Success') {
					jQuery.fn.setActiveStep(2);
				} else if (msg == 'SuccessOver') {
					jQuery.fn.setActiveStep(3);
				} else {
					$('#billing-address').html(msg);
					$('#billing-address').show();
				}
			}
		});
		return false;
	},
	ajaxSendShipAddressForm: function() {
		$('.ajax-loader').show();
		$.ajax({
			type: "POST",
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				msg = jQuery.trim(msg);
				$('.ajax-loader').hide();
				if (msg == 'Success') {
					jQuery.fn.setActiveStep(3);
				} else {
					$('#ship-address').html(msg);
					$('#ship-address').show();
				}
			}
		});
		return false;
	},	lookup: function(url, where) {
		offset = $(this).offset();
		$('#lookup-form').css('top', offset.top);
		$('#lookup-form').css('left', offset.left);
		$('#lookup-form').html('<div id="list"><img src="/themes/images/admin/progress.gif" alt="Loading.." /></div>');
		$('#lookup-form').show();
		value = '';
		if (where != '')
		{
			value = $('#' + where).val();
		}
		$.ajax({
			type: "POST",
			url: url,
			data: { field: value },
			success: function(msg) {
				$('#lookup-form').hide();
				$('#lookup-form').html(msg);
				$('#lookup-form').show('normal');
			}
		});		
	},
	ajaxSendShipMethodForm: function() {
		$('.ajax-loader').show();
		$.ajax({
			type: "POST",
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				msg = jQuery.trim(msg);
				$('.ajax-loader').hide();
				if (msg == 'Success') {
					jQuery.fn.setActiveStep(5);
                    $('.ajax-loader').show();
                    $('#order-review').hide();
                    $.ajax({
                        type: "POST",
                        url: '/core/customer/cart/ajaxcheckorder',
                        data: $(this).serialize(),
                        success: function(msg) {
                            msg = jQuery.trim(msg);
                            $('.ajax-loader').hide();
                            $('#order-review').html(msg);
                            $('#order-review').slideDown('normal');
                        }
                    });
				} else {
					$('#ship-method').html(msg);
					$('#ship-method').show();
				}
			}
		});
		return false;
	},
	ajaxSendPaymentMethodForm: function() {
		$('.ajax-loader').show();
		$.ajax({
			type: "POST",
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				msg = jQuery.trim(msg);
				$('.ajax-loader').hide();
				if (msg == 'Success') {
					jQuery.fn.setActiveStep(5);
					$('.ajax-loader').show();
					$('#order-review').hide();
					$.ajax({
						type: "POST",
						url: '/core/customer/cart/ajaxcheckorder',
						data: $(this).serialize(),
						success: function(msg) {
							msg = jQuery.trim(msg);
							$('.ajax-loader').hide();
							$('#order-review').html(msg);
							$('#order-review').slideDown('normal');
						}
					});
				} else {
					$('#payment-method').html(msg);
					$('#payment-method').show();
				}
			}
		});
		return false;
	},
	markRow: function() {
		$(this).parent().find('td').removeClass('marked');
		$(this).find('td').addClass('marked');
    	value = $(this).find('.lookup-no').text();
    	$('#lookup-value').val(value);
	},
	closeLookup: function(field) {
		value = $('#lookup-value').val();
		if (field != '')
		{
			if (value == '')
			{
				$('#lookup-form').hide();
				return false;
			}
			$('#' + field).val(value);
		}
		$('#lookup-form').hide();
	},
	showFormTab: function(tab) {
		$('div#tabs div.tab-caption').removeClass('tab-active');
		$(this).addClass('tab-active');
		$('form.tabbed-form fieldset').parent().prev().hide();
		$('form.tabbed-form fieldset').parent().hide();
		$('form.tabbed-form fieldset#fieldset-' + tab).parent().show();
		$('form.tabbed-form fieldset#fieldset-save').parent().show();
	},
	insertSalesLine: function() {
		$('#sales-line-subform').fadeTo('normal', 0.3);
		$.ajax({
			type: "POST",
			url: $(this).attr('action'),
			data: $(this).serialize(),
			success: function(msg) {
				msg = jQuery.trim(msg);
				$('#sales-line-subform').html(msg);
				$('form.line-sub-form .element-label').hide();
				$('#sales-line-subform').fadeTo('normal', 1);
			}
		});
		return false;
	},
	setActiveStep: function(step) {
		$('#shopping-progress div.shopping-step-header').removeClass('active-step');
		$('#shopping-progress div.shopping-step-content').hide();
		$('#step' + step).find('div.shopping-step-header').addClass('active-step');
		$('#step' + step).find('div.shopping-step-content').show();
		
		return false;
	},
	showAddressForm: function() {
		if ($(this).val() == 'new') {
			$('.element-form div.errors').hide();
			$('.element-form div.errors').prev('.element-label').hide();
			$('fieldset#fieldset-newaddress').show();
		} else {
			$('fieldset#fieldset-newaddress').hide();
		}
	},
	refreshPhotobox: function(id) {
		$.ajax({
			type: 'post',
			url: '/core/index/page/photo',
			data: {photobox : id},
			success: function(msg) {
				msg = jQuery.trim(msg);
				elem = $('#photobox-' + id + ' .photos');
				elem.html(msg);
				
				/*img = elem.find('.photobox-image');
				if (img.width() > img.height()) {
					img.css('width', '190px');
				}
				if (img.height() > img.width()) {
					img.css('height', '190px');
				}*/
				elem.fadeTo(0, 0.1);
				elem.fadeTo(1500, 1.0);
			}
		});
	},
    refreshItembox: function(boxType) {
        $.ajax({
            type: 'post',
            url: '/core/index/page/ajaxitem',
            data: {type : boxType},
            success: function(msg) {
                msg = jQuery.trim(msg);
                elem = $('#itembox-'+boxType);
                elem.html(msg);
                //elem.fadeTo(0, 0.1);
                //elem.fadeTo(500, 1.0);
            }
        });
    },    
	resizeElem: function(width) {
		if ($(this).width() > width) {
			$(this).width(width);
		}
	}
});