﻿function onLoad() {
    var h = 0;
    var windowH = 804;
    
    if ($('.content').height() < $('body').height()) {
        if ($(window).height() > 804) {
            windowH = $(window).height();
            $('.bodyBg').css('height', windowH + 'px');
        }
        h = windowH - $('.content').attr('offsetTop') - 34;

        $('.content').css('height', h + 'px');
        $('.container').css('height', windowH - 34 + 'px');    
    }
}

$(window).resize(function () { onLoad(); });

$(document).ready(function () {
    if (pfConfigurator) runPf('AnimatedPictureFrame', pfConfigurator);
    inputWrapper();
    $('.search input[name="query"]').focus(function () { $(this).val(''); });
});

function inputWrapper() {
    $('.contentRight input.submit, .contentStyle input.form_submit, .contentStyle input.test_form').each(function () {
        if (!$(this).parent().hasClass('submit_wrapper')) {
            $(this).wrap('<div class="submit_wrapper">');
        }
    });
}

function runPf(name, conf) {
    try {
        if (document.getElementsByName(name).length > 1) {
            /* Give  params to embed for FF */
            document.getElementsByName(name)[1].init(conf);
        } else {
            /* Give  params to object for IE */
            document.getElementsByName(name)[0].init(conf);
        }
    }
    catch (e) {
        setTimeout(function () { runPf(name, conf) }, 200);
    }
}

function openDialog(params) {
    params = params || {};

    var dialog = window.showModalDialog('/dialogs/commonPopupWindow.html',
		params, 'center:yes; dialogHeight:550px;resizable:yes;dialogWidth:550px ')

    if (params.answer.resCode == 'OK') {
        return params.answer;
    }
}

function mapPage(isAdmin) { 
    // add links to map
    var mapLinks = '';
    for (var i = 0; i < vdalShops.shops.length; i++) {
        mapLinks = mapLinks + '<a class="town" style="top:' + vdalShops.shops[i].y + 'px; left:' + vdalShops.shops[i].x + 'px;" href="#" title="" townName="' + vdalShops.shops[i].name + '"/>';
    }    
    $('.mapRight').append(mapLinks);

    // add options to select
    var options = '';
    for (var i = 0; i < vdalShops.shops.length; i++) {
        options = options + '<option value="' + vdalShops.shops[i].name + '">' + vdalShops.shops[i].name + '</option>';
    }
    $('.mapLeft select').append(options);


    // select option
    $('#dmap').change(function () {
        $('a').removeClass('town_active');
        townName = $(this).find('option:selected=true').attr('value');
        currTown = $('a[townName="' + townName + '"]');
        $(currTown).addClass('town_active');
        showTownName(currTown);
        currShop = findShopByname(townName);

		//Changed, because when edit article don't see cahnge
		var articleText =  $('.shopDescription');
		$(articleText).find('div[articleId]').hide();
		$(articleText).find('div[articleId='+currShop.articleId+']').show();
		
    });

    // map overing
    $('.town').hover(
        function () { showTownName(this); },
        function () { $('a').removeClass('town_active'); $('.townName_left').remove(); }
    );
}

function findShopByname(nameStr) {
    for (var i = 0; i < vdalShops.shops.length; i++) {
        if (vdalShops.shops[i].name  == nameStr)
            return vdalShops.shops[i];
    }
    return null;
}

function screening(str) {    
    str = str.replace("'", "\\'");
    str = str.replace('"', '\\"');
    return str;
}

function showTownName(obj) {
    $('.townName_left').remove();
    $('a').removeClass('town_active');

    $(obj).addClass('town_active');
    $('.mapRight').append('<div class="townName_left"><div class="townName_right"></div></div>');
    $('.townName_left').css({ 'top': $(obj).css('top'), 'left': $(obj).css('left') });
    $('.townName_right').text($(obj).attr('townName'));
}
