﻿$(function () {

    $('.dropdown').each(function () {
        $(this).parent().eq(0).hoverIntent({
                    timeout: 100,
                    over: function () {
                        var current = $('.dropdown:eq(0)', this);
                        current.slideDown(100);
                    },
                    out: function () {
                        var current = $('.dropdown:eq(0)', this);
                        current.fadeOut(200);
                    }
                });
    });


    $.fn.vcards = function(options) {
        if (typeof( YMaps ) == 'undefined') {
            console.error('Yandex maps are required.');
            return;
        }
        var opts = $.extend($.fn.vcards.defaults, options);
        return this.each(function() {
            $this = $(this);
            $mapContainer = $this.find('.mapContainer');
            if ($mapContainer.length > 0 && !( $mapContainer.find('img').length > 0 )) {
                var addr = $this.find('.country-name .value-title').attr('title') + ', ' + $this.find('.locality .value-title').attr('title') + ', ' + $this.find('.street-address .value-title').attr('title');
                var map = {
                    map: new YMaps.Map($mapContainer),
                    addr: addr,
                    geocoder: new YMaps.Geocoder(addr),
                    name: $this.find('.fn .human-value').text()
                };
				
                YMaps.Events.observe(map.geocoder, map.geocoder.Events.Load, function () {
                    if (map.geocoder.length()) {
                        geoPoint = map.geocoder.get(0).getGeoPoint();

                        map.map.setCenter(geoPoint, opts.zoom);
                        map.map.addControl(new YMaps.Zoom());

                        var style = new YMaps.Style();
                        style.iconStyle = new YMaps.IconStyle();
                        style.iconStyle.href = opts.icon.url;
                        style.iconStyle.size = new YMaps.Point(opts.icon.width, opts.icon.height);
                        style.iconStyle.offset = new YMaps.Point(opts.icon.offsetX, opts.icon.offsetY);

                        var placemark = new YMaps.Placemark(geoPoint, {style: style});
                        placemark.name = map.name;
                        placemark.description = map.geocoder.request;
                        map.map.addOverlay(placemark);

                    } else {
                        $(map.map._element).html('Íè÷åãî íå íàéäåíî').hide();
                    }
                }, map);

                YMaps.Events.observe(map.geocoder, map.geocoder.Events.Fault, function (error) {
                    $(map.map._element).html("Ïðîèçîøëà îøèáêà: " + error.message).hide();
                });
				$this.data('map', map);
            }
        });
    };
    $.fn.vcards.defaults = {
        zoom: 15,
        icon: {
            url: "/images/transnavigaciya1.gif",
            width: 50,
            height: 90,
            offsetX: -9,
            offsetY: -29
        }
    };

    (contacts = function(){
        $vcards = $('.service-page  #tab_1 .vcard');
	
        if($vcards.length <= 0){
            return;
        }
        $list = $('<ul class="contacts-switcher-menu"></ul>');
        
        $vcards.each(function(i, element){
            $('<li'+ ($(element).hasClass('active') ? ' class="active"' : '' ) +'><span>' + $(element).find('.locality .value-title').attr('title') + '</span></li>').click(function(event){
                //console.log(event);			
                $(this).parent().parent().parent().find('.active').removeClass('active');
		
                index = $list.find('li').index($(this));
                $list.children('li:nth-child(' + (index+1) + ')').addClass('active');
                $('#tab_1 .vcard:nth-child('+ (index+1) +')').addClass('active');

				var map = $('#tab_1 .vcard:nth-child('+ (index+1) +')').data('map');
				if (map){
					$('#tab_1 .vcard:nth-child('+ (index+1) +')').data('map').map.redraw();
    			};                
				$('#tab_2 .price:nth-child('+ (index+1) +')').addClass('active');

            }).appendTo($list);
        });
		
		$list.insertBefore($('#tab_1'));
		//$list.prependTo($('#tab_1'));
		//$list.clone().prependTo($('#tab_2'));

})();

$('.tab_invis').click(function(){
	$('.contacts-switcher-menu').css("display", "none");
});

$('.tab_vis').click(function(){
	$('.contacts-switcher-menu').css("display", "block");
});




$(function(){
	if ($('.contacts-switcher-menu').find('.active').length > 0 ){
		$('.contacts-switcher-menu').find('.active').click();
	} else {
		$($('.contacts-switcher-menu li')[0]).click();
	}
	$('footer').css({
		'bottom': '1px'
	});
})


    function formatTitle(title, currentArray, currentIndex, currentOpts) {
        return '<div class="fancy-title">' + (title && title.length ? title : '' ) + '<span class="image-counter">' + (currentIndex + 1) + '/' + currentArray.length + '</span></div>';
    }

    $.fn.fancybox.defaults.transitionIn = 'elastic';
    $('.fancybox').fancybox();
    $('.gallery article figure a').fancybox({
                titlePosition: 'over',
                titleFormat: formatTitle
            });
//    $('.calc_buttons button').fancybox({
//        content: $('.calculation-result'),
//        padding: 15,
//        transitionIn: 'elastic'
//    });
    $("#phone_number").mask("+7 (999) 999­-99­-99");
    $('.callback').fancybox({
        content: $('.callback-form'),
        padding: 15,
        transitionIn: 'elastic'
            });
    $('.tabbed').tabs({
		show: function(){
			$('footer').css('bottom', '1px');
			$('footer').css('bottom', 0);
		}
	});
    $('.service-page .vcard').vcards();
    $('.department-page .vcard').vcards();
    // $('.mapContainer>img').each(function(){
    //     $this = $(this);
    //     $link = $('<a href="'+ $this.attr('src') +'"></a>').colorbox();
    //     console.log($link);
    //     $this.wrap();
    // }));
    $('.mapContainer>img').each(function(){
        $this = $(this);
        $link = $('<a href="'+ $this.attr('src') +'" title="Просмотреть в большом размере"></a>');
        $this.wrap($link);
    });
    var partners_switcher = (function(){
        var $widget = $('#partners_widget');
        var $partners = $('#partners_widget img');
        if ($partners.length > 1){
        var $viewport = $('#partners_widget .viewport');
        var maxWidth = 200,
            maxHeight = 100,
            counter = 0;
        
        $widget.click(function(){
           window.location = $(this).find('a').attr('href');
        });

        $partners.each(function(){
            $this = $(this);
            maxWidth = (maxWidth < $this.width()) ? $this.width() : maxWidth;
            maxHeight = (maxHeight < $this.height()) ? $this.height() : maxHeight;

            if ( counter == 0 ){
                $this.addClass('active');
            } else {
                $this.hide();
            }
            counter++;
        });
        $viewport.css({
            width: maxWidth,
            height: maxHeight
        }).addClass('animating');

        changeLogo = function(){
            var $active = $('#partners_widget .active');
            var current_index = $partners.index($active);
            var new_index = (current_index + 1) % ($partners.length);
            var $next = $($partners.get(new_index));
            //console.log(current_index + '?' + new_index);
            $active.fadeOut(1200);
            $next.fadeIn(800, function(){
                //console.log('cb');
                $active.removeClass('active');
                $next.addClass('active');
            });
         };
        var logoChanger = window.setInterval(changeLogo, 5000);
        }
        return {};
    }());
});

