from ui import Button, RootPanel, Frame
import Window

from popups import MyDialog

def greet(sender):
    Window.alert("Hello, AJAX!")

from ui import Button, CheckBox, VerticalPanel, HorizontalPanel, HTML, DockPanel, HasAlignment, FlowPanel, HTMLPanel, MenuBar, MenuItem, ScrollPanel, Composite
#from Logger import Logger
import DOM

def makeLabel(caption):
    html = HTML(caption)
    html.setStyleName("ks-layouts-Label")
    return html

class Box(Composite):

    def __init__(self, txt, heading, style, boxwidth):

        self.p = VerticalPanel()
        self.p.setHorizontalAlignment(HasAlignment.ALIGN_CENTER)
        self.p.setVerticalAlignment(HasAlignment.ALIGN_TOP)

        self.p.add(makeLabel(heading))
        self.p.add(HTML(txt, true))
        self.p.setStyleName(style)
        self.p.setWidth(boxwidth)

        self.setWidget(self.p)

        self.dlg = None

    def add_button(self, text, parent):

        self.popup_button = Button(text, parent)
        self.p.add(self.popup_button)
        self.p.setCellHorizontalAlignment(self.popup_button, HasAlignment.ALIGN_RIGHT)
        self.p.setCellVerticalAlignment(self.popup_button, HasAlignment.ALIGN_BOTTOM)

    def show_dialog(self, dlgwidth):

        left = self.getAbsoluteLeft() + 10
        top = self.getAbsoluteTop() + 10
        width = Window.getClientWidth()
        height = Window.getClientHeight()

        if left + dlgwidth > width - 10:
            left = (width / 2) - (width - dlgwidth) / 2
        if left + dlgwidth > width - 10:
            left = 10
            dlgwidth = width - 20

        dlgheight = 500
        if top + dlgheight > height - 10:
            top = (height / 2) - (height - dlgheight) / 2
        if top + dlgheight > height - 10:
            top = 10
            dlgheight = height - 20

        self.dlg.set_width(dlgwidth)
        self.dlg.set_height(dlgheight)
        self.dlg.setPopupPosition(left, top)
        self.dlg.show()

    def onShow(self):
        pass

class Creativity(Box):

    def __init__(self, boxwidth):

        txt = "<ul class='nodec'>"
        txt += "<li /> <a href='http://allpoetry.com/poets/lkcl'>Allpoetry</a>"
        txt += "<li /><a href='./poems/'>Poems</a>"
        txt += "<li /> <a href='http://freemind.sf.net'>FreeMind</a> and "
        txt += "       <a href='http://leo.sf.net'>Leo</a>"
        txt += "<li /><a href='http://wiki.lkcl.net/Creativity'>Creative Wiki</a>"

        txt += "</ul>"

        Box.__init__(self, txt, 'Creativity', 'ks-creativitybox', boxwidth)

        self.dlg = MyDialog('Creativity', './creativity.html')

    def onShow(self):
        pass

class Technology(Box):

    def __init__(self, boxwidth):

        txt = "<ul class='nodec'>"
        #txt += "<li /> Network protocols"
        txt += "<li /> Secure E-Commerce"
        txt += "<li /> Secure Web hosting"
        txt += "<li /> Complex programming"
        txt += "<li /> Reverse-engineering"
        txt += "<li /> <a href='./amcaius'>Property Key Management</a>"
        txt += "</ul>"

        Box.__init__(self, txt, 'Technology', 'ks-technologybox', boxwidth)

        self.dlg = MyDialog('Technology', './technology.html')

    def onShow(self):
        pass

class Contact(Box):

    def __init__(self, boxwidth):

        txt = "<ul class='nodec'>"
        txt += "<li /><a href='mailto:lkcl@lkcl.net'>lkcl@lkcl.net</a>"
        txt += "<li />Tel: +44 20 8133 8223"
        txt += "<li />Mob: +44 78 66 77 00 79"
        txt += "<li /><a href='skype:lkcl..?chat'>Skype 'lkcl..'</a>"
        txt += "</ul>"

        Box.__init__(self, txt, 'Contact', 'ks-contactbox', boxwidth)

        self.dlg = MyDialog('Email', './output_email/email.html')

    def onShow(self):
        pass

class More(Box):

    def __init__(self, boxwidth):

        txt = "<ul class='nodec'>"
        txt += "<li /><a href='http://advogato.org/person/lkcl/'>Advogato.org</a>"
        txt += "<li /><a href='http://www.cb1.com/~lkcl/'>CB1 Home page</a>"
        txt += "<li /><a href='http://www.google.com/search?q=Luke+Kenneth+Casson+Leighton&ie=ISO-8859&hl=en&btnG=Google+Search'>Google Search</a>"
        txt += "<li /><a href='http://www.nabble.com/forum/Search.jtp?expand=y&user=1103'>Nabble Search</a>"
        txt += "</ul>"

        Box.__init__(self, txt, 'More...', 'ks-morebox', boxwidth)

    def onShow(self):
        pass

class Links(Box):

    def __init__(self, boxwidth):

        txt = "<ul class='nodec'>"
        txt += "<li /><a href='http://www.linkedin.com/in/lkclnet'>LinkedIn Profile</a>"
        txt += "<li /><a href='./cv.txt'>CV</a>"
        txt += "<li /><a href='./crypto/'>Cryptography</a>"
        txt += "<li /><a href='./reports/index.html'>Hardware Reports</a>"
        txt += "</ul>"

        Box.__init__(self, txt, 'Links', 'ks-linksbox', boxwidth)

    def onShow(self):
        pass

