var fav_changed = 0;   // Избранное не изменено. После изменения становится fav_changed = 1;

$(document).ready(function(){
	setBottom();
	
	//setTab('tab2');

    setTab(tab_id);

	$('#index_city').sSelect();
	
	$('#select_button').click(function(){
		var cityID = $('#index_city').getSetSSValue();
		$(this).attr('href', 'city.php?tid='+cityID);
	});
	
	$('.tab').live('mouseover', function(){
		$(this).children('span').css('textDecoration', 'none');
	});
	$('.tab').live('mouseout', function(){
		$(this).children('span').css('textDecoration', 'underline');
	});
	
	$('.tab').live('click', function(){ 
                if($(this).attr('id') == "tab1" || $(this).attr('id') == "tab2" || $(this).attr('id') == "tab4" || ($(this).attr('id') == "tab3" && fav_changed == 0))
                    setTab($(this).attr('id')); 
                else {
                    page_tid = $(this).attr('tid');
                    page_cid = $(this).attr('cid');
                    document.location='city.php?tid='+page_tid+'&cid='+page_cid+'&fav=1';
                }
        });
	$('.tab_a').live('click', function(){ setTab(0); });
	
	$('.cat, .cat_a').each(function(){
		$(this).children('i').css({'background': 'transparent url(../img/' + $(this).attr('id') + '.png) 0 0 no-repeat'});
		$(this).children('b').css({'background': 'transparent url(../img/' + $(this).attr('id') + '_a.png) 0 0 no-repeat'});
	});
	$('.shop').each(function(){
		$(this).css({'background': 'transparent url(../img/' + $(this).attr('id') + '.png) 0 3px no-repeat'});
	});
	
	$('.cat').live('click', function(){ catSelect($(this).attr('id')); });
	$('.cat_all').live('click', function(){ catSelectAll(); });
	//$('.cat_a').live('click', function(){ catUnselect($(this).attr('id')); });
	//$('.cat_all_a').live('click', function(){ catUnselectAll(); });
	
	$('.gps_favorites').click(function(){ setTab('tab3'); });

	$('.map_point').click(function(){ showMapInfo($(this).attr('id')); });

	$('.rss input').focus(function () {
		if (this.value == 'Введите Ваш e-mail') {
			this.value = '';
			$(this).css('color', '#666666');
		}
	});
	$('.rss input').blur(function () {
		if (this.value == '') {
			this.value = 'Введите Ваш e-mail';
			$(this).css('color', '#cecece');
		}
	});
	
	$('.gps_search input').focus(function () {
		if (this.value == 'Найдите ближайший магазин / Введите Ваш адрес') {
			this.value = '';
		}
	});
	$('.gps_search input').blur(function () {
		if (this.value == '') {
			this.value = 'Найдите ближайший магазин / Введите Ваш адрес';
		}
	});

	if($('.cat2').size()) {
		$('.cat2').each(function(){
			$(this).css({'background': 'transparent url(img/' + $(this).attr('id') + '.png) 0 0 no-repeat'});
			var text = $(this).children('span');
			text.css('top', 15 - (text.height())/2 + 'px');
		});
		//catSelect2('cat_1');
		if(first_cat)
			catSelect2(first_cat);
	}
	$('.cat2').live('click', function(){ catSelect2($(this).attr('id')); });
	if($('.cat2').size()) {
		$('.cat_container').scroll(function(){ 
			$('.cat_sel').css('top', $('.cat2_a').position().top - 2 + 'px');
		});
	}

	if($('.cont_link').size()) {
		contSelect($('.cont_link_a').attr('id'));
		$('.cont_link').live('click', function(){ contSelect($(this).attr('id')); });
	}
	
	$('.del_fav, .gps_del_fav').live('click', function() { switch_fav('del', $(this).attr('fav')); });
	$('.add_fav').live('click', function() { switch_fav('add', $(this).attr('fav')); });

	$('#close, #white_bg').click(function(){ hidePopup(); });
	$('#close2, #white_bg').click(function(){ hidePopup2(); });

	$('.card').click(function(){ showPopup2($(this).attr('id')); });

	if($('.news_link').size()) {
		//newsSelect($('.news_link').eq(0).attr('id'));
		$('.news_container').scroll(function(){ 
			$('.cat_sel2').css('top', $('.news_link_a').position().top - 2 + 'px');
		});
		$('.news_link u').live('click', function(){ newsSelect($(this).parents('.news_link').attr('id')); });
	}

	if($('.buy_link').size()) {
		$('.buy_container').scroll(function(){ 
			$('.cat_sel2').css('top', $('.buy_link_a').position().top - 2 + 'px');
		});
		$('.buy_link').live('click', function(){ buySelect($(this).attr('id')); });
	}
});

$(window).resize(function(){ setBottom(); });

