import os
from django import template
from django.conf import settings

from darkroom.models import PhotoSize, Photo, Slideshow

register = template.Library()

@register.simple_tag
def next_in_gallery(photo, gallery):
    next = photo.get_next_in_gallery(gallery)
    if next:
        return '<a title="%s" href="%s"><img src="%s"/></a>' % (next.title, next.get_absolute_url(), next.get_thumbnail_url())
    return ""

@register.simple_tag
def previous_in_gallery(photo, gallery):
    prev = photo.get_previous_in_gallery(gallery)
    if prev:
        return '<a title="%s" href="%s"><img src="%s"/></a>' % (prev.title, prev.get_absolute_url(), prev.get_thumbnail_url())
    return ""
    
@register.simple_tag
def video_object(video, player, skin):
    if video:
	pathless_video=video.flv_video.name.split('.')[0]
        pathless_video = "http://penobscotbaypress.com/media/"+pathless_video
        movie_ext = video.flv_video.name[-3:]
        movie_slug=video.flv_video.name[:-4]
        #return '<p>Movie goes here</p>'
        movie_path = settings.MEDIA_URL + video.flv_video.name
        player_path = settings.MEDIA_URL + "flash/" + player
        skin_path = settings.MEDIA_URL + "flash/" + skin
        return '<script type="text/javascript">AC_FL_RunContent("codebase","http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0","width","644","height","402","title","%s","src","%s","quality","high","pluginspage","http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash","movie","%s" ); //end AC code</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="644" height="402" title="%s"><param name="movie" value="%s"><param name="quality" value="high"><embed src="%s" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="644" height="402"></embed></object></noscript>' % (video, pathless_video, pathless_video, video, pathless_video, pathless_video)
    return True

@register.simple_tag
def slideshow_object(slideshow, width, height):
    if slideshow:
        return '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="800" height="596" id="soundslider"><param name="movie" value="http://penobscotbaypress.com/media/darkroom/slideshows/%s/soundslider.swf?size=2&format=xml&embed_width=800&embed_height=596&autoload=false" /><param name="allowScriptAccess" value="always" /><param name="quality" value="high" /><param name="allowFullScreen" value="true" /><param name="menu" value="false" /><param name="bgcolor" value="#FFFFFF" /><embed src="http://penobscotbaypress.com/media/darkroom/slideshows/%s/soundslider.swf?size=2&format=xml&embed_width=%s&embed_height=%s&autoload=false" quality="high" bgcolor="#FFFFFF" width="%s" height="%s" menu="false" allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash"></embed></object>' % (slideshow.folder, slideshow.folder, width, height, width, height)
    return ""

@register.simple_tag
def lightbox_object(object, preview_url):
    if object.__class__ == Photo:
        # Get the photo size related to lightboxes, height will need to be set
        boxsize = PhotoSize.objects.get(name='lightbox')
        height = str(int(boxsize.height)+40)
        if object.image.height < object.image.width:
            width = '820' #780
        else:
            width = '360' #335
        return '<a href="%s?KeepThis=true&TB_iframe=true&height=%s&width=%s" title="%s" class="thickbox" rel="photos"><img src="%s" alt="%s" class="gallery_thumb" /></a>' % (object.get_lb_url(), height, width, object.description, preview_url, object.title)
    elif object.__class__ == Slideshow:
        height='540'
        width='560'
        return '<a href="%s?KeepThis=true&TB_iframe=true&height=%s&width=%s" title="%s" class="thickbox" "><img src="%s" alt="%s" class="gallery_thumb" /></a>' % (object.get_absolute_url(), height, width, object.description, preview_url, object.title)
    return ""
