﻿function Blind(expandElementId, minValue, maxValue, flowVertical, shrinkElementId) {
    var targetValue;

    if (typeof (flowVertical) == 'undefined')
        flowVertical = true;

    var expandElement = document.getElementById(expandElementId);

    if (flowVertical) {
        if (expandElement.offsetHeight < maxValue)
            targetValue = maxValue;
        else
            targetValue = minValue;
    }
    else {
        if (expandElement.offsetWidth < maxValue)
            targetValue = maxValue;
        else
            targetValue = minValue;
    }

    AdjustSize(expandElementId, targetValue, flowVertical, shrinkElementId)
}

function AdjustSize(expandElementId, targetValue, flowVertical, shrinkElementId) {
    if (typeof (flowVertical) == undefined)
        flowVertical = true;

    var expandElement = document.getElementById(expandElementId);
    var shrinkElement;
    if (typeof (shrinkElementId) != undefined)
        shrinkElement = document.getElementById(shrinkElementId);

    var doExpand = false;
    if (flowVertical)
        doExpand = expandElement.offsetHeight < targetValue;
    else
        doExpand = expandElement.offsetWidth < targetValue;

    var step = -2;
    if (doExpand)
        step = 2;

    if (flowVertical) {
        expandElement.style.height = (expandElement.offsetHeight + step) + 'px';
        expandElement.style.display = '';

        if (shrinkElement) {
            shrinkElement.style.height = (shrinkElement.offsetHeight + (step * -1)) + 'px';
            if (shrinkElement.style.height == '0px')
                shrinkElement.style.display = 'none';
        }

        if ((doExpand && expandElement.offsetHeight < targetValue) || (!doExpand && expandElement.offsetHeight > targetValue))
            window.setTimeout("AdjustSize('" + expandElementId + "', " + targetValue + ", " + flowVertical + ", '" + shrinkElementId + "')", 1);
    }
    else {
        expandElement.style.width = (expandElement.offsetWidth + step) + 'px';
        expandElement.style.display = '';

        if (shrinkElement) {
            shrinkElement.style.width = (shrinkElement.offsetWidth + (step * -1)) + 'px';
            if (shrinkElement.style.width == '0px')
                shrinkElement.style.display = 'none';
        }

        if ((doExpand && expandElement.offsetHeight < targetValue) || (!doExpand && expandElement.offsetHeight > targetValue))
            window.setTimeout("AdjustSize('" + expandElementId + "', " + targetValue + ", " + flowVertical + ", '" + shrinkElementId + "')", 1);
    }
}
function Visible(ElementId, bVisible) {
    var Object = document.getElementById(ElementId);

    if (bVisible) {
        Object.style.display = 'block';
    } else {
        Object.style.display = 'none';
    }
}
//Search Field
var searchDefaultValue = '';
function initiateSearch(id) {
    if (document.getElementById(id)) {
        searchDefaultValue = document.getElementById(id).value;
        document.getElementById(id).className = 'input inactiv';
        document.getElementById(id).onfocus = function () { handleSearch(id, true); };
        document.getElementById(id).onblur = function () { handleSearch(id, false); };
    }
}
function handleSearch(id, b) {
    if (id != null && document.getElementById(id) != null) {
        if (b) {
            if (document.getElementById(id).value == searchDefaultValue) {
                document.getElementById(id).value = '';
                document.getElementById(id).className = 'input';

            }
        } else {
            if (document.getElementById(id).value == '') {
                document.getElementById(id).value = searchDefaultValue;
                document.getElementById(id).className = 'input inactiv';

            }
        }
    }
}
//round Border
function initiateRoundBorder(id, showBorder) {
    if (showBorder == undefined)
        showBorder = true;
    var parent = document.getElementById(id);
    if (parent) {
        var img = parent.getElementsByTagName('img');
        for (var i = 0; i < img.length; i++) {
            if (img[i].getAttribute('rel') != 'noBorder' &&
            img[i].parentNode.getAttribute('rel') != 'noBorder' &&
            img[i].parentNode.parentNode.getAttribute('rel') != 'noBorder' &&
            img[i].parentNode.parentNode.parentNode.getAttribute('rel') != 'noBorder' &&
            img[i].parentNode.parentNode.parentNode.parentNode.getAttribute('rel') != 'noBorder' &&
            img[i].parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute('rel') != 'noBorder') {
                if ($.browser.msie) {
                    var div0, div1, div2, div3;
                    div0 = document.createElement('div');
                    div1 = document.createElement('div');
                    div2 = document.createElement('div');
                    div3 = document.createElement('div');



                    var newData = img[i];

                    if (showBorder == true) {
                        if (newData.getAttribute('align') == 'right') {
                            div0.className = 'roundBorderRight';
                        }
                        else {
                            div0.className = 'roundBorderLeft';
                        }

                        newData.removeAttribute('align');
                    }
                    else {
                        /*
                        if (newData.getAttribute('align') == 'right') {
                            $(newData).css('margin', '0 0 7px 7px;');
                            newData.setAttribute('align', 'right');
                        }
                        else {
                            $(newData).css('margin', '0 7px 7px 0;');
                            newData.setAttribute('align', 'left');
                        }
                        */
                        $(newData).css('border-width', '10px;');
                        $(newData).css('border-color', document.bgColor);
                    }
                    img[i].parentNode.replaceChild(div0, img[i]);

                    div3.appendChild(newData);
                    div2.appendChild(div3);
                    div1.appendChild(div2);
                    div0.appendChild(div1);
                } else {
                $(img[i]).addClass('roundBorderCss');
                }
            }
        }
    }
    setDocumentHeight();
}
//round Border
function createRoundBorder(p, i, s) {
    var parent = $(p);
    var items = $(p).find(i);
    if (parent && items && items.length > 0) {
        if ($.browser.msie) {
            /*for (var i = items.length; i > 0; i--) {
            items[i].parent().html('<div class="roundBorderLeft"><div><div><div>' + items[i].parent().html() + '</div></div></div></div>');
            }*/
            items.each(function (key, value) {
                $(value).html('<div class="roundBorderLeft"><div><div><div>' + $(value).html() + '</div></div></div></div>');
            });
        } else {
            if (s == 'small') {
                items.children('a').addClass('roundSmallBorderCss');
            } else {
                items.addClass('roundBorderCss');
            }
        }
    }
}
//gallery
function createGallery(p, c, i, w) {
    var parent = $(p);
    var items = $(p).find(i);
    if (parent && items && items.length > 0 && w > 0) {
        galleryItemWidth = w;
        var count = (items.length - 4);

        if (count <= 0) {
            galleryMaxOffset = 0;
        } else {
            galleryMaxOffset = ((galleryItemWidth * (count))) * (-1);
        }
        parent.css({
            'overflow': 'visible',
            'width': galleryItemWidth * 4
        });
        $(c).css({
            'width': galleryItemWidth * 4,
            'overflow': 'hidden',
            'position': 'relative'
        });
        galleryItem = parent.find('ul');
        galleryItem.css({
            'position': 'relative',
            'left': '0'
        });
        if (count != 0) {
            parent.append('<a id="' + parent.attr('id') + 'Next" class="ButtonNext">&nbsp;</a><a id="' + parent.attr('id') + 'Prev" class="ButtonPrevious">&nbsp;</a>');

            $(p + 'Next').click(function () { goToGallery(galleryItemWidth * (-1) * 4, p); });
            $(p + 'Prev').click(function () { goToGallery(galleryItemWidth * 4, p); });

            $(p + 'Prev').css('opacity', 0.2);
        }
    }
}
var galleryItem = null;
var galleryMaxOffset = 0;
var galleryItemWidth = 0;
function goToGallery(i, p) {
    var offsetInt = parseInt(galleryItem.css('left'));
    offsetInt = offsetInt + i;

    $(p + 'Next').css('opacity', 1);
    $(p + 'Prev').css('opacity', 1);

    if (offsetInt <= galleryMaxOffset) {
        offsetInt = galleryMaxOffset;
        $(p + 'Next').css('opacity', 0.2);
    }
    if (offsetInt >= 0) {
        offsetInt = 0;
        $(p + 'Prev').css('opacity', 0.2);
    }
    $(galleryItem).animate({
        left: offsetInt
    }, 500, function () {
        //console.log(parseInt(galleryItem.css('left')));
    });
}

//setHeight of the Page
function setDocumentHeight() {
    var windowHeight = 0;
    var dataHeight = document.getElementById('Page').offsetHeight;
    if ($.browser.msie) {
        windowHeight = document.body.offsetHeight;
    } else {
        windowHeight = window.innerHeight;
    }
    //alert(windowHeight + ' / ' + dataHeight);
    if (windowHeight < dataHeight) {
        $('#Document').css('min-height', dataHeight);
        $('#DocShadow').css('min-height', dataHeight);
    } else {
        $('#Document').css('min-height', 0);
        $('#DocShadow').css('min-height', 0);
    }
}
$(document).ready(function () {

    //create gallery
    createGallery('#PageBottomGallery', '#PageBottomGalleryCanvas', 'li', 138);
    //round border for Gallery
    createRoundBorder('#PageBottomGallery', 'li', 'small');
	//round border for block
    createRoundBorder('#blockBorder', '.blockBorderItem', '');
});
