﻿var coordsXOld = 10000;
var options = {
    'useHTML' : true,
    'elements': [
        {
          'selector': '.img3',
          'firstAnim':true,
          'properties': {
            'x': {
              'left': {
                'initial': 50,
                'multiplier': 0.01,
                'unit': '%'
              }
            }
          }
        },
        {
          'selector': '.img2',
          'firstAnim':true,
          'properties': {
            'x': {
              'left': {
                'initial': 50,
                'multiplier': 0.006,
                'unit': '%'
              }
            }
          }
        },
        {
          'selector': '.img1',
          'firstAnim':true,
          'properties': {
            'x': {
              'left': {
                'initial': 50,
                'multiplier': 0.002,
                'unit': '%'
              }
            }
          }
        }
    ]
};
              
 $(document).ready(function() 
 {
     if ($("#content .container").is('div')) {
        $("#content .container").imageLoader({
        images: [{ src: '/templates/images/3.png', 'class': 'img3', style : 'left:50%', alt: 'Маркетград' },
           { src: '/templates/images/2.png', 'class' : 'img2', style : 'left:50%', alt: 'Маркетград' },
           { src: '/templates/images/1.png', 'class' : 'img1', style : 'left:50%', alt: 'Маркетград' },
           { src: '/templates/images/colorlessMarket.jpg', 'class' : 'colorless', alt: 'Маркетград' }]
        }, function(){
            $("#content .container img").show();
            $("#content .container").bind('mousemove', function(e) { 
                parallax(e);
            })
            .mouseleave(function() {
                $('html').unbind();
                coordsXOld = 10000;
                $("#content .container").bind('mousemove', function(e) { 
                    parallax(e);
                });
                $('.img1, .img2, .img3, .colorless').stop(true, false);
                for (var i = options.elements.length - 1; i >= 0; i--) {
                    var time = Math.abs($(options.elements[i].selector).css('left').replace('%','') - 50) / (options.elements[i].properties.x.left.multiplier);
                    $(options.elements[i].selector).animate({'left':'50%'}, (time < 400) ? time : 400, function() {
                        $('.colorless').fadeTo(200,1);
                        $('html').unbind();
                    });
                }
            });
        });
    }
 });

function parallax(e) 
{
    if (Math.abs(coordsXOld - e.pageX) > 100) {
        coordsXOld = e.pageX;
        $('.colorless').stop(true,false).fadeTo(0,0);
        el = $("#content .container");
        var center = {
            'x': Math.floor(parseInt(el.width()) / 2)
        }

        var pos = {
            'x': (e.pageX - el.offset().left)
        } 

        var offset = {
            'x': (pos.x - center.x)
        }
        
        for (var i = options.elements.length - 1; i >= 0; i--) {
            
            var opts = {}, value, p, time;

            for (var property in options.elements[i].properties.x) {
              p = options.elements[i].properties.x[property];
              value = p.initial + (offset.x * p.multiplier);
              opts[property] = value + p.unit;
              time = Math.abs(value - $(options.elements[i].selector).css(property).replace('%','')) / (options.elements[i].properties.x[property].multiplier);
            }

            $(options.elements[i].selector).stop(true, false).animate(opts, (time < 400) ? time : 400, 'linear', function() {
                $('.img1, .img2, .img3').stop(true, false);
                $('html, #content .container').unbind('mousemove');
                coordsXOld = 10000;
                $('#content .container').parallax(options);
            });
        }
    }
}

function email(to, message) 
{
    if (to) {
        mail = to
    } else {
        mail = "in" + "fo";
    }
    mail+='&#64;'+'greenice'+'.net';
    document.write('<a href="mailto:'+mail+'"'+(message ? ('onClick="exitTracker._trackEvent(&#39;'+message+'&#39;, &#39;'+mail+'&#39;)"') : '')+'>'+mail+'</a>' );
}


