//$.getScript('js/tip.js');

$(document).ready(function() { 
	/*
	$('#restTab').hide();
	$('#lifeTab').hide();
	$('#businessTab').hide();
	$('#realtyTab').show();
	*/
	$('.content .tabs a').bind('click', function() { 
		/*
		$(".ajaxHolder").hide();
		$(".ajaxHolder").load(this.href, function() { $(".ajaxHolder").fadeIn("slow"); bindPhotoBehaviour(); });
		*/
		$('#restTab').hide();
		$('#lifeTab').hide();
		$('#businessTab').hide();
		$('#realtyTab').hide();
		$('#'+this.href.replace('http://www.kipr.ru/', '')+'Tab').fadeIn("slow");
		
		$('.content .tabs li').removeClass('here');
		$(this).parents("li").addClass('here');
		$(this).blur();
		if ($('#userPanel').attr('class') != 'active') {
			if ($(this).attr('class') == 'photo') {
				$('#userPanel a.add-entry').attr('href', 'ajax/addphoto.html');
				$('#userPanel a.add-entry span span span').text('Добавить фото');
				
			}
			else {
				$('#userPanel a.add-entry').attr('href', 'ajax/addentry.html');
				$('#userPanel a.add-entry span span span').text('Добавить запись');
			}
		}
		return false;
	 }); 
	 
	 $('#objPhotoCarousel').jcarousel({
	 	scroll: 2
	 });
	 $('#objPhotoCarousel a').bind('click', function() {
	 	//alert('1');
	 	$('#photoLarge').attr('src', this.href);
	 	$('#photoLink').attr('href', this.href + '?height=1000&width=1000');
	 	return false;
	 });
	 	 
	 $('#propertyInfo .gallery .tabs a').bind('click', function() { 
		$("#photoTab").hide();
		$("#videoTab").hide();
		$("#schemeTab").hide();
		$('#'+this.href.replace('http://www.kipr.ru/', '')+'Tab').fadeIn("slow");
		
		$('#propertyInfo .gallery .tabs li').removeClass('here');
		$(this).parents("li").addClass('here');
		$(this).blur();
		return false;
	 }); 	 
	 
	 $('.content .tabs2 a').bind('click', function() { 
		$(".ajaxHolder2").hide();
		$(".ajaxHolder2").load(this.href, function() { $(".ajaxHolder2").fadeIn("slow"); });
		
		$('.content .tabs2 li').removeClass('here');
		$(this).parents("li").addClass('here');
		$(this).blur();
		return false;
	 }); 
	
	$('#navigation.brown .qa').html('<span><span><span>Вопросы и ответы</span></span></span>');
	
	$('a.dialog').bind('click', function() {
		$('#container').before('<div id="bigBadaWrapper"></div>');
		$('#bigBadaWrapper').before('<div id="popup"></div>');
		$('#popup').load(this.href, function() { $('#popup a.submit').bind('click', function() { $('#popup form').submit(); return false; }); } );
		$(this).blur();
		return false;
	});
	
	$('#responses .hidden .content p').before('<div class="showLink">[ <a href="#">Скрытый отзыв. Нажмите чтобы посмотреть</a> ]</div>');
	
	$('#responses .hidden .content .showLink a').bind('click', function() {
		$(this).parents("div").removeClass('hidden');
		$(this).parents("div.showLink").remove();
		return false;
	});
	
	$('#comments .hidden .content p').before('<div class="showLink"><a href="#">Скрытый комментарий. Нажмите чтобы посмотреть</a></div>');
	
	$('#comments .hidden .content .showLink a').bind('click', function() {
		$(this).parents("div").removeClass('hidden');
		$(this).remove();
		return false;
	});
	
	$('#tours .checkall').toggle( 
		function () {
			$('#tours td input[type=checkbox]').attr('checked', 'true');
			$('#tours tr.filter td input[type=checkbox]').attr('checked', '');
			$(this).text('Убрать выделение');
			$(this).blur();
		}
		,
		function () {
			$('#tours td input[type=checkbox]').attr('checked', '');
			$(this).text('Выделить все');
			$(this).blur();
		}
	);
	
	$('#tours tr.actions .check input').toggle( 
		function () {
			$('#tours td input[type=checkbox]').attr('checked', 'true');
			$('#tours tr.filter td input[type=checkbox]').attr('checked', '');
		}
		,
		function () {
			$('#tours td input[type=checkbox]').attr('checked', '');
		}
	);
	
	$('#tags .tabs a').bind('click', function() { 
		$(this).removeClass('inactive');
		$('#tags .tabs a.here').addClass('inactive');
		$('#tags .tabs a.here').removeClass('here');
		$(this).addClass('here');
		$('#tags .content').load(this.href);
		$(this).blur();
		return false;
	});
	
	$('#expandForm .more, #mainContent .content .metadata a.more').toggle(
		function () {
			$('#expandForm').addClass('expanded');
			$(this).blur();
			return false;
		}
		,
		function () {
			$('#expandForm').removeClass('expanded');
			$(this).blur();
			return false;
		}
	);	
	
	// Insert after each .ajaxHolder on the page the .clearFix div which prevents pageflicking on tab switch
	$('.ajaxHolder').after('<div class="clearfix"></div>');
	
	
	// Make inputs hilite on focus
	$('#nastyForm input, #nastyForm textarea, #nastyForm select').bind('focus', function() { 
		$(this).addClass('active');
	});
	$('#nastyForm input, #nastyForm textarea, #nastyForm select').bind('blur', function() {
		$(this).removeClass('active');
	});
	
	// Password Strength Meter
	$('#password').keyup(function(){$('#nastyForm table td.passwordMeter div').attr('class', passwordStrength($('#password').val(),$('#username').val()))});
	
	// Password Match Checker 
	$('#passconfirm').keyup(function() { 
		if ($('#password').val() == $('#passconfirm').val())
			$('#passNotify').html('<b>Пароли совпадают</b>')
		else
			$('#passNotify').html('<b class="hi">Пароли НЕсовпадают</b>');
	});
	
	// Nasty form 
	$('#nastyForm a.addMore').bind('click', function() {
		var html = $.ajax({
		 url: this.href,
		 async: false
		}).responseText;
		$(this).before(html);
			// Reassign focus and blur events to newly added elements.
			$('#nastyForm input, #nastyForm textarea, #nastyForm select').bind('focus', function() { 
			$(this).addClass('active');
			});
			$('#nastyForm input, #nastyForm textarea, #nastyForm select').bind('blur', function() {
				$(this).removeClass('active');
			});
		return false;
		
	});
	
	// userPanel behaviour 
		
	$('#userPanel a.add-entry').toggle(
									   function() {
										   $(this).blur();
										   $('#userPanel').addClass('active');
											var html = $.ajax({
																url: this.href,
																async: false,
																cache: false
															  }).responseText;
											$('#userPanel .ajax').html(html);
											
									   },
									   function() {
										   $(this).blur();
										   $('#userPanel').removeClass();
										   $('#userPanel .ajax').empty();
									   }
	); // toggle add-entry on userPanel
	
	$('#userPanel a.configure').bind('click', function() { 
		alert('Что будем настраивать?');
	});
	
	// feedback form behaviour
	$('#comments #add-feedback').bind('click', function() {
		$('#comments #add-feedback').remove();
		var isResponse = $(this).hasClass('response');
		if(isResponse != 'null') {
			var html = $.ajax(
					{
						type : "POST",
						data : "isresponse=true",
						url: this.href,
						async: false,
						cache: true 
					}).responseText;
				$('#comments').append(html);
				$('#comments form textarea').focus();
				return false;		
			}
		else {
				var html = $.ajax(
					{
						url: this.href,
						async: false,
						cache: true 
					}).responseText;
				$('#comments').append(html);
				$('#comments form textarea').focus();
				return false;		
			}
	});
	
	$('#comments a.reply').bind('click', function() {
		$('#comments .ajax').remove();
		$(this).parents('.comment').append('<div class="ajax"></div>');
		var html = $.ajax(
			{
				url: this.href,
				async: false,
				cache: true 
			}).responseText;
		$('#comments .ajax').html(html);
		$('#comments .ajax form').prepend('<input type="hidden" name="reply-to" value="' + $(this).attr('id') + '" />');
		$('#comments .ajax form textarea').focus();
		$(this).bind('click', function() { $(this).addClass('disabled'); return false; });
		return false;
	});
	
	// Photoalbums behaviour
	function bindPhotoBehaviour() {
		$('#photos a.edit-album').bind('click', function() {
			var item = $('#photos .item');
			var albumName = $('#photos h1').text();
			var button = '<a href="#" class="submit" style="margin: 0 0 0 15px">Сохранить</a>';
			$('#photos').prepend('<form action="#" method="get" name="albumName" id="albumNameForm"><input type="text" id="albumName" class="text yellow" name="albumname" value="' + albumName + '" /></form>');
			$('#photos').addClass('editing');
			$('#albumNameForm').append(button);
			$('#albumNameForm .submit').bind('click', function() { alert('this would be ajax event'); return false; })
			$('#photos h1, #photos .metadata').remove();
			$('#photos .item').each(function() {
				var id = $(this).attr('id');
				var actions = '<a href="#" class="edit" title="Редактировать"></a><a href="#" class="delete" title="Удалить"></a>';
				var $kids = $(this).children();
				var text = $(this).children('h3 a').text();
				$('h3', this).children('a').remove();
				$(this).children('h3').append(text);
				
				$('h3', this).append(actions);
				
				$('h3 .edit', this).bind('click', function() {
					alert('edit photo #' + $(this).parents('.item').attr('id'));
					return false;
				});
				$('h3 .delete', this).bind('click', function() {
					alert('delete photo #' + $(this).parents('.item').attr('id'));
					return false;
				});
			}); // each
			return false;
		}); // bind 
	} // function()
	bindPhotoBehaviour();

	bindMyObjects();	
}); // document.ready