function setBottom() {
	var frameH = document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	if ($('.index_main')) {
		$('.index_main').css('height', (frameH > 780)?(frameH + 'px'):(780 + 'px'));
		$('.index_bg').css('height', (frameH > 780)?(frameH + 'px'):(780 + 'px'));
	}
	if ($('.main')) {
		$('.main').css('height', (frameH > 833)?(frameH + 'px'):(833 + 'px'));
	}
}

function getParam(sParamName){
    var Params = location.search.substring(1).split("&");
    var variable = "";
    for (var i = 0; i < Params.length; i++){ 
        if (Params[i].split("=")[0] == sParamName){ 
            if (Params[i].split("=").length > 1) variable = Params[i].split("=")[1];
            return variable;
        }
    }
    return "";
}

function setCity(cityID) {
	if(cityID && cityID != 0) {
		$('#city').append('<img src="img/towns/city' + cityID + '.png" id="city_img" />');
		
		if($.browser.msie && $.browser.version == '6.0') {
			var img = $('#city img');
			var imgW = img.width();
			var imgH = $('#city').height();
			img.width(imgW + 'px');
			img.height(imgH + 'px');
			fixPNG(document.getElementById('city_img'));
		}

//		if($.browser.msie && $.browser.version == '6.0') {
//			$('#city').append('<img src="/img/towns/city' + cityID + '.png" style="visibility:hidden;" id="city_img" />');
//			var imgW = document.getElementById('city_img').offsetWidth;
//			$('#city').css({'background': 'url(/img/towns/city' + cityID + '.png) left top no-repeat', 'margin-left': 121 - imgW/2 - 500 + 'px'});
//			fixPNG(document.getElementById('city'));
//		}
//		else
//			$('#city').css('background', 'url(/img/towns/city' + cityID + '.png) center top no-repeat');
	}
}

function setTab(id) {

	$('.tab_a').children('b').hide();
	$('.tab_a').children('i').show();
	$('.tab_a').attr('class', 'tab');
	$('.tab_block').hide();
	
	if(id) {
		$('#' + id).attr('class', 'tab_a');
		$('#' + id).children('i').hide();
		$('#' + id).children('b').show();
		$('#' + id + '_block').show();
		if(id == 'tab1') {
			$('.cat span, .cat_a span, #cat_all span').each(function(){$(this).css('top', 15 - ($(this).height())/2 + 'px');});
		}
	}
}

function catSelect(id) {
	$('#cat_all').children('b').hide();
	$('#cat_all').children('i').show();
	$('#cat_all').attr('class', 'cat_all');

	$('.cat_a').children('b').hide();
	$('.cat_a').children('i').show();
	$('.cat_a').attr('class', 'cat');

	$('#' + id).children('i').hide();
	$('#' + id).children('b').show();
	$('#' + id).attr('class', 'cat_a');
}

function catSelectAll() {
	$('.cat_a').children('b').hide();
	$('.cat_a').children('i').show();
	$('.cat_a').attr('class', 'cat');

	$('#cat_all').children('i').hide();
	$('#cat_all').children('b').show();
	$('#cat_all').attr('class', 'cat_all_a');
}

function catSelect2(id) {
	var cat = $('#' + id);
	$('.cat_sel').css('top', cat.position().top - 2 + 'px');
	$('.cat_sel').show();
	$('.cat2_a').each(function(){
		$(this).css({'background': 'transparent url(img/' + $(this).attr('id') + '.png) 0 0 no-repeat'});
	});
	$('.cat2_a').attr('class', 'cat2');
	cat.attr('class', 'cat2_a');
	cat.css({'background': 'transparent url(img/' + id + '_a2.png) 0 0 no-repeat'});

	$('.cat_block').hide();
	$('#' + id + '_block').show();
}

function contSelect(id) {
	var cont = $('#' + id);
	$('.cat_sel').css('top', cont.position().top - 14 + 'px');
	$('.cont_link_a').attr('class', 'cont_link');
	cont.attr('class', 'cont_link_a');
	
	$('.cont_block').hide();
	$('#' + id + '_block').show();
}

function showMapInfo(id) {
	var point = $('#' + id);
	$('.map_info').css({'top': point.position().top - 255 + 'px', 'left': point.position().left - 20 + 'px'});
	$('.map_info').show();
	
	$(document).click(function(e){ hideMapInfo(e); });
}

function hideMapInfo(e) {
	var info = $('.map_info');
	var infoL = info.offset().left;
	var infoT = info.offset().top;
	var mCur = mousePageXY(e);
	if (!(mCur.x >= infoL && mCur.x <= infoL + 322 && mCur.y >= infoT && mCur.y <= infoT + 310)) {
		info.hide();
	}
}

function mousePageXY(e) {
	var x = 0, y = 0;
	
	e = e || window.event;
	
	if (e.pageX || e.pageY) {
		x = e.pageX;
		y = e.pageY;
	}
	else if (e.clientX || e.clientY) {
		x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
		y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
	}
	
	return {"x":x, "y":y};
}

