// JavaScript Document

spj.include('client.js');
spj.register('ajax');
spj.include('menus.js');
spj.include('hormenu.js');
spj.include('dbg.js');


var divImagesOpened = new Array();
var hguid = 1;
var cartBox;
var userBar;
var hMenu;

function MessageBox(message,title,width,height,icon)
{
	
	this.width = width ? width : 400;
	this.height = height ? height : 400;
	
	this.iconsDir = '/images/layout/08/elements';
	
	this.table = document.createElement('table');
	this.table.id = 'table_' + hguid++;
	this.table.style.width = this.width+"px";
	
	var tbody = document.createElement('tbody');
	this.table.appendChild(tbody);
	
	trTitle = document.createElement('tr');
	trMessage = document.createElement('tr');
	tbody.appendChild(trTitle);
	tbody.appendChild(trMessage);
	
	tdTitle = document.createElement('td');
	//tdTitle.style.width="100%";
	
	
	var tdIcon = document.createElement('td');
	tdIcon.rowSpan = 2;
	
	if(!icon){
		var icon = new Image();
		icon.src = this.iconsDir+'/notes.png';
	}
	else{
		if(typeof(icon)=='string'){
			var iconSrc = icon;
			var icon = new Image();
			if(iconSrc.indexOf('/')){
				icon.src = iconSrc;
			}
			else{
				icon.src = this.iconsDir+'/'+iconSrc
			}	
		}
		
	}
	this.icon = icon;
	tdIcon.appendChild(this.icon);
	
	tdMessage = document.createElement('td');
	
	trTitle.appendChild(tdIcon);
	trTitle.appendChild(tdTitle);
	trMessage.appendChild(tdMessage);
	
	//alert('message: '+message);
	
	var messageNode = null;
	if(typeof(message) == 'string'){
		//var msgRegExp = new RegExp('\n','i');
		//var messageHTML = message.replace(msgRegExp,'<br />');	
		var messageHTML = message;	
		//var messageHTML = 'TRALALA';	
		//alert('messageHTML: '+messageHTML);
		//messageNode = document.createTextNode(messageHTML);
		//messageNode = document.createTextNode('');
		var messageNode = document.createElement('div');
		//messageNode.appendChild(document.createTextNode(messageHTML));
		messageNode.innerHTML = '<p style="font-family:Verdana, Arial, Helvetica, sans-serif;">'+messageHTML+'</p>';
	}
	else{
		messageNode = message;
	}
	
	if(typeof(title) == 'string'){
		titleNode = document.createTextNode(title);
	}
	else{
		titleNode = title;	
	}
	
	tdTitle.appendChild(titleNode);
	tdTitle.className = 'listBitTitle';
	tdMessage.appendChild(messageNode);
	
	//document.body.appendChild(this.table);
	
	this.popUpWindow = new PopUpWindow(this.table,this.width,this.height);
	
	
	this.open = function()
	{
		this.popUpWindow.open();
	};
	this.close = function()
	{
		this.popUpWindow.close();	
	};
	
	
}


function NoteEditBox(text,width,height)
{
	this.text = text;
	
	this.appendTo = null;
	
	//var functionSave = null;
	
	this.width = width ? width : 300;
	this.height = height ? height : 400;
	
	
	this.save = function()
	{
		if(this.saveFunction)
		{
			this.saveFunction(this.text);	
		}
	}
	
	this.div = document.createElement('div');
	this.div.style.width = this.width+"px";
	var table = document.createElement('table');
	this.div.appendChild(table);
		
	var tbody = document.createElement('tbody');
	table.appendChild(tbody);
	
	var trText = document.createElement('tr');
	tbody.appendChild(trText);
	
	var tdText = document.createElement('td');
	trText.appendChild(tdText);
	
	this.textArea = document.createElement('textarea');
	this.textArea.value = this.text;
	tdText.appendChild(this.textArea);
	//tdText.appendChild(document.createTextNode('TRALALALALA'));
	
	
	
	var trControls = document.createElement('tr');
	tbody.appendChild(trControls);
	
	var tdControls = document.createElement('td');
	trControls.appendChild(tdControls);
	
	
	this.btSave = document.createElement('button');
	tdControls.appendChild(this.btSave);
	
	var gText = this.text;
	var gSave = this.save;
	
	this.btSave.onclick = function(){
		var evt = spj.event.get();
		gSave(gText);
	};
	
	
	this.open = function()
	{
		var mousePosition = spj.mouse.position();
		if(this.appendTo){
			this.appendTo.appendChild(this.div);	
		}
		else{
			document.appendChild(this.div);
		}
	};
	
	
}




function EditBox()
{
	
}



