$(document).ready(function() {
    DoLayout();
});

//function DoLayout() {
//    InitCollapsedAreas();
//    InitYouTubeMovie();
//    InitGallery();
//    EqualColumns();
//}

function EqualColumns() {
    EqualContentHeight(new Array("LeftColumn", "RightColumn"));
    EqualSignsHeight(new Array("ContentWrapper", "Contact", "Partners", "Link")); 
}

function EqualContentHeight(divs) {
    // get max height
    var maxHeight = 0;
    for (var i = 0; i < divs.length; i++) {
        divHeight = $("#" + divs[i]).outerHeight();
        if (maxHeight < divHeight)
            maxHeight = divHeight;
    }    
    // equal divs
    for (var i = 0; i < divs.length; i++) {
        $("#" + divs[i]).css("height", maxHeight);
    }
}

function EqualSignsHeight(divs) {
    // get height as a sum
    height = 6;  
    for (var i = 0; i < divs.length; i++) {
        height += $("#" + divs[i]).outerHeight();
    }
    // equal divs
    $("#LeftSigns").css("height", height);
    $("#RightSigns").css("height", height);
}

function InitCollapsedAreas() {
    var repeaterItems = $(".RepeaterItem");
    for (var i = 0; i < repeaterItems.length; i++) {

        var isCollapsed = $("#IsCollapsed-" + repeaterItems[i].id).val();
        var collapsedTextLength = $("#CollapsedTextLength-" + repeaterItems[i].id).val();

        if (collapsedTextLength > 0 && collapsedTextLength < 100)
            collapsedTextLength = 100;

        if (isCollapsed == "True") {
            var expandedText = GetExpandedText(repeaterItems[i].id);
            var collapsedText = GetCollapsedText(repeaterItems[i].id, collapsedTextLength);

            expandedText += "<a href='#' onclick='Collapse(" + repeaterItems[i].id + "); return false;' class='collapseLink'>" + $("#ctl00_hdnShowLessTranslation").val() + "</a>"
            collapsedText += "<a href='#' onclick='Expand(" + repeaterItems[i].id + "); return false;' class='collapseLink'>" + $("#ctl00_hdnShowMoreTranslation").val() + "</a>"

            $("#ExpandedText-" + repeaterItems[i].id).val(expandedText);
            $("#CollapsedText-" + repeaterItems[i].id).val(collapsedText);

            $("#RepeaterItemText-" + repeaterItems[i].id).html(collapsedText);
        }
    }
}

function Collapse(itemId) {
    var contentHeightBefore = $("#Content").outerHeight();
    var newsHeightBefore = $("#HotNews").outerHeight();
    
    var collapsedText = $("#CollapsedText-" + itemId).val();
    $("#RepeaterItemText-" + itemId).html(collapsedText);

    var contentHeightAfter = $("#Content").outerHeight();
    var newsHeightAfter = $("#HotNews").outerHeight();

    var difContent = contentHeightBefore - contentHeightAfter;
    var difNews = newsHeightBefore - newsHeightAfter;

    var leftColumnHeight = $("#LeftColumn").outerHeight();
    var rightColumnHeight = $("#RightColumn").outerHeight();
    var leftSignsHeight = $("#LeftSigns").outerHeight();
    var rightSignsHeight = $("#RightSigns").outerHeight();

    $("#LeftColumn").css("height", leftColumnHeight - difContent - difNews);
    $("#RightColumn").css("height", rightColumnHeight - difContent - difNews);
    $("#LeftSigns").css("height", leftSignsHeight - difContent - difNews);
    $("#RightSigns").css("height", rightSignsHeight - difContent - difNews);

    //EqualColumns();
}

function Expand(itemId) {
    var contentHeightBefore = $("#Content").outerHeight();
    var newsHeightBefore = $("#HotNews").outerHeight();
    
    var expandedText = $("#ExpandedText-" + itemId).val();
    $("#RepeaterItemText-" + itemId).html(expandedText);

    var contentHeightAfter = $("#Content").outerHeight();
    var newsHeightAfter = $("#HotNews").outerHeight();

    var difContent = contentHeightAfter - contentHeightBefore;
    var difNews = newsHeightAfter - newsHeightBefore;

    var leftColumnHeight = $("#LeftColumn").outerHeight();
    var rightColumnHeight = $("#RightColumn").outerHeight();
    var leftSignsHeight = $("#LeftSigns").outerHeight();
    var rightSignsHeight = $("#RightSigns").outerHeight();

    $("#LeftColumn").css("height", leftColumnHeight + difContent + difNews);
    $("#RightColumn").css("height", rightColumnHeight + difContent + difNews);
    $("#LeftSigns").css("height", leftSignsHeight + difContent + difNews);
    $("#RightSigns").css("height", rightSignsHeight + difContent + difNews);

    //EqualColumns();
}

function GetExpandedText(itemId) {
    return $("#RepeaterItemText-" + itemId).html();
}

