var Across = {

    FormType: 'update',
	UpdateData : 0,
	
    setType: function(to)
    {
        Across.FormType = to;
    },

    back: function()
    {
        history.go(-1);
    },

    reloadWin: function(to)
    {
        if (to == false)
        {
            window.location = window.location.href;
        }
        else
        {
            window.location = to;
        }
    },

    clearField: function(item)
    {
        $(item).attr('value', '');
    },
	
    newsletterSubscibe: function(thisFom)
    {
        Across.actualForm = thisFom;

        $.post("/", 'Func=newsletter.subscribe&' + $(thisFom).serialize(), function(data)
        {

            if (data.error)
            {
					var string = '<ul>';
					
					jQuery.each(data.items, function(i, val) {
						string += '<li><label for="newsletter_'+i+'">'+val+'</label></li>';
				    });
					
					string += '</ul><br/>';

					$('#subs-errors').html(string);
            }
            else
            {
                $('.newsletter').html(data.message);
            }
        }, "json");
    },

    sendM: function(thisFom)
    {
        Across.actualForm = thisFom;

        $.post("/", 'Func=contacts.sendMessage&' + $(thisFom).serialize(), function(data)
        {

            if (data.error)
            {
                jQuery.each(data.items, function(i, val)
                {
                    $('#' + i).val(val);
                });
            }
            else
            {

                Across.actualForm.reset();
                alert(data.message);
            }
        }, "json");
    },

    sendStoreM: function(store_email, thisFom)
    {
        Across.actualForm = thisFom;

        $.post("/", 'Func=stores.contact.sendMessage&store_email=' +store_email+ '&' + $(thisFom).serialize(), function(data)
        {

            if (data.error)
            {
                jQuery.each(data.items, function(i, val)
                {
                    $('#' + i).val(val);
                });
            }
            else
            {

                Across.actualForm.reset();
                alert(data.message);
            }
        }, "json");
    },

    initTab: function()
    {
        var tabContainers = $('div.tabcontent > div');
        var tabs = $('div.tabs > a');

        if ($('div.tabcontent > div.active').size() > 0)
        {
            $('div.tabcontent > div').hide().filter('.active').show();

            tabs.removeClass('active');
            tabs.each(function(i)
            {
                if ($(this).attr('rel') == $('div.tabcontent > div.active').attr('id'))
                {
                    $(this).addClass('active');
                }
            });
        }
        else
        {
            tabContainers.hide().filter(':first').show();
        }

        $('.tabs a').click(function()
        {

            tabContainers.hide();
            tabs.removeClass('active');
            $(this).addClass('active');

            $('#' + $(this).attr('rel')).show();
        });
    },

    showImages: function()
    {
        $('div.tabcontent > div').hide();
        $('#images').show();
        $(document).scrollTo($('#images'), 800);

        $('div.tabs > a').each(function(i)
        {

            if ($(this).attr('rel') == 'images')
            {
                $('div.tabs > a').removeClass('active');
                $(this).addClass('active');
            }
        });
    },

    initLeftTabs: function()
    {
        var tabContainers = $('div.left-data > div.boxbody-np');
        var tabs = $('div.left-data > div.tabs-image > a');

        if ($('div.tabs-image > a').size() > 0)
        {
            $('div.left-data > div.boxbody-np').hide().filter('.active').show();

            tabs.removeClass('active');

            tabs.each(function(i)
            {
                if ($(this).attr('rel') == $('div.left-data > div.boxbody-np').filter('.active').attr('id'))
                {
                    $(this).addClass('active');
                }
            });
        }
        else
        {
            tabContainers.hide().filter(':first').show();
        }

        $('div.tabs-image > a').click(function()
        {

            tabContainers.hide();
            tabs.removeClass('active');
            $(this).addClass('active');

            tabContainers.each(function(i, val)
            {
                if ($(this).attr('rel') == $(val).attr('id'))
                {
                    $(this).addClass('active');
                }
            });

            $('#' + $(this).attr('rel')).show();
        });
    },

    copyFields: function(items)
    {
        jQuery.each(items, function(i, val)
        {
            $('#' + val).val($('#' + i).val());
        });
    },

    pushBasket: function(id)
    {
        $.post("/", {
            Func : 'products.basket.pushBasket',
            id   : id,
            qty  : 1
        },
        function(data)
        {
			if(data.url != undefined)
			{
				Across.reloadWin(data.url);
			}
			else if(data.alert != undefined)
			{
				alert(data.alert);
			}
			else
			{
				
				$('.basket-items').html(data.data);
				$('.basket-text').html(data.title);
	
				Across.openBasket();
	
				$(document).scrollTo($('.basket-header'), 800);
				
			}

        }, "json");
    },

    changeAmount : function (item, to)
    {
        $('#basket_'+item).val(to);

        $.post("/", 'Func=products.basket.updateBasket&&reload_data='+Across.UpdateData+'&return=1&' + $('#basket').serialize(), function(data)
        {
            $('.basket-items').html(data.data);
            $('.basket-text').html(data.title);

			if(data.basket)
			{
				$('#data').html(data.basket);	
			}

			if(data.alert != undefined)
			{
				$('#basket_' + data.id).val(data.amount);
				alert(data.alert);
			}
        }, "json");
    },

    updateBasket: function(thisFom)
    {
        Across.actualForm = thisFom;

        $.post("/", 'Func=products.basket.updateBasket&' + $(thisFom).serialize(), function(data)
        {
            //return;
            if (Across.FormType == 'update')
            {
                Across.reloadWin(false);
            }
            else
            {
                Across.reloadWin(data);
            }
        }, "text");
    },
	
    updatePayment: function(thisFom)
    {
        Across.actualForm = thisFom;

        $.post("/", 'Func=products.basket.updatePayment&' + $(thisFom).serialize(), function(data)
        {
			$('#data').html(data.basket);
        }, "json");
    },
	
    updateShipping: function(thisFom)
    {
        Across.actualForm = thisFom;

        $.post("/", 'Func=products.basket.updateShipping&' + $(thisFom).serialize(), function(data)
        {
			$('#data').html(data.basket);
        }, "json");
    },
	
    getStoreCity: function(thisFom)
    {
        Across.actualForm = thisFom;

        $.post("/", 'Func=products.basket.getStoreCity&' + $(thisFom).serialize(), function(data)
        {
			$('#data').html(data.basket);
        }, "json");
    },
	
    getStore: function(thisFom)
    {
        Across.actualForm = thisFom;

        $.post("/", 'Func=products.basket.getStore&' + $(thisFom).serialize(), function(data)
        {
			$('#data').html(data.basket);
        }, "json");
    },

    setStore: function(thisFom)
    {
        Across.actualForm = thisFom;

        $.post("/", 'Func=products.basket.setStore&' + $(thisFom).serialize(), function(data)
        {
			$('#data').html(data.basket);
        }, "json");
    },
	
	checkStore: function(thisForm)
	{

		if ( ($('#shipping_personal').attr('checked') == true) && !$('input.store:checked').val() )
		{
			alert('Nem választott ki átvevőpontot!');
			return false;
		}
		return true;
	},
	
	getCityList : function(id)
	{
		if ( $('#store-list-'+id).css('display') == 'block' )
		{
			$('#store-list-'+id).css('display', 'none');
			return;			
		}
		
		$.post("/", {
            Func 		: 'home.default.getStoreList',
            county_id   : id
        },
        function(data)
        {
            $('#store-list-'+id).html(data);
			$('#store-list-'+id).css('display', 'block');
			$(document).scrollTo($('#store-list-'+id), 1000);

        }, "text");		
	},
	
	getStoreList : function (city_id, county_id)
	{
		$.post("/", {
            Func 		: 'home.getStoreList',
            county_id   : county_id,
			city_id		: city_id
        },
        function(data)
        {
            $('#store-list-'+county_id).html(data);

        }, 'text');
	},
	
	getStoreMap : function(store_id)
	{

		if ($("#store_"+store_id+'> .map').length > 0)
		{
			$('.map').hide();
			$("#store_"+store_id+' .map').show();
			return;
		}
		
		$.post("/", {
            Func 		: 'home.default.getStoreMap',
			store_id		: store_id
        },
        function(data)
        {
			$('.map').hide();
			$("#store_"+store_id).append(data.data);
			
			$("#map_canvas_"+store_id).css('border', '3px solid #ffffff');
			
			if (data.coordinate_x)
			{			
				var latlng = new google.maps.LatLng(data.coordinate_x, data.coordinate_y);
				Across.ShowMap(latlng, data.address, data.title,  'map_canvas_'+store_id);
			}
			else
			{
				var geocoder = new GClientGeocoder();

				geocoder.getLatLng(
					data.address,
					function(point) {
	
						Across.ShowMap(point, data.address, data.title, 'map_canvas_'+store_id);
					});
			}

        }, "json");		
	},
	
	ShowMap : function (point, address, title, canvas)
	{
		var map = new GMap2(  document.getElementById(canvas) );

		map.setCenter(point, 13);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		//marker.openInfoWindowHtml('<strong>' + title + '</strong><br/> '+address);

		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
	},
	
	hideMap : function() {
		
		$('.map').hide();
	},
	
	setMaps : function(city_id)
	{

		$.post("/", {
            Func 		: 'stores.store.getStoreMap',
			city_id		: city_id
        },
        function(data)
        {
			if(data.data)
			{
				jQuery.each(data.data, function(i, item){
	
					if (item.coordinate_x)
					{			
						var latlng = new google.maps.LatLng(item.coordinate_x, item.coordinate_y);
						Across.ShowMap(latlng, item.address, item.title,  'map_canvas_'+item.store_id);
					}
					else
					{
						var geocoder = new GClientGeocoder();

						geocoder.getLatLng(
							item.address,
							function(point) {
								Across.ShowMap(point, item.address, item.title, 'map_canvas_'+item.store_id);
							});
					}
				});
			}

        }, "json");		
	},

    voteComment : function (vote, item)
    {
        $('#vote').val(vote);
        $('.votes > a').removeClass('active');
        $(item).addClass('active');
    },

    storeComment : function (thisFom)
    {
        if ( $('#vote').val() == 0)
        {
            alert('Kérem adja meg az értékelését, 1-től 5-ig!');
            return;
        }
        Across.actualForm = thisFom;

        $.post("/", 'Func=products.productinfo.storeComments&' + $(thisFom).serialize(), function(data)
        {
            if (data.error)
            {
                jQuery.each(data.items, function(i, val)
                {
                    $('#' + i).val(val);
                });
            }
            else
            {
                $('#comments').html(data.message);
            }
        }, "json");
    },

	setBasketOpener : function ()
	{
		$(".basket-opener").click(function () {

			if ( $(".basket-items").css('display') == 'none')
			{
                Across.openBasket();
			}
			else
			{
                Across.closeBasket();
			}
		});
		
		$(".basket-title").click(function () {

			if ( $(".basket-items").css('display') == 'none')
			{
                Across.openBasket();
			}
			else
			{
                Across.closeBasket();
			}
		});		
	},

    openBasket : function (item)
    {
        var src = $(".basket-opener").attr('src');
		$(".basket-opener").attr('src', src.replace(/inactive/i, 'active'));
        $(".basket-items").slideDown();
    },

    closeBasket : function (item)
    {
        var src = $(".basket-opener").attr('src');
		$(".basket-opener").attr('src',  src.replace(/active/i, 'inactive'));
        $(".basket-items").slideUp();
    },
	
	setStoreOpener : function ()
	{
		$(".store-opener").click(function () {

			if ( $(".store-list").css('display') == 'none')
			{
                Across.openStore();
			}
			else
			{
                Across.closeStore();
			}
		});
	},

	toStoreOpener : function ()
	{
        var src = $(".store-opener").attr('src');
		$(".store-opener").attr('src', src.replace(/inactive/i, 'active'));

        $.post("/", 'Func=home.getStoreCounties', function(data)
        {
			$('.store-list').html(data);
	        $(".store-list").show();
			$(document).scrollTo($('#store_list'), 800);
	
		}, "text");  		
	},

    openStore : function (item)
    {
        var src = $(".store-opener").attr('src');
		$(".store-opener").attr('src', src.replace(/inactive/i, 'active'));

        $.post("/", 'Func=home.getStoreCounties', function(data)
        {
			$('.store-list').html(data);
	        $(".store-list").show();
	
		}, "text");        
    },

    closeStore : function (item)
    {
        var src = $(".store-opener").attr('src');
		$(".store-opener").attr('src',  src.replace(/active/i, 'inactive'));
        $(".store-list").hide();
    },

	sendInvitation : function (thisForm)
    {
        Across.actualForm = thisForm;

		$.post("/",
			'Func=products.sendInvitation&'+$(thisForm).serialize(),
			function(data) {

				if (data.error)
				{
					jQuery.each(data.items, function(i, val) {
						$('#'+i).val(val);
				    });
				}
				else
				{
					$('#forward').html(data.message);
					$('#forward').addClass('forward-message');
				}
			},
			"json"
		);
    },
	
	getForumTopicForm : function(cat_id)
	{
		
		$('.forum-error-title').click(
			function() {
				Across.closeMsgWindow('forum-error');
			}
		);
		
		$('.forum-box-title').click(
			function() {
				Across.closeMsgWindow('forum-box');
			}
		);
		
		$.post("/",'Func=forum.default.getForumTopicForm&cat_id='+cat_id,
		function(data)
		{
			
			if(data.error)
			{
				$('.forum-error').fadeIn();
				$('.forum-error-title').html(data.title);
				$('.forum-error-body').html(data.body);
			}
			else
			{
				$('.forum-box').fadeIn();
				$('.forum-box-title').html(data.title);
				$('.forum-box-body').html(data.body);
			}
			
		},
		"json"
		);
			
	},
	
	addForumTopic : function(cat_id,thisform)
	{
		
		$.post("/",'Func=forum.default.addForumTopic&cat_id='+cat_id+'&'+$(thisform).serialize(),
		function(data)
		{
			
			if(data.error)
			{
				
				$('.inner-message').fadeIn();
				$('.inner-message-title').html(data.title);
				
			}
			else
			{
				location.reload();
			}
			
		},
		"json"
		);
		
	},
	
	
	addFavourite : function(topic_id)
	{
		
		$('.forum-error-title').click(
			function() {
				Across.closeMsgWindow('forum-error');
			}
		);
		
		$('.forum-box-title').click(
			function() {
				Across.closeMsgWindow('forum-box');
			}
		);
		
		$.post("/",'Func=forum.topiclist.addFavourite&id='+topic_id,
		function(data)
		{
			
			if(data.error)
			{
				
				$('.forum-error').fadeIn();
				$('.forum-error-title').html(data.title);
				$('.forum-error-body').html(data.body);
				
			}
			else
			{
				location.reload();
			}
			
		},
		"json"
		);
		
	},
	
	addProductFavourite : function(item_id)
	{
		
		$('.item-error-title').click(
			function() {
				Across.closeMsgWindow('item-error');
			}
		);
		
		$('.item-box-title').click(
			function() {
				Across.closeMsgWindow('item-box');
			}
		);
		
		$.post("/",'Func=products.productinfo.addFavourite&id='+item_id,
		function(data)
		{
			
			if(data.error)
			{
				
				$('.item-error').fadeIn();
				$('.item-error-title').html(data.title);
				$('.item-error-body').html(data.body);
				
			}
			else
			{
				location.reload();
			}
			
		},
		"json"
		);
		
	},
	
	removePreorder : function(item_id)
	{
		
		$.post("/",'Func=users.orders.removePreorder&id='+item_id,
		function(data)
		{
				Across.reloadWin(data.url)
		},
		"json"
		);
		
	},	
	
	topicReminder: function(topic_id,cat_id)
	{
		
		$('.forum-error-title').click(
			function() {
				Across.closeMsgWindow('forum-error');
			}
		);
		
		$('.forum-box-title').click(
			function() {
				Across.closeMsgWindow('forum-box');
			}
		);
		
		$.post("/",'Func=forum.topiclist.topicReminder&topic_id='+topic_id+'&cat_id='+cat_id,
		function(data)
		{
			
			if(data.error)
			{
				
				$('.forum-error').fadeIn();
				$('.forum-error-title').html(data.title);
				$('.forum-error-body').html(data.body);
				
			}
			else
			{
				location.reload();
			}
			
		},
		"json"
		);
		
	},
	
	getForumMessageForm : function(topic_id,cat_id,parent_msg_id)
	{
		
		$('.forum-error-title').click(
			function() {
				Across.closeMsgWindow('forum-error');
			}
		);
		
		$('.forum-box-title').click(
			function() {
				Across.closeMsgWindow('forum-box');
			}
		);
		
		$.post("/",'Func=forum.list.getForumMessageForm&topic_id='+topic_id+'&cat_id='+cat_id+'&parent_msg_id='+parent_msg_id,
		function(data)
		{
			
			if(data.error)
			{
				
				$('.forum-error').fadeIn();
				$('.forum-error-title').html(data.title);
				$('.forum-error-body').html(data.body);
				
			}
			else
			{
				$('.forum-box').fadeIn();
				$('.forum-box-title').html(data.title);
				$('.forum-box-body').html(data.body);
			}
			
		},
		"json"
		);
		
	},
	
	addForumMessage : function(topic_id,cat_id,parent_msg_id,thisform)
	{
		
		$.post("/",'Func=forum.list.addForumMessage&topic_id='+topic_id+'&cat_id='+cat_id+'&parent_msg_id='+parent_msg_id+'&'+$(thisform).serialize(),
		function(data)
		{
			
			if(data.error)
			{
				
				$('.inner-message').fadeIn();
				$('.inner-message-title').html(data.title);
				
			}
			else
			{
				location.reload();
			}
			
		},
		"json"
		);
		
	},
	
	closeMsgWindow : function(classname)
	{
		$('.'+classname).fadeOut();
	},	
	
	doLogin :function(thisform)
	{
		
		$.post("/",'Func=users.gologin&'+$(thisform).serialize(),
		function(data)
		{
			
			if(data.errors)
			{
				$('.inner-message').html(data.message);
				$('.inner-message').fadeIn();
				
			}
			else
			{
				
				location.reload();
			}
			
		},
		"json"
		);
		
	},
	
	popwin : function (url,width,height)
	{
		if ( !width ) width = '800';
		if ( !height ) height = '540';

	    var popupWin = window.open(url,'popupWin','width='+width+'px,height='+height+'px,left=100,top=100,menubar=no,status=no,toolbar=no,scrollbars=yes,resizable=no,screenX=100,screenY=100,directories=no,location=no');

		popupWin.focus();
        return false;
	},

    printView : function ()
    {
        Across.popwin('?print');
    },	
	
	showList: function (tableid)
	{
		//$('#'+tableid).slideDown("slow");
		$(document).scrollTo($('#'+tableid), 800);
	},
	
	hideList: function (tableid)
	{
		//$('#'+tableid).slideUp("slow");
		$(document).scrollTo($('#letters'), 800);
	},		
	
	showToggle : function (boxid, boxid2)
	{
		$('#'+boxid).show("slow");
		$('#'+boxid2).show("slow");
	},
	
	hideToggle : function (boxid, boxid2)
	{
		$('#'+boxid).hide("slow");
		$('#'+boxid2).hide("slow");
	},
	
	viewProdList : function (tableclass)
	{
		if ($('.'+tableclass+'-open').css('display') == 'none')
		{
			$('.'+tableclass+'-view').toggle();	
		}
		else {
			$('.'+tableclass+'-view').show();	
		}
		
		$('.'+tableclass+'-open').hide();
	},
	
	openProdList : function (tableclass)
	{
		if ($('.'+tableclass+'-open').css('display') == 'none')
		{
			$('.'+tableclass+'-open').show();
			$('.'+tableclass+'-view').show();
		}
		else {
			$('.'+tableclass+'-open').hide();
			$('.'+tableclass+'-view').hide();
		}
	},

	setBounce : function ()
	{
		$("#ppp").click(function () {
			  $('#ppp').effect("bounce", { times:3 }, 300);
		});
	},
	
	toggleCatBlock : function (boxid)
	{
		if ( $('#block_'+boxid).css('display') == 'none')
		{
			$('#arrow_'+boxid).removeClass('subcat-arrow-none');
			$('#arrow_'+boxid).addClass('subcat-arrow-block');
		}
		else
		{
			$('#arrow_'+boxid).removeClass('subcat-arrow-block');
			$('#arrow_'+boxid).addClass('subcat-arrow-none');
		}
		
		$('#block_'+boxid).toggle();
	},
	
	adultConfirm: function()
	{
		
		$.post("/", 'Func=products.adultConfirm', function(data)
        	{
				
				dialogHandler.popup({
			
				Title : 'Figyelem',
				Body : data 
		
				});
			
			
        	}, "text");
		
		
	},
	adultLogin : function()
	{
		$.post("/",'Func=products.adultLogin', function(data)
		{
			location.reload();
		}, "text");
	}

};

$(document).ready(function()
{
    Across.initTab();
//    Across.initLeftTabs();
    Across.setBasketOpener();
	//Across.setStoreOpener();
	$("#search-string").autocomplete("/gyorskereses/", { minChars:3, width: 220});
	Across.setBounce();
	Across.setMaps($("#store_city_id").text());
});