// Guess what these lines do ?
function closePopup() {
	$("#bigBadaWrapper").remove();
	$("#popup").remove();
};

function bindMyObjects(){
	$('#secondaryContent .block .header a.trigger').toggle(
		function() {
			// $('#secondaryContent .block .content').fadeOut(300);
            var hasClass = $(this).parents('.block').hasClass('collapsed');
            // expand the block otherwise
            if(hasClass == true) {
                $(this).parents('.block').children('.content').fadeIn(300);
                $(this).parents('.block').removeClass('collapsed');
                $(this).blur();
                //$(this).text('Expand');
                //setCookie(this, 'unset');
				set_cookie('myobjcol', 0);
                return false;  
            }
            else {
                $(this).parents('.block').children('.content').fadeOut(300);
                $(this).parents('.block').addClass('collapsed');
                $(this).blur();
                //$(this).text('Collapse');
                //setCookie(this);
				set_cookie('myobjcol', 1);
                return false;
            }
		},
		function() {
            var hasClass = $(this).parents('.block').hasClass('collapsed');
            if(hasClass == true) { 
                $(this).parents('.block').children('.content').fadeIn(300);
                $(this).parents('.block').removeClass('collapsed');
                $(this).blur();
                //$(this).text('Collapse');
                //setCookie(this, 'unset');
				set_cookie('myobjcol', 0);
                return false;
            }
            else {
                    $(this).parents('.block').children('.content').fadeOut(300);
                    $(this).parents('.block').addClass('collapsed');
                    $(this).blur();
                    //$(this).text('Expand');
                    //setCookie(this);
					set_cookie('myobjcol', 1);
                    return false;
                }
		}
	);
	
	$('#secondaryContent .block .content a.grow').toggle(
		function() {
            $(this).parents('.block').children('.content').children('.list').animate({ "height" : "380px" });
			// $('#secondaryContent .block .list').animate({ "height" : "380px" });
			$(this).blur();
			return false;
		},
		function() {
			$(this).parents('.block').children('.content').children('.list').animate({ "height" : "164px" });
			$(this).blur();
			return false;
		}
	);
	
	$('#secondaryContent .block .content .list a.checkbox').toggle(
		function () {
			$(this).parent('.item').addClass('selected');
			$(this).blur();
			return false;
		}
		,
		function () {
			$(this).parent('.item').removeClass('selected');
			$(this).blur();
			return false;
		}
	);
	
	$('#secondaryContent .block .content .actions a.check-all').toggle(
		function () {
			$('#secondaryContent .block .content .list .item').each(function () {
				$(this).addClass('selected');
			});
			$(this).blur();
			$(this).addClass('checked');
			return false;
		}
		,
		function () {
			$('#secondaryContent .block .content .list .item').each(function () {
				$(this).removeClass('selected');
			});
			$(this).blur();
			$(this).removeClass('checked');
			return false;		
		}
	);
};

function set_cookie(name, value){
	expires = new Date();
 	expires.setTime(expires.getTime() + (1000 * 86400 * 365));
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
};

function AddMyObject(id){
	$("#myholder").load("/_newtech/dynamic/addobj.php?id=" + id + "&x=" + (new Date()).getTime(), "", bindMyObjects);
};

function DeleteMyObject(id){
	$("#myholder").load("/_newtech/dynamic/myobjdelete.php?id=" + id + "&x=" + (new Date()).getTime(), "", bindMyObjects);
};