
/**
 * crossBrowser event control
 */
$.fn.extend({target:function(){return this[0].target || this[0].srcElement;},stopPropagation:function(){if (this[0].stopPropagation) {this[0].stopPropagation();}else{this[0].cancelBubble = true;}},preventDefault: function() {if (this[0].preventDefault) {this[0].preventDefault();}else{this[0].returnValue = false;}},stopEvent: function() {this.stopPropagation(this[0]);this.preventDefault(this[0]);}});
/**
 * 'later' helper function: jQuery.later( object _object_scope, number _time_miliseconds, string _method [, arguments])
 */
jQuery.later = function (_o, _s, _m) {var that = _o,args = Array.prototype.slice.apply(arguments, [3]);if (typeof _m === 'string'){_m = that[_m];}	time = setTimeout(function () {_m.apply(that, args);}, _s);	return time;};


var active = 0;
var counter = 0;

/**
 * kb ezzel tudjuk egy kepnek a megjeleniteset szabalyozni
 */
var pagespeed = 18;

$(window).load(function(){

	var divs = $('#Headlines div');
	var divLength = divs.length;

	while( divLength-- ) {

		if((divLength))$(divs[divLength]).find('h1,p').css('opacity',0); // tobbi elem felirata eltunik

		var img = $(divs[divLength]).find('img');

		if( img.width() > img.height() ) { //fekvokep
			if ( img.width() * 0.64  > 320) { // jo nagy
				img.css({ width: 500})
			}
		} else { // allokep
			if ( img.height() * 0.64  > 500) { // jo nagy
				img.css({ height: 320})
			}
		}

		var imgW = img[0].offsetWidth;
		$(divs[divLength]).find('a.img').css({ 'margin-left': (imgW/2)*-1 }); // kozepre a kepet


		divs[divLength].index = divLength;

		$(divs[divLength]).append('<div class="counter"><b></b></div>');

		$(divs[divLength]).css({
			left: (divLength*161)+((divLength)?344:2),
			width: (divLength)?158:((imgW > 500)?500:imgW),
			'max-width': (imgW > 500)?500:imgW
		}).find('a.img,h1,p,div.counter').mouseover(function(e){
			$(e).stopEvent();
			animateItemsBy( $(this).parent() );
		});
		$(divs[divLength]).find('h1').css({
			width: Math.round( ((imgW > 500)?500:imgW) * 0.7 )
		});
		$(divs[divLength]).find('p').css({
			width: Math.round( ((imgW > 500)?500:imgW) * 0.6 )
		});
		toBlockText( $(divs[divLength]).find('h1 a') )
		toBlockText( $(divs[divLength]).find('p') )

		$(divs[divLength]).find('p').append('<span class="bg"></span>');
		$(divs[divLength]).find('h1').css({
			bottom: $(divs[divLength]).find('p')[0].offsetHeight + 25
		});


		var link = $(divs[divLength]).find('a.img').attr('href');

		$(divs[divLength]).find('h1,p').attr('rel',$(divs[divLength]).find('a.img').attr('href') )

		$(divs[divLength]).find('h1,p').click(function(){
			location.href = $(this).attr('rel');
		})

	}

	var bwidth = divs.eq(active).find('.counter b').width();
	var dwidth = divs.eq(active).width();
	counter = setInterval(function(){
		divs.eq(active).find('.counter b').css({
			width: bwidth+=2,
			opacity: bwidth/dwidth
		});
		if (bwidth == dwidth) {
			clearInterval(counter);
			divs.eq(active).find('.counter b').css({width: 0});
			animateItemsBy( (active == 3)?divs.eq(0):divs.eq(1) )
		}
	},pagespeed)




/**
 * akkorara amekkora a kep de legfeljebb 500px
 */

});

$(document).ready(function(){

	$('textarea#comment').keyup(function(e){
		var rowsNeeded = 2;
		var arr = $('textarea#comment').val().split("\n");
		var arrLen = arr.length
		while (arrLen--) {
			rowsNeeded += Math.ceil(arr[arrLen].length / 70) || 1
		}
		if (rowsNeeded > 0 && $(this).attr('rows') != rowsNeeded) {
			$(this).attr('rows', rowsNeeded);
		}
	});

	$('#commentform input[type=text]').each(function(){
		var item = $(this);
		if (item.val() == item.attr('title') || item.val() == '') {
			item.val(item.attr('title')).addClass('default');
		}
	});

	$('input[type=text]').focus(function(){
		var item = $(this);
		item.removeClass('default').addClass('active').val((item.val() == item.attr('title')) ? '' : item.val());
	}).blur(function(){
		var item = $(this);
		item.removeClass('active').val((item.val() == item.attr('title') || item.val() == '') ? item.attr('title') : item.val());
		if( item.val() == item.attr('title') || item.val() == '' ){
			item.addClass('default');
		}
	});

	if($('body.home div#Main div.daily_pic').length > 0 )
		toBlockText($('body.home div#Main div.daily_pic h2 a'));

})


function toBlockText( item ){

	var text = item.text().split(' ');

		text = jQuery.map(text, function(n, i){
			return ['<b>',n,'</b>'].join('');
		});

	item.html( text.join('') )
}


function animateItemsBy( obj ){

	var item =  obj;
	clearInterval(counter);
	item.find('.counter b').css({width: 0,opacity:0});

	active = item[0].index;

	item.stop().animate({
		width: item.css('maxWidth'),
		left:(item[0].index * 161)+2

	},function(){
		var divs = item.parent().children('div');
		var bwidth = item.find('.counter b').width();
		var dwidth = item.width();
		counter = setInterval(function(){
			divs.eq(active).find('.counter b').css({
				width: bwidth+=2,
				opacity: bwidth/dwidth
			});
			if (bwidth == dwidth) {
				clearInterval(counter);
				divs.eq(active).find('.counter b').css({width: 0});
				animateItemsBy( (active == 3)?divs.eq(0):divs.eq(active+1) )
			}
		},pagespeed)
	}).find('h1,p').stop().animate({
			marginLeft: 0,
			opacity: 1
		}).end()
	.prevAll(':not(span)').each(function(){
		$(this).stop().animate({
			width: 158,
			left: ($(this)[0].index * 161)+2
		}).find('h1,p').stop().animate({
			marginLeft: -100,
			opacity: 0
		},200).end().find('.counter b').css({width: 0,opacity:0});

	}).end().nextAll(':not(span)').each(function(){
		$(this).stop().animate({
			width: 158,
			left: ($(this)[0].index * 161)+ ( ($(this)[0].index )?344:2)
		}).find('h1,p').stop().animate({
			marginLeft: -30,
			opacity: 0
		},200).end().find('.counter b').css({width: 0,opacity:0});
	}).end();

}


