var thumbWidth = 99;

$(document).ready(function(){
    $(".goBack").click(function(){
        history.go(-1);
        return false;
    });
    
    Main.bind();
    
});

var Class_Main = function(){
};

Class_Main.prototype = {
    bind: function()
    {
    	$("a.fancybox").fancybox({titlePosition: 'over'});
		$("a[rel='fancybox']").fancybox({titlePosition: 'over'});
		$("a[rel^='fancybox']").fancybox({titlePosition: 'over'});		
		$("a[href*='var/wysiwyg'][href$='jpg']").attr("rel", "uploaded_gallery");	
		$("a[href*='var/wysiwyg'][href$='jpg']").fancybox();
		
		$('.attach-files form').live('submit', function(){
			$.fancybox.showActivity();
			var errors = $(this).find(".errors");
			errors.hide();
			
			$(this).ajaxSubmit({
				dataType: 'json',
				success: function(response){
					$.fancybox.hideActivity();
					
					if (response.status == 'error')
					{
						errors.show().html(response.errors);
						
						setTimeout(function(){
							errors.hide();
						}, 4000);
					}
					else
					{
						Main.reloadAttachments();
						$.fancybox.close();
					}
				}
			});
			
			return false;
		});
		
		$(".remove-attached").live('click', function(){
			var button = $(this);
			
			$.get(button.attr("href"), function(response){
				button.parents("div:first").remove();
			});
			
			return false;
		});
		Main.reloadAttachments();

		$("a.action-attachfiles").fancybox({
			onComplete: function(){

			}
		});
        /*
$(".thumb-small").unbind("click").click(function(){
            var objClass = $(this).attr("rel");

            $(this).parents(".content:first").find(".thumb-big").hide();
            $("." + objClass).css({display: 'block'});
            
            return false;
        });
        
*/
        var portfolioContainer = $(".content-portfolio-item");
        var thumbs;
        var scroller;
        var amount = 0;
        var arrowLeft;
        var arrowRight;
        
        $.each(portfolioContainer, function(i, portfolioItem){
            thumbs = $(portfolioItem).find(".thumb-small");
            scroller = $(portfolioItem).find(".thumbs-scroller");
            
            scroller.css({width: thumbs.length * thumbWidth});
            arrowRight = $(portfolioItem).find(".arrow-right");
            arrowLeft = $(portfolioItem).find(".arrow-left");
            
            Main.bindArrowRight(arrowRight, arrowLeft, scroller, thumbs);
            Main.bindArrowLeft(arrowLeft, arrowRight, scroller, thumbs);
        });
        
        
        var imagesContainer = $(".image-container");
        var current;
        var navigation;
        var next;
        var prev;
        $.each(imagesContainer, function(i, containerItem){
            navigation = $(containerItem).next(".navigation");
            
            arrowRight = $(navigation).find(".right");
            arrowLeft = $(navigation).find(".left");
            
            arrowLeft.unbind("click").click(function(){
                current = $(containerItem).find(".image:visible");
                prev = current.prev(".image");
                if (prev.html())
                {
                    current.hide();
                    prev.show();
                    prev = false;
                }
                
                return false;
            });
            
            arrowRight.unbind("click").click(function(){
                current = $(containerItem).find(".image:visible");
                next = current.next(".image");
                if (next.html())
                {
                    current.hide();
                    next.show();
                    next = false;
                }
                
                return false;
            });
            
            //Main.bindArrowRight(arrowRight, arrowLeft, scroller, thumbs);
            //Main.bindArrowLeft(arrowLeft, arrowRight, scroller, thumbs);
        });

    },
    
    reloadAttachments: function()
    {
	    var container = $(".attachments-list");
	    if (container.length != 0)
	    {
	    	$.get("pages/loadattachments", function(response){
				container.html(response);
	    	});
    	}
    },
    
    bindArrowRight: function(arrowRight, arrowLeft, scroller, thumbs)
    {
        arrowRight.unbind("click").click(function(){
            var amount = parseInt(scroller.css('right')) + thumbWidth;
            scroller.animate({right: amount + 'px'});
            
            arrowLeft.removeClass("hide-image");
            Main.bindArrowLeft(arrowLeft, arrowRight, scroller, thumbs);
            
            if (amount == thumbWidth * thumbs.length - (thumbWidth * 4))
            {
                arrowRight.addClass("hide-image");
                arrowRight.unbind("click").bind("click", function(){
                    return false;
                });
            }
            else
            {
                Main.bindArrowRight(arrowRight, arrowLeft, scroller, thumbs);
            }
            
            return false;
        });
    },
    
    bindArrowLeft: function(arrowLeft, arrowRight, scroller, thumbs)
    {
        arrowLeft.unbind("click").click(function(){
            var amount = parseInt(scroller.css('right')) - thumbWidth;
            scroller.animate({right: amount + 'px'});
            
            arrowRight.removeClass("hide-image");
            Main.bindArrowRight(arrowRight, arrowLeft, scroller, thumbs);
            
            if (amount == 0)
            {
                arrowLeft.addClass("hide-image");
                arrowLeft.unbind("click").bind("click", function(){
                    return false;
                });
            }
            else
            {
                Main.bindArrowLeft(arrowLeft, arrowRight, scroller, thumbs);
            }
            
            return false;
        });
    },
    
    formLoadValues: function(formObj, values, errors)
    {
        var fieldObj;
        
        $.each(values, function(field, value){
            fieldObj = formObj.find('[name="data[' + field + ']"]');
            
            if (fieldObj.is("input[type='text']"))
            {
                fieldObj.val(value);
            }
            else if (fieldObj.is("input[type='checkbox']"))
            {
                if (value != '' && value != 0)
                {
                    fieldObj.attr("checked", true);
                }
                else
                {
                    fieldObj.attr("checked", false);
                }
            }
            else if (fieldObj.is("input[type='radio']"))
            {
                fieldObj.attr("checked", false);
                fieldObj = formObj.find('[name="data[' + field + ']"][value="' + value + '"]');
                fieldObj.attr("checked", true);
            }
            else if (fieldObj.is("select"))
            {
                fieldObj.find('option[value="' + value + '"]').attr("selected", true);
            }
        });
        
        $.each(errors, function(field, value){
            fieldObj = formObj.find('[name="data[' + field + ']"]');

            fieldObj.parents("tr:first").find("td:first").addClass("error");
        });
    }
};

var Main = new Class_Main();
