$(function() {
	if(window.innerWidth){
	    $('.index #galleria').height(window.innerHeight - 190);
	    $('.photo-gal #galleria').height(window.innerHeight - 120);
	} else if (document.documentElement && document.documentElement.clientWidth){
	    $('.index #galleria').height(document.documentElement.clientHeight - 190);
	    $('.photo-gal #galleria').height(document.documentElement.clientHeight - 120);
	} else if (document.body.clientWidth){
	    $('.index #galleria').height(document.clientHeight - 190);
	    $('.photo-gal #galleria').height(document.clientHeight - 120);
	}
	
    $('.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');
        }
    };
    
    $("#g_play").hide();
    
    _nI = $("#galleria img");
    jQuery.each(_nI, function() {
        _wS = $(this).width()* $('#galleria').height() /$(this).height ().toFixed(0)
        $(this).css({
            left: '50%',
            marginLeft: - _wS/2,
            height: $('#galleria').height()
        });
    });
});

$(window).load(function() {
    $(".m-navigation").fadeIn(1200);
    
    _nI = $("#galleria img");
    jQuery.each(_nI, function() {
        _wS = $(this).width()* $('#galleria').height() /$(this).height ().toFixed(0)
        $(this).css({
            left: '50%',
            marginLeft: - _wS/2,
            height: $('#galleria').height()
        });
    });
    $("#galleria img:first").addClass('i-active').fadeIn();
    $('#galleria').removeClass('loading');
    $('body').removeClass('bBlack');
    setTimer();
    
    
    $("#g_pause").click(function(event) {
        event.preventDefault();
        clearInterval(play);
        $(this).hide();
        $("#g_play").show();
    });
    $("#g_play").click(function(event) {
        event.preventDefault();
        setTimer();
        $(this).hide();
        $("#g_pause").show();
    });
    $("#g_prev").click(function(event) {
        event.preventDefault();
        $o = $('#galleria img.i-active').prev(); 
        if ( $o.length === 0) {
            $o = $('#galleria img:last');
        }
        clearInterval(play); 
        setTimer();
        slideRotate(); 
    });
    $("#g_next").click(function(event) {
        event.preventDefault();
        $o = $('#galleria img.i-active').next();
        if ( $o.length === 0) {
            $o = $('#galleria img:first');
        }
        clearInterval(play); 
        setTimer();
        slideRotate(); 
    });
});

$(window).resize(function() {
	if(window.innerWidth){
	    $('.index #galleria').height(window.innerHeight - 190);
	    $('.photo-gal #galleria').height(window.innerHeight - 120);
	} else if (document.documentElement && document.documentElement.clientWidth){
	    $('.index #galleria').height(document.documentElement.clientHeight - 190);
	    $('.photo-gal #galleria').height(document.documentElement.clientHeight - 120);
	} else if (document.body.clientWidth){
	    $('.index #galleria').height(document.clientHeight - 190);
	    $('.photo-gal #galleria').height(document.clientHeight - 120);
	}
	
    _nI = $("#galleria img");
    jQuery.each(_nI, function() {
        _wS = $(this).width()* $('#galleria').height() /$(this).height ().toFixed(0)
        $(this).css({
            left: '50%',
            marginLeft: - _wS/2,
            height: $('#galleria').height()
        });
    });
});


function slideRotate(){
    $('#galleria img').fadeOut().removeClass('i-active');
    $($o).fadeIn().addClass('i-active');
}

function setTimer(){
    play = setInterval(function(){ 
        $o = $('#galleria img.i-active').next(); 
        if ( $o.length === 0) {
            $o = $('#galleria img:first');
        }
        slideRotate(); 
    }, 5000);
};

$(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 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'
        })
        $('#loader').removeClass('loading').append(this);
        $(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,
        width:  '318'
        });
    }
}

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'
        })
        $('#loader').removeClass('loading').append(this);
        $(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'
        });
    }
}

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);
	})
}