/**
* Управление загрузкой аттачей в feedback
*/
$(document).ready(function() {
	$('#attach-button').live('click', function(){
		var attachId = $('#attach-list > div').length;
        if (attachId < 5) {
            $('#attach-list').show();        
            $('#attach-list').append(
                '<div class="feedback-attach">'+
                    '<div class="attach-fields">' +
                        '<div class="inputFileContainer">' +
                            '<input type="button" class="button" value="Выбрать файл"/>' +
                            '<input class="file" type="file" name="feedbackAttach[' + attachId + ']" size="1"/>' +
                        '</div>' +
                    '</div>' +
                    '<div class="attach-info" style="display:none;">'+
                        '<div class="filename"><span></span>&nbsp;&nbsp;<a href="#">Удалить</a></div>' + 
                    '</div>'+
                '</div>'
            )
            if (attachId == 4) {
                $('#attach-button').addClass('no-attach').text('Больше прикрепить нельзя');
            }
            initEvents();
        }
		return false;
	});
    
    $('.loading .cancel').click(function() {
        //swfu.cancelQueue();
        window.uploadform_send = false;
        $('.loading').hide();
        $('.submitButton span').removeClass('active');
        return false;
    });
	
	function initEvents(){
		$('.feedback-attach input.file').change(function(){
			var fileName = $(this).attr('value');
            
			var attach = $(this).parents('.feedback-attach');
			$('.attach-fields', attach).hide();
			$('.attach-info .filename span', attach).html(fileName);
			$('.attach-info', attach).show();
			$('.attach-info a', attach).click(function(){
				$(this).parents('.feedback-attach').remove();
                $('#attach-button').removeClass('no-attach').text('Прикрепить файлы');
                return false;
			});
		});
	}


/**
* Инициализация карты в контактах
*/
    if ($('#feedBackForm').length) {
        if (GBrowserIsCompatible()) {
        
            function createMarker(latlng, html, options) {
                var marker = new GMarker(latlng, { icon: options });
                GEvent.addListener(marker, "click", function() {
                    map.openInfoWindowHtml(latlng, html);
                });
                if (options.view) {
                    map.openInfoWindowHtml(latlng, html);
                }
                return marker;
            }
            
            $(document).keydown(function(e){
                if (e.keyCode==27 && $('#popup').css('display') == 'block'){
                    $('#popup .close').click();
            }});

            var map = new GMap2(document.getElementById("googleMap"));
            var customUI = map.getDefaultUI();
            customUI.maptypes = {
                hybrid : false,
                normal : false,
                physical : false,
                satellite : false
            }
            map.setUI(customUI);

            map.setMapType(G_HYBRID_MAP);
            
            map.setCenter(new GLatLng(44.585475, 33.480273), 16);
           
            var icon = new GIcon(G_DEFAULT_ICON);
            icon.view = false;
            icon.image = "/templates/images/bus.png";
            icon.iconSize = new GSize(32, 37);
            icon.iconAnchor  = new GPoint(16, 37);
            icon.imageMap  = [3,3, 28,3, 28,28, 21,28, 16,35, 10,28, 3,28];
            map.addOverlay(createMarker(new GLatLng(44.588253, 33.479836), '<h3>Остановка "Студгородок"</h3><br/><p>Маршруты №14, 10, 110, 109, 107, 83, 95, 95a</p>', icon));
            
            icon.image = "/templates/images/bus.png";
            map.addOverlay(createMarker(new GLatLng(44.587905, 33.478720), '<h3>Остановка "Студгородок"</h3><br/><p>Маршруты №14, 10, 110, 109, 107, 83, 95, 95a</p>', icon));
            
            icon.image = "/templates/images/cart.png";
            map.addOverlay(createMarker(new GLatLng(44.583053, 33.478817), '<h3>Картинг - центр</h3>', icon));
            
            icon.image = "/templates/images/university.png";
            map.addOverlay(createMarker(new GLatLng(44.594888, 33.475754), '<h3>Севастопольский национальный технический университет</h3>', icon));
            
            icon.image = "/templates/images/builder.png";
            map.addOverlay(createMarker(new GLatLng(44.581670, 33.480400), '<h3>ООО "Строитель"</h3>', icon));
            
            icon.image = "/templates/images/truck.png";
            map.addOverlay(createMarker(new GLatLng(44.582855, 33.479976), '<h3>Транспортная компания "Автолюкс"</h3>', icon));
            
            var iconGreen = new GIcon(G_DEFAULT_ICON);
            iconGreen.image = "/templates/images/greeniceIco.png";
            iconGreen.iconSize = new GSize(88, 38);
            iconGreen.iconAnchor  = new GPoint(44, 38);
            iconGreen.imageMap  = [3,3, 84,3, 84,29, 52,29, 45,35, 40,29, 3,29];
            
            map.addOverlay(createMarker(new GLatLng(44.582854, 33.481011), '<h3>Greenice</h3><br/><p>99053, Украина, г. Севастополь,<br/> Фиолентовское шоссе 1/2, корпус Б</p>', iconGreen));

            $('#showMap').click(function() {
                showMap();
            });
            
            $('#popup .close, #popupBackground').click(function() {
                $('#popup').hide();
                $('#popupBackground').hide();
                if (document.location.href.indexOf('#map') != -1) {
                    document.location.replace(document.location.href.replace('#map','#'));
                }
                return false;
            });
            
            if (document.location.hash == "#map") {
                showMap();
            }

            
        }
    }
	
});
function showMap () {
    $('#popup').show();
    $('#popup').css({
        marginTop : ($('body').scrollTop() + $('html').scrollTop()) - ($('#popup').height()+6)/2
    });
    $('#popupBackground').show();
    $('#popupBackground').height($('#wrap').height());
}