function CheckCalc() {
	$('#calc_from').removeClass('error');
	$('#calc_to').removeClass('error');
	$('#calc_weight').removeClass('error');
	$('#calc_volume').removeClass('error');

	var from = $("#calc_from").attr("value");
	var to = $("#calc_to").attr("value");
	var weight = $("#calc_weight").attr("value");
	var volume = $("#calc_volume").attr("value");

	$.post("/netcat/modules/default/Calc.php",
	{
		from: from,
		to: to,
		weight: weight,
		volume: volume
	},
	doResultCheckCalc
	);
}

function doResultCheckCalc(data){
	var res = explode("|", data);

	if (res[0] == 'N'){
		$('#calc_from').addClass('error');
		$('#calc_to').addClass('error');
	}  
	if (res[1] == 'N'){
		$('#calc_weight').addClass('error');
	}   
	if (res[2] == 'N'){
		$('#calc_volume').addClass('error');
	} 	
	if (res[2] == 'Y'){
		$("#calculation-result").html(res[3]);
		$.fancybox({content: $('.calculation-result'), padding: 15, transitionIn: 'elastic'});
	}
}

function CheckCallBack() {
	$('#phone_number').removeClass('error');
	$('#city_order').removeClass('error');
	$('#phone_name').removeClass('error');

	var number = $("#phone_number").attr("value");
	var name = $("#phone_name").attr("value");
	var city = $("#city_order").attr("value");

	$.post("/netcat/modules/default/CallBack.php",
	{
		number: number,
		city: city,
		name: name,
	},
	doResultCheckCallBack
	);
}

function doResultCheckCallBack(data){
	var res = explode("|", data);

	if (res[0] == 'C'){
		$('#city_order').addClass('error');
	}  
	if (res[0] == 'N'){
		$('#phone_number').addClass('error');
	}  
	if (res[1] == 'N'){
		$('#phone_name').addClass('error');
	}    
	if (res[1] == 'Y'){
		$("#callback-form").html(res[2]);
	}
}

function explode(seporator, str) {
	var aa;  
	aa=str.toString().split(seporator.toString());    
	return aa;
}

// $(function(){
//     $('.nagr .photo a').unbind().fancybox();
// })
