
resizeUI = function() {
    	
    var wh = $(window).height();
    var mh = $('.home #main').innerHeight();			
    var top = (wh > mh) ? Math.round((wh-mh)/2) : 0;
    
    $('.home #main').css('margin-top', top);
    $('.home.publisher #bg').height(top+391);
    
    $('.home.publisher-blue #bg').height(top+545);
    $('.home.publisher-blue #bg-img').height(top+545);
    
    $('.home.consumer #bg').height(top+401);
    
    $('.home.consumer2 #bg').height(top+501);
}

resizeBBDImg = function() {

    var p = Math.min(1, ($(window).width() - 60) / 1024);

    var w = Math.floor(p * 1024);
    var h = Math.floor(p * 734);

    $('.breakdown #content #image img').width(w).height(h);
    $('.breakdown #content #image').width(w).height(h);

    $('.breakdown #content .header h1').width(Math.min(950, $(window).width()));
    $('.breakdown #content .header ul.bbd-nav').width(Math.min(950, $(window).width()));
    $('.breakdown #content .notes').width(Math.min(950, $(window).width()) - 20);
    $('.breakdown #footer').width(Math.min(950, $(window).width()) - 20);
}

fieldgroupBehaviour = function() {
    if ($(this).val() != '1') {
        $(this).closest('.fieldgroup').find('.microtron').hide();
        $(this).closest('.input').removeClass('sep');
    } else {
        $(this).closest('.fieldgroup').find('.microtron').show();
        $(this).closest('.input').addClass('sep');
    }
    if ($(this).val() == '2' || $(this).val() == '3') {
        $(this).css('color', '#696969');
    } else {
        $(this).css('color', '#001c4b');
        //$(this).children('option[value=1]')[0].disabled = 'disabled';
        //$(this).children('option[value=2]')[0].disabled = 'disabled';
    }
}

setInputBehavioursEx = function() {
    $('.fieldgroup select.where').each(function(idx, el) {
        fieldgroupBehaviour.apply(el);
        $(el).bind('click change keypress', function() {
            fieldgroupBehaviour.apply(this);
        });
    });
    $('.floats input[type=radio]').each(function(idx, el) {
        el.id = 'radio' + idx;
        $(el).next().attr('for', 'radio' + idx);
    })
}

hide_fz = function() {
    $('#zoom').trigger('click');
}

collectDataFromHorizontalLinksPage = function() {

    var remaining_links = document.getElementById('linksCount').value;
    var result = document.getElementById('collectedValues');

    result.value = '';
    //alert(remaining_links);
    for (i = 1; i <= 5 - remaining_links; i++) {
        if($('#linkTitle input')[i - 1] != null)
        {
          result.value += $('#linkTitle input')[i - 1].value + ';';
        }
        if($('#linkUrl input')[i - 1] != null)
        {
          result.value += $('#linkUrl input')[i - 1].value + ';';
        }

    }

}

collectDataFromVerticalTabsPage = function() {

    var remaining_tabs = document.getElementById('tabsCount').value;
    
    var result = document.getElementById('collectedValues');
    //alert(remaining_tabs);
    result.value = '';
    for (i = 0; i < 5 - remaining_tabs; i++) {
        result.value += $('#tabTitle input')[i].value + ';';
        result.value += $('#tabUrl input')[i].value + ';';
        result.value += $('#tabType select')[i].value + ';';
        result.value += $('#microtonType select')[i].value + ';';
        result.value += $('#standardSize input')[i].checked + ';';
        result.value += $('#newSize input')[i].checked + ';';
        result.value += $('#Width input')[i].value + ';';
        result.value += $('#Height input')[i].value + ';';
    }

}