function GetCollapsedText(itemId, size) {
    var html = $("#RepeaterItemText-" + itemId).html().replace(/\r\n/gim, "");
    var tagArray = new Array();
    var i = 0;
    var j = 0;
    var str = "";

    for (i; i < html.length && j < size; i++) {

        // get two chars
        var c = html[i];
        var cc = (i + 1 < html.length && i + 1 < size) ? html[i + 1] : "";

        var tag = "";


        // special html comment <!--[if !supportEmptyParas]-->
        if (c == '<' && cc == '!') {
            i++;
            while (html[i] != '>') {
                i++;
            }
        }
        // build start tag
        else if (c == '<' && cc != '/') {
            i++;
            while (html[i] != '>' && html[i] != ' ') {
                tag += html[i];
                i++;
            }

            if (tag != 'BR' && tag != 'br') {
                tagArray.push(tag);
            }

            if (html[i] == ' ') {
                while (html[i] != '>') {
                    i++;
                }
            }
        }
        // build end tag
        else if (c == '<' && cc == '/') {
            i += 2;
            while (html[i] != '>') {
                tag += html[i];
                i++;
            }

            tagArray.pop();
        }
        // special html key words
        else if (c == '&' && cc != ';') {
            i++;
            while (html[i] != ';') {
                i++;
            }
        }
        // text
        else if(c != ' ') {
            j++;
        }
    }

    var out = html.substring(0, i) + ' ... ';

    while (tagArray.length > 0) {
        out += '</' + tagArray.pop() + '>';
    }

    return out;
}

function InitYouTubeMovie() {
    var movieItems = $(".MovieItem");
    for (var i = 0; i < movieItems.length; i++) {
        //$(movieItems[i]).html("You need Flash player 8+ and JavaScript enabled to view this video.");
        var movieURL = $("#Movie-" + movieItems[i].id).val();
        var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
        var atts = { id: movieItems[i].id };
        swfobject.embedSWF(movieURL, movieItems[i].id, "640", "385", "8", null, null, params, atts);
        swfobject.expressInstallCallback();
    }
    //var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
    //var atts = { id: "MoviePlayer" };
    //swfobject.embedSWF("http://www.youtube.com/v/3NTzmFFwF40?border=0&amp;enablejsapi=1&amp;playerapiid=ytplayer",
    //"MoviePlayer", "425", "344", "8", null, null, params, atts);
}

function InitGallery() {
    $('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability

    $('ul.gallery_demo').galleria({
        history: true, // activates the history object for bookmarking, back-button etc.
        clickNext: true, // helper for making the image clickable
        insert: '#main_image', // the containing selector for our main image
        onImage: function(image, caption, thumb) { // let's add some image effects for demonstration purposes

            // fade in the image & caption
            image.css('display', 'none').fadeIn(500);
            caption.css('display', 'none').fadeIn(500);

            // fetch the thumbnail container
            var _li = thumb.parents('li');

            // fade out inactive thumbnail
            _li.siblings().children('img.selected').fadeTo(500, 0.3);

            // fade in active thumbnail
            thumb.fadeTo('fast', 1).addClass('selected');

            // add a title for the clickable image
            image.attr('title', 'Next image >>');

            // paho
            ExpandContentForGallery();
        },
        onThumb: function(thumb) { // thumbnail effects goes here

            // fetch the thumbnail container
            var _li = thumb.parents('li');

            // if thumbnail is active, fade all the way.
            var _fadeTo = _li.is('.active') ? '1' : '0.3';

            // fade in the thumbnail when finnished loading
            thumb.css({ display: 'none', opacity: _fadeTo }).fadeIn(1500);

            // hover effects
            thumb.hover(
					function() { thumb.fadeTo('fast', 1); },
					function() { _li.not('.active').children('img').fadeTo('fast', 0.3); } // don't fade out if the parent is active
				)
        }
    });
}

function ExpandContentForGallery() {
    var contentHeight = $("#Content").outerHeight();
    var contentTop = $("#Content").offset().top;

    var imageCaptionHeight = $("#main_image .caption").outerHeight();
    var imageCaptionTop = $("#main_image .caption").offset().top;
    var imageCaptionBottom = imageCaptionTop + imageCaptionHeight;

    var movieHeight = $(".MovieItemWrapper").outerHeight();
    var movieTop = $(".MovieItemWrapper").offset().top;  

    if (movieTop < imageCaptionBottom) {
        var dif = imageCaptionBottom - movieTop;
        $("#Content").css("height", contentHeight + dif + 50);
    }
    else if (movieTop > imageCaptionBottom) {
        var dif = movieTop - imageCaptionBottom;
        $("#Content").css("height", contentHeight - dif + 50);
    }

    var hotNewsHeight = $("#HotNews").outerHeight();
    var hotNewsTop = $("#HotNews").offset().top;
    var hotNewsBottom = hotNewsHeight + hotNewsTop;
    
    var rightColumnHeight = $("#RightColumn").outerHeight();
    var rightColumnTop = $("#RightColumn").offset().top;
    var rightColumnBottom = rightColumnHeight + rightColumnTop;

    if (rightColumnBottom < hotNewsBottom) {
        var dif = hotNewsBottom - rightColumnBottom;
        $("#RightColumn").css("height", rightColumnHeight + dif);
    }
    else if (rightColumnBottom > hotNewsBottom) {
        var dif = rightColumnBottom - hotNewsBottom;
        $("#RightColumn").css("height", rightColumnHeight - dif);
    }

    EqualColumns();
}

//"http://www.youtube.com/v/3NTzmFFwF40?border=0&amp;enablejsapi=1&amp;playerapiid=ytplayer"