// Определяем Эксплорер
var IE='\v'=='v';
var browser = FF = false;
var picsInterval = false;

var imgSlides = new Array();
var Slides = new Array();
var currentSlide = 0;

menuTimeout = null;

function w100(){
	$('.w100').each(function(){
		// Ставим уникальные ID и  создаем объекты-дубли
		if( $(this).attr('id_w100') == null ){
			$(this).attr('id_w100', $(this).attr('id' ) == null ? 'id'+(Math.random()+'').substr(2,10) : $(this).attr('id' )+ '_w100' );
			$('body').prepend( '<div id="'+$(this).attr('id_w100')+'"><img></div>' );
			$('#'+$(this).attr('id_w100')).css({ position:'absolute', zIndex:0, width:0, height:0, top:'-1000px', backgroundImage:$(this).css('background-image'), backgroundColor:$(this).css('background-color'), backgroundPosition:$(this).css('background-position'), backgroundRepeat:$(this).css('background-repeat'), marginTop:$(this).css('margin-top'), marginBottom:$(this).css('margin-bottom') })
			$('#'+$(this).attr('id_w100')+' img').css({ width:'1px', height:0 })
		}
		// Обновляем позиции при изменении размера
		if( $(this).attr('id_w100') != null ){
			$(this).css({ background:'none' });
			$('#'+$(this).attr('id_w100')).css({ width:'100%', height: $(this).height()+'px', top:$(this).position().top+'px' });
			setTimeout( '$(\'#'+$(this).attr('id_w100')+'\').css({ width:$((document).width()-(IE?20:0))+\'px\' })', 25 );
		}
	});
}

// Корректирвока размера некоторых элементов
function sizeCorrеction(){
	// Фон под объектами 100%-ной ширины
	w100();
}

window.onresize = function(){
	sizeCorrеction();
}
document.onclick = function(){
	hideMenu('popup_01');
}

function preventBubble(e){ 
	if (e && e.stopPropagation ){ //если метод stopPropagation поддерживается  
		e.stopPropagation();
	} else {
		e.cancelBubble=true;
	}
}



// Смена картинки
function changeTheme( object_class ){
	$('.'+object_class ).animate( { left:'-=100%' }, { duration:500, easing:'easeInOutSine' } ).queue(function(){
		if( $(this).position().left<0 ){
			$(this).css( { left:'100%' } );
		}
		$(this).dequeue();
	}); 
}

// Смена картинки
function scrollTheme( dir, object_class, obj ){
	if( obj == null || obj.parentNode.className != 'select' ){
		$('.'+object_class ).animate( { left: (dir=='r'?'+':'-') + '=100%' }, { duration:500, easing:'easeInOutSine' } ); 
	}
}

function changeSelector(id){
	$('#h1_selector .select').addClass('unselect');
	$('#'+id).parent().removeClass('unselect');
	$('#h1_selector .select span').attr('onclick', $('#h1_selector .select span').attr('onclick').replace('\/\/','') );
	$('#'+id).attr('onclick', '//'+$('#'+id).attr('onclick') );
}

// Класс ТЕЛЕФОН ГОРОДА
var cityPhones = new Array();
function cityPhone( id, city, code_1, phone_1, code_2, phone_2 ){
	this.id = id;
	this.city = city;
	this.code_1 = code_1;
	this.phone_1 = phone_1;
	this.code_2 = code_2;
	this.phone_2 = phone_2;
}