init = function() {


    // page BMI_211B

    //var remaining_hlink = $('hiddenValue')[0].value;
    //alert($('qwer').value);
    if (document.getElementById('linksCount') != null) {
        var remaining_hlink = document.getElementById('linksCount').value;

        $('a.submit').click(function() {

            collectDataFromHorizontalLinksPage();

            return true;
        });


    }

    $('.step2 #add a').click(function() {
        var remaining_hlink = document.getElementById('linksCount').value;
        
        if (remaining_hlink > 0) {
            remaining_hlink = remaining_hlink - 1;
            
            document.getElementById('linksCount').value = remaining_hlink;
            
            el = $($('#links .fieldgroup')[0]).clone();

            el.find('input')[0].value = '';
            el.find('input')[1].value = '';
            el.find('input')[0].name = 'title' + (5 - remaining_hlink);
            el.find('input')[1].name = 'url' + (5 - remaining_hlink);
            el.find('input')[1].id = 'urlid' + (5 - remaining_hlink);
            el.find('#smallbtn a')[0].href = 'javascript:OpenHorizontalLinkPopup(' + (5 - remaining_hlink) + ');';
            el.find('#urltbid input')[0].id = 'hiUrlID' + (5 - remaining_hlink);
            el.find('#urltbid input')[0].value = 'urlid' + (5 - remaining_hlink);

            $('#links').append(el);
            $('#add span')[0].innerHTML = 'You can add ' + (remaining_hlink) + ' more links';
            //setInputBehaviours();
            if (!remaining_hlink) $('#add').remove();
        }
        return false;
    });



    // page BMI_211C
    if (document.getElementById('tabsCount') != null) {
        //$('.numericinput').numeric();

        var remaining_tabs = document.getElementById('tabsCount').value;
        $('.step3 #add a').click(function() {

            if (remaining_tabs > 0) {
                el = $('.fieldgroup.prototype').clone().removeClass('prototype');

                $('#links').append(el);
                $('#add span')[0].innerHTML = 'You can add ' + (--remaining_tabs) + ' more Vertical Tabs';
                //setInputBehaviours();
                setInputBehavioursEx();
                if (!remaining_tabs) $('#add').remove();
                document.getElementById('tabsCount').value = remaining_tabs;
            }
            return false;
        })

        $('a.submit').click(function() {
            collectDataFromVerticalTabsPage();

            return true;
        });

        /*
        result.value += $('#tabTitle input')[i - 1].value + ',';
        result.value += $('#tabUrl input')[i - 1].name = 'url' + i;
        result.value += $('#tabType select')[i - 1].name = 'tabType' + i;
        result.value += $('#microtonType select')[i - 1].name = 'microtonType' + i;
        result.value += $('#Width input')[i - 1].name = 'width' + i;
        result.value += $('#Height input')[i - 1].name = 'height' + i;

            result.value += $('#standardSize input')[i - 1].name = 'standardSize' + i;
        result.value += $('#newSize input')[i - 1].name = 'newSize' + i;
        */
        // prototype fieldgroups (for add buttons) should be removed before submitting.
        // btw, prototypes should be outside of forms, so it's a safety action
        //$('.prototype').remove();
        //$(this).parents('form').submit();


        $('.manage .x a.help').fancyZoom({
            width: 760,
            height: 500,
            iframe: true,
            hasBg: true,
            bgColor: '#696969',
            bgAlpha: .6
        });

        // page BN_113

        $('.choose .item .img').hover(
		function() {
		    $('a.more', $(this).parent()).css({
		        background: '#4f8fee',
		        color: 'white'
		    });
		},
		function() {
		    $('a.more', $(this).parent()).removeAttr('style');
		}
	);
    }





    setInputBehavioursEx();

    // page BMI_212A and BMI_212B

    //    $('.addnote #commentbox').bind('focus', function(e) {
    //        !e.target._defVal && (e.target._defVal = $(e.target).val());
    //        !e.target._typed && ($(e.target).val(''));
    //    }).blur(function(e) {
    //        !e.target._typed && ($(e.target).val(e.target._defVal));
    //        if ($(e.target).val() == '') $('#addnote-btn').removeClass('enabled');
    //    }).keydown(function(e) {
    //        e.target._typed = true;
    //        $('#addnote-btn').addClass('enabled');
    //        if ($(e.target).val() == '') $('#addnote-btn').removeClass('enabled');
    //    });

    // page _private.php

    $('.breadcrumb a.active').click(function() { return false; });

    if (/msie 6/i.test(navigator.userAgent))
        $('.breadcrumb .first:not(.active) .lft, .breadcrumb .last:not(.active) .rgt').each(function(idx, el) {
            $(el.parentNode).mouseover(function() {
                el.x = parseInt(el.style.backgroundPosition.match(/-?\d+/));
                el.style.backgroundPosition = el.style.backgroundPosition.replace(/-?\d+/, el.x - 40);
            }).mouseout(function() {
                el.style.backgroundPosition = el.style.backgroundPosition.replace(/-?\d+/, el.x);
            });
        });

    /*    (setInputBehaviours = function() {
    $('.default input').focus(function(e) {
    hilight = $(e.target).parents('.default.hilight').length > 0;
    !e.target._defVal && (e.target._defVal = e.target.value);
    !e.target._typed && (e.target.select(), !hilight && (e.target.value = ''));
    }).blur(function(e) {
    !e.target._typed && (e.target.value = e.target._defVal);
    }).keydown(function(e) {
    e.target._typed = true;
    });
    })();*/




    //    $('a.submit').click(function() {
    //        collectData();
    //        // prototype fieldgroups (for add buttons) should be removed before submitting.
    //        // btw, prototypes should be outside of forms, so it's a safety action
    //        //$('.prototype').remove();
    //        //$(this).parents('form').submit();
    //        return true;
    //    });

    $('.click-to-zoom').fancyZoom({
        iframe: false,
        //width: 450,
        //height: 635,
        hasBg: true,
        bgColor: '#696969',
        bgAlpha: .6
    });


    // home pages

    resizeUI();


}

//$(document).ready(function() {
//    init();
//});

$(window).ready(function() {
    init();
});


$(window).resize(function() { resizeUI(); });


ValidateHorizontalLinkTitle = function(sender, args) {
    
    var title = document.getElementById(sender.id.replace('cvTitle', 'tbTitle')).value;
    var url = document.getElementById(sender.id.replace('cvTitle', 'tbUrl')).value;

    if ((title != '') || (title == '' && url == '')) {
        args.IsValid = true;
    }
    else { args.IsValid = false; }
    
}

ValidateHorizontalLinkUrl = function(sender, args) {
    var title = document.getElementById(sender.id.replace('cvUrl', 'tbTitle')).value;
    var url = document.getElementById(sender.id.replace('cvUrl', 'tbUrl')).value;

    if ((url != '') || (title == '' && url == '')) {
        args.IsValid = true;
    }
    else { args.IsValid = false; }
    
}

OpenHorizontalLinkPopup = function(id) {
var url = document.getElementById(document.getElementById('hiUrlID' + id).value).value;
    
    if(url.indexOf('http://') == -1)
    {
      url = 'http://' + url;
    }
    window.open (url,'Validate',"status=1,scrollbars=yes"); 
    
}

OpenVerticalTabPopup = function(id) {
    var url = document.getElementById(document.getElementById('hiTabID' + id).value).value;
if(url.indexOf('http://') == -1)
    {
      url = 'http://' + url;
    }
    window.open (url,'Validate',"status=1,scrollbars=yes"); 
}

function checkForInt(evt) {
var charCode = ( evt.which ) ? evt.which : event.keyCode;
return ( charCode >= 48 && charCode <= 57 );
}