// Вызов функции "добавить в Избранное / Удалить из избранного" для Результатов поиска
function switch_fav(what, id) {

    if(what == "add") {
        $('.add_fav[fav=' + id + ']').attr('class', 'del_fav');
        $('.del_fav[fav=' + id + '] span').text('Удалить из избранного');

        document.getElementById("fav_frame").src='fav_switch.php?id='+id+'&act=add&fav=1';	
	favNum++;
        $('#fav' + id).show();

    } else {
        $('.del_fav[fav=' + id + ']').attr('class', 'add_fav');
        $('.add_fav[fav=' + id + '] span').text('В избранное');
		
        document.getElementById("fav_frame").src='fav_switch.php?id='+id+'&act=del&fav=1';		
	if (favNum != 0) favNum--;
        $('#fav' + id).hide();
    }
	
    $('#fav_num').text(favNum);
    $('.gps_favorites div').text(favNum);
    fav_changed = 1;

    return false;
}

function showPopup() {
	$('#white_bg').show();
	$('#white_bg').css('height', $(document).height() + 'px');
	$('#popup').css('top', $(document).scrollTop() + ($(window).height() - $('#popup').height()) / 2 + 'px');
	$('#popup').show();
}

function showPopup1(id) {
	$('#white_bg').show();
	$('#white_bg').css('height', $(document).height() + 'px');
	$('#'+id).css('top', $(document).scrollTop() + ($(window).height() - $('#'+id).height()) / 2 + 'px');
	$('#'+id).show();
}

function showPopup2(card) {
	$('#white_bg').show();
	$('#white_bg').css('height', $(document).height() + 'px');
	$('#popup2').css('top', $(document).scrollTop() + ($(window).height() - $('#popup2').height()) / 2 + 'px');
	$('#popup2').show();
	$('#' + card + '_about').show();
}

function hidePopup() {
	$('#popup').hide();
	$('#white_bg').hide();
}

function hidePopup1(id) {
	$('#'+id).hide();
	$('#white_bg').hide();
}

function hidePopup2() {
	$('.card_about').hide();
	$('#popup2').hide();
	$('#white_bg').hide();
}

// Отправляем емэйл из всплывающего окна формы
function sendEmail() {
        if(document.getElementById("email_input").value != "") {
            $('#email_input').hide();
            $('#email_send').hide();
            $('#select_reg_pop').hide();
            $('#email_note').text("Cпасибо! Ваш email добавлен в список рассылки!");
            document.getElementById("email_frame").src="sender_add_mail.php?email="+document.getElementById("email_input").value+"&reg_id="+document.getElementById("reg_input").value;	
        } 
}
// Отправляем емэйл из формы внизу каждой страницы
function sendEmail0() {
        var email = document.getElementById("email_input0").value;
        if(email != "" &&  email != "Введите Ваш e-mail") {
            $('#white_bg').show();
	    $('#popup').css('top', $(document).scrollTop() + ($(window).height() - $('#popup').height()) / 2 + 'px');
	    $('#popup').show();

            $('#email_zag').hide();
            $('#email_input').hide();
            $('#email_send').hide();
            $('#email_note').text("Cпасибо! Ваш email добавлен в список рассылки!");
            document.getElementById("email_frame").src="sender_add_mail.php?email="+email;	
        } 
}


// Показываем на карте список точек данного партнера в данном городе
function show_partners_old(pid, cid) {
    alert("pid="+pid+", cid="+cid);
	document.getElementById("gmap_box").innerHTML = "hello";
}

var myOdometer;
function odometer_run () {
	var div = document.getElementById("partner_counter");
	myOdometer = new Odometer(div, {
		value: n, 
		digits: 4, 
		tenths: false, 
		digitHeight: 26, 
		digitWidth: 15, 
		bustedness: 0
		});
	odometer_update ();
}

function odometer_update () {
/*
	n = n + 0.0002;
	myOdometer.set(n);
	setTimeout("odometer_update()", 0);
*/
}

function newsSelect(id) {
	var news = $('#' + id);
	$('.cat_sel2').css('top', news.position().top - 2 + 'px');
	$('.cat_sel2').show();
	$('.news_link_a').attr('class', 'news_link');
	news.attr('class', 'news_link_a');
	
	$('.news_link, .news_link_a').each(function(){
		var text = $(this).children('span');
		text.css('top', 20 - (text.height())/2 + 'px');
	});

	$('.news_block').hide();
	$('#' + id + '_block').show();
}

function buySelect(id) {
	var buy = $('#' + id);
	$('.cat_sel2').css('top', buy.position().top - 2 + 'px');
	$('.cat_sel2').show();
	$('.buy_link_a').attr('class', 'buy_link');
	buy.attr('class', 'buy_link_a');
	
	$('.buy_link, .buy_link_a').each(function(){
		var text = $(this).children('span');
		text.css('top', 20 - (text.height())/2 + 'px');
	});

	$('.buy_block').hide();
	$('#' + id + '_block').show();
}

