$(function() {
	if(window.innerWidth){
	    $('.text-end').height(window.innerHeight - 480);
	} else if (document.documentElement && document.documentElement.clientWidth){
	    $('.text-end').height(document.documentElement.clientHeight - 500);
	} else if (document.body.clientWidth){
	    $('.text-end').height(document.clientHeight - 500);
	}

    $('.tab')
    .mouseover(function(event){
        var $this = $(this);
        $this.data('mouseover', true);
        if(!$this.data('submenuId')){
            $this.data('submenuId', $(this).attr('rel'));
        }

        if($this.data('submenuId')){
            var offset = $(this).parent('li').position();
            var offset1 = $(this).parents('ul:first').position();
            $('#' + $this.data('submenuId')).css({left:(offset.left + offset1.left )}).fadeIn(150);
        }
    })
    .mouseout(function(event){
        var $this = $(this);
        $this.data('mouseover', false);
        if($this.data('submenuId')){
            setTimeout("$('#" + $this.data('submenuId') + "').hideSubmenu();", 500);
        }
    });
    
    $('ul.inners-menu')
    .mouseenter(function(event){
        $(this).data('mouseover', true);
        $('.tab[rel="'+ $(this).attr('id') +'"]').addClass('actives');

    })
    .mouseleave(function(event){
        $(this).data('mouseover', false);
        setTimeout("$('#" + $(this).attr('id') + "').hideSubmenu();", 500);
    });

    $.fn.hideSubmenu = function(){
        if(!$(this).data('mouseover') && !$('.tab[rel='+$(this).attr('id')+']').data('mouseover')){
            this.fadeOut(150);
            $('.tab[rel="'+ $(this).attr('id') +'"]').removeClass('actives');
        }
    };
    
    $('a.openImageView').click(function(event){
    	event.preventDefault();
    	openWin($(this).attr('href'), 800, 600);
    });

});

$(window).resize(function() {
	if(window.innerWidth){
	    $('.text-end').height(window.innerHeight - 480);
	} else if (document.documentElement && document.documentElement.clientWidth){
	    $('.text-end').height(document.documentElement.clientHeight - 500);
	} else if (document.body.clientWidth){
	    $('.text-end').height(document.clientHeight - 500);
	}
});

$(function(){
    $('.menu-city a').click(function(event){
        event.preventDefault();
        $this = $(this);
        $this.parents('.shops').find('.city').hide();
        var id = $this.attr('href').substring(1);
        $(this).parent('li').addClass('active');
        var treadth = this;
        $(this).parents('.shops').find('.menu-city a').each(function(){
            if ($(this)[0] != treadth) {
                $(this).parent().removeClass("active");
            }
        });
        $container = $('#' + id);
        $container.fadeTo("slow", 1);
    });
});

function openWin(p, w, h) {
   $.modal('<iframe src="' + p + '" height="' + (h-2) + '" width="' + (w-2) + '" style="border:0">', {
        containerCss:{
            width: w,
            height: h
        },
        modal: true
   });
}


function addImage(img_url, obj, id) {
    var objImg = obj;
    var img = new Image();
    if ($('img[id='+id+']').length != 0) {
        $('#loader img').css({
            display: 'none'
        });
        $('#' + id).css({
            display: 'block'
        });
        $('.inactive img').attr({
            width: "60"
        }).css({
            opacity: '0.5'
        });
        $(objImg).css({
            opacity: '1'
        });
        $('img[rel=first]').css({
            opacity: '1'
        });
        $('img').attr('rel', '');
    } else {
        $(img).load(function () {
            $(this).hide();
            $('#loader img').css({
                display: 'none'
            })
            $anchor = $("<a />", {
                href: 'view/' + id.split('-')[1],
                target: '_blank',
                click: function(e) {
                    e.preventDefault();
                    openWin($(this).attr('href'), 800, 600);
                }
            });
            $anchor.append(this);
            $('#loader').removeClass('loading').append($anchor);
            $(this).fadeIn();
            $('.inactive img').attr({
                width: "60"
            }).css({
                opacity: '0.5'
            });
            $(objImg).css({
                opacity: '1'
            });
            $('img[rel=first]').css({
                opacity: '1'
            });
            $('img').attr('rel', '');
        }).error(function () {
        }).attr({
            src: img_url,
            id: id
        }).bind('click', {id: id}, function(e){

        });
    }
}

function addImage1(img_url, obj, id) {
    var objImg = obj;
    var img = new Image();
    if ($('img[id='+id+']').length != 0) {
        $('#loader img').css({
            display: 'none'
        });
        $('#' + id).css({
            display: 'block'
        });
        $('.inactive img').attr({
            width: "60"
        }).css({
            opacity: '0.5'
        });
        $(objImg).css({
            opacity: '1'
        });
        $('img[rel=first]').css({
            opacity: '1'
        });
        $('img').attr('rel', '');
        $('.pall').hide();
        $('#p' + id).show();
    } else {
        $(img)
        .load(function () {
            $(this).hide();
            $('#loader img').css({
                display: 'none'
            })
            $anchor = $("<a />", {
                href: 'view/' + id.split('-')[1],
                target: '_blank',
                click: function(e) {
                    e.preventDefault();
                    openWin($(this).attr('href'), 800, 600);
                }
            });
            $anchor.append(this);
            $('#loader').removeClass('loading').append($anchor);
            $(this).fadeIn();
            $('.inactive img').attr({
                width: "60"
            }).css({
                opacity: '0.5'
            });
            $(objImg).css({
                opacity: '1'
            });
            $('img[rel=first]').css({
                opacity: '1'
            });
            $('img').attr('rel', '');
            $('.pall').hide();
            $('#p' + id).show();
            $('div[rel=first]').show();
            $('div').attr('rel', '');
        })
        .error(function () {})
        .attr({
            src: img_url,
            id: id,
            width: '252'
        }).css({height: 'auto'});
    }
}

function gup( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

window.sharers = {
    'twitter': 'http://twitter.com/home?status=Currently%20reading%20',
    'facebook': 'http://www.facebook.com/sharer.php?u=',
    'vkontakte': 'http://www.vkontakte.ru/share.php?url='
}
window.shareParams = {};

function getShareUrl(){
    var value = window.location.protocol + '//' + window.location.hostname + window.location.pathname;
    var paramString = '';
    for(var param in window.shareParams){
        if(paramString.length){
            paramString += '&';
        }
        paramString += param + '=' + window.shareParams[param];
    }
    if(paramString.length){
        value += '?' + paramString;
    }
    return value;
}

function setSelectedIndex(index){
    console.log('received ' + index);
    window.shareParams.preview = index;
    updateShareButtons();
}

function updateShareButtons(){
    var shareUrl = getShareUrl();
    $.each(window.sharers, function(name){
        $('a#' + name + 'ShareButton').attr('href', this + shareUrl);
    })
}
