var imgsIds=[];
jQuery(document).ready(function($) { 

var photoC=getCookie('photo_fav');
check_photos();
var itemy =  (document.getElementById('itemsContainer'));
 
if(location.pathname.indexOf('lista_podreczna/lista')==-1) {


  var czyBeta = (location.host.indexOf('beta')!=-1);
  //if(console) console.profile('ListaPodreczna');
  
  if(itemy){
  
  //---- wersja z klonowaniem - okazuje sie wolniejsza ---------
  /*
  if(location.pathname.indexOf('fryzury141.html')!=-1){
     var klon = jQuery('#itemsContainer').clone();
	 klon.find('img.img_items').each(function(i){ 
		   var id=this.id.split('_')[1]; 
		   var divHTML=wstawHTML(id);
		   imgsIds[i]=Number(id);
		   if(this.src.indexOf('default_min.jpg')==-1 || czyBeta) {
				jQuery(this).after(divHTML);
				if(photoC.indexOf(id)!=-1) { 
					 var imgObj=klon.find('#img_small_'+id)[0];
					 zamien_src(imgObj); 
					 imgObj.style.display="";
				 }
			}//if - jesli brak obrazka lub beta
	   });//each/
	jQuery('#itemsContainer').replaceWith(klon);
	}
	*/
  //----------------

	jQuery('#itemsContainer img.img_items').each(function(i){ 
		   var id=this.id.split('_')[1]; 
		   var divHTML=wstawHTML(id);
		   imgsIds[i]=Number(id); 
		   if(this.src.indexOf('default_min.jpg')==-1 || czyBeta) {
				jQuery(this).after(divHTML);
				if(photoC.indexOf(id)!=-1) { 
					 var imgObj=document.getElementById('img_small_'+id);
					 zamien_src(imgObj); 
					 imgObj.style.display="";
				 }
			}//if - jesli brak obrazka lub beta
	});//each/
	   
	   
	   
   }//getElementById
   
}//if

   if(itemy){
	   jQuery('#itemsContainer a.items_a').hover(function(){ 
								   try { var img=this.getElementsByTagName('img')[1]; jQuery(img).fadeIn(200); } catch(e){ }
								  },
					 function(){ 
								try {  
									   var img=this.getElementsByTagName('img')[1];
									   if(img.src.indexOf('plus')!=-1) { img.style.display="none"; }
									 }
									 catch(e){}
								
								}
		);//hover
	}
	//if(console) console.profileEnd();
	

	
	 //zamieniaj linki w liscie podrecznej
   if(location.pathname.indexOf('lista_podreczna/lista')!=-1)
     {
		jQuery('.items_a').each(function(i)
			{
				if(this.id.indexOf('a-zapisana')!=-1)
				   {
					  var id=this.id.split('_')[1];
					 //jesli istnieje list ID
					 var listID=this.id.split('_')[2];
						if(listID!=undefined || listID!=null)
						    podmieniaj_link(this,id+'/'+listID,1,'itemlist');
				   }
			});
	 }
	 
	 
	 
	 
});//$(document).ready
   
function wstawHTML(imgId) {
return '<div class="div_dodaj"><a onclick="add_photo('+imgId+',this); return false;" title=""><img class="img_ulubione" id="img_small_'+imgId+'" style="display:none;" src="/img/plus3.gif" /></a></div>';
}


function podmieniaj_link(obj,id,akcja,par) {
 //pobierz obiekt linka i do itemu i zamien mu hrefa
 var objA=obj;
 var objAJ = jQuery(objA);
 if(akcja==1) {
	if(objAJ.attr('defaultHref')==undefined) objAJ.attr('defaulthref',objAJ.attr('href'));
	objAJ.attr('href','/lista_podreczna/'+par+'/'+id);
   }
 else {
		objAJ.attr('href',objAJ.attr('defaultHref'));
	  }
}
   
function add_photo_by_id(id)
{
  var photoC=getCookie('photo_fav');
  if(check_photos()>49) { alert('Limit zdjec w ulubionych wynosi 50 !'); return;  }
  var c=photoC+','+Number(id);
  if(photoC.indexOf(id)==-1) { 
							  setCookie('photo_fav',c,100); 
							  alert('Zdjęcie zostało dodane do Listy Podręcznej');
							 }
  else alert('To zdjęcie znajduje się już w Twojej liście podręcznej');
  check_photos();
}
  
   
function add_photo(id,obj)
{
var photoC=getCookie('photo_fav');
var img_o=obj.getElementsByTagName('img')[0];
if(check_photos()>49) { alert('Limit zdjec w ulubionych wynosi 50 !'); return;  }
var c=photoC+','+Number(id);
if(photoC.indexOf(id)==-1) { 
							 setCookie('photo_fav',c,100); 
							 zamien_src(img_o); obj.setAttribute("title","");
							}
else { 
		obj.setAttribute("title",""); 
		//alert(obj);
		usun_zdjecie(id); zamien_src(img_o); 
	 }
check_photos();
 }
 
 function wyczysc_koszyk()
 {
  setCookie('photo_fav','',100);
  jQuery('.img_ulubione').each(function()
  {
    if(this.src.indexOf('minus')!=-1) { zamien_src(this); this.style.display="none"; }
  });//each
  check_photos();
 }
  
function usun_zdjecie(id)
  {
   var photoC=getCookie('photo_fav');
   if(photoC.indexOf(',')!=-1)
   {
	    var photos=photoC.split(',');
		for(var i=0,str=''; i<photos.length; i++) { if(photos[i]!=id && photos[i]!='') str+=photos[i]+','; }
		setCookie('photo_fav',str,100);
   }
 }   
 
 function zamien_src(o)
 {
  var os=o.src;
  if(os.indexOf('plus')!=-1) o.src=os.replace(/plus/,'minus');
  else o.src=os.replace(/minus/,'plus');
  }
  
  
function check_photos()
 {
  var count=0;
  var photoC=String(getCookie('photo_fav'));
  var photos=[];
 if(photoC.indexOf(',')!=-1)
   {
    photos=photoC.split(',');
	count=photos.length-1;
    document.getElementById('koszyk_photo_liczba').innerHTML=count; 
	jQuery('#pokaz_koszyk').css("font-weight","bold");
   }
   else { 
		 document.getElementById('koszyk_photo_liczba').innerHTML='0'; 
		 jQuery('#pokaz_koszyk').css("font-weight","normal");
		 }
   //------------wygaszanie span#lista_podr_del w przypadku 0
   if(parseInt(count)>0) {
		jQuery('#lista_podr_del').css('display','inline');
		//przesuniecia divow z nowymi fotami w gornym wygaszanym menu w headerze
		jQuery('#nowe_foty_all').css('left',715-40);
		jQuery('#nowe_komentarze_all').css('left',633-40);
   }
   else  {
			jQuery('#lista_podr_del').css('display','none');
			jQuery('#nowe_foty_all').css('left',715);
			jQuery('#nowe_komentarze_all').css('left',633);
   }
   //-----------------------------------------------------------
   return count;
 } 
 
 function pokaz_koszyk() {
  document.location.href='http://'+location.host+'/lista_podreczna';
 }
 
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+";domain="+location.host+";path=/");
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}