﻿

jQuery(function () {


    (function ($) {
		
		//Resume scroll top
		var h = document.location.hash;
		if( h.indexOf("#y") == 0 ) {
			$(document).scrollTop(parseInt(h.substring(2)));
		}		
		

        $(function () {
            //Remove links from breadcrumb until it does not exceed two lines and add ellipsis
            var crumb = $(".breadcrumb ul");

            var measurer = $("<li>|</li>").appendTo(crumb);
            var lineHeight = measurer.height();
            measurer.remove();

            var maxHeight = lineHeight * 2.2; // ~ Less than three lines

            while ($("li", crumb).length > 3 && crumb.height() > maxHeight) {
                $("li:eq(1)", crumb).html("<span class='ellipsis'>...</span>");
                $("li:eq(2)", crumb).remove();
            }
        });

        $.fn.ThreeDots.settings.ellipsis_string = "…";

        function ellipsis(target, maxRows) {
            target = $(target);			
            target.each(function () {
				//$(".ellipsis_text", this).show();
                if (!$(this).parents(".no-ellipsis").length) {
                    $(this).attr("title", $(this).text());
                    $(this).ThreeDots({ max_rows: maxRows });
                }
            });
        }

        function getLineHeight(e) {
            var old = e.html();
            e.html("|");
            var lineHeight = e.height();
            e.html(old);
            return lineHeight;
        }
		
		var processVTabs = function() {
			$("div.vertical-tab-area").each(function() {
				var ul = $("ul.vertical-tabs", this);					
				
				var selected = $(".selected-tab", ul);					
				selected.next().addClass("post-selected");
				selected.prev().addClass("pre-selected");
					
				
				/*$(".tab", ul).click(function(e) {
					document.location.href = $("a", this).attr("href") + "#y" + $(document).scrollTop();
					e.preventDefault();
				});*/
				
				var opacity = 1;
				$(".tab-arrow", ul).each(function() {
					$(this).css("opacity", opacity);
					opacity -= .1;
				});
				
				var height = ($(this).height() - ul.height());			
				if( height > 1 ) {
					var res = $("<li class='tab-spacer'><div class='tab-spacer-inner'></div></li>").appendTo(ul);
					$("div", res).css("height", height + "px");
								
				}
				
				$("ul.vertical-tabs li.tab .tab-body a").each(function() {
					var lh = getLineHeight($(this));
					var lines = $(this).height()  / lh;			
					if( lines < 1.1 ) {
						$(this).addClass("single-line-tab");
					}
				});
			});
		};

		processVTabs();
		
		
		var ajaxifyThemes = function() {
			$("div.themes").each(function () {
			
				var themeUrl = $(".theme-body > a", this).attr("href");		
				if( themeUrl ) {				
					$(".video-player img", this).click( function(e) {					
						document.location.href = themeUrl + (themeUrl.indexOf("?") != -1 ? "&" : "?") + "auto-play=true#video";
						e.preventDefault();
						e.stopPropagation();
						return false;
					});
				}	

				$(".tab", this).each(function() {
					
					$(this).click(function(e) {
						var a = $("a", this);
						var href = a.attr("href");
						var qs = href.substring(href.indexOf("?") + 1);
						$("div.themes").load(getPartialUrl("Gadgets", qs) + " .themes > *", null, function (text) {
							//Find inline scripts and execute them (for flowplayer stuff)
							var scripts = /<script[^>]*>((.|\r|\n)*?)<\/script>/gi;
							var m;
							while( (m=scripts.exec(text)) != null ) {
								//alert(m[1]);
								eval(m[1]);
							}
							ajaxifyThemes();
							processVTabs();
						});					
						e.preventDefault();
					});
				});
			});		
		};
		
		ajaxifyThemes();					        



        ellipsis("div.col2 div.newsitem h3 a", 4);
        ellipsis("div.col2 div.newsitem p", 2);

        $("div.col1 div.newsitem").each(function () {
            var header = $("h3 a", this);

            var lh = getLineHeight($(".ellipsis_text", header));
            var lines = header.height() / lh;
            
            if( lines >= 3 ) {
              ellipsis(header, 3);
              $("p", this).hide();
            } else {
              ellipsis(header, 2);
              //header.css("lineHeight", "1.15em");			
              ellipsis($("p", this), 3 - (lines > 1.5 ? 1 : 0));
              //ellipsis($("p", this), 2);
            }
        });		

		
		ellipsis("div.col2 h4 .tag", 1);
		ellipsis("div.col1 h4 .tag", 1);
		/*$("div.col2 h3 .tag").each(function() {					
		});*/

        ellipsis("div.list-body li.item h3 a", 3);
        ellipsis("div.list-body li.item p", 2);

        //mouse over, highlighted news items on frontpage
        $("div.col1 .newsitem").hover(
            function () { $(this).addClass("newsitem-hover"); },
            function () { $(this).removeClass("newsitem-hover"); }
        ).click(function () { location.href = $("a", this).attr("href"); });

        //uniform
        //$("input, select").uniform();

        //tabs with more than one line of text
        $("ul.tabs li.tab").each(function () {

            $tab = $(this);

            var $body = $('.tab-body', $tab), $a = $('.tab-body a', $tab);
            var currentHeight = $a.height();
            var text = $a.text();
            $a.text("|");
            if ($a.height() < currentHeight) {
                $body.addClass("tall-body");
            }
            $a.text(text);

        });


        //        var toolbar = $("#toolbar-custom-links");        
        //        $("div.toolbar-add > a").each(function () {
        //            $(this).remove().appendTo(toolbar);
        //        });

        //Table styles. Alternating row styles etc.
        $(".spider table, .bodytext table").each(function () {

            if ($(this).is(".unformatted-table")) {
                return true;
            }

            $(this).attr("cellpadding", "").attr("cellspacing", "");
            $(this).addClass("hover-table");

            var noHead = $(this).is(".no-header") || $(this).is(".no-header-full-width");

            var rows = $("tr", this);
            rows.each(function (ix) {

                // The table is already formatted. Abort...
                if ($(this).is(".row")) {
                    return false;
                }

                var cssClass = "row";

                if (ix == 0 && !noHead) {
                    //Header
                    cssClass += " header-row";
                } else {
                    //Body                    

                    cssClass += " body-row";

                    //Index relative to first body row
                    var ixBody = ix - (noHead ? 0 : 1);

                    cssClass += " " + (ixBody % 2 == 0 ? "even" : "odd");
                    cssClass += " " + (ixBody % 2 == 0 ? "even-row" : "odd-row");

                    if (ixBody == 0) {
                        cssClass += " first first-row";
                    }
                    if (ix == rows.length - 1) {
                        cssClass += " last last-row";
                    }
                }

                $(this).addClass(cssClass);
            });

        });

	if( document.all ) {
		$("ul.factbox-items span.tag").each(function() {
			if( $(this).height() > 20 ) {
				$(this).css("width", "100%");
			}
		});
	}

    })(jQuery);

});
