$(document).ready(function(){

	//$(document).pngFix();

//    $().UItoTop();
    $().UItoTop({ easingType: 'easeOutQuart' });

	$("body").append('<div id="inline"></div>');

	$("table.zebra > tbody > tr:nth-child(odd)").addClass("odd");
	$("table.zebra > tbody > tr:nth-child(even)").addClass("even");

	$("table.order > tbody > tr:nth-child(odd)").addClass("odd");
	$("table.order > tbody > tr:nth-child(even)").addClass("even");

	/*
	$("#search").click(function(e){
		$(this).val('').unbind().autocomplete("/search.php?a=suggest", {
			width		: 195,
			selectFirst	: false
		});
	});
	*/

	// Creating custom :external selector
	$.expr[':'].external = function(obj){
	    return !obj.href.match(/^mailto\:/)
	            && (obj.hostname != location.hostname);
	};

	// Add 'external' CSS class to all external links
	$('#main .content a:external').not(".fake, .notex").attr('target', '_blank').addClass('external');

	//setEqualHeight($(".columns .layout-box"));
	//setEqualHeight($("#content .cats .cat"));
	setEqualHeight($("#viewed .items .item"));

	//$('#item').backOpacity({background: '#000', opacity: '0.5'});
	//$('#item').next().corner();

	$('a.fancy').colorbox({
		current		: "{current} из {total}",
		previous	: "предыдущее",
		next		: "следующее",
		close		: "закрыть",
		transition	: "none",
		speed		: 0,
		maxWidth	: "100%",
		maxHeight	: "100%",
		title 		: function(){
	    	var url = $(this).attr('href');
	    	var title = $(this).find('img').attr('alt');
	    	return '<strong>' + title + '</strong>' + ' (' + '<a href="'+url+'" target="_blank">открыть в новом окне</a>' + ')';
		}
	});

	$('a.fancy-iframe').colorbox({
//		inline			: true,
		iframe			: true,
		innerWidth		: "640px",
		innerHeight		: "480px",
		href			: function(){
			return $(this).attr('src');
		},
		title			: function(){
	    	var url = $(this).attr('href');
	    	var title = $(this).attr('title');
	    	return '<strong>' + title + '</strong>' + ' (' + '<a href="'+url+'" target="_blank">открыть в новом окне</a>' + ')';
		}
	});

//	$.localScroll({
//	$.localScroll({duration : 250, offset : {top:-$(window).height()/2}});
//	$("a.scrollto").parent().localScroll({
//	$("a").not(".noscroll").parent().localScroll({
//	$("a").not(".noscroll").parent().localScroll({
//		duration 	: 250,
//		offset 		: {top:-40},
//		hash 		: true,
//		onAfter		: function(target){
//			Highlight($(target));
//		}
//	});

	$('a.inline-show').live('click', function(e) {

	    var el = e.href ? e : this;
	    var href = $(el).attr('href');
	    var body = $(href).html();

		/*
		var $inline = $("#inline");

		$inline.slideUp(250, function(){
			$inline.remove();
		    $(el).parent().append('<div id="inline">' + body + '</div>');
		    $('#inline').slideDown(250);
		});
		*/

		$("#inline").remove();
		$(el).parent().append('<div id="inline"><div class="content">' + body + '</div></div>');
		$('#inline').slideDown(250);

		$.scrollTo($('#inline'), {
			duration : 250,
			offset : {top:-22}
		});

		//e.preventDefault();
	});

	$(".tip, .qtip").simpletip({
		onBeforeShow: function() {
			var html = this.getParent().parent().find('.inline').html();
			this.getTooltip().html(html);
		},
		content		: '',
		fixed		: true,
		position 	: 'top'
	});

	// TODO: some conditions
	//$(".type_public .wrap").hide();

	$('.do-checkRadioGroup').click(function(e) {

		var el = e.href ? e : this;

		$(el).closest('fieldset').find(':radio').click();
	});

	$("select[name='city']").change(function(){

		var $other = $('.city_other');

	    if ($(this).val() == '0') {
	    	$other.show().find('input').focus();
	    } else {
	    	$other.hide();
	    }
	});

	postLoadInline();

});

function postLoadInline(){

	var url = window.location.toString();
	var max = 0;
	var a = null;

	$('a.inline-show').each(function(){
		// сравниваем адрес страницы и ссылку из атрибута
		if(url.indexOf(this.href) >= 0 && this.href.length > max){
			a = this;
			max = this.href.length;
		}
	});

	if (a) {
		$(a).trigger('click');
		$.scrollTo('#inline', { duration : 250, offset : {top:-40}});
	}
}

function formatTitle(title, currentArray, currentIndex, currentOpts) {
    //return '<div id="tip7-title"><span><a href="javascript:;" onclick="$.fancybox.close();"><img src="/data/closelabel.gif" /></a></span>' + (title && title.length ? '<b>' + title + '</b>' : '' ) + 'Изображение ' + (currentIndex + 1) + ' из ' + currentArray.length + '</div>';
	return '<div id="tip7-title">' + (title && title.length ? '<b>' + title + '</b>' : '' ) + 'Изображение ' + (currentIndex + 1) + ' из ' + currentArray.length + '</div>';
}

function Highlight($target) {

	var color_default = '#fff';
	var color_higlight = '#ffa';
	var background = ($target.css('background-color')) ? (($target.css('background-color') !== 'transparent' && $target.css('background-color') !== 'rgba(0, 0, 0, 0)') ? $target.css('background-color') : color_default) : color_default;

	$target.animate(
		{ backgroundColor: color_higlight },
		50,
		function(){
			$(this).animate(
				{ backgroundColor: background },
				500,
				function(){ $target.css('background-color', background); }
			);
		}
	);

}

function setEqualHeight(columns) {
	var tallestcolumn = 0;
	columns.each(
		function() {
			currentHeight = $(this).height();
			if(currentHeight > tallestcolumn) {
				tallestcolumn = currentHeight;
			}
		}
	);
	columns.height(tallestcolumn);
}

function setMaxHeight(container) {
	var tallestcolumn = 0;
	container.children().each(
		function() {
			currentHeight = $(this).height();
			if(currentHeight > tallestcolumn) {
				tallestcolumn = currentHeight;
			}
		}
	);
	container.height(tallestcolumn);
	return tallestcolumn;
}

function pause(ms) {
	var date = new Date();
	var curDate = null;

	do { curDate = new Date(); }
	while(curDate-date < ms);
}

function post_to_url(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);    // Not entirely sure if this is necessary
    form.submit();
}

var kkeys = [];
var sequence = "79,76,79,76,79";
var sequence2 = "74,75,74,75,74";
$(document).keydown(function(e) {
	kkeys.push(e.keyCode);
	if (kkeys.toString().indexOf(sequence) >= 0 || kkeys.toString().indexOf(sequence2) >= 0) {
	$(document).unbind('keydown',arguments.callee);
		var $logo = $("#logo img");
		$logo.fadeOut('slow', function(e) {
			$logo.attr('src', '/i/logo-t.png').fadeIn('fast');
		});
	}
});