function PopUpWindow(content,width,height)
{
	this.width = width ? width : 400;
	this.height = height ? height : 400;
	
	var wWidth = spj.window.innerWidth();
	var wHeight = spj.window.innerHeight();
	
	//alert('width: '+wWidth+'\nHeight: '+wHeight); 
	
	
	var posLeft = (wWidth-this.width)/2;
	var posTop  = ((wHeight-this.height)/2)+spj.window.scrollY();
	//alert('posTop: '+posTop);
	//alert("posLeft: "+posLeft+" posTop: "+posTop);
	//alert('scrollY: '+spj.window.scrollY());
	this.div = document.createElement('div');
	this.div.id = "popupWindow_" + hguid++;
	this.div.style.top  = posTop+"px";
	this.div.style.left = posLeft+"px";
	this.div.style.position = "absolute";
	this.div.style.width = this.width+"px";
	this.div.style.backgroundColor = "#FFFFFF";
	this.div.style.borderWidth = "1px 1px 1px 1px";
	this.div.style.borderStyle = "solid";
	table = document.createElement('table');
	//this.div.appendChild(table);
	
	
	//alert("div Width: "+this.div.style.width);
	
	
	tbody = document.createElement('tbody');
	table.appendChild(tbody);
	
	tr = document.createElement('tr');
	tbody.appendChild(tr);
	
	td = document.createElement('td');
	tr.appendChild(td);
	
	//td.appendChild( document.createElement('H1').appendChild(document.createTextNode('TRALALALALALALALA'))    )  ;
	td.appendChild( content )  ;
	
	//===============================================
	
	var frameTable = document.createElement('table');
	frameTable.style.width = '100%';
	
	frameTBody = document.createElement('tbody');
	frameTable.appendChild(frameTBody);
	
	frameTitleTr = document.createElement('tr');
	frameTBody.appendChild(frameTitleTr);
	
	frameTitleTd = document.createElement('td');
	frameTitleTd.style.backgroundColor = "#EEEEEE";
	frameTitleTd.style.textAlign = "right";
	frameTitleTd.className = "titleMedium";
	frameTitleTr.appendChild(frameTitleTd);
	
	frameContentTr = document.createElement('tr');
	frameTBody.appendChild(frameContentTr);
	
	frameContentTd = document.createElement('td');
	frameContentTr.appendChild(frameContentTd);
	
	closeLink = document.createElement('a');
	//var cPopUpWindow = this;
	closeLink.href = "javascript:";
	var PopUpWindowID = this.div.id;
	closeLink.onclick = function()
	{
		spj.document.removeChildByID(PopUpWindowID);
	}
	frameTitleTd.appendChild(closeLink);
		
	closeLinkText = document.createTextNode("x");
	closeLink.appendChild(closeLinkText);
	
	frameContentTd.appendChild(table);
	this.div.appendChild(frameTable);
	
	
	this.open = function()
	{
		document.body.appendChild(this.div);	
	};
	
	this.close = function()
	{
		document.body.removeChild(this.div);
	};
	
}


function toggleDisplay(elem)
{
	//alert('elem id: '+elem);
	if(typeof(elem)=='object'){
		var elem = elem;
	}
	else{
		var elem = document.getElementById(elem);
	}
	elem.style.display = elem.style.display=='none' ? 'block' : 'none';
}


function popUpMessage_doRegister()
{
	//alert('into popup!');
	
	var evt = spj.event.get();
	var mouseX = evt.clientX;
	var mouseY = evt.clientY;
	
	var message = 'Αυτή η λειτουργία απαιτεί να έχετε εισαχθεί στο σύστημα, εάν είστε μέλος. Εάν δεν είστε ήδη, παρακαλούμε εγγραφείτε ';
	var messageLink = document.createElement('a');
	messageLink.appendChild( document.createTextNode('[εδώ]') );
	messageLink.href = "/register/";
	
	var messageText = document.createTextNode(message);
	var messageP = document.createElement('p');
	
	messageP.style.fontSize = "12px";
	messageP.appendChild(messageText);
	messageP.appendChild(messageLink);
	
	var title = "Απαιτείται η Προσοχή σας!";
	//alert('eoeoeoeoeoeoeoeo');
	var msgBox = new MessageBox(title,messageP,null,null);
	msgBox.open();
	//alert('orde');
	
	
}

function closeImage(id)
{
	document.body.removeChild(document.getElementById(id));
}


