var wbstatus=lastpos=0;

jQuery.fn.rating = function () {
	return this.each (function () {
		pollinit ($(this));
	});
}

$(document).ready(
function () {
	$('.png').pngFix();
	$('.infoicon').pngFix();
	FLIR.init(new FLIRStyle( { mode:'wrap' } ) );
	$(".flir").each(function(){ FLIR.replace(this);});
	$('a').hover(function(){$(this).addClass('hover')},function(){$(this).removeClass('hover')});
	$("a.lightbox, a.videobox").fancybox({
		'overlayShow'			: true,
		'zoomSpeedIn'			: 400,
		'zoomSpeedOut'			: 300,
		'height':'250',
		'width':'425',
		'centerOnScroll':false
	});

/*
$("a.videobox").fancybox({
		'width'				: '400',
		'height'			: '250',
        'autoScale'     	: false,
		'overlayShow'			: true,
		'zoomSpeedIn'			: 400,
		'zoomSpeedOut'			: 300,
		'height':450,
		'width':425,
		'centerOnScroll':false
	});

*/







	initResizeBlock();
	if($('.floatgallery a').length>0)  scrollinit();
	$('.btnpoll').rating();

});

function showdetail(sender){
	$('div.widefault').hide();
	$('div.widyna').html($(sender).parent().find('.detailinfo').html()).show();
}
function showdefaultwi(sender){
	$('div.widyna').hide();
	$('div.widefault').show();
}

function initResizeBlock(){
	$('.infoicon a').hover(
	function(){showdetail(this);},
	function(){showdefaultwi(this);}
	)


	$('.wideinfo').hover(function(){$('.hidebtn').addClass('whover');},function(){$('.hidebtn').removeClass('whover');});
	if(wbstatus==0){			  $('.hidebtn').addClass('onwhover');}

	$('.wideinfo').bind('click',function(){
		if (wbstatus==1){
			$('div.wideinfo .wicol').not('.first').fadeOut(1000);
			$('.wideinfo').animate({'width':'250px'},1000,'swing');
			$('.hidebtn').addClass('onwhover');
			wbstatus=0;
		}else {
			$('.wideinfo').animate({'width':'930px'},1000,'swing');
			$('.wicol').fadeIn(1000);
			wbstatus=1;
			$('.hidebtn').removeClass('onwhover');
		}

	})


}


function scrollinit(){
	var clientwidth=$('.floatgallery').width();
	var maxxpos=$('.gallerywrap a:last').offset().left+$('.gallerywrap a:last').width()-$('.gallerywrap a:first').offset().left-clientwidth;
	if(maxxpos<0){
		$('.scrollbtn').hide();
		$('.floatgalleryblock').css({'padding-left':0,'width':'880px'});
	}


	var way=0;
	$('.scrollbtn').bind('click',function(){
		var newpos;
		if($(this).hasClass('scrolleft')){way=-1;}
		if($(this).hasClass('scrolright')){way=1;}

		newpos= $('.floatgallery').scrollLeft()+(500*way);
		$('.scrolright, .scrolleft').show();
		scroll2pos(newpos,maxxpos,1500);



	});

   if($('.gallerywrap a.active').length>0){
	var activepos =$('.gallerywrap a.active').offset().left-$('.gallerywrap a:first').offset().left-400;
	scroll2pos(activepos,maxxpos,200);
	}



	//    	})



}


function scroll2pos(pos,maxpos,scrtime){

	newpos= Math.max(0,pos);
	newpos= Math.min(maxpos,newpos);
$('.floatgallery').animate({'scrollLeft':newpos+'px'},scrtime,"swing",function(){
	if(newpos>=maxpos){$('.scrolright').fadeOut();}
	if(newpos<=0){$('.scrolleft').fadeOut();}

});

}

/**anketa**/

function pollinit(obj){
	var textinsert;
	var i=0;
	var srating=$(obj).attr("rel");
	//srating=srating.toFixed(1);
	textinsert='';



/*
	$(obj).bind('mouseout',function(){
		display(this,srating,'static') ;
	});
*/
/*	for(i=0;i<5;i++){
		textinsert+='<a class="rate'+i+'"><span class="star"></span></a>';
	}*/
//	textinsert+='<span class="pollresult">'+((srating+1).toPrecision(3))+'</span>';

//	$(obj).html(textinsert);
//	display($(obj),srating,'static') ;

	if($(obj).hasClass('spenabled')){
		$(obj).append('<a class="vtbutton" href="#">Vybrať  ako najlepšiu realizáciu</a>');
		$(obj).find('a').bind('click',function(){hlasuj($(this));return false;});



/*
		$(obj).find('span.star').each(function(){
			$(this).bind('mouseover',function(){
				display($(this).closest('.starpoll'),$(this).parent().attr("class").split('rate')[1],'') ;
			}).bind('click',function(){hlasuj($(this));});
			});*/
	}   else {                     
	//	  console.log(obj);       
		  if(obj.attr('rel')==2)$(obj).html('<span class="vtbutton vtb2">Zvolili ste ako najlepšiu realizáciu</span>');
		  if(obj.attr('rel')==1)$(obj).html('<span class="vtbutton vtb1">&nbsp;&nbsp;</span>');
		  if(obj.attr('rel')==0)$(obj).html('<span class="vtbutton vtb0">-hlasuj--</span>');
		
		
		
		}
}


	function hlasuj(obj){
		//var rating=$(obj).closest("a").attr("class").split('rate')[1];
	//	alert(obj);
	//	console.log(obj);
		
		var rid=$(obj).closest(".btnpoll").attr("id").split("p_")[1];
		$.ajax({
			type: "POST",
			url: "../core/ajax/hlasuj1.php",
			data: ({id:rid,rating:1}),
			dataType: 'json',
			success: function(msg){
			//	console.log(msg);                                                                                                                         
				$('.btnpoll').removeClass('spenabled').attr("rel",1);
				$(obj).closest('.btnpoll').attr("rel",msg.value);//.removeClass('spenabled').rating().fadeIn('slow');       
				$('.btnpoll').rating();

				//$(obj).closest('.btnpoll').fadeOut('fast').attr("rel",msg.value).removeClass('spenabled').rating().fadeIn('slow');
			}
		});


	}

	function display(obj,xrating,clsName)	{
		var i=0;
		xrating=Math.round(xrating);
		for(i=0;i<5;i++){
			if (i<=xrating){
				$(obj).find(".rate"+i+" span.star").removeClass('static').addClass('active '+clsName);
			}else {
				$(obj).find(".rate"+i+" span.star").removeClass('active').removeClass('static');
			}
		}
	}

