// JavaScript Document
function randomString(length) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
    if (!length) length=Math.floor(Math.random()*chars.length);
    var str='';
    for (var i = 0; i < length; i++) {str+=chars[Math.floor(Math.random()*chars.length)];}
    return str;
}

$(document).ready(function(){
	try {document.execCommand("BackgroundImageCache", false, true);} catch (e) {}
	var pd = jQuery.browser.msie && parseFloat(jQuery.browser.version) == 6 ? 0 : 5;

	if (!jQuery.cookie('subscribe')) {
		jQuery.cookie('subscribe', '1', {expires: 60, path: '/', domain: 'devushkam.su'});
	
		jQuery.fancybox({
			'href':'http://'+document.domain+'/form.php?'+randomString(8),
			'scrolling':'no',
			'showCloseButton':true,
			'titleShow':false,
			'autoDimensions':false,
			'width':755,
			'height':'auto',
			'overlayOpacity':0.7,
			'overlayColor':'#000',
			'padding':pd
		});
	}

	// set default email address in '#mail' container
//	$('#mail').attr('href','mailto:info@devushkam.su');
	
	// email anti-spam
	$('span.snd_eml').each(function(i){
		var c = $(this).text().split(' (0) ').join('@');
		var t = $(this).attr('title');
		if (t)	$(this).replaceWith('<a href="mailto:' + c + '">' + t + '</a>');
		else 	$(this).replaceWith('<a href="mailto:' + c + '">' + c + '</a>');
	});

	// highlight 'Mail' button						   
	$('#mail').hover(function() {
		$('#bluebox').css('background-position', '0 -25px');
	}, function() {
		$('#bluebox').css('background-position', '0 0');
	});
	
	// highlight 'Home' button
	$('#home').hover(function() {
		$('#bluebox').css('background-position', '0 -50px');
		}, function() {
		$('#bluebox').css('background-position', '0 0');
	});
	
	// open/close submenu container (ul.slevel)
	$('#left > ul > li > a').click(function(){
		$(this).toggleClass('link');
		$(this).next('ul.slevel').slideToggle("fast");
		return false;
	});
	
	// highlight active menuitem and show submenu
	var pageName = jQuery.uri.key('file');
	var pageName = pageName == '' || pageName == null ? 'index.html' : pageName;
	var regexp = /^([^0-9]*)([0-9]+)\.html?$/;
	matches = regexp.exec(pageName);
	if (matches != null) pageName = matches[1] + '1.html';
		
	$('#left ul.slevel a[href$=' + pageName + ']').each(function(i){
		$(this).click(function() {return false;});
		$(this).parents('li').children('a').addClass('link');
		$(this).parents('ul.slevel').css('display','block');
	});
	
	// "read more" links highlight
	$('#content .article > a').each(function(i){
		//$(this).before('&raquo; ');
		var aStyle = $(this).parent().attr('class');
		var arr = aStyle.split(' ');
		if(arr.length < 2) return;
		var clr = $(this).parent().css('color');
		var aClass = 'bg'+arr[1];
		$(this).hover(function() {
			$(this).addClass(aClass);
			$(this).css('text-decoration','none');
			$(this).css('color','#FFF');
		}, function() {
			$(this).removeClass(aClass);
			$(this).css('text-decoration','underline');
			$(this).css('color',clr);
		});
	});
	
	// adds separator (' | ') between footer(sape) links
//	$('#fcenter > a:not(a:last)').each(function(i){$(this).after(' | ');});
	
	// 3 column list
	$('#content .article .list li:even').css('background','url(img/bg_menuitem.jpg) repeat-y');
	$('#content .article .list li:odd').css('border-bottom','1px dashed #D0EAFC');
	$('#content .article .list li a').each(function(i){$(this).before('&rsaquo; ');});
	// 2 column table bg coloring
	$('#content .article .table p:odd').css('background','none');

	// table rows coloring
	$('table tr:odd').addClass('trodd');
	$('table tr td:not(:first-child)').addClass('vborder');
	$('table tr:not(tr:first)').hover(function() {
		$(this).addClass('trhover');
	}, function() {
		$(this).removeClass('trhover');
	});

});

function sbscrb_focus(obj) {
	if (jQuery(obj).val() == 'Введите ваше имя' || jQuery(obj).val() == 'Введите ваш e-mail') jQuery(obj).val('');
}
function sbscrb_blur(obj, text) {
	if (!jQuery(obj).val()) jQuery(obj).val(text);
}
function sbscrb_submit() {
	if (!jQuery('input[id|="fldname"]').val() || jQuery('input[id|="fldname"]').val() == 'Введите ваше имя') {
		alert('Пожалуйста, укажите ваше имя!');
		return false;
	}
	if (!jQuery('input[id|="fldemail"]').val() || jQuery('input[id|="fldemail"]').val() == 'Введите ваш e-mail') {
		alert('Пожалуйста, укажите ваш электронный почтовый ящик!');
		return false;
	}
	jQuery.fancybox.close();
	return true;
}
