﻿/*--- MODEL ---*/

// Elastic list items of the footer menu
function initFooterMenu(){
	var W = $('#footer').width();
	if(W < 1280){
		$('#footer ul').css({
			'right': '255px',
			'left': Math.floor(11 * W / 28 - 1630 / 7) + 'px'
		});
		changeLiMargins('#footer ul', {'maxValue' : 60});
	}
	else{
		$('#footer ul').css({
			'right': '',
			'left': ''
		});
		$('#footer ul li').css('margin-left', '');
	}
}

// Header menu must have at least 15px margins between items
function initHeaderMenu(Start){
	var lis = $('#menu > li');
	var w = 0;
	
	for(var i = 0; i < lis.length; i++){
		w += lis.eq(i).width();
	}
	
	var trw = $('#triangle_top').css('width', '').width();

	var minMenuWidth = w + 15 * (i - 1);
	var minParentWidth = minMenuWidth + parseInt($('#menu').css('margin-right')) + trw;
	
	// Корректировка ширины меню при начальной загрузке страницы
	if (Start == 1) { minMenuWidth = 731; minParentWidth = 1142; }
	
	var parentWidth = $('#menu').parent().width();
	var menuMarginLeft = (parentWidth - minMenuWidth - parseInt($('#menu').css('margin-right')));
	
	if(parentWidth < minParentWidth){
		$('#menu').css('margin-left', (menuMarginLeft-1) + 'px');
		$('#triangle_top').width(menuMarginLeft + 'px');
		$('.logoLink').width((menuMarginLeft - 50) + 'px');
		$('.logoLink').height((menuMarginLeft / 5) + 'px');
		lis.not(':eq(0)').css('margin-left', '15px');
	}
	else{
		$('#menu').css('margin-left', '');
		$('#triangle_top').css('width', '');
		if (Start == 1) { changeLiMargins('#menu', 1, {'minValue' : 15}); }
		else { changeLiMargins('#menu', {'minValue' : 15}); }
	}
}

// Header phones
function initHeaderPhones(Start){
	if($('#header').width() <= 1280){
	    $('#header .phones').css({
			'left' : $('#triangle_top').width() + 'px',
			'width' : ''
		});
	}
	else{
	    $('#header .phones').css({
			'left' : '',
			'width' : (1280 - 30 - 381) + 'px'
		});
	}
	
	if (Start == 1) { changeLiMargins('#header .phones', 2); }
	else { changeLiMargins('#header .phones'); }
}

/* Changing left-margins between list elements floated left.
 * "jqsel" is a container for menu items.
 * "values" is an object with minimum and maximum allowed values of margin left
*/
function changeLiMargins(jqsel, inStart, values){
	var maxValue = (values && typeof values['maxValue'] == 'number') ? values['maxValue'] : Infinity;
	var minValue = (values && typeof values['minValue'] == 'number') ? values['minValue'] : 0;
	var wp = $(jqsel).width(); // width of total menu container, e.g. <ul>
	var lis = $(jqsel).children('li'); // menu items, e. g. <li>
	var w = 0;
	
	if(maxValue < minValue)
	    throw new Error('Wrong values of "maxValue" and "minValue"');
	
	// a total width of all the menu items
	for(var i = 0; i < lis.length; i++){
		w += lis.eq(i).width();
	}
	
	// Корерктировка расчетной суммарной ширины элементов меню
	if (inStart == 1) { w = 626; }
	else if (inStart == 2) { w = 581; }
	
	// a required margin-left for all the menu items
	var ml = Math.floor((wp - w - 1) / (i - 1));
	if(ml >= minValue && ml < maxValue)
	    lis.not(':eq(0)').css('margin-left', ml + 'px');
	else
	    lis.not(':eq(0)').css('margin-left', '');
}

jQuery(document).ready(function($){

/*--- EVENT HANDLER ---*/
    $(window).resize(function(){
		initHeaderMenu();
		initFooterMenu();
		initHeaderPhones();
	});
	
	
/*--- CONTROLLER ---*/
    // Header & footer menus
	// Запуск функций меню для разделов и телефонов с дополнительным параметром Start:
	initHeaderMenu(1);
	initFooterMenu();
	initHeaderPhones(1);
	
	
	/* Centering of the header images
	(function(){
		var ml = Math.floor($('#header img.bg').width() * 0.5);
		var mt = Math.floor($('#header img.bg').height() * 0.8);
		
		$('#header img.bg').css({
			'margin-left' : -ml,
			'margin-top' : -mt
		});
	})()*/
	
	// Проверка - если есть галерея ставим ее вместо главной фотки.
	if ( $('div.gallery').length ) { $('#header img.bg').css('display', 'none'); $("div.gallery").appendTo('#header'); }
	
	
	// Gallery in the header
	$('.gallery > ul li img').click(function(ev){
		var current = $(ev.target);
		var i = $('.gallery > ul li img').index(current);
		var img = $('.gallery > img');
		
		if(img.eq(i).hasClass('this'))
			return;
		
		img.filter('.this').removeClass('this').end().eq(i).addClass('this');
	});
		
	$('.col_3 li').each( function() {
		if ( $(this).index() == 1 || $(this).index() == 4 || $(this).index() == 7 || $(this).index() == 10 ) $(this).after('<li></li>');
	} );

})