function setPhone(id){
	//$('#a_city').attr('innerHTML',cityPhones[id].city).attr( 'onclick', 'showMenu(\'popup_01\');' );
	$('#a_city').attr('innerHTML',cityPhones[id].city);
	if( cityPhones[id].code_1 != null && cityPhones[id].phone_1 != null && cityPhones[id].code_1 != '' && cityPhones[id].phone_1 != '' ){
		$('#phone_1').css({ display:'block' });
		$('#span_code_1').attr('innerHTML','('+cityPhones[id].code_1+')');
		$('#span_number_1').attr('innerHTML',cityPhones[id].phone_1);
	} else {
		$('#phone_1').css({ display:'none' });
	}
	if( cityPhones[id].code_2 != null && cityPhones[id].phone_2 != null && cityPhones[id].code_2 != '' && cityPhones[id].phone_2 != '' ){
		$('#phone_2').css({ display:'block' });
		$('#span_code_2').attr('innerHTML','('+cityPhones[id].code_2+')');
		$('#span_number_2').attr('innerHTML',cityPhones[id].phone_2);
		$('#online_order').css({ top:'450px' });
	} else {
		$('#phone_2').css({ display:'none' });
		$('#online_order').css({ top:'390px' });
	}
	//hideMenu('popup_01');
}

function showMenu(id){
	$('#'+id).css({ display:'block' });
	if( IE ){
		$('#phone_order .txt span.bord').css({ borderBottom:'none' }); 
	}
	//$('#a_city').attr( 'onclick', "javascript:hideMenu('popup_01')" );
}

function hideMenu(id){
	if( menuTimeout != null ){
		clearTimeout( menuTimeout );
	}
	
	if( IE ){
		$('#'+id).css({ display:'none' }); 
		$('#phone_order .txt span.bord').css({ borderBottom:'dashed #ff0ee5 2px' }); 
	}else {
		$('#'+id).animate( { opacity:'0'} ).queue( function(){ 
			$(this).css({ display:'none', opacity:'1' }); 
			$(this).dequeue();
		});
	}
	//$('#a_city').attr( 'onclick', "javascript:showMenu('popup_01');" );
}



// Все загружено
$(document).ready(function(){

	// Подгонка размеров
	sizeCorrеction();
	
	// Градиенты
	if( document.all['phone_1'] != null ){
		$('body.b-main #phone_1').gradientText({ colors: ['#fc2fe7', '#a40277'], toProcess: ['span'] });
	}
	if( document.all['phone_2'] != null ){
		$('body.b-main #phone_2').gradientText({ colors: ['#fc2fe7', '#a40277'], toProcess: ['span'] });
	}
	
	
	if( Slides.length>0 ){
		for( i=0; i<Slides.length; i++ ){
			imgSlides[i] = new Image();
			imgSlides[i].src = Slides[i];
		}
	}
	
	
	$('#online_order').hover(
		function(){
			$(this).attr( 'src', '/img/order-hover.png' );
		},
		function(){
			$(this).attr( 'src', '/img/order.png' );
		}
	);
	
	// Меню
	$('.popup_menu .td-m .item:first').css({ borderTop:'solid #ff2981 2px' });
	$('.popup_menu .td-m .item:last').css({ borderBottom:'solid white 1px' });
	
		// HOVER в меню 
	$('.popup_menu .td-m .item').hover(
		function(){
			$(this).addClass("iHover");
		},
		function(){
			$(this).removeClass("iHover");
		}	
	);

	$('.popup_menu').hover(
		function(){
			id = $(this).attr('id');
			if( menuTimeout != null ){
				clearTimeout( menuTimeout );
			}
		},
		function(){
			menuTimeout = setTimeout( 'hideMenu(\''+$(this).attr('id')+'\')', 1250 );
		}
	);
	
	
	// Коррекция
	$('#h1_selector .select').each(function(){
		$(this).css({ width:($(this).width()-10)+'px' });
	});

	// Для рабочей страницы
	$('div.prod-item .price').each(function(){
		$('img',this).css({ left:($('span',this).width()+5)+'px', cursor:'hand' });
	});
/*	
	$('div.prod-item2 .price').each(function(){
		$('img',this).css({ left:( ( $('span',this).position().left > 100 ? $('span',this).position().left : 200 ) + $('span',this).width()+5)+'px', cursor:'hand' });
	});
*/	
});



