jQuery.extend({
init_popnews: function(option){

  option = $.extend({
	from:"",
    linev:"",
	lineh:"",
	to_left:350,
	to_top:100,
	left1:"",
	left2:"",
	left3:"",
	right1:"",
	right2:"",
	mylink:"",
	myblock:"",
	playingtitle:"Trenutni:",
	nexttitle:"Slijedi:",
	prevtitle:"Prethodni:",
	newsspeed:10000,
	isauto:true,
	flashpause:false
	
  }, option);
		var from=option.from;
		var linev=option.linev;
		var lineh=option.lineh;
		var to_left=option.to_left;
		var to_top=option.to_top;
		var left1=option.left1;
		var left2=option.left2;
		var left3=option.left3;
		var right1=option.right1;
		var right2=option.right2;
		var mylink=option.mylink;
		var myblock=option.myblock;
		var playingtitle=option.playingtitle;
		var nexttitle=option.nexttitle;
		var prevtitle=option.prevtitle;
		var newsspeed=option.newsspeed;
		var isauto=option.isauto;
		var flashpause=option.flashpause;
		
/////////////////////////////////Init///////////////////////////////////////////////////////////
	var action=1;
var bb=document.body.clientWidth;  
var tt=document.body.clientHeight; 
var cc=bb/2-to_left;
var dd=tt/2-to_top;
var myv='<img src=point.gif id=v>';
var myh='<img src=point.gif id=h>';
var menu='<div id=menu class=menu ></div>';
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			$("body").css("overflow","hidden");
			scroll(0,0);
			var mytitlearry = new Array();
				$('#'+from+' .news_pop').each(function(n){
				mytitlearry[n]=$(this).attr('rel');				
			});
$('#'+linev).html(myv).show();
$('#'+lineh).html(myh).show();

$('#v').attr({width:'1',height:tt});
$('#'+linev).animate({top:'0',left:cc},500);
$('#h').attr({width:bb,height:'1'});
$('#'+lineh).animate({top:dd,left:'0'},500);
var active=0;
var mysize=$('#'+from+' .news_pop').size();
$('body').append(menu).show();

$('#'+myblock).show().appendTo('body');
$('#'+myblock).animate({width: '100%',opacity: 0.95}, 500);
active=-1;
///////////////////////////Next Function///////////////////////////////////////////////////////////////


function mtnext(){
	clearTimeout(timer);
	action=1;
	$('.dir').css({color:""});
	$('#next').css({color:"gold"});
	$('#loading').css({left:cc-30,top:dd-30});
	$('#loading').show('fast').appendTo('body');
	$('#menu').empty();

	$('#'+linev).empty();
	$('#'+linev).css({top:'0',left:'0'});
	$('#'+lineh).empty();
	$('#'+lineh).css({top:'0',left:'0'});
active=active+1;
if (active == mysize)
{active=0;}
var mynext=active+1;
var myshow=active+1;
if (mynext >=mysize)
{mynext=0;}

var mycontent=$('#'+from+' .news_pop:eq('+active+')').html();
var flash=$('#'+from+' .news_pop:eq('+active+')').find('object').attr('classid');
var mytitle=$('#'+from+' .news_pop:eq('+active+')').attr('rel');
var mynexttitle=$('#'+from+' .news_pop:eq('+mynext+')').attr('rel');

//var mynextcontent=$('#'+from+' .news_pop:eq('+mynext+') img:eq(0)').clone();
var mynextcontent=$('#'+from+' .news_pop:eq('+mynext+') #1st').clone();
$('#'+right2).html(mycontent);
$('#'+left3).html(mynextcontent);
var myImage0 = new Image();
myImage0.src=$(mynextcontent).attr('src');
var mywidth1=myImage0.width;
var myheight1=myImage0.height;

$('#'+left3).css({left:bb,top:dd+40});
//var myfirst=$('#'+right2+' img:eq(0)').clone();
var myfirst=$('#'+right2+' #1st').clone();
//$('#'+right2+' img:eq(0)').remove();
$('#'+right2+' #1st').remove();
$('#'+linev).html(myv);
$('#'+lineh).html(myh);

$('#v').attr({width:'1',height:tt});
$('#'+linev).animate({top:'0',left:cc},500);
$('#h').attr({width:bb,height:'1'});
$('#'+lineh).animate({top:dd,left:'0'},500);

$('#'+left1).html(myfirst);
var myImage = new Image();
myImage.src=$(myfirst).attr('src');
var mywidth=myImage.width;
var myheight=myImage.height;


$('#'+left1).css({width:mywidth+10,left:cc,top:dd+500});
$('#'+right1).html('<h3>'+mytitle+'</h3>');
$('#'+left2).html('<h3>'+playingtitle+''+myshow+'/'+mysize+'</h3><div class=np>'+nexttitle+''+mynexttitle+'</div>');
$('#'+left2).css({width:'500px',left:bb,top:dd+40});
$('#'+right2).css({width:'500px',left:cc+10,top:dd-500});//attation here

$('#'+right2).animate({width:'500px',left:cc+10,top:dd+10},1000);
$('#'+left1).animate({width:mywidth+10,left:cc-mywidth-20,top:dd-myheight-20},1000);

$('#'+right1).animate({width:'400px',left:cc+10,top:dd-50},1000);
$('#'+left2).animate({width:'500px',left:cc-200},1000);
$('#'+mylink).animate({width:'500px',left:cc+410,top:dd-30},1000);

$('#'+left3).animate({width:mywidth1+10,left:cc-mywidth1-60,top:dd+100},1000,function(){
	$('#loading').hide();	
});
$('#menu').stop();
$('#menu').empty();
$('#menu').css({width:'500px',height:'1px',left:cc+520,top:dd+20});
var n=0;
x=active;
$('#menu').append('<ul>');
while(n<mysize){	
	menu_write(x);
x=x+1;
if(x==mysize){x=0;}
n=n+1;
}
$('#menu').append('</ul>');
$('#menu').animate({height:'500px',left:cc+520,top:dd+110},5000);

if (!flash && isauto===true || flash && flashpause===false){
	timer=setTimeout(mtnext,newsspeed);
}
if (flash && flashpause!==false){
	$('#pause').click();
}
$('#menu a').click(function(){
	$('#menu').stop();
	$('#menu').empty();
$('#menu').css({width:'500px',height:'1px',left:cc+520,top:dd+20});
	active=$(this).attr('rel');
	active=parseInt(active,10);
	active=active-1;
	$('#next').click();
});
}







function menu_write(z){
	if (z==active)
	{
	$('#menu').append('<li style="font-weight:bold">'+mytitlearry[z]+'</li>');
	//$(this).css({font-weight: 'bold'});
	}
	if (z!=active)
	{
	$('#menu').append('<li><a href=# class=mymenu rel='+z+'>'+mytitlearry[z]+'</a></li>');
	}

}

/////////////////////////////////prev click//////////////////////////////////////////////////////////

function mtprev(){
		clearTimeout(timer);
		action=2;
		$('.dir').css({color:""});
		$('#prev').css({color:"gold"});
	$('#loading').css({left:cc-30,top:dd-30});
	$('#loading').show('fast').appendTo('body');
	$('#'+linev).empty();
	$('#'+linev).css({top:'0',left:'0'});
	$('#'+lineh).empty();
	$('#'+lineh).css({top:'0',left:'0'});
	$('#'+linev).html(myv);
	$('#'+lineh).html(myh);

		active=active-1;
		if (active < 0)
		{active=mysize-1;}
		var myprev=active-1;
		if (myprev<0)
		{myprev=mysize-1;}
		var myshow=active+1;


var mycontent=$('#'+from+' .news_pop:eq('+active+')').html();
var flash=$('#'+from+' .news_pop:eq('+active+')').find('object').attr('classid');

var mytitle=$('#'+from+' .news_pop:eq('+active+')').attr('rel');
var mynexttitle=$('#'+from+' .news_pop:eq('+myprev+')').attr('rel');
$('#'+right2).html(mycontent);	

//var myfirst=$('#'+right2+' img:eq(0)').clone();
var myfirst=$('#'+right2+' #1st').clone();
$('#'+left1).html(myfirst);
var myImage = new Image();
myImage.src=$(myfirst).attr('src');
var mywidth=myImage.width;
var myheight=myImage.height;
//$('#'+right2+' img:eq(0)').remove();
$('#'+right2+' #1st').remove();

//var myprevcontent=$('#'+from+' .news_pop:eq('+myprev+') img:eq(0)').clone();
var myprevcontent=$('#'+from+' .news_pop:eq('+myprev+') #1st').clone();
$('#'+left3).html(myprevcontent);
var myImage1 = new Image();
myImage1.src=$(myprevcontent).attr('src');
var mywidth1=myImage1.width;
var myheight1=myImage1.height;

$('#'+linev).html(myv);
$('#'+lineh).html(myh);

$('#'+left3).css({left:bb,top:dd+40});
$('#v').attr({width:'1',height:tt});
$('#'+linev).animate({top:'0',left:cc},500);
$('#h').attr({width:bb,height:'1'});
$('#'+lineh).animate({top:dd,left:'0'},500);

$('#'+left1).css({width:mywidth+10,left:cc,top:dd+500});
$('#'+right1).html('<h3>'+mytitle+'</h3>');
$('#'+right1).css({width:'400px',left:cc,top:dd+500});
$('#'+left2).html('<h3>'+playingtitle+''+myshow+'/'+mysize+'</h3><div class=np>'+prevtitle+''+mynexttitle+'</div>');
$('#'+left2).css({width:'500px',left:bb,top:dd+40});
$('#'+right2).css({width:'500px',left:cc+10,top:dd-500});//attation here
$('#'+right2).animate({width:'500px',left:cc+10,top:dd+10},1000);
$('#'+left1).animate({width:mywidth+10,left:cc-mywidth-20,top:dd-myheight-20},1000);

$('#'+right1).animate({width:'400px',left:cc+10,top:dd-50},1000);
$('#'+left2).animate({width:'500px',left:cc-200},1000);
$('#'+mylink).animate({width:'500px',left:cc+410,top:dd-30},1000);

$('#'+left3).animate({width:mywidth1+10,left:cc-mywidth1-60,top:dd+100},1000,function(){
		$('#loading').hide();	
});
$('#menu').stop();
$('#menu').empty();
$('#menu').css({width:'500px',height:'1px',left:cc+520,top:dd+20});
var n=0;
x=active;
$('#menu').append('<ul>');
while(n<mysize){	
	menu_write(x);
x=x-1;
if(x<0){x=mysize-1;}
n=n+1;
}
$('#menu').append('</ul>');
$('#menu').animate({height:'500px',left:cc+520,top:dd+110},5000);

if (!flash && isauto===true || flash && flashpause===false){

	timer=setTimeout(mtprev,newsspeed);

}
if (flash && flashpause!==false){
	$('#pause').click();
}
$('#menu a').click(function(){
$('#menu').stop();
$('#menu').empty();
$('#menu').css({width:'500px',height:'1px',left:cc+520,top:dd+20});	
	active=$(this).attr('rel');
	active=parseInt(active,10);
	active=active+1;
	$('#prev').click();
});
}

$('#next').click(function(){
	
mtnext();
$('.dir').css({color:""});
$(this).css({color:"gold"});
});


$('#prev').click(function(){
	
mtprev();
$('.dir').css({color:""});
$('#prev').css({color:"gold"});
});
$('#close').click(function(){
			clearTimeout(timer);
			$('.dir').css({color:""});
$('#close').css({color:"gold"});

$('#'+right2).slideUp('slow',function(){
$('#'+left1).fadeOut('slow').empty();
$('#'+right1).fadeOut('slow').empty();
$('#'+left2).fadeOut('slow').empty();
$('#'+mylink).hide('slow');	
$('#close').css({color:""});
$('#'+left3).fadeOut('slow').empty();
$('#'+linev).animate({left:0},1500,function(){
	$('#'+linev).hide().empty();	
});
$('#'+lineh).animate({top:0},1500,function(){
		$('#'+lineh).hide().empty();
$('#'+myblock).hide();
			$("body","html").css({height: "auto", width: "auto"});
			$("html").css("overflow","");
			$("body").css("overflow","");

			});
	$('#'+right2).empty();
		});
	$("#close").unbind("click");
	$("#prev").unbind("click");
	$("#next").unbind("click");
	$("#pause").unbind("click");
	$('#'+left3).unbind("click");
	$('#'+right2).unbind("click");
	$(document).unbind("dblclick");
	$('#menu a').unbind("click");
	$('#menu').remove();

	});

$('#pause').click(function(){
	clearTimeout(timer);
	$('.dir').css({color:""});
$('#pause').css({color:"gold"});

});
$(document).dblclick(function(){
$('#close').click();
});

$('#'+right2).click(function(){
$('#pause').click();
});
$('#'+left3).click(function(){
			clearTimeout(timer);

if (action==1)
{	$('#next').click();}
if (action==2)
{	$('#prev').click();}
});
document.onkeydown = function(k){ 	
				if (k == null) { // ie
					keycode = event.keyCode;
				} else { // mozilla
					keycode = k.which;
				}
				if(keycode == 27){ // escape
					$('#close').click();
				} 
};

	timer=setTimeout(mtnext,100);



///////////////////////////////////////////////////////////////////////////////////////////////////////

}
	

});
