$(document).ready(function () {
	$(".video .youtube_wrapper a").each(function(){
		var t = $(this);
		var o = $('.object', t.parents('.video').get(0));
		expression = /\?v\=([a-zA-Z0-9\_]{5,})/;
		matches = expression.exec(t.attr('href').toString());
		if (matches) t.attr('rel',matches[1]);
		t.attr('href', '#' + o.attr('id'));
		t.fancybox({
			'zoomSpeedIn':	0, 
			'zoomSpeedOut':	0,
			'frameWidth' : '525',
			'frameHeigth': '425'
		});
		t.mousedown(function(){
			if (o.html() == '') {
				o.html('<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'+t.attr('rel')+'&amp;hl=en&amp;fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/'+t.attr('rel')+'&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object>');
			}
		});
	});
	adjust_heights();
	
	$('.rss a.email').click(function() {
		alert('Pentru a-ti trimite stiri prin email folosim un serviciu extern - FeedBlitz, in pagina caruia vei fi dus acum. Trebuie doar sa completezi adresa de e-mail pe care vrei sa primesti noutatile');	
		return true; 
	});
	
	ajax_voting();	
})

function ajax_voting(){
	$('.vote a').click(vote);
	$('.change_vote').click(change_vote);
}

function vote() {
	var _this = this;
	var root = $(this).parents('.vote_widget');
	// disable both votes
	$('.vote a', root).hide();
	root.prepend('<span class="ajax_on">trimitem votul..</span>');
	
	var link = $(this).attr('href') + '.json';
	$.getJSON(link, {}, function(data) {
		$('.ajax_on', root).remove();
		if (data['error']) {
			$('a', root).not('.change_vote').show();
			alert(data['msg']);
		} else {
			// show your vote
			var _class = ($(_this).hasClass('yes') ? 'yes' : 'no');
			a = $('.vote_widget').prepend('<p>Votul tau: <strong class="your_vote">' + 
				(_class == 'yes' ? 'DA' : 'NU') + '</strong></p>');
			$('.change_vote', root).show();

			// increment the number of votes
			var v = $('.votes .' + _class, root);
			v.text(Number(v.text()) + 1).addClass('modified');
			
			// visuals
			setTimeout('removeModified()', 600);
			if (_class == 'yes') {
				hightlight($('.favourite.add'));
			}
		}
	});
	
	return false;
}

function change_vote() {
	var _this = this;
	var root = $(this).parents('.vote_widget');
	var link = $(this).attr('href') + '.json';
	$.getJSON(link, {}, function(data) {
		if (data['error']) {
			alert(data['msg']);
		} else {
			$('.your_vote', root).text(data['msg']);
			
			var v = $('.votes .yes', root);
			var new_votes = Number(v.text()) + (data['msg']=='DA' ? 1 : -1);
			v.text(new_votes);
			
			var v = $('.votes .no', root);
			var new_votes = Number(v.text()) + (data['msg']=='DA' ? -1 : 1);
			v.text(new_votes);
		}
	});
	return false;
}

function hightlight(e) {
	var t = 500;
	e
		.animate({'border' : '1px dotted gray'}, t).animate({'border' : '0'}, t)
		.animate({'border' : '1px dotted gray'}, t).animate({'border' : '0'}, t)
		.animate({'border' : '1px dotted gray'}, t).animate({'border' : '0'}, t)
		.animate({'border' : '1px dotted gray'}, t).animate({'border' : '0'}, t)
	;	
}

function adjust_heights() {
	var main = $('#main'); 
	var sidebar = $('#sidebar');
	// reset previous values, if set
	sidebar.css('height', null);
	main.css('height', null);
	
	var sidebar_height = sidebar.height();
	var main_padd = 12;
	// height + padding-bottom
	var main_height = main.height() + main_padd;
	if (sidebar_height > main_height) {
		main.height(sidebar_height - main_padd);
	} else {
		sidebar.height(main_height);		
	}
}

function add_tabs_change_listener() {
	$('.ui-tabs-nav').bind('tabsshow', function(event, ui) {
		adjust_heights();	
	});
	
}

function removeModified(){
	$('.modified').removeClass('modified');
}

