$(document).ready(function(){

    $(".goBack").click(function(){
        history.go(-1);
        return false;
    });
    
    $("body").click(function(){
        $("#modal").remove();
    });
    
    $("#register").click(function(){
        $(this).parents("form:first").submit();
        return false;
    });
    
});

var Class_Main = function(){
}

Class_Main.prototype = {

    modal: function(url)
    {   
        $("#modal").remove();
        $.get(url, function(data){
            $("#modal").remove();
            $("body").append('<div id="modal">' + data + '</div>');
            
            var box = $("#modal");
            
            var windowHeight = parseInt($(window).height());
//            var topPx = $(window).scrollTop();
            var topPx = (windowHeight - parseInt(box.css('height'))) / 2;
            
            var windowWidth = parseInt($(window).width());
            var leftPx = (windowWidth - parseInt(box.css('width'))) / 2;
            
            Main.toCenter(box);
            //box.css({top: topPx + 'px', left: leftPx + 'px'});
            
            $("#modal .close").unbind("click").click(function(){ $("#modal").remove(); return false; });
        });
    },
    
    toCenter: function(obj)
    {
    	var newWidth = obj.width();

	    // vertical center
	    var windowHeight = parseInt($(window).height());
	    var topPx = $(window).scrollTop();
	    if (topPx < 200)
    		topPx = 200;

	    posTop = topPx;
    
    
    	// horizontal center
    	var windowWidth = parseInt($(window).width());
    	var boxWidth = parseInt(newWidth);
    	var leftPx = (windowWidth / 2) - (boxWidth / 2);
    

	    obj.css({top: posTop + 'px', left: leftPx + 'px'})
    },
		
	formSubmit:function(formVar, goAfter, errorContainer)
	{
		var form = $(formVar);
		var response;
		
		if (typeof errorContainer == 'undefined')
		{
            var errorContainer = '#warning';
		}

		form.ajaxSubmit({
            cache: false,
			dataType : 'json',
	    	success: function(response){

				if (response['status'] == 'success')
				{
					if (typeof goAfter == 'undefined')
					{
						Main.refresh();
					}
					else if (goAfter)
					{
						Main.go(goAfter);
					}
					
				}
				else
				{
				    $(errorContainer).hide();
					$(errorContainer).html(response['message']);
					$(errorContainer).fadeIn('slow').slideDown('fast');
					
					setTimeout('$("' + errorContainer + '").fadeOut("slow").slideUp("fast")', 5000);
				}
			}			
	    });
		
		return false;
	},
	
	go:function (url)
	{
		window.location = BASE_HREF + url;
	},
	
	refresh:function()
	{
		window.location = window.location;
	},
	
	getSubcategoriesSelect:function (categoryId, fieldObj)
	{
        fieldObj.html('<option value="0">Visi tipai</option>');
		$.getJSON('products/categories/getsubcategories/' + categoryId, function (data){
            $.each(data, function(i, value){
                fieldObj.append('<option value="' + value['id'] + '">' + value['title'] + '</option>');
            });
		});
	}
}

var Main = new Class_Main();