INPUTFOCUS = {
	init: function() {
		var inp = document.getElementsByTagName('input');
		for (var i = 0; i < inp.length; i++) {
			if (inp[i].type == 'text') {
				inp[i].setAttribute('rel', inp[i].defaultValue)
				inp[i].onfocus = function() {
					if (this.value == this.getAttribute('rel')) {
						this.value = '';
					} else {
						return false;
					}
				}
				inp[i].onblur = function() {
					if (this.value == '') {
						this.value = this.getAttribute('rel');
					} else {
						return false;
					}
				}
				inp[i].ondblclick = function() {
					this.value = this.getAttribute('rel')
				}
			}
		}
	}
}

TOPSLIDER = {
	init: function() {
		$('#slider-top').tinycarousel({ start: 1 });
		$('#slider1').tinycarousel();
		$('#slider2').tinycarousel({ start: 1 });
	}
};

SEARCHBAR = {
	init: function() {
		$('#side-search').hide();
		$(window).bind('scroll', function() {
			$("#side-search").toggle($(this).scrollTop() > 200);
		});
	}
};

ARCHIVELIST = {
	init: function() {
		$(".moretext").hide();
		$(".morelesslink").show();
		var moreLessText = $(".morelesslink").text();
		$(".morelesslink").click(function() {
			if ($(this).next(".moretext").is(":hidden")) {
				$(this).html("view less");
				$(this).next(".moretext").slideToggle(400);
				$(this).addClass("less");
				$(this).removeClass("more");
			} else {
				$(this).text(moreLessText);
				$(this).next(".moretext").slideToggle(400);
				$(this).addClass("more");
				$(this).removeClass("less");
			}
		});
	}
};

TABS = {
	init: function() {
		$(".tab_content").hide();
		$("ul.tabs li:first").addClass("active").show();
		$(".tab_content:first").show();
		$("ul.tabs li").click(function() {
			$("ul.tabs li").removeClass("active");
			$(this).addClass("active");
			$(".tab_content").hide();
			var activeTab = $(this).find("a").attr("href");
			$(activeTab).fadeIn();
			return false;
		});
	}
};

BODYTABS = {
	init: function() {
		$(".tab_content_main").hide();
		$("ul.tabs_main li:first").addClass("active").show();
		$(".tab_content_main:first").show();
		$("ul.tabs_main li").click(function() {
			$("ul.tabs_main li").removeClass("active");
			$(this).addClass("active");
			$(".tab_content_main").hide();
			var activeTab = $(this).find("a").attr("href");
			$(activeTab).fadeIn();
			return false;
		});
	}
};

COMMENT = {
	init: function() {
		// cache the DOM element
		var myCommentPreview = $("#commentpreview");

		$("#commentpreview").hide();

		$("#prevcomment").click(function() {
			$(this).closest("div").slideUp(50);
			myCommentPreview.slideDown(400);
			return false;
		});
	}
};

ASPECTRATIO = {
    init: function() {
		ASPECTRATIO.rowtop();
        ASPECTRATIO.row();
        ASPECTRATIO.promo();
        ASPECTRATIO.viewport();
    },

    rowtop: function() {
		if ( $("#home").length ) {
			return true;
		} else {
			$(".row-top img").each(function() {
				$(this).closest("div").css("overflow", "hidden");
				var boxHeight = $(this).closest("div").height(),
					boxWidth = $(this).closest("div").width(),
					imgHeight = $(this).height(),
					imgWidth = $(this).width();
				if (imgWidth > imgHeight) {
					// landscape
					$(this).width(imgWidth).css("height", "auto");
				} else if (imgWidth <= imgHeight && imgHeight != null) {
					// portrait or square
					$(this).height(imgHeight).css("width", "auto");
				}
				// remove this – just makes it obvious which images are affected
				//$(this).closest("div").css("border-color","red");
			});
		}
    },
    row: function() {
        $(".row img").each(function() {
            $(this).closest("div").css("overflow", "hidden");
            var boxHeight = $(this).closest("div").height(),
				boxWidth = $(this).closest("div").width(),
				imgHeight = $(this).height(),
				imgWidth = $(this).width();
            if (imgWidth > imgHeight) {
                // landscape
                $(this).width(imgWidth).css("height", "auto");
            } else if (imgWidth <= imgHeight) {
                // portrait or square
                $(this).height(imgHeight).css("width", "auto");
            }
            // remove this – just makes it obvious which images are affected
            //$(this).closest("div").css("border-color", "red");
        });
    },
    promo: function() {
        $(".promo img").each(function() {
            $(this).closest("div").css("overflow", "hidden");
            var boxHeight = $(this).closest("div").height(),
				boxWidth = $(this).closest("div").width(),
				imgHeight = $(this).height(),
				imgWidth = $(this).width();
            if (imgWidth > imgHeight) {
                // landscape
                $(this).width(imgWidth).css("height", "auto");
            } else if (imgWidth <= imgHeight) {
                // portrait or square
                $(this).height(imgHeight).css("width", "auto");
            }
            // remove this – just makes it obvious which images are affected
            //$(this).closest("div").css("border-color", "red");
        });
    },
    viewport: function() {
        $("#slider2 .viewport img").each(function() {
            $(this).closest("div").css("overflow", "hidden");
            var boxHeight = $(this).closest("div").height(),
				boxWidth = $(this).closest("div").width(),
				imgHeight = $(this).height(),
				imgWidth = $(this).width();
            $(this).height(imgHeight).css("width", "auto");
            // remove this – just makes it obvious which images are affected
            //$(this).css("border-color","red");
        });
    }
};

$(function(){
	INPUTFOCUS.init()
	TOPSLIDER.init();
	SEARCHBAR.init();
	ARCHIVELIST.init();
	TABS.init()
	BODYTABS.init();
	COMMENT.init();
	ASPECTRATIO.init();
});
