var cosConn=CONNEXION('cosContinut');
var compareConn=CONNEXION('compareZone');
var VECTOR_COMPARA=new Array();

function createDraggables() {
	var d= document.getElementsByTagName("img");
	var len=d.length;
	for ( var i = 0 ; i < len; i++ )
    if (d[i].id.indexOf('img_')!=-1 || d[i].id.indexOf('imgpromo_')!=-1 || d[i].id.indexOf('imgcompare_')!=-1) {
	d[i].style.cursor="move";
	dndMgr.registerDraggable( new CustomDraggable($(d[i].id),d[i].id) );
	}
}

function createCosDraggables() {
	var d= document.getElementsByTagName("img");
	var len=d.length;
	var drag_on=false;
	for ( var i = 0 ; i < len; i++ )
    if (d[i].id.indexOf('imgdel_')!=-1) {
	d[i].style.cursor="move";
	dndMgr.registerDraggable( new CustomDraggable($(d[i].id),d[i].id) );
	drag_on=true;
	}
	return drag_on;
}

var CustomDraggable = Class.create();

CustomDraggable.prototype = (new Rico.Draggable()).extend( {

   initialize: function( htmlElement, name) {
      this.htmlElement = $(htmlElement);
      this.name = name;
   },

   select: function() {
      this.selected = true;
      var el = this.htmlElement;

      // show the item selected.....
      el.style.color           = "#ffffff";
      el.style.backgroundColor = "#08246b";
      el.style.border          = "1px solid #ccc";
   },

   deselect: function() {
      this.selected = false;
      var el = this.htmlElement;
      el.style.color           = "#2b2b2b";
      el.style.backgroundColor = "transparent";
      el.style.border = "1px solid #fff";
   },
   
   cancelDrag: function() {
	  var el = this.htmlElement;
   	  new Rico.Effect.FadeTo( this.htmlElement, 1, 150, 2 );
   },

   endDrag: function() {
      var el = this.htmlElement;
      if ( CustomDraggable.removeOnDrop )
      this.htmlElement.style.display = 'none';
   },

	getSingleObjectDragGUI: function() {
	var el = this.htmlElement;
	var names = "";
	names += '<img src="'+this.htmlElement.src+'" style="width:50px" border=0>';
	var div = document.createElement("div");
	div.style.width = "50px";
	div.style.height = "50px";
	div.style.zIndex= "9999";
	new Insertion.Top( div, names );
	new Rico.Effect.FadeTo( div, .5, 150, 4 );
	return div;
},

   getDroppedGUI: function() {
      var el = this.htmlElement;
      var div = document.createElement("div");
      new Insertion.Top( div, "" );
	  if (this.name.indexOf('imgdel_')!=-1) {
	  cos_delete(this.name.replace('imgdel_',''));
	  }
	  if (this.name.indexOf('img_')!=-1) {
	  cumpar(this.name.replace('img_',''));
	  }
	  if (this.name.indexOf('imgpromo_')!=-1) {
	  cumpar(this.name.replace('imgpromo_',''));
	  }
      return div;
   }

} );

var CustomDropzone = Class.create();

