/* main js */

var IE = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);
var timeout = null;
var flag = true;
var scrollWidth = 0;
var helpText = '';
var helpTimeout = null;
var showHelp = false;

$(document).ready(function(){
	if ($('#mainTabs').length) {
		$('#mainTabs').tabs();
	}
	
//	if ($('.calendar').length) {
//		$('.calendar').datepicker();
//	}
	
	if ($('#auctionList').length) {
		$('#auctionList tr').hover(function () {
			$(this).addClass('blue-bg');
		}, function () {
			$(this).removeClass('blue-bg');
		});
	}
	if ($('#scrollArray').length) {
		scrollWidth = $('#scrollArray .array-content').width();
		$('#scrollArray').width(scrollWidth);
		if (scrollWidth <= 195) {
			$('#scrollBtnLeft').hide();
			$('#scrollBtnRight').hide();
		} else if ($('#wrap').scrollLeft()+195 == scrollWidth) {
			$('#scrollBtnLeft').addClass('btn-left-no');
		} else if ($('#wrap').scrollLeft() == 0) {
			$('#scrollBtnRight').addClass('btn-right-no');
		}
		$('#scrollArray img').bind('click', function () {
			$('#lotFoto').attr('href', $(this).attr('alt'));
			$('#lotFoto img').attr('src', $(this).attr('src'));
		});
	}
	$('#lotFoto').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.7,
		imageLoading: '/themes/smolotka/images/lightbox-ico-loading.gif',
		imageBtnClose: '/themes/smolotka/images/lightbox-btn-close.gif',
		containerResizeSpeed: 350
	 });
	if ($('#newsGrid').length) {
		hideBigTextNews();	
	}
	if ($('#declarationGrid').length) {
		hideBigTextDeclaration();
	}
	if ($('#memberMenu').length) {
		$('#memberMenu .member-menu-inside').each(function () {
			var w = 0;
			$(this).parents('.m').addClass("active");
			$(this).find('ul').each(function () {
				$(this).width($(this).width()); //for IE6
				w += $(this).width()+9;
			});
			$(this).parents('.m').removeClass("active");
			$(this).width(w-9);
		});
		$('#memberMenu .m').hover(function () {
			$(this).addClass("active");
			var $menuInside = $(this).find('.member-menu-inside');
			var pos = $menuInside.offset().left + $menuInside.width();
			if (pos > $(window).width()) {
				$menuInside.css('left', $(window).width()-pos-34 + 'px');
			}
		}, function () {$(this).removeClass("active");})
	}
	
	if ($('.formGrid').length) {
		$('body').append('<div id="helpBlock" class="help-block"><div id="helpContent" class="help-content"></div></div>');
		$('.formGrid .h div').hover(function () {
			helpText = jQuery.trim($(this).attr('title'));
			if (helpText.length > 0) {
				$(this).attr('title', '');
				$('#helpContent').html(helpText);
				$('#helpBlock').css({top:$(this).offset().top+11+'px', left:$(this).offset().left-176+'px'});
				if (IE) $('#helpBlock').bgiframe();
				$('#helpBlock').fadeIn();
			}
		}, function () {
			$(this).attr('title', helpText);			
			
			$('#helpBlock').hover(function () {
				showHelp = true;
			}, function () {
				showHelp = false;
				$(this).hide();
			});
			
			clearTimeout(helpTimeout);
			helpTimeout = setTimeout(function () {
				if (!showHelp) {
					$('#helpBlock').hide();
				}
			}, 300);
		});
		
		if ($('#changeMoney').length) {
			setActiveMoney($('#changeMoney'));
		}
	}
	
	$(window).resize(function () {
		if ($('#newsGrid').length) {
			$('#newsGrid .wrap').width(280);
			hideBigTextNews();	
		}
		if ($('#declarationGrid').length) {
			$('#declarationGrid .wrap').width(280);
			hideBigTextDeclaration();
		}
	});
});

function resizeImg (curr, w) {
	$(curr).parents('.for-btn').find('div').removeClass('active');
	$(curr).addClass('active');
	$(curr).parents('.list-grid').find('td.fot img').attr('width', w);
}

function hideBigTextNews () {
	$('#newsGrid td').each(function () {
		var w = $(this).innerWidth() - 10;
		var linkWidth = $('a', this).width() + 13;
		if ($(this).hasClass("l")) w -= 20;
		if (linkWidth < w && linkWidth > $('.wrap', this).width()) {
			$('.wrap', this).width(linkWidth);
		}
		if (linkWidth > w) {
			$('.wrap', this).width(w);
			$('.dot', this).show();
		} else {
			$('.dot', this).hide();
		}
	});
}

