

function ellip(obj, more, hobj) {
    if (typeof (more) == 'undefined') more = '&hellip;';
    if (typeof (hobj) == 'undefined') { hobj = obj; }

    var rnd = parseInt(Math.random() * 100000);
    
    txt = obj.innerHTML;
    hor = hobj.offsetHeight + findTop(hobj);
    parts = txt.split(' ');
    arrpos = parts.length;

    obj.innerHTML = obj.innerHTML + '<span id="el' + rnd + '">&nbsp;</span>';
    span = document.getElementById('el' + rnd);
	toppos = findTop(span) + parseInt(span.offsetHeight);
	while (hor < toppos || arrpos < 1) {
        arrpos--;
        dum = parts[arrpos]
        parts[arrpos] += ' <span id="el' + rnd + '">' + more + '</span><br /><br />';
        obj.innerHTML = parts.join(' ');
        var span = document.getElementById('el' + rnd);
        var toppos = findTop(span) + parseInt(span.offsetHeight);
        parts[arrpos] = dum;
    }
}

function findTop(obj) {
    var curleft = curtop = 0;
    if (obj && obj.offsetParent) {
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curtop += obj.offsetTop
        }
    }

    return curtop;
}

$(document).ready(function() {

	
	$('select').msDropDown();
	
	$.sifr({
		build: 436,
		version: 3,
		path: 'swf/',
		save: true,
		debug : true
	});
	
	$('.arnopro16').each( function(i, l) {
		$(this)
			.sifr({ 	
				font		: 'arnopro',
				color		: '#8c58a4', 
				fontSize	: 16, 
				height		: 20, 
				link 		: $(this).children('a').attr('href'), 
				css: { 
					a : {textDecoration: "none"} } 
				});
	})
	
	$('.arnopro21').each( function(i, l) {
		$(this)
			.sifr({ 	
				font		: 'arnopro',
				color		: '#8c58a4', 
				fontSize	: 21, 
				height		: 20, 
				link 		: $(this).children('a').attr('href'), 
				css: { 
					a : {textDecoration: "none"} } 
				});
	})
	
	$('.arnopro16_sel').each( function(i, l) {
		$(this)
			.sifr({ 	
				font		: 'arnopro',
				color		: '#333333', 
				fontSize	: 16, 
				height		: 20, 
				link 		: $(this).children('a').attr('href'), 
				css: { 
					a : {textDecoration: "none"} } 
				});
	}) 
	
	$('.arnopro21_sel').each( function(i, l) {
		$(this)
			.sifr({ 	
				font		: 'arnopro',
				color		: '#333333', 
				fontSize	: 21, 
				height		: 20, 
				link 		: $(this).children('a').attr('href'), 
				css: { 
					a : {textDecoration: "none"} } 
				});
	}) 
	
	
	var name = $( "#name" ),
	email = $( "#email" ),
	password = $( "#password" ),
	allFields = $( [] ).add( name ).add( email ),
	tips = $( ".validateTips" );
	origtext = tips.text();
	
	function updateTips( t ) {
		tips
			.append( t )
	}

	function checkLength( o, n, min, max ) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass( "ui-state-error" );
			updateTips( "Дължината на " + n + " трябва да бъде между " +
				min + " и " + max + " знака." );
			return false;
		} else {
			return true;
		}
	}

	function checkRegexp( o, regexp, n ) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass( "ui-state-error" );
			updateTips( n );
			return false;
		} else {
			return true;
		}
	}

	
	$( "#dialog-form" ).dialog({
		autoOpen: false,
		height: 250,
		width: 350,
		modal: true,
		buttons: {
			"OK": function() {
				var bValid = true;
				allFields.removeClass( "ui-state-error" );
				tips.text('');
				bValid = checkLength( name, "името", 3, 80 ) && bValid;
				bValid = checkLength( email, "email-а", 6, 80 ) && bValid;

				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "например: joe@example.com" ) && bValid;
				

				if ( bValid ) {
					$.post(
						"/register.php"
						, $("#frmRegister").serialize()
						, function(data) {
							if (data) {
								tips.text('Регистрацията е успешна!')
								setTimeout( function() { $( "#dialog-form" ).dialog("close"); }, 3000); 	
							}
						}
					);
					
					//alert(name.val());
					/*$( "#users tbody" ).append( "<tr>" +
						"<td>" + name.val() + "</td>" + 
						"<td>" + email.val() + "</td>" + 
						"<td>" + password.val() + "</td>" +
					"</tr>" ); 
					$( this ).dialog( "close" ); */
				}
			},
			"Отказ": function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			allFields.val( "" ).removeClass( "ui-state-error" );
		}
	});
	
	$('#register').click( function() {
		tips.text(origtext)
		$( "#dialog-form" ).dialog( "open" );	
	})
	
	setTimeout(function() {
		$('.single_tout_height').each(function (i){
			ellip($(this).find('.single_tout_txt').get(0),'<a href="' + $(this).attr('rel') + '" class="link_more">&hellip;</a>', this )
		})}
	,100);
	
	$('.main_article_single_txt a[href*="http://www.emodno.com/ww/wp-content/uploads/"]').attr('rel','prettyPhoto[gallery1]');
	$('.main_article_single_txt a[href*="http://www.emodno.com/ww/wp-content/uploads/"]').prettyPhoto();
	
	
	$('.contact_submit input, .comment_submit input')
		.bind('mouseover', function() { $(this).css('background-position', '0px -28px'); })
		.bind('mouseout', function() { $(this).css('background-position', '0px 0px'); })
	

	
	
	if ($('.search_input input').val()) $('.search_input input').css('background-position', '0px -18px'); else $('.search_input input').css('background-position', '0px 0px');
	$('.search_input input')
		.bind('focus', function() { $(this).css('background-position', '0px -18px'); })
		.bind('blur', function() { if ($(this).val()) $(this).css('background-position', '0px -18px'); else $(this).css('background-position', '0px 0px'); })
		
	$('.side_nav_arrow a').click( function() {
		$('.side_nav_boxes_pack').animate(
			{ width: 'toggle' },
			{ complete : function() { 
				opened = $('.side_nav_boxes_pack').is(':visible');
				document.cookie = "close_side_nav=" + (opened ? 0 : 1) + "; path=/";
				$('.side_nav_arrow').find('img').attr('src', opened ? '/img/btn_sidenav_arrow_close.gif' : '/img/btn_sidenav_arrow_open.gif');
			}}
		);
	});
	
	
	// register
	
	// share buttons 
	u=window.location.href;t=document.title;
	$('#btn_facebook').attr('href','http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t));
	$('#btn_twitter').attr('href','http://twitter.com/home?status=Currently reading ' + u);
	$('#btn_svejo').attr('href','http://svejo.net/story/submit_by_url?url=' + encodeURIComponent(u));	
		
	
		
});
