﻿	addNamespace("KY.Web.DynaMarquee");

	KY.Web.DynaMarquee = Class.create();
	
	Object.extend(KY.Web.DynaMarquee.prototype, {
		objXML: null,
		state: null,
		books: null,
		count: 0,
		display:5,
		step: 5,
		pos: 0,
		divpos: 0,
		timer: null,
		timer2: -1,
		hoverdiv: null,
		slideTimer: null,
		slideDirection: 1,
		isRunning: false,
		timerInterval: 10000,
		r: null,
		w: 0,
		fulldiv: null,
		fulllink: null,
		ondivmouseover: function(e) {
			if (this.timer2 != -1) { clearTimeout(this.timer2); }

			this.div.style.display='inline';
		},
		ondivmouseout: function(e) {
			this.timer2 = setTimeout(this.divhide.bind(this),1000);
		},
		divhide: function(e) {
			this.div.style.display='none';
			this.timer2 = -1;
		},
		onmouseover: function(e) {
			if(this.timer != null) clearTimeout(this.timer); this.timer = null;
		},
		onmouseout: function(e) {
			if(this.timer != null) clearInterval(this.timer);
			this.timer = setInterval(this.onnext.bind(this), this.timerInterval);
		},
		onnext: function(e) {
			if (this.isRunning) { return; }
			this.isRunning = true;
			this.slideDirection = 1;
			var html = "<TABLE align=\"center\" width=\"100%\" border=\"0\" cellspacing=\"6\">" +
			"<TR align=\"center\">";
			var temppos = this.pos;
			if (temppos >= this.count-1) { temppos = 0; }
			for (i = 0; i < this.display; i++) {
				var price = "";
				switch (gsparabirimi) {
					case 2:  price = this.books[temppos].kyprice2 + " " + gsparabirimitext; break;
					case 5:  price = this.books[temppos].kyprice5 + " " + gsparabirimitext; break;
					case 6:  price = this.books[temppos].kyprice6 + " " + gsparabirimitext; break;
					case 7:  price = this.books[temppos].kyprice7 + " " + gsparabirimitext; break;
				}
				html += "<TD valign=\"top\">" +
							"<TABLE background=\"/resim/pro_bg.gif\" height=\"180px\" width=\"100%\">" +
								"<TR>" +
									"<TD width=\"94px\" align=\"center\" valign=\"top\" height=\"180px\">" +
									"<a href=\"/kitap/default.asp?id=" + this.books[temppos].id + "\"><img id=\"" + this.ele.id + "_ih_" + this.timer + "_" + this.books[temppos].id + "\" border=\"0\" src=\"/getimage1.asp?resimkod=" + this.books[temppos].id + "&boyut=60\" width=\"60px\"><BR>" +
									"<span class=\"normalbold\">" + this.books[temppos].name + "</span></a><BR>" + this.books[temppos].writer + "<BR>" + 
									"<span class=\"normalboldrenkli\">" + price + "</span>" +
									"</TD>" +
								"</TR>" +
							"</TABLE>" +
						"</TD>";
				activateHoverMenu(this.ele.id + "_ih_" + this.timer + "_" + this.books[temppos].id, this.books[temppos].id, this.div.id);
				if (temppos == this.count-1) { temppos = 0; }
				else { temppos++; }
			}
	
			for(i=0; i<5; i++) {
				if (this.pos + 1 > this.count-1) { this.pos = 0;}
				else { this.pos++; }
			}
	
			html += "</TR></TABLE>";
			
			document.getElementById(this.state).innerHTML = html;
			 
			this.showSlide();
		},
		onprev: function(e) {
			if (this.isRunning) { return; }
			this.isRunning = true;
			this.slideDirection = 0;
			var html = "<TABLE align=\"center\" width=\"100%\" border=\"0\" cellspacing=\"6\">" +
			"<TR align=\"center\">";
			var temppos = this.pos;
			for(i=0; i<10; i++) {
				if (temppos - 1 < 0) { temppos = this.count-1;}
				else { temppos--; }
			}
			var temppos1 = temppos;

			for (i = 0; i < this.display; i++) {
				var price = "";
				switch (gsparabirimi) {
					case 2:  price = this.books[temppos].kyprice2 + " " + gsparabirimitext; break;
					case 5:  price = this.books[temppos].kyprice5 + " " + gsparabirimitext; break;
					case 6:  price = this.books[temppos].kyprice6 + " " + gsparabirimitext; break;
					case 7:  price = this.books[temppos].kyprice7 + " " + gsparabirimitext; break;
				}
				html += "<TD valign=\"top\">" +
							"<TABLE background=\"/resim/pro_bg.gif\" height=\"180px\" width=\"100%\">" +
								"<TR>" +
									"<TD width=\"94px\" align=\"center\" valign=\"top\" height=\"180px\">" +
									"<a href=\"/kitap/default.asp?id=" + this.books[temppos].id + "\"><img id=\"" + this.ele.id + "_ih_" + this.timer + "_" + this.books[temppos].id + "\" border=\"0\" src=\"/getimage1.asp?resimkod=" + this.books[temppos].id + "&boyut=60\" width=\"60px\"><BR>" +
									"<span class=\"normalbold\">" + this.books[temppos].name + "</span></a><BR>" + this.books[temppos].writer + "<BR>" + 
									"<span class=\"normalboldrenkli\">" + price + "</span>" +
									"</TD>" +
								"</TR>" +
							"</TABLE>" +
						"</TD>";
				activateHoverMenu(this.ele.id + "_ih_" + this.timer + "_" + this.books[temppos].id, this.books[temppos].id, this.div.id);
				if (temppos == this.count-1) { temppos = 0; }
				else { temppos++; }
			}
			this.pos = temppos1;
			for(i=0; i<5; i++) {
				if (this.pos + 1 > this.count-1) { this.pos = 0;}
				else { this.pos++; }
			}
	
			html += "</TR></TABLE>";
			
			document.getElementById(this.state).innerHTML = html;
			 
			this.showSlide();
		},
		onfullshow: function(e) {
			if (this.fulldiv.style.display=='none')
			{
				this.fulldiv.style.display='inline';
				var r = MS.Position.getBounds(this.fulllink);
				r.left -=160;
				r.top += 30;
				MS.Position.setBounds(this.fulldiv, r);
			}
			else
			{
				this.fulldiv.style.display='none';
			}
		},
		showSlide: function() {
			var curObj = document.getElementById(this.state); 
			var nextObj = document.getElementById((this.state == this.ele.id + "_div1")? this.ele.id + "_div2" : this.ele.id + "_div1"); 
			if (this.slideDirection == 1) { this.curpos = this.w; }
			else { this.curpos = -1 * this.w; }
			curObj.style.left = this.curpos + "px";
			curObj.style.yIndex++;
			curObj.style.display = nextObj.style.display = "inline";

			if (curObj.filters) {
				curObj.filters.alpha.opacity = nextObj.filters.alpha.opacity = 40;
			}
			else if (document.getElementById(this.state).style.MozOpacity) {			
				curObj.style.MozOpacity = nextObj.style.MozOpacity =0.4;
			}
			this.timerSlide = setInterval(this.moveSlide.bind(this), 50);
		},
		moveSlide: function() {
			var curObj = document.getElementById(this.state); 
			var nextObj = document.getElementById((this.state == this.ele.id + "_div1")? this.ele.id + "_div2" : this.ele.id + "_div1"); 

			if (this.curpos > 0 && this.slideDirection == 1)
			{
				this.curpos = Math.max(this.curpos-25,0);
				curObj.style.left = this.curpos + "px";
			}
			else if (this.curpos < 0 && this.slideDirection == 0)
			{
				this.curpos = Math.min(this.curpos+25,0);
				curObj.style.left = this.curpos + "px";
			}
			else
			{
				clearInterval(this.timerSlide);
				if (curObj.filters) {
					curObj.filters.alpha.opacity = 100;
				}
				else if (curObj.style.MozOpacity) {
					curObj.style.MozOpacity = 1;
				}
				this.state = (this.state == this.ele.id + "_div1")? this.ele.id + "_div2" : this.ele.id + "_div1";
				nextObj.style.display = "none";
				this.isRunning = false;
			}
		},
		replacebookname: function(name) {
			var duzelt = name;
			duzelt = duzelt.replace("\n\r", "<br>");
			if (duzelt.indexOf("-") >= 0) 
			{ 
					if (duzelt.indexOf("- ") == 0) { duzelt = duzelt.replace("-","- "); }
			}
			if (duzelt.indexOf("/") >= 0) 
			{ 
					if (duzelt.indexOf("/ ") == 0) { duzelt = duzelt.replace("/","/ "); }
			}
			
			if (duzelt.length > 20)
			{
				if (duzelt.indexOf("&") >= 0) 
				{
					duzelt= duzelt.substring(0, duzelt.indexOf("&"));
				}
			}
			if (duzelt.indexOf("&") >= 0) 
			{
				duzelt = duzelt.replace("&", "<br>");
			}
			return duzelt;

		},
		ondata: function() {
			if(this.objXML.readyState == 4)
			{
				if(this.objXML.status != 200) { return; }
			}
			else 
			{
				return;
			}
	
			a = new Array();
					
			var response = this.objXML.responseXML.documentElement;
			var bs = response.getElementsByTagName("kitap");
			var fulldivHTML = "<table style=\"border: 2px solid #000080\" bgcolor=\"#EAEFF4\" border=\"0\" width="+ this.w + "px>";
			for (i = 0; i < bs.length ; i++) {
				if (bs[i] == null) { continue; }

				var b = bs[i];
				var nf = null;
				var field = null;
				var order;
				var bookid;
				var bookname = "";
				var bookwriter = "";
				var price = 0;
				var kyprice2 = 0;
				var kyprice5 = 0;
				var kyprice6 = 0;
				var kyprice7 = 0;

				//alert("i="+ i + "  " + i % 7);
				if (i % 7 == 0)
				{
					fulldivHTML += "<tr  border=\"0\">";
				}

				field = b.getElementsByTagName("sirano");
				order = field[0].firstChild.data;
				field = b.getElementsByTagName("kitapno");
				bookid = field[0].firstChild.data;
	
				if (b.getElementsByTagName("kitapadi") != null)
				{
					field=b.getElementsByTagName("kitapadi");
					if (field[0].firstChild != null) { bookname = field[0].firstChild.data; }
				}
				
				if (b.getElementsByTagName("yazar") != null)
				{
					field=b.getElementsByTagName("yazar");
					if (field[0].firstChild != null) 
					{ 
						bookwriter = field[0].firstChild.data;
						if (bookwriter .substring(bookwriter .length-1) == ",")
						{
							bookwriter = bookwriter .substring(0,bookwriter .length-1);
						}
					}
				}

				field=b.getElementsByTagName("kyfiyat2");
				nf = new NumberFormat(field[0].firstChild.data);
				nf.setPlaces(2);
				nf.setSeparators(true);
				kyprice2 = nf.toFormatted();

				field=b.getElementsByTagName("kyfiyat5");
				nf = new NumberFormat(field[0].firstChild.data);
				nf.setPlaces(2);
				nf.setSeparators(true);
				kyprice5 = nf.toFormatted();
				
				field=b.getElementsByTagName("kyfiyat6");
				nf = new NumberFormat(field[0].firstChild.data);
				nf.setPlaces(2);
				nf.setSeparators(true);
				kyprice6 = nf.toFormatted();

				field=b.getElementsByTagName("kyfiyat7");
				nf = new NumberFormat(field[0].firstChild.data);
				nf.setPlaces(2);
				nf.setSeparators(true);
				kyprice7 = nf.toFormatted();

				var book = new KY.Web.Book(i, bookid, bookname, bookwriter, "", price, kyprice2, kyprice5, kyprice6, kyprice7);
				a[i] = book;
				this.count = i + 1;
				
				switch (gsparabirimi) {
					case 2:  price = kyprice2 + " " + gsparabirimitext; break;
					case 5:  price = kyprice5 + " " + gsparabirimitext; break;
					case 6:  price = kyprice6 + " " + gsparabirimitext; break;
					case 7:  price = kyprice7 + " " + gsparabirimitext; break;
				}

				fulldivHTML += "<TD valign=\"top\" border=\"0\">" +
						"<table width=\"100%\" height=\"100px\" border=\"0\">" +
							"<tr>" +
								"<TD  border=\"0\" width=\"100px\" height=\"100px\" align=\"center\" valign=\"top\">" +
									"<a href=\"/kitap/default.asp?id=" + bookid + "\"><img id=\"forhover_cokilgifull_" + bookid + "\" border=\"0\" src=\"/getimage.asp?resimkod=" + bookid + "&boyut=40\" width=\"40px\"><BR>" +
									"<span class=\"normalbold\">" + this.replacebookname(bookname) + "</span></a><BR>" +
									"<span class=\"normalboldrenkli\">" + price + "</span><BR>" +
								"</TD>" +
							"</TR>" +
							"</TABLE>" +
						"</TD>"
											

				if (i % 7 == 6 && i != 0)
				{
					fulldivHTML += "</tr>"
				}

			}
			fulldivHTML += "<tr><td  border=\"0\" colspan=\"7\" align=\"right\" id=\"" + this.fulldiv.id + "-closebtn\" >kapat</td></tr>"
			fulldivHTML += "</table>"
			this.fulldiv.innerHTML = fulldivHTML;
			$(this.fulldiv.id + "-closebtn").style.cursor="pointer";
			addEvent($(this.fulldiv.id + "-closebtn"), "click", this.onfullshow.bind(this));
			
			this.books = a;
			if(this.timer != null) clearInterval(this.timer);
			this.onnext();
			this.timer = setInterval(this.onnext.bind(this), this.timerInterval);
		},
		
		initialize: function(objId, url, preBtnId, nextBtnId, interval, stepcount, w, h, linkID) {
			this.step = stepcount;
			this.timerInterval = interval;
			this.ele = $(objId);
			this.fulllink = $(linkID);

			var r = MS.Position.getBounds(this.ele);
			this.ele.innerHTML = "";
			var btnPrev = document.getElementById(preBtnId);
			var btnNext = document.getElementById(nextBtnId);

			btnPrev.style.cursor = "pointer";
			btnNext.style.cursor = "pointer";


			this.curpos = w;
			this.w = w;
			
			this.ele.innerHTML = '<div style="position:relative;width:'+w+'px;height:'+h+'px;overflow:hidden"><div id="'+objId+'_div1" style="position:absolute;background-color:#EAEFF4;width:'+w+'px;height:'+h+'px;left:'+w+'px;filter:alpha(opacity=40);-moz-opacity:0.4;"></div><div id="'+objId+'_div2" style="position:absolute;background-color:#EAEFF4;width:'+w+'px;height:'+h+'px;left:'+w+'px;filter:alpha(opacity=40);-moz-opacity:0.4;"></div></div>';
			this.div = document.createElement("div");
			this.div.id = this.ele.id + "_hoverdiv";
			this.div.style.display="none";
			document.body.appendChild(this.div);

			this.fulldiv = document.createElement("div");
			this.fulldiv.id = this.ele.id + "_fulldiv";
			this.fulldiv.style.display="none";
			this.fulldiv.width=w+"px";
			this.fulldiv.height="400px";

			document.body.appendChild(this.fulldiv);
			
			addEvent(this.div, "mouseover", this.ondivmouseover.bind(this));
			addEvent(this.div, "mouseout", this.ondivmouseout.bind(this));
			
			addEvent(this.ele, "mouseover", this.onmouseover.bind(this));
			addEvent(this.ele, "mouseout", this.onmouseout.bind(this));
			addEvent(btnPrev, "mouseover", this.onmouseover.bind(this));
			addEvent(btnPrev, "mouseout", this.onmouseout.bind(this));
			addEvent(btnPrev, "click", this.onprev.bind(this));
			addEvent(btnNext, "mouseover", this.onmouseover.bind(this));
			addEvent(btnNext, "mouseout", this.onmouseout.bind(this));
			addEvent(btnNext, "click", this.onnext.bind(this));
			addEvent(this.fulllink, "click", this.onfullshow.bind(this));
			this.fulllink.style.cursor = "pointer";

			this.state = this.ele.id + "_div1";
		
			this.objXML = null;

			if (window.XMLHttpRequest) {
				this.objXML = new XMLHttpRequest();
			}
			else if (window.ActiveXObject) {
				this.objXML = new ActiveXObject("Microsoft.XMLHTTP");
			}	
			
			if (this.objXML) {
				this.objXML.onreadystatechange = this.ondata.bind(this);
			}
			this.objXML.open("GET", window.location.protocol + "//" + window.location.hostname + url, true);
			DebugWindow("KY.Web.DynaMarquee:initialize", window.location.protocol + "//" + window.location.hostname + url)
			this.objXML.setRequestHeader("Accept-Charset", "iso-8859-9");
			this.objXML.setRequestHeader("Accept-Language", "tr");

			if(!MS.Browser.isIE) {
				this.objXML.setRequestHeader("Connection", "close");
			}
			this.objXML.send(null);
			
		}
	}, false);

	KY.Web.Book = Class.create();
	Object.extend(KY.Web.Book.prototype, {
		order: null,
		id: 0,
		name: "",
		writer: "",
		publisher: "",
		price: 0,
		kyprice2: 0,
		kyprice5: 0,
		kyprice6: 0,
		kyprice7: 0,
		discount: 0,
		initialize: function(orderId, pId, pName, pWriter, pPublisher, p, p2, p5, p6, p7) {
			this.order = orderId;
			this.id = pId;
			this.name = pName;
			this.writer = pWriter;
			this.publisher = pPublisher;
			this.price = p;
			this.kyprice2 = p2;
			this.kyprice5 = p5;
			this.kyprice6 = p6;
			this.kyprice7 = p7;
		}
	}, true);
	

