function IdeaCompare(maxCompareCount) {
	this.MAX_COMPARE_PRODUCT_COUNT = maxCompareCount;
	this.infoGroupId = 0;
	this.products = new Array();
	this.load();
}

IdeaCompare.prototype.addToCompare = function(productElement) {
	if(this.products.length == this.MAX_COMPARE_PRODUCT_COUNT) {
		// buraya alert falan verdirilebilir.
		return false;
	}
	
	pid = parseInt(productElement.getAttribute('pid'));
	groupId = parseInt(productElement.getAttribute('groupid'));
	
	if(isNaN(pid) || isNaN(groupId) || pid<=0 || groupId<=0) {
		return false;
	}
	if(this.infoGroupId!=0 && groupId!=this.infoGroupId) {
		// buraya alert falan verdirilebilir.
		return false;
	}
	
	this.infoGroupId = groupId;
	
	// pid in products?
	for(var i=0;i<this.products.length;i++) {
		if(this.products[i] == pid) {
			// buraya alert falan verdirilebilir.
			return false;
		}
	}
	
	this.products[this.products.length] = pid;
	this.updateCookie();
	this.updateComparePanel();
	//alert('Ürün kıyas listenize eklenmiştir');
}

IdeaCompare.prototype.hasProduct = function(productId) {
	for(var i=0;i<this.products.length;i++) {
		if(this.products[i] == productId) {
			return true;
		}
	}
	return false;
}

IdeaCompare.prototype.updateComparePanel = function() {
	if(this.products.length != 0) {
		var inputs=document.getElementsByTagName('input');
		var elementId='';
		for(var i=0;i<inputs.length;i++) {
			pid = parseInt(inputs[i].getAttribute('pid'));
			groupId = parseInt(inputs[i].getAttribute('groupid'));
			if(isNaN(pid) || isNaN(groupId) || pid<=0 || groupId<=0) {
				continue;
			}
			elementId=inputs[i].getAttribute('id');
			if(groupId != this.infoGroupId) {
				$(elementId).style.visibility='hidden';
				$(this.nextElementId($(elementId))).style.visibility='hidden';
			}
			$(elementId).checked = this.hasProduct(pid);
		}
	} else {
		var inputs=document.getElementsByTagName('input');
		var elementId='';
		for(var i=0;i<inputs.length;i++) {
			pid = parseInt(inputs[i].getAttribute('pid'));
			groupId = parseInt(inputs[i].getAttribute('groupid'));
			if(isNaN(pid) || isNaN(groupId) || pid<=0 || groupId<=0) {
				continue;
			}
			elementId=inputs[i].getAttribute('id');
			if(groupId != this.infoGroupId) {
				$(elementId).style.visibility='visible';
				$(this.nextElementId($(elementId))).style.visibility='visible';
			}
			$(elementId).checked = this.hasProduct(pid);
		}
	}
}

IdeaCompare.prototype.nextElementId = function(element) {
	if(document.all) {
		var parent = element.parentNode;
		var children = parent.childNodes;
		for(var i=0;i<children.length;i++) {
			if(children[i] == element) {
				if(i<children.length-2) {
					return children[i+2].getAttribute('id');
				}
				return false;
			}
		}
		return false;
	} else {
		return element.nextSibling.nextSibling.getAttribute('id');
	}
}

IdeaCompare.prototype.removeFromCompare = function(productElement) {
	var pid = parseInt(productElement.getAttribute('pid'));
	if(isNaN(pid) || pid<=0) {
		return false;
	}
	var index=0;
	while(index<this.products.length && this.products[index]!=pid) {
		index++;
	}
	if(index<this.products.length) {
		if(this.products.length>1) {
			for(var i=index;i<this.products.length-1;i++) {
				this.products[i] = this.products[i+1];
			}
			this.products.length = this.products.length-1;
		} else {
			this.products = new Array();
			this.infoGroupId = 0;
		}
	}
	this.updateCookie();
	this.updateComparePanel();
}

IdeaCompare.prototype.load = function() {
	this.products = this.getCookie('IdeaCompareProducts').split(',');
	this.infoGroupId = parseInt(this.getCookie('IdeaCompareGroupId').split(','));
	if(this.products.length>this.MAX_COMPARE_PRODUCT_COUNT || isNaN(this.infoGroupId) || this.infoGroupId<=0) {
		this.products = new Array();
		this.infoGroupId = 0;
	}
}

IdeaCompare.prototype.updateCookie = function() {
	this.setCookie('IdeaCompareProducts',this.products.join(','));
	this.setCookie('IdeaCompareGroupId',this.infoGroupId);
}

IdeaCompare.prototype.getCookie = function(name) {
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + 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 "";
}

IdeaCompare.prototype.setCookie = function(name,value) {
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	document.cookie = name+'='+value+'; expires='+date.toGMTString()+'; path=/';
}
