var piecount = 3;
$(document).ready(function(){

	$("div.news").each(function(){
		popupimg($(this));
	});
	$("#newslist").each(function(){
		setfixh($(this));
	});
});
var newspos = 1;
function popupimg(el){
	el.find(".popupimg:gt(2)").hide();
 	var i = el.find(".popupimg").length;
	if(i >3){
	var img = el.find(".popupimg:eq(2)").position();
	var posleft = img.left;
	var postop = img.top;
	var golefttop = postop+100;
	var goleftleft = posleft+213;
	var gorighttop = postop+45;
	    el.find(".popupimg:eq(2)").parent().after("<div class='goright' style='top:"+gorighttop+"px; left:"+goleftleft+"px;'></div>");
            el.find(".popupimg:eq(2)").parent().after("<div class='goleft' style='top:"+golefttop+"px; left:"+goleftleft+"px;'></div>");
	};
	var clickright = 0;
	el.find(".goright").click(function() {
	    var x = clickright + 3;
	    var y = clickright + 1;
	
	    var showimg = el.find(".popupimg:eq("+x+")");
	    var cHclassimg = el.find(".popupimg:eq("+y+")");
	    var hideimg = el.find(".popupimg:eq("+clickright+")");
	    if(showimg.length){
	          cHclassimg.addClass("firstimg");
	          hideimg.hide();
	          showimg.show();
	          clickright++;
	    };
  	});
	el.find(".goleft").click(function() {
	    var x = clickright + 2 ;
	    var y = clickright - 1 ;

	    var showimg = el.find(".popupimg:eq("+y+")");
	    var cHclassimg = el.find(".popupimg:eq("+clickright+")");
	    var hideimg = el.find(".popupimg:eq("+x+")");
	    if(showimg.length){
	          hideimg.hide();
	          cHclassimg.removeClass("firstimg");
	          showimg.show();
	          clickright--;
	    };
	});
	
};
function setfixh(el){
        var img = $("img");

         img.each(function(e) {
         if (this.complete) {
               if(el.height() < 697)
	       el.height(697);
         }else{
        	 $(this).bind("load", function(e) {
	              if(el.height() < 697)
		      el.height(697);
        	 });
         }
     });
     	//if(el.height() < 1600){
	        var itemlenght  = $("div.item").length;
	      	var showitem  = Math.floor(Math.random()*itemlenght);
       	 	var qwe = shitem(showitem, itemlenght);
	//};
	/*
	if(el.height() > 1600){
	        var itemlenght  = $("div.item").length;
	      	var showitem  = Math.floor(Math.random()*itemlenght);
	      	for(i=0; i<piecount; i++){
	      	var qwe = shitem(showitem, itemlenght);
	      	showitem = qwe;
		};
	};*/
};

function shitem(showitem, maxnum){
         if(showitem < maxnum-1){
           if($("div.item:eq("+showitem+")").hasClass('visitem'))
             showitem = shitem(showitem, maxnum);
		$("div.item:eq("+showitem+")").addClass('visitem');
		showitem++;
  	 }else{
           if($("div.item:eq("+showitem+")").hasClass('visitem'))
             showitem = shitem(showitem, maxnum);
	 	$("div.item:eq("+showitem+")").addClass('visitem');
	 	showitem = 0;
   	 }
    return showitem;
};