function viewImage(thumb)
{
	var divWidth = 460;
	var thumbID = thumb.id;
	//alert('thumb id: '+thumb.id);
	var idNumber  = thumbID.substr(thumbID.lastIndexOf("_")+1);

        var yOffset = spj.navigator.isIE ?  document.documentElement.scrollTop : window.pageYOffset;


	//alert("id is: "+thumbID+"\idNumber: "+idNumber);
	if(document.getElementById("imageTitle_"+idNumber)!='undefined' && document.getElementById("imageTitle_"+idNumber)){
		var thumbTitle = document.getElementById("imageTitle_"+idNumber).innerHTML;
	}
	else{
		//var thumbTitle = document.createTextNode("").innerHTML;	
		var thumbTitle = "";	
	}
	var divImage = document.createElement("div");
	//divColor.href = 'javascript:closeColor("'+divColor.id+'")';
	divImage.id = "divColor_"+idNumber;
	divImage.style.width = divWidth;
	divImage.style.position = "absolute";
	//var windowWidth = getWindowWidth();
	var windowWidth = spj.window.innerWidth();
	//var posLeft = ((windowWidth-divWidth)/2)-(divWidth/2);
	//alert('divWidth: '+divWidth);
	var posLeft = (windowWidth-divWidth)/2;
	//alert('window width: '+windowWidth+', divWidth: '+divWidth+', posLeft: '+posLeft);
	//alert("posLeft: "+posLeft);
	//divColor.style.left  = String(posLeft+"px");
	var posLeftString = String(posLeft+"px");
	divImage.style.top = 20+yOffset+"px";
	divImage.style.left = posLeftString;
	//divColor.style.setExpression( "left",posLeftString );
	//divColor.style.display ="none";
	var table = document.createElement("table");
	table.style.backgroundColor = "#FFFFFF";
	table.style.border="1px solid black";
	var tBody = document.createElement("tbody");
	table.appendChild(tBody);
	var trImage = document.createElement("tr");
	tBody.appendChild(trImage);
	var trText  = document.createElement("tr");
	tBody.appendChild(trText);
	var trOptions = document.createElement("tr");
	tBody.appendChild(trOptions);
	
	
	var tdImage = document.createElement("td");
	var tdText  = document.createElement("td");
	tdText.style.textAlign = "center";
	tdText.className = "listBitTitle";
	var tdOptions = document.createElement("td");
	trOptions.appendChild(tdOptions);
	
	var optionsTable = document.createElement("table");
	optionsTable.style.width = "100%";
	optionsTable.style.backgroundColor = "#EEEEEE";
	tdOptions.appendChild(optionsTable);
	var optionsTBody = document.createElement("tbody");
	optionsTable.appendChild(optionsTBody);
	var trOpOptions = document.createElement("tr");
	optionsTBody.appendChild(trOpOptions);
	var tdOptionClose = document.createElement("td");
	tdOptionClose.style.textAlign = "left";
	trOpOptions.appendChild(tdOptionClose);
	var tdOptionChoose = document.createElement("td");
	tdOptionChoose.style.textAlign = "right";
	trOpOptions.appendChild(tdOptionChoose);
	var optionCloseLink = document.createElement("a");
	optionCloseLink.className = "titleSmall"
	var optionCloseLinkText = document.createTextNode("Κλείσιμο παραθύρου");
	optionCloseLink.appendChild(optionCloseLinkText);
	optionCloseLink.href = 'javascript:closeImage("'+divImage.id+'")';
	//optionCloseLink.onclick="closeColor('+divColor.id+')";
	//optionCloseLink.text = "Κλείσιμο Παραθύρου";
	//var optionClose = document.createTextNode( '<a href="#" onclick="closeColor('+divColor.id+')" >Κλείσιμο Παραθύρου</a>'	);
	
	
	///var optionChooseLink = document.createElement("a");
	///optionChooseLink.className = "titleMedium";
	///optionChooseLink.appendChild(document.createTextNode("Επιλογή αυτού του χρώματος για αγορά"));
	///optionChooseLink.href = 'javascript:chooseColor("'+divColor.id+'")';
	tdOptionClose.appendChild(optionCloseLink);
	///tdOptionChoose.appendChild(optionChooseLink);
		
	
	var rex = new RegExp("th_","i");
	var imageSource = thumb.src.replace(rex,"");
	
	var image = document.createElement("img");
	image.src = imageSource;
	
	//alert('imageSource: '+imageSource);
	//alert('image width: '+image.style.width);
	//get the image width:
	var imageWidth = divWidth;
	imgLink = spj.ajax.getRequestObject();
	imgLink.onreadystatechange  = function()
	{
		//alert('into onreadystatechange');
		var evt = spj.event.get();
		var target = spj.event.getTarget();
		if(target.readyState == 4) {
	        if(target.status == 200)
	        {
	            // image exists
	            if(image.style.width > divWidth){
	            	posLeft = (windowWidth-image.style.width)/2;
	            	divImage.style.left = posLeft+"px";
	            }
	        }
	        else
	        {
	            // image doesn't exist
	            nB=true;
	        }
   		}
		
	}
	imgLink.open("head",imageSource,false);
	imgLink.send(null);
	
	
	
	image.style.border = "0px";
	var imageLink = document.createElement("a");
	imageLink.href = 'javascript:closeImage("'+divImage.id+'")';
	imageLink.alt = "Κλείσιμο Παραθύρου";
	imageLink.appendChild(image);
	
	var textSpan = document.createElement("span");
	var text = document.createTextNode(thumbTitle);
	
	textSpan.appendChild(text);
	
	//=====================================
	
	tdImage.appendChild(imageLink);
	tdText.appendChild(textSpan);
	
	trImage.appendChild(tdImage);
	trText.appendChild(tdText);
	
	//table.appendChild(trImage);
	//table.appendChild(trText);
	
	divImage.appendChild(table);
	
	
	if(divImagesOpened.length>0){
		for(var i=0; i<divImagesOpened.length; i++){
			var cImageOpened = document.getElementById(divImagesOpened[i]);
			if(cImageOpened){
				document.body.removeChild( cImageOpened );
			}
		}
		divImagesOpend = new Array();
	}
	document.body.appendChild(divImage);
	divImagesOpened.push(divImage.id);
}



