jQuery.fn.debug = function() {
  return this.each(function(){
    $.log(this);
  });
};
jQuery.log = function(message) {
    if (window.console && jQuery.browser.mozilla) {
        console.debug(message);
    } else {
        //    alert(message);
    }

};


// Create Namespace
Site = {};
// MainClass
Site.Main = function() {
	$.log( 'Site.Main' );
	this.initialize();
}

Site.Main.prototype = {
    initialize: function() {
        $.log('Site.Main.initialize');

        switch (window.template) {
            case "home":
                this.showHome();
                break;
            case "projecten":
                this.showProjecten();
                break;
            case "projecten_detail":
                this.showProjectenDetail();
                break;
            case "nieuws":
                this.showNieuws();
                break;
            case "medewerkers":
                this.showNieuws();
                break;
            case "standaard":
                this.showStandaard();
                break;
        }
        this.fixLinks();
    },
    fixLinks: function() {
        $(".text a").each(function(i, e) {
            if ($(e).attr("href").substring(0, 5) == "http:" && $(e).attr("href").indexOf("steenhuisbukman") == -1) {
                $(e).attr("target", "_blank");
            } else {
                $(e).attr("target", "_self");
            }
            if (!$(e).attr("title")) {
                $(e).attr("title", "Bezoek: " + $(e).html());
            }
        });
    },
    showHome: function() {
        $.log('showHome');
        this.initSlideShow();
        $('#wrap').hide()
                  .fadeIn('slow');
        //  this.showNextSlide();
    },
    showProjecten: function() {
        $.log('showProjecten');

        this.hideMenuTimeout = 0;

        //$('#project_list a').visited().addClass('visited');

        var holder = this;
        $('#tagmenutitle').hover(function() {
            $("ul.tagmenu").fadeIn("slow");
        },
                                    function() {
                                        holder.hideTagMenuDelayed();
                                    });
        $("ul.tagmenu").hover(function() {
            clearTimeout(holder.hideMenuTimeout);
            $("ul.tagmenu").fadeIn("slow");
        }, function() {
            holder.hideTagMenuDelayed();
        });
    },
    showProjectenDetail: function() {
        if (window.imageindex == null) {
            this.currentSlide = 0;
        }

        this.replaceImageDivs();

        var currentSlideId = window.imageindex;
        var currentSlide = 0;
        this.slideCount = $("#project_imageholder div.item").length;
        $("#project_imageholder div.item").each(function(i, e) {
            $(e).attr("id", "proj" + i)
                .hide()
			//$.log("id for image: " + $("img", e).attr("id") + " -> " + currentSlideId);
            if ($("img", e).attr("id") == "id_" + currentSlideId) {
                $(e).fadeIn("slow");
               // $.log("found slide with id " + currentSlideId);
                currentSlide = i;
            }
        });

        this.currentSlide = currentSlide;

        var holder = this;
        $("li.imagenavitem a").each(function(i, e) {
         //   $.log($(e).html());
            $(e).attr("href", "#" + $(e).html());
            $(e).click(function() {
                holder.showProjectenDetailByIndex(i);
            });
        });

        /*
        $("#nextimage").attr("href", "#shownext")
        .click(function() {
        holder.showNextProjectenDetail();
        });
        $("#previmage").attr("href", "#showprevious")
        .click(function() {
        holder.showPreviousProjectenDetail();
        });
        */
        $("#project_imageholder div.item").click(function() {
        holder.showNextProjectenDetail();
        });
        
    },
    showProjectenDetailByIndex: function(index) {
	  //  this.replaceImageDivs();
    
      //  $.log("showProjectenDetailByIndex" + this.currentSlide);
        $("#proj" + this.currentSlide).fadeOut("slow");
        $($("li.imagenavitem a")[this.currentSlide]).removeClass("selected");
        this.currentSlide = index;
        $("#proj" + this.currentSlide).fadeIn("slow");
        $($("li.imagenavitem a")[this.currentSlide]).addClass("selected");
        $("#title2").html($("#proj" + this.currentSlide + " img").attr("alt"));
        $("#title2").hide();
        $("#title2").fadeIn("slow");
    },
    showNextProjectenDetail: function() {
      //  $.log("showNextProjectenDetail" + this.currentSlide);
        $("#proj" + this.currentSlide).fadeOut("slow");

        $($("li.imagenavitem a")[this.currentSlide]).removeClass("selected");
        this.currentSlide++;
        if (this.currentSlide >= this.slideCount) {
            this.currentSlide = 0;
        }
        $($("li.imagenavitem a")[this.currentSlide]).addClass("selected");
        $("#proj" + this.currentSlide).fadeIn("slow");
        $("#title2").html($("#proj" + this.currentSlide + " img").attr("alt"));
        $("#title2").hide();
        $("#title2").fadeIn("slow");
    },
    showPreviousProjectenDetail: function() {
        $("#proj" + this.currentSlide).fadeOut("slow");
        this.currentSlide--;
        if (this.currentSlide < 0) {
            this.currentSlide = this.slideCount - 1;
        }
        $("#proj" + this.currentSlide).fadeIn("slow");
        $("#title2").html($("#proj" + this.currentSlide + " img").attr("alt"));
        $("#title2").hide();
        $("#title2").fadeIn("slow");
    },
    hideTagMenuDelayed: function() {
        var holder = this;
        clearTimeout(this.hideMenuTimeout);
        this.hideMenuTimeout = setTimeout(holder.hideTagMenu, 1000);
    },
    hideTagMenu: function() {
        $("ul.tagmenu").fadeOut("slow");
    },
    showNieuws: function() {
        $.log('showNieuws');
        $('#news_holder div.item').each(function(i, e) {
            $(e).click(function() {
                document.location = $("a", e).attr("href");
            });
            $(e).hover(
					function() {
					    $.log('hover' + e);
					    $("div.left", this).addClass("hover");
					    $("div.right", this).addClass("hover");
					},
					function(e) {
					$("div.left", this).removeClass("hover");
					$("div.right", this).removeClass("hover");
					}
			);

        });
    },
    showStandaard: function() {
        $.log('showStandaard');
    },
    initSlideShow: function() {
        this.replaceImageDivs();
        var slideCount = 0;
        $('#home_image div.item').each(function(i, e) {
            $(e).hide();
            $(e).attr('id', 'slide' + i);
            slideCount++;
        });
        this.slideCount = slideCount;
        this.currentSlide = -1;
        setTimeout(this.showNextSlide, 10);
        if (this.slideCount > 1) {
            setInterval(this.showNextSlide, 5000);
        }
    },
    replaceImageDivs: function() {
        var holder = this;
        $("div.img").each(function(i, e) {
            $(e).html(holder.replaceImageDiv(e));
        });
    },
    replaceImageDiv: function(e) {
        var html = "";
        var src = $("div.src", e).html();
       // alert('src:'+src);
        var alt = $("div.alt", e).html();
        var href = $("div.href", e).html();
        var title = $("div.title", e).html();

        if (alt == "" || alt == null) alt = "";

        html += "<img src=\"" + src + "\" alt=\"" + title + "\" title=\"" + alt + "\" />";
        if (href != "" && href != null) {
            html = "<a href=\"" + href + "\" title=\"" + alt + "\">" + html + "</a>";
        }
        return html;
    },
    showNextSlide: function() {
        $.log('showNextSlide ' + window.site.currentSlide + ' ' + window.site.slideCount);
        $('#slide' + window.site.currentSlide).fadeOut(2000);
        if (window.site.currentSlide++ > window.site.slideCount - 2) {
            window.site.currentSlide = 0;
        }
        $('#slide' + window.site.currentSlide).fadeIn(1500);
    }

}


/* ----- Start Vandergoes ----- */

$(document).ready(function() {
    $.log('ready');
	window.site = new Site.Main();
});