function hideBigTextDeclaration () {
	$('#declarationGrid td').each(function () {
		var w = $(this).innerWidth() - 18;
		var linkWidth = $('.link', this).width() + $('.rate', this).innerWidth();
		if (linkWidth < w && linkWidth > $('.wrap', this).width()) {
			$('.wrap', this).width(linkWidth);
		}
		if (linkWidth > w) {
			$('.wrap', this).width(w);
			$('.dot', this).show();
		} else {
			$('.dot', this).hide();
		}
	});
}

function scrollImages (direction) {
	if (direction == 'l') {
		$('#scrollBtnRight').removeClass('btn-right-no');
		$('#wrap').scrollLeft($('#wrap').scrollLeft()+2);
		if ($('#wrap').scrollLeft()+195 == scrollWidth) {
			$('#scrollBtnLeft').addClass('btn-left-no');
		}
	} else {
		$('#scrollBtnLeft').removeClass('btn-left-no');
		$('#wrap').scrollLeft($('#wrap').scrollLeft()-2);
		if ($('#wrap').scrollLeft() == 0) {
			$('#scrollBtnRight').addClass('btn-right-no');
		}
	}
	clearInterval (timeout);
	timeout = setInterval(function () {scrollImages(direction)}, 15);	
}

function stopScrollImages () {
	clearInterval (timeout);
}

function showAnswerForm (curr, data) {
	if ($('#answerForm').length) {
		$('#answerForm').remove();	
	}
	var form = '<div id="answerForm" class="answer-form" style="left:'+($(curr).offset().left-21)+'px">';
	form += '<form action="auction_details.php" method="POST">';
	form += '<input type="hidden" name="auction_id" value="'+data.aId+'" />';
	form += '<input type="hidden" name="option" value="post_answer" />';
	form += '<input type="hidden" name="question_id" value="'+data.qId+'" />';
	form += '<textarea id="answerArea" name="message_content"></textarea>';
	form += '<div class="for-btn clearfix">';
	form += '<input type="submit" class="btn-answer" value="ОТВЕТИТЬ" />';
	form += '<a href="#" class="reset-form" onclick="$(\'#answerArea\').val(\'\'); return false;">Очистить форму</a>';
	form += '</div>';
	form += '</form>';
	form += '</div>';
	$(curr).parents('.comment').append(form);
	// load answer if it is edit answer option
	$('#answerArea').text($('#answ'+data.qId).text());
	$(curr).addClass('active');
	$(document).bind('click', function (event) {
		var ev = $(event.target);
		//$(ev).css('background', 'red');
		if (!ev.is('.answ-link')) {
			if (!ev.is('#answerForm') && !ev.is('#answerForm textarea') && !ev.is('#answerForm div') && !ev.is('#answerForm a') && !ev.is('#answerForm input')) {
				$(curr).removeClass('active');
				$('#answerForm').remove();
			}
		}
	});
}

function showCommentForm () {
	if ($('#addCommentForm').is(':hidden')) {
		$('#addCommentForm').slideDown();
	} else {
		$('#addCommentForm').slideUp();
	}
}

function otherType (curr) {
	var $addInput = $(curr).parents('td').find('input.hide');
	if ($(curr).val() == '99999' && $addInput.is(':hidden')) {
		$addInput.fadeIn('slow');
	} else {
		$addInput.hide();	
	}
}

function setActiveMoney (curr) {
	$('.money').removeClass('eur').removeClass('uah').removeClass('rub').removeClass('usd');
	switch ($(curr).val()) {
		case 'EUR':
			$('.money').addClass('eur');
		break;
		case 'UAH':
			$('.money').addClass('uah');
		break;
		case 'RUR':
			$('.money').addClass('rub');
		break;
		default:
			$('.money').addClass('usd');
		break;
	}
}

function addInputFole (curr) {
	$(curr).before('<input type="file" />')
}

/**
 * Toggles user favorite shop
 * @param shopId id of shop
 * @return false
 */
function toggleFavoriteShop(shopId, linkObj) {
    var isFav = 'Удалить из избранного';
    var notFav = 'Добавить в избранное';
    var $link = $(linkObj);
    $.get(
        '/ajax.php',
        {option: 'FavoriteShop', store_id: shopId},
        function (resp) {
            if (resp && resp != '0') {
                if ($link.text() == isFav) {
                    $link.text(notFav);
                } else {
                    $link.text(isFav);
                }
            }
        }
    );
    return false;
}
