# -*- coding: utf-8 -*- 
#
# Plugin Default
# Framework: Python Web2py (www.web2py.com)
#
# Copyright (c) 2010- Mulone, Pablo Martin (http://martin.tecnodoc.com.ar/)
# License Code: Unlicense
# 

from gluon.tools import PluginManager

class PluginDefault:
        

    def __init__(self):               
        
        self.plugins = PluginManager()                
        self.setup()                
        self.files()       
        
    def setup(self):        
        self.plugins.default.version = '1.0'
        self.plugins.default.jquery = True
        self.plugins.default.resetcss = True
        self.plugins.default.ezcss = True
        self.plugins.default.layoutcss = True
        self.plugins.default.modernizr = True
        self.plugins.default.calendar = True
        self.plugins.default.statusbar = True
        self.plugins.default.headjs = True
        self.plugins.default.onready = ''' 
                    var flash = jQuery('.flash');
                    flash.hide();
                    if(flash.html()) flash.slideDown();
                    web2py_ajax_init();
        '''
                
        
    def files(self):
        
        if self.plugins.default.resetcss:
            response.files.append(URL('static','css/reset.css'))
        
        if self.plugins.default.ezcss:
            response.files.append(URL('static','css/ez.css'))
            
        if self.plugins.default.layoutcss:
            response.files.append(URL('static','css/layout.css'))
        
        if self.plugins.default.jquery:
            response.files.insert(0,URL('static','js/plugin_default/jquery.js'))
            
        if self.plugins.default.modernizr:
            response.files.insert(0,URL('static','js/plugin_default/modernizr.min.js'))
            
        if self.plugins.default.calendar:
            response.files.append(URL('static','css/plugin_default/calendar.css'))
            response.files.append(URL('static','js/plugin_default/calendar.js'))
            
        if self.plugins.default.statusbar:
            response.files.append(URL('static','css/plugin_default/superfish.css'))
            response.files.append(URL('static','js/plugin_default/superfish.js'))
            self.plugins.default.onready += "jQuery('ul.sf-menu').superfish();"
            
            
    def _response_files(self):
        
        meta = ''
        css = ''
        script = ''
        head = ''
        
        for _item in response.meta or []:
            meta += '<meta name="%s" content="%s" />' % (_item,response.meta[_item])
            
        if self.plugins.default.headjs:
            script += '<script src="%s" type="text/javascript"></script>' % URL('static','js/plugin_default/head.min.js')
            ## head.js("/path/to/jquery.js", "/google/analytics.js", "/js/site.js");   
        
        count=1
        for _k,_file in enumerate(response.files or []):
            if _file in response.files[:_k]:
                continue
            _file0=_file.lower().split('?')[0]
            if _file0.endswith('.css'):
                css +='<link href="%s" rel="stylesheet" type="text/css" />' % _file
            elif _file0.endswith('.js'):
                if self.plugins.default.headjs:
                    if count>1:
                        head += ',"%s"' % _file
                    else:
                        head += '"%s"' % _file
                else:                   
                    script += '<script src="%s" type="text/javascript"></script>\n' % _file
            count+=1
        
        if self.plugins.default.headjs:
            onready = self.plugins.default.onready
            script += '''<script>head.js(%s, function() { %s });</script>''' % (head, onready)
        
        return meta + css + script
    
    
    def render_response_files(self):
        
        return self._response_files()
        

            
plugin_default = PluginDefault()