var escapeHtml = function(html) {
    return html
        .replace(/&/g,  "&amp;")
        .replace(/\"/g, "&quot;")
        .replace(/</g,  "&lt;")
        .replace(/>/g,  "&gt;")
    ;
}


var unescapeHtml = function(html) {
    return html
        .replace(/&amp;/g, '&')
        .replace(/&quot;/g, '"')
        .replace(/&lt;/g,  '<')
        .replace(/&gt;/g,  '>')
    ;
}


var GalleryOptions = {
    overlayBgColor  : '#001A3B',
    overlayOpacity  : 0.55,
    imageLoading    : '/static/img/lightbox/loading.gif',
    imageBtnClose   : '/static/img/lightbox/close.gif', 
    imageBtnPrev    : '/static/img/bg-prev.png', /*  lightbox/prev.gif */
    imageBtnNext    : '/static/img/bg-next.png', /*  lightbox/next.gif */
    fixedNavigation: true,
    lastItemInobject: null,
    title_attr: 'data-title'
};
var Gallery = function(selector) {
    $(function(){
        $(selector).lightBox(GalleryOptions);
        $(selector+' img').easyTooltip({
             useElement: '<div id="item"><div class="bg-tip-top"><div class="bg-tip-top-inner"></div></div><div class="bg-tip"><div class="bg-tip-inner"><p>&nbsp;</p></div></div><div class="bg-tip-bottom"><div class="bg-tip-bottom-inner"></div></div></div>'
        });
    });
}

var Caroucell = function(selector) {
    $(function(){
        /* caroucell */
        $(selector).each(function (i, elem) {
            var elem_index = i;
        	$(elem).parent().attr('data-index', elem_index);
        	$(elem).jCarouselLite({
                btnNext: 'div[data-index='+elem_index+'] .next',
                btnPrev: 'div[data-index='+elem_index+'] .prev',
                speed: 1000,
                visible: 4,
                mouseWheel: false,
                circular: false,
                afterEnd: function () {
                    $(elem).find('a').lightBox(GalleryOptions);
                }            
            });
        })
    });
}

var Promo = function()
{
    $(function(){
        var
            toggleButton = $('#cut-toggler'),
            earthActive = $('div.earth-raz')
        ;
        $('#cut-toggler').toggle(
            function(e){
                toggleButton.html('Показать на карте');
                earthActive.show();
            },
            function(e){
                toggleButton.html('Показать в разрезе');
                earthActive.hide();
            }
        );
        $('div.earth div.popup p')
            .mouseover(function(e){
                var target = $(this);
                var name = target.closest('DIV').attr('class').replace('popup', '').replace(/^\s+|\s+$/g, '');
                $('div.earth-'+name).show();
            })
            .mouseout(function(e){
                var target = $(this);
                var name = target.closest('DIV').attr('class').replace('popup', '').replace(/^\s+|\s+$/g, '');
                $('div.earth-'+name).hide();
            });
    });
}

var StylizeSelect = function() {
    $(function(){
        $('select').sSelect();
    });
}

var BeautyQueryString = function() {
    var form = $('.map-filter form');
    $(function(){
        form.submit(function(e) {
            $('select').each(function(){
                var select = $(this);
                if (! select.val()) {
                    select.attr('disabled', 'disabled');
                }
            });
            location.href = form.attr('action') + '?' + form.serialize();
            return false;
        });
    });
}

function hide_slider_controls() {
    $('div.slider').each(function (i, item) {
		count = $('li', item).length;
		if (count < 5) {
			par = $(item).parent();
			$('.prev, .next', par).hide();
		}
	});
}

$(function () {
	if ( $.browser.msie ) $('.top-search input').css('margin-top', '3px');
	$('input[placeholder], textarea[placeholder]').placeholder();
	
	$('div.content-all table').each(function (i, table) {
		if (table.className == '') {
			table.className = 'ordinary';
			$('tr:even', table).css('background-color', '#f7fbfc');
			$('tr:first-child', table).css('background-color', '#f1f4f7');
		}
	});
	
	hide_slider_controls();
	
	var cm = $('div.content-main');
	var h1 = cm.children('.about').height();
	var h2 = cm.children('.completed-projects').height();
	var h3 = cm.children('.short-news').height();
	
	if (h1>h3 || h2>h3) {
		//console.log(h1, h2, h3);
		var h = (h2>h1?h2: h1);
		cm.children('div.short-news').height(h);
	}
});

function init_gallery(galleryId, pluginId, galleryUrl, pluginUrl) {
    var url = galleryId ? galleryUrl : (pluginId ? pluginUrl : null);

    if (url) {
        $.get(url, {}, function(html) {
            $('#gallery-place-'+galleryId).html(html);
            hide_slider_controls();
        });
    } 

   $('div.pagination').hide();
   Gallery('div.one-picture a');
}

