/* holdbackthesky custom JavaScript behaviours */

var hbts = window.hbts || {};

hbts.page = {
    imageWidth      : DI.image.imageWidth,
    imageHeight     : DI.image.imageHeight,
    imageComments   : DI.image.imageComments,
    contentSelector : ".content",
    photoSelector   : ".photoWrapper",
    detailsSelector : ".detailsWrapper",
    hPhotoClass     : "hphoto",
    vPhotoClass     : "vphoto",
    vDetailsWidth   : 300,
    
    init: function() {
        if (hbts.page.imageWidth > hbts.page.imageHeight) {
            $(hbts.page.contentSelector).addClass(hbts.page.hPhotoClass);
            $(hbts.page.contentSelector).css('width', hbts.page.imageWidth + "px");
            $(hbts.page.detailsSelector).css('width', hbts.page.imageWidth + "px");
        }
        else {
            var width = parseInt(hbts.page.imageWidth, 10) + hbts.page.vDetailsWidth;
            $(hbts.page.contentSelector).addClass(hbts.page.vPhotoClass);
            $(hbts.page.contentSelector).css('width', (width + "px"));
        }
        $(hbts.page.photoSelector).show();
    }
};

hbts.image = {
    imgSelector     : ".photo",
    
    init: function() {
        $(hbts.image.imgSelector).fadeIn("slow");
    }
};	

hbts.comments = {
    imageID     : DI.image.imageID,
    commentForm : ".commentForm",
    commentList : "#commentsMid",
    
    init: function() {
        $(hbts.comments.commentForm).hide();
        hbts.comments.formatCommentList();
        
        $("a.viewAll, a.addComment").click(function() {
            $(hbts.comments.commentForm).toggle();
            return false;
        });
    },
    formatCommentList: function() {
        if ($(hbts.comments.commentList).length > 0) {
            $("li:odd").addClass("odd");
            $("li:even").addClass("even");
        }
    }
};

hbts.validation = {
    commentForm : ".commentForm",
    errorHTML   : "<div class=\"formErrors\"><p></p></div>",

    /** Initialise form validation */
    init: function() {
        var form = $('.validate');
        
        // apply event handler to form
        form.submit(function() {
            return hbts.validation.checkRequired(this);
        });
    },
    
    checkRequired: function(form) {
        // check for required fields
        var required = $(form).find('.required'),
            errors = [],
            errorMsg = "The form contains missing values:",
            $errorHTML = $(hbts.validation.errorHTML);
            
        // loop through and check values
        required.each(function() {
            var parent = $(this).parent('.formField'),
                label = parent.children('label:first');
            if ($(this).val() === "" || $(this).val() === "-") {
                var labelText = label.html().replace(':','');
                errors.push(labelText + ' is incomplete');
                parent.addClass('error');
            }
        });
        
        if (errors.length > 0) {
            // remove error message
            $(hbts.validation.commentForm).find(".formErrors").remove();
            // add initial error message
            $errorHTML.find("p").html(errorMsg);
            
            /*
jQuery.each(errors, function() {
                $errorHTML.find("ul").append("<li>"+this+"</li>");
            });
*/
            // append errorMsg to document
            $(hbts.validation.commentForm).find('form').prepend($errorHTML.get(0));
            
            return false;
        }
        else {
            // remove error message
            $(hbts.validation.commentForm).find(".formErrors").remove();
            return true;
        }        
    }
};		
					
$(document).ready(function(){
	hbts.page.init();
	hbts.image.init();
	hbts.comments.init();
	hbts.validation.init();
});
					