function in_array(needle, haystack) {
	var n = haystack.length;
	for (var i=0; i<n; i++) {
		if (haystack[i]==needle) {
			return true;
		}
	}
	return false;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function cursorSetStyle(evt,cursorStyle)
{
	var event = spj.event.get();
	var target = spj.event.getTarget(event);
	target.style.cursor = cursorStyle;
}


function elementTween(element)
{
	spj.display.fx.TweenResizeHeight.tweenElementCached(element);
}


function page_print()
{
	var content = null;
	if(arguments.length>0){
		content = arguments[0];
	}
	else{
		//content = $('#page-subject').html();
		content = document.getElementById('page-subject').innerHTML;
	}
	
	//alert('content: '+content);
	//alert('host: '+location.host);
	var prWindow = window.open('http://'+location.host+'/actions/print',null,'scrollbars=1,resizable=1,width=1010',false);
	var prDoc = prWindow.document;
	
	/*
	if(spj.navigator.isIE){
		
		//alert('IS IE');
		//var subject = prWindow.document.getElementById('page-subject');
		//alert(subject.toString());
		//alert('page-subject constructor: '+subject.constructor );
		
		//subject.innerHTML = content;
	}
	else{
		alert('is mozilla!');
		prWindow.onload = function()
		{
			alert('into onload');
			
			var cDoc = prWindow.document;
			
			var subject = cDoc.getElementById('page-subject');
			subject.innerHTML = content;
			
			var npElems = cDoc.getElementsByClassName('print-no');
			if(npElems.length>0){
				//alert('print-no elems: '+npElems.length);
				for(var i=0; i<npElems.length; i++){
					//alert('removing element');
					//npElems[i].parentNode.removeChild(npElems[i]);
					spj.document.removeChild(npElems[i]);
				}
				
			}
			this.print();
			
			//prWindow.document.getElementById('page-subject').innerHTML = content;
			
			
		};
	}
	*/
	/*
	$(prDoc).ready(function(){
		$(this).find('#page-subject').html(content);
		//$(this).find('.print-no').remove();
		prWindow.print();
	});
	*/
	
	//alert('window subject: '+$(prWindow).find('#page-subject').html());
	/*
	$(prWindow).find('#page-subject').html(content);
	
	$(prWindow).find('.print-no').remove();
	
	prWindow.print();
	*/
	return false;
	
};



function app_init()
{
	//alert('into init()');
	//alert(document.getElementById('cartBox'));
	
	
	
	$('.clickable').mouseover(function(){
		$(this).css('cursor','pointer');		
	});
	
	$('.clickable').mouseout(function(){
		$(this).css('cursor','auto');
	});
	
	//find element link for page print:
	$('.button-print').click(function(){
		page_print();
	});
	
	
	//alert('eeeee');
	$("a.newWindow").live('click',function(e){
		//alert('New window');
		e.preventDefault();
		///window.open('http://'+document.domain+$(this).attr('href'));
		window.open($(this).attr('href'));
		//return false;	
	});
	
	
	var hMenu = new HMenu('shops');
	hMenu.init();
	//var hMenuDisplayFX = new spj.display.fx.TweenResizeHeight();
	//hMenu.setPopUpsTween('spj.display.fx.TweenResizeHeight');
	spj.storage.add('hMenu',hMenu);
	//alert('EEEE');
	if(document.getElementById('cartBox')){
		//alert('there is cartBox');
		cartBox = new CartBox();
		cartBox.init();
		//manage cartbox display based on specific areas:
		var loc = location.href;
		var cbRex = /order\//;
		if(cbRex.test(loc)){
			document.getElementById('cartBox').style.display = 'none';
		}
		
		//CartBox_Init();
		//userBar = new UserBar();
	}
	
	
	
	
	//dbg_init();
	
}

spj.window.Loader.add('app_init');
spj.window.Loader.load();

