var original_window_title = document.title;
var update_title = false;

$(function() {
	$('#usericons *').tooltip({
		track: true,
		showURL: false,
		delay: 0,
		fade: 250
	});
	
	$('#forum_latest_topics *').tooltip({
		track: true,
		showURL: false,
		delay: 0,
		fade: 250
	});
	
	$('.fav_star *').tooltip({
		track: true,
		showURL: false,
		delay: 0,
		fade: 250
	});
	
	$('.continue_playback_block *').tooltip({
		track: true,
		showURL: false,
		delay: 0,
		fade: 250
	});
	
	$('.post_pm_link').tooltip({
		track: true,
		showURL: false,
		delay: 0,
		fade: 250
	});
	
	$('.new_pm_notify').tooltip({
		track: true,
		showURL: false,
		delay: 0,
		fade: 250
	});
	
	$('#search_input').autocomplete('/video/search_autocomplete.php', {
		width: 350,
		delay: 800,
		minChars: 3,
		max: 50,
		scroll: true,
		selectFirst: false
	});

});

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) { 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end == -1) {
				c_end=document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
} // getCookie

function setCookie(name, value, expires, path, domain, secure) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
} // setCookie

function topBox(elem) {
	if ($("#"+elem).is(":visible")) {
		$("#"+elem).slideUp("fast");
	} else {
		$("#"+elem).slideDown("fast");
	}
} // topBox

function togglePostLinkData(elem, url, im, tinyim, txt) {
	if ($("#"+elem).is(":visible")) {
		$("#"+elem).slideUp("fast");
	} else {
		$("#"+elem).html('<div class="close_btn" onclick="togglePostLinkData(\'' + elem + '\', \'\', \'\', \'\');"><img src="/wp-content/themes/wide/images/close.gif" alt="Закрыть" title="Закрыть" border="0" /></div>' +
			'Без картинки:<br />' +
			'HTML: <input type="text" class="post_link" readonly="readonly" value=\'<a href=&quot;' + url + '&quot;>' + txt + '</a>\' /><br />' +
			'BBcode: <input type="text" class="post_link" readonly="readonly" value=\'[url=' + url + ']' + txt + '[/url]\' /><br />' +
			'<br />' +
			'С большой картинкой:<br />' +
			'HTML: <input type="text" class="post_link" readonly="readonly" value="<a href=&quot;' + url + '&quot;><img src=&quot;' + im + '&quot; alt=&quot;&quot; border=&quot;0&quot; /></a>" /><br />' +
			'BBcode: <input type="text" class="post_link" readonly="readonly" value="[url=' + url + '][img]' + im + '[/img][/url]" /><br />' +
			'<br />' +
			'С маленькой картинкой:<br />' +
			'HTML: <input type="text" class="post_link" readonly="readonly" value="<a href=&quot;' + url + '&quot;><img src=&quot;' + tinyim + '&quot; alt=&quot;&quot; border=&quot;0&quot; /></a>" /><br />' +
			'BBcode: <input type="text" class="post_link" readonly="readonly" value="[url=' + url + '][img]' + tinyim + '[/img][/url]" /><br />');
		$("#"+elem).slideDown("fast");
	}
} // setPostLinkData

function showCatChildren(elem) {
	if (!$("#"+elem).is(":visible")) {
		$("#"+elem).slideDown("normal");
		$("#arrow_"+elem).attr({'src':'/wp-content/themes/wide/images/arrow_open.png'});
		setCookie(elem, 'show', 365, '/')
	}
}

function hideCatChildren(elem) {
	if ($("#"+elem).is(":visible")) {
		$("#"+elem).slideUp("normal");
		$("#arrow_"+elem).attr({'src':'/wp-content/themes/wide/images/arrow_closed.png'});
		setCookie(elem, 'hide', 365, '/')
	}
}

function toggleCatChildren(elem) {
	if (!$.browser.msie) {
		dispose_framestrip();
		if ($("#"+elem).is(":visible")) {
			hideCatChildren(elem);
		} else {
			showCatChildren(elem);
		}
		fetch_random_strip();
	}
} // toggleCatChildren

function favorites_do_action(elem, url) {
	$('#'+elem).fadeTo("fast", 0.33,
											function () {
												$('#'+elem).attr({'src':'/wp-content/themes/wide/images/star_loading.gif'});
												$('#'+elem).attr({'alt':'Подождите немного...'});
												$('#'+elem).attr({'title':'Подождите немного...'});
												$('#'+elem).fadeTo("fast", 1,
																					function () {
																						$.ajax({
																							type: "GET",
																							url: '/video/favorites_action.php?url=' + url,
																							success: function (msg) { $('#'+elem).fadeTo("fast", 0.33,
																																													function () {
																																														$('#'+elem).attr({'src':'/wp-content/themes/wide/images/'+msg});
																																														$('#'+elem).fadeTo("fast", 1);
																																														if (msg == 'star_inactive.png') {
																																															$('#'+elem).attr({'alt':'Добавить запись в закладки'});
																																															$('#'+elem).attr({'title':'Добавить запись в закладки'});
																																														}
																																														else {
																																															$('#'+elem).attr({'alt':'Запись находится у Вас в закладках'});
																																															$('#'+elem).attr({'title':'Запись находится у Вас в закладках'});
																																														}
																																														$('.fav_star *').tooltip({
																																															track: true,
																																															showURL: false,
																																															fade: 250
																																														});
																																													}); },
																							dataType: "plain"
																						});
																					})
											});
} // favorites_do_action

function show_ajax_frames_container(t, cnt) {
	if (t == 'favorites') {
		sc = '/video/favorites_block.php';
		hide_ajax_frames_container('190');
	}
	else if (t == 'playback') {
		sc = '/video/playback_history_block.php';
		hide_ajax_frames_container('230');
	}
	$('#ajax_frames_container_big').show();
	$.ajax({
		type: "GET",
		url: sc,
		success: function (msg) {
			$('#ajax_frames_container').html('<div style="width: ' + (cnt * 180) + 'px; height: 160px;">' + msg + '</div>');
		},
		dataType: "plain"
	});
} // show_ajax_frames_container

function hide_ajax_frames_container(height_to) {
	$('#ajax_frames_container_big').hide();
	$('#ajax_frames_container_big').css('height', height_to + 'px');
	$('ajax_cross_container').css('height', height_to + 'px');
	$('#ajax_frames_container').css('height', height_to + 'px');
	$('#ajax_frames_container').html('<img src="/wp-content/themes/wide/images/loading_big.gif" alt="Подождите немного..." border="0" style="padding-left: 20px; padding-top: 20px;" />');
} // hide_ajax_frames_container

function collapseCatsByCookie(fetch_strip) {
	dispose_framestrip();
	$('ul.children').each(function() {
		cat_state = getCookie($(this).attr('id'));
		if ((cat_state == "") || (cat_state == "show")) {
			showCatChildren($(this).attr('id'));
		}
		else {
			hideCatChildren($(this).attr('id'));
		}
	}); // for each cat
	if (fetch_strip == null) {
		fetch_random_strip();
	}
} // collapseCatsByCookie
