var winW = 630, winH = 460;
if (parseInt(navigator.appVersion)>3) {
 if (navigator.appName=="Netscape") {
  winW = window.innerWidth;
  winH = window.innerHeight;
 }
 if (navigator.appName.indexOf("Microsoft")!=-1) {
  winW = document.body.offsetWidth;
  winH = document.body.offsetHeight;
 }
}
winWx = winW/2-150;


function AddToWishlist(cookieValue) {
	var today = new Date();
	var expire = new Date();
	//~ if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*356);
	var mcookie = ReadCookie('wishlist');
	if (mcookie=="" || mcookie==null) { mcookie=escape(cookieValue)+','; } else {
			var pattern=new RegExp('\\b'+escape(cookieValue)+',');
			var mcheck = mcookie.search(pattern);
			if (mcheck == -1) {
				mcookie = mcookie + escape(cookieValue)+',';
		}
	}
	if (cookieValue !="" || cookieValue !=null) { 
		document.cookie = 'wishlist'+"="+mcookie+";expires="+expire.toGMTString();
		document.getElementById('wishlist').innerHTML = mcookie.split(",").length-1;
	}
	alert('Produsul a fost adaugat in Wishlist.');
}

function RemoveFromWishlist(cookieValue) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 3600000*24*356);
	var mcookie = ReadCookie('wishlist');
	if (mcookie=="" || mcookie==null) { alert('The item is not in the list!'); } else {
			var mcookiea = mcookie.split(",");
			var j = 0;
			mcookie ='';
			while (j < mcookiea.length-1) {
				//~ alert(mcookiea[j]);
				if (mcookiea[j] == cookieValue) {
					//~ mcookiea.splice(j, 1);
				} else { 
					mcookie = mcookie + mcookiea[j]+',';
					//~ j++;
				}
				j++;
			}
	}
	if (cookieValue !="" || cookieValue !=null) { 
		document.cookie = 'wishlist'+"="+mcookie+";expires="+expire.toGMTString();
		document.getElementById('wishlist').innerHTML = mcookie.split(",").length-1;
	}
	location.reload(true);
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function Wishlist() {
    var mcookie = ReadCookie('wishlist');
    document.write(mcookie.split(",").length-1);
}

function OpenOtherTab(n_lay) {
	var lay=document.getElementById('lay4image');
	var l4i_c=document.getElementById(n_lay);
	
	
	lay.style.width=winW+"px";
	lay.style.height=winH+"px";
	lay.style.display="block";

	l4i_c.style.left=winWx+"px";
	l4i_c.style.display='block';
	l4i_c.style.top = document.body.scrollTop +250 +'px';
	
	if (n_lay=='l4i_salveaza') {
		setTimeout("CloseOtherTab('"+n_lay+"')",500);
	}
}

function CloseOtherTab(lay) {
	document.getElementById('lay4image').style.display="none";
	document.getElementById('lay4image').style.top="0px";
	document.getElementById(lay).style.display="none";
	
	if (lay=='l4i_salveaza') {
		var x0= new String();
		x0=location.href;
		var x=x0.split("id=");
		var x1 = x[1].replace(/id=/, "");
        x1 = x1.replace(/#/, "");
		addCookie('fav_anunt', x1);
	}
}

function textCounter( field, countfield, maxlimit ) {
  if ( field.value.length > maxlimit )
  {
    field.value = field.value.substring( 0, maxlimit );
    alert( 'Mesajul poate contine cel mult 255 de caractere.' );
    return false;
  }
  else
  {
    countfield.value = maxlimit - field.value.length;
  }
}

//function "visual add to cart" jquery
(function($) {

	$.extend({
		add2cart: function(source_id, target_id, callback) {
    
      var source = $('#' + source_id );
      var target = $('#' + target_id );
      
      var shadow = $('#' + source_id + '_shadow');
      if( !shadow.attr('id') ) {
          $('body').prepend('<div id="'+source.attr('id')+'_shadow" style="display: none; background-color: #ddd; border: solid 1px darkgray; position: static; top: 0px; z-index: 100000;">&nbsp;</div>');
          var shadow = $('#'+source.attr('id')+'_shadow');
      }
      
      if( !shadow ) {
          alert('Cannot create the shadow div');
      }
      
      shadow.width(source.css('width')).height(source.css('height')).css('top', source.offset().top).css('left', source.offset().left).css('opacity', 0.5).show();
      shadow.css('position', 'absolute');
      
      shadow.animate( { width: target.innerWidth(), height: target.innerHeight(), top: target.offset().top, left: target.offset().left }, { duration: 500 } )
        .animate( { opacity: 0 }, { duration: 100, complete: callback } );
        
		}
	});
})(jQuery);