class Spirituality(Box):

    def __init__(self, boxwidth):

        txt = "<ul class='nodec'>"
        txt += "<li /><a href='http://enlightenment.com'>Enlightenment.com</a>"
        txt += "<li /><a href='http://www.iawwai.com'>IawWai</a>"
        txt += "<li /><a href='./articles/goethe_theory_of_knowledge'>Practical Theory of Knowledge</a>"

        txt += "</ul>"

        Box.__init__(self, txt, 'Spirituality', 'ks-spiritualitybox', boxwidth)

    def onShow(self):
        pass

class Fun(Box):

    def __init__(self, boxwidth):

        txt = "<ul class='nodec'>"
        txt += "<li /><a href='http://www.royalskatenight.nl'>Tue Den Haag Skating</a>"
        txt += "<li /><a href='http://www.fridaynightskate.nl'>Fri Amsterdaam Skating</a>"
        txt += "<li /><a href='http://www.skatemarathon.com/results/1999/m100.html'>New York 1999 100km</a>"
        txt += "<li /><a href='http://www.a2a.net/99result/86all.html'>Athens to Atlanta 99, 86m</a>"
        txt += "<li /><a href='http://evenementen.uitslagen.nl/2006/rotterdamonwheels/uitslag.php?on=3&ct=Man&p=2'>Rotterdam 2006 Marathon</a>"
        txt += "<li /><a href='http://shut-up-and-dance.nl'>Wild Parties</a>"
        txt += "</ul>"

        Box.__init__(self, txt, 'Fun', 'ks-funbox', boxwidth)

    def onShow(self):
        pass


    
class Layouts(Composite):

	def __init__(self):

		panel = VerticalPanel()
		panel.setSpacing(8)
		panel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER)
		panel.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE)
		
		self.panel = panel

		self.create_layout()
		self.setWidget(panel)
		self.setStyleName("ks-layouts")

		Window.addWindowResizeListener(self)

	def create_layout(self):

		self.creativity = Creativity("240px")
		self.creativity.add_button('More', self)
		self.technology = Technology("240px")
		self.technology.add_button('More', self)

		self.contact = Contact("240px")
		self.contact.add_button('Email', self)
		self.more = More("240px")
		self.links = Links("240px")

		self.fun = Fun("240px")
		self.spirituality = Spirituality("240px")

		width = Window.getClientWidth()

		if width > 800:

			self.ads = Frame("./ads.html")
			self.ads.setStyleName("ad-frame")
			toprow = HorizontalPanel()
			middlerow = HorizontalPanel()
			bottomrow = HorizontalPanel()

		elif width > 640:

			self.ads = Frame("./ads_shorter.html")
			self.ads.setStyleName("ad-frame-shorter")
			toprow = HorizontalPanel()
			middlerow = VerticalPanel()
			bottomrow = HorizontalPanel()

		else:

			self.ads = Frame("./ads_short.html")
			self.ads.setStyleName("ad-frame-short")
			toprow = VerticalPanel()
			middlerow = VerticalPanel()
			bottomrow = VerticalPanel()

		self.toprow = toprow
		self.middlerow = middlerow
		self.bottomrow = bottomrow

		toprow.setSpacing(8)
		toprow.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE)
		toprow.setHorizontalAlignment(HasAlignment.ALIGN_CENTER)

		middlerow.setSpacing(8)
		middlerow.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE)
		middlerow.setHorizontalAlignment(HasAlignment.ALIGN_CENTER)

		bottomrow.setSpacing(8)
		bottomrow.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE)
		bottomrow.setHorizontalAlignment(HasAlignment.ALIGN_CENTER)

		toprow.add( self.technology, DockPanel.EAST)
		toprow.add( self.creativity, DockPanel.WEST)

		middlerow.add(self.more, DockPanel.WEST)
		middlerow.add(self.contact, DockPanel.CENTER)
		middlerow.add(self.links, DockPanel.EAST)

		bottomrow.add( self.spirituality, DockPanel.EAST)
		bottomrow.add( self.fun, DockPanel.WEST)

		txt = "a squeeze-me site, designed with "
		txt += "<a href='http://code.google.com/p/pyjamas'>pyjamas</a>."
		txt += " squish me with your browser"
		txt += " (even to as little as 300 pixels wide!)"
		txt += " and i should still look reasonable,"
		txt += " and not have a horizontal scroll-bar."
		txt += " here's the <a href='./site_code'>source code</a> for this site."
		self.html = HTML(txt, true)
		self.html.setStyleName("ks-footer")


		self.panel.add(self.ads)
		self.panel.add(toprow)
		self.panel.add(middlerow)
		self.panel.add(bottomrow)
		self.panel.add(self.html)
		
	def onWindowResized(self, width, height):

		self.toprow.removeFromParent()
		self.middlerow.removeFromParent()
		self.bottomrow.removeFromParent()
		self.html.removeFromParent()
		self.ads.removeFromParent()

		self.create_layout()

	def onShow(self):
		pass

	def onClick(self, sender):

		if sender == self.creativity.popup_button:
			self.creativity.show_dialog(600)
		elif sender == self.technology.popup_button:
			self.technology.show_dialog(400)
		elif sender == self.contact.popup_button:
			self.contact.show_dialog(600)


class index:
    def onModuleLoad(self):
        b = Layouts()
        RootPanel().add(b)