CustomDropzone.prototype = (new Rico.Dropzone()).extend( {

   initialize: function( htmlElement) {
      this.htmlElement  = $(htmlElement);
      this.absoluteRect = null;
      this.offset = navigator.userAgent.toLowerCase().indexOf("msie") >= 0 ? 0 : 1;
   },

   activate: function() {
	   if (this.htmlElement.id=='cos' || this.htmlElement.id=='compareZone') {
	   this.htmlElement.style.backgroundColor="#E5F6FF";
	   new Rico.Effect.FadeTo( this.htmlElement, 1, 150, 5 );
	   }
	},

   deactivate: function() {
	   if (this.htmlElement.id=='cos') {
	   this.htmlElement.style.backgroundColor="#f7f7f7";
	   new Rico.Effect.FadeTo( this.htmlElement, 1, 250, 5 );
	   }
	   if (this.htmlElement.id=='compareZone') {
	   this.htmlElement.style.backgroundColor="#ffffff";
	   new Rico.Effect.FadeTo( this.htmlElement, 1, 250, 5 );
	   }	   
   },

   showHover: function() {
	   if (this.htmlElement.id=='cosDelete') {
	   this.htmlElement.className="cosdel_full";
	   }
   },

   hideHover: function() {
	   if (this.htmlElement.id=='cosDelete') {
       this.htmlElement.className="cosdel_empty";
	   }
   },

   accept: function(draggableObjects) {

      n = draggableObjects.length;
      for ( var i = 0 ; i < n ; i++ ) {
	  if (this.htmlElement.id=='cosDelete' && draggableObjects[i].name.indexOf('imgdel_')!=-1) {
      cos_delete(draggableObjects[i].name.replace('imgdel_',''));
	  }
	  
	  if (this.htmlElement.id=='compareZone' && draggableObjects[i].name.indexOf('imgcompare_')!=-1) {
      addCompare(draggableObjects[i].name);
	
	  }	  
	  
	  if (draggableObjects[i].name.indexOf('img_')!=-1) {
	  cumpar(draggableObjects[i].name.replace('img_',''));
	  }
	  
	  
	  if (draggableObjects[i].name.indexOf('imgcompare_')!=-1 && this.htmlElement.id=='cos') {
	  cumpar(draggableObjects[i].name.replace('imgcompare_',''));
	  }	  
	  
	  if (draggableObjects[i].name.indexOf('imgpromo_')!=-1) {
	  cumpar(draggableObjects[i].name.replace('imgpromo_',''));
	  }
	  }
   },

   canAccept: function(draggableObjects) {
      for ( var i = 0 ; i < draggableObjects.length ; i++ ) {
         if ( draggableObjects[i].name.indexOf('imgdel_')!=-1 && (this.htmlElement.id=='cos' || this.htmlElement.id=='compareZone'))
            return false;
         if ( draggableObjects[i].name.indexOf('imgdel_')==-1 && this.htmlElement.id=='cosDelete')
            return false;
		if ( $(draggableObjects[i].name).parentNode.className.indexOf('alpha_normal')==-1 && this.htmlElement.id=='compareZone')
            return false;
      }
      return true;
   }
} );

function cumpar(id_produs) {
var	param='id_prod='+id_produs;
param+='&action=add';
cosConn.openLink('cos.php',param,'post','cosContinut','','Se incarca...');	
}

function cos_delete(id_produs) {
var	param='id_prod='+id_produs;
param+='&action=delete';
cosConn.openLink('cos.php',param,'post','cosContinut','','Se incarca...');	
}

function cos_clean() {
var param='action=clean';
cosConn.openLink('cos.php',param,'post','cosContinut','','Se incarca...');	
}

function cos_refresh()
{
var	param='action=refresh';
cosConn.openLink('cos.php',param,'post','cosContinut','','Se incarca...');
}

function addCompare(nume) {
 if(VECTOR_COMPARA.length<3)
	  {
			var l=VECTOR_COMPARA.length;
			var OK=true;
			for (var t=0;t<l;t++)
			if   (VECTOR_COMPARA[t] && VECTOR_COMPARA[t].sursa==$(nume).src) {OK=false;break;}
			if (OK)
			{
			VECTOR_COMPARA[l]=new Object();
			VECTOR_COMPARA[l].sursa=$(nume).src;
			VECTOR_COMPARA[l].id=nume.replace('imgcompare_','')
			VECTOR_COMPARA[l].img='<img src="'+$(nume).src+'" width=50 height=50 style="margin-left:10px">';
			new Insertion.Top('compareZone','<img src="'+$(nume).src+'"  width=50 height=50 style="margin-left:10px">')
			}
			if (VECTOR_COMPARA.length>1) {
			$('compara').disabled = false;
			}
	  }
	  else {
	  $('compara').disabled = true;
	  }
}

function comparaDZ_clean() {
if (VECTOR_COMPARA.length>0) {	
VECTOR_COMPARA=new Array();
if ($('compareZone')) $('compareZone').innerHTML="";
}
}

function comparePage(){
	if (VECTOR_COMPARA.length>1) {
	$('compareZone').style.display = 'none';
	$('nb_prod').style.display = 'none';
	$('back').style.display = 'block';
	var xConn=CONNEXION('cautare_avansata');
	var	param ='';
	for (var t=0;t<VECTOR_COMPARA.length;t++)
		if (VECTOR_COMPARA[t]) {
		param +='&p'+(t+1)+'='+VECTOR_COMPARA[t].id;
		}
	xConn.openLink('compara.php',param,'post','cautare_avansata','undefined','&nbsp;Loading...');		
	}
}