/*
 * (c) by SuNflOw1991
 * 
 * Fragen usw. entweder im Forum:
 *    
 * http://php-fusion.at
 * http://phpfusion-support.de
 * 
 * oder per E-Mail:
 * conrad.patrick.1@googlemail.com
 * 
 */ 

(function($){
    $(document).ready(function(){    
        // Klicks binden
        // $('ul#admin-ul-nav a.admin_nav_link').bind('click', getPage);
        $('div.admin-body').find('a.change_desc').bind('click', changeDesc);
        $('div.admin-body').find('a.change_view').bind('click', changeView);
        
        function changeDesc(e) {
            var clickedLink = $(this),
                readVal     = clickedLink.attr('id').split('_'),
                curPageId   = readVal[2],
                changeFrom  = readVal[1];
                
            var dataString = {
                'do'         : 'changeDesc',
                'aidlink'    : AID_LINK,
                'changeFrom' : changeFrom
            }
            
            $.ajax({
                type: 'POST',
                url: PATH_ADMIN + 'template/ajax/ajaxHandler.inc.php',
                data: dataString,
                dataType: 'json',
                success: function(data) {
                    if(data.success == true) {                        
                        $('div.admin-body').find('a.change_desc').attr('id', 'change_' + data.changeTo + '_' + curPageId);                                                                                            
                        responsePage(curPageId, AID_LINK, data.changeOt, data.changeTo);    
                    }                                                                                         
                }
            }); // Ajax            
            return false;
        }
        
        function changeView(e) {
            var clickedLink = $(this),
                readVal     = clickedLink.attr('id').split('_'),
                curPageId   = readVal[2],
                changeFrom  = readVal[1];

            var dataString = {
                'do'         : 'changeView',
                'aidlink'    : AID_LINK,
                'changeFrom' : changeFrom
            }

            $.ajax({
                type: 'POST',
                url: PATH_ADMIN + 'template/ajax/ajaxHandler.inc.php',
                data: dataString,
                dataType: 'json',
                success: function(data) {
                    if(data.success == true) {
                        $('div.admin-body').find('a.change_view').attr('id', 'change_' + data.changeTo + '_' + curPageId);
                        responsePage(curPageId, AID_LINK, data.changeTo, data.changeOt);
                    }
                }
            }); // Ajax            
            return false;
        }
        
        function getPage(e) {
            var clickedLink    = $(this),
                LinkFullID     = clickedLink.attr('id').split('_'),
                pageIDtoGet    = LinkFullID[2],
                changeDescVal_ = $('div.admin-body').find('a.change_desc').attr('id').split('_'),
                changeDescVal  = changeDescVal_[1],
                changeViewVal_ = $('div.admin-body').find('a.change_view').attr('id').split('_'),
                changeViewVal  = changeDescVal_[1];
            
            // Wenn geklickter Link schon Aktiv nichts machen!
            if(clickedLink.parent('li').hasClass('active')) { return false; }
                
            responsePage(pageIDtoGet, AID_LINK, changeViewVal, changeDescVal);
            return false;                        
        }
                
        function responsePage(pageId, aidlink, changeV, changeD) {
            var slideContainer = $('div#slideContent'),
                msgBox         = $('div#messageBox'),
                cssErrorMap    = {
                    'background-color' : '#F7DAD8', 
                    'border' : '1px #D3322A solid'    
                },
                cssDefaultMap  = {
                    'background-color' : '#F5F5F5', 
                    'border' : '1px #E1E1E1 solid'
                },
                tplContainer   = $('div#tplContainer');
                
            var dataString = {
                    'do' : 'getPage',
                    'page_id' : pageId,
                    'aid' : aidlink
                }; 
                
            slideContainer.slideUp('_', function() {
                msgBox.html(LOCALE.ac_ajax_01
                            + '<div id=\'loaderDiv\' style=\'padding-top: 1em;\' align=\'center\'><img src=\'' 
                            + PATH_ADMIN + 'template/images/loader.gif\' alt=\'' 
                            + LOCALE.ac_ajax_02 
                            + '\' title=\'' 
                            + LOCALE.ac_ajax_02 
                            + '\' /></div>')
                        .slideDown('slow', function() {
                            $.ajax({
                                type: 'POST',
                                url: PATH_ADMIN + 'template/ajax/ajaxHandler.inc.php',
                                data: dataString,
                                dataType: 'json',
                                success: function(data) {
                                    if(data.success != true) {
                                        msgBox.css(cssErrorMap)
                                              .html(data.error);                                              
                                    } else {
                                        if (data.links == null) { 
                                            document.location.href= PATH_ADMIN + 'index.php' + aidlink; 
                                        } else {                                                                                    
                                            var CrumbContainer = $('div.content').find('div.breadCrumb'),
                                                oTblText       = data.openTblTxt,
                                                viewType       = data.viewType; 
                                            
                                            // Update Crumbs
                                            updateBreadCrumb(CrumbContainer, oTblText);
                                                                                        
                                            // Neuer Titel
                                            slideContainer.find('div.capmain').find('h2').text(oTblText);
                                            
                                            // Neue Links rendern                                                                                                                                                                     
                                            var aLinks = new Array();
        
                                            for(i=0, len= data.links.length; i < len; i++) {
                                                aLinks.push(renderLink(data.links[i], tplContainer, data.viewType, data.aid));
                                            }
        
                                            // prüfen ob AdminNavi ul vorhanden ist, wenn nicht erstellen
                                            var adminNav = slideContainer.find('ul#admin-nav');
        
                                            if (adminNav.length == 0) {
                                                var newUl = '<ul id=\"admin-nav\"></ul><div class=\"clear\"></div>';
                                                                                                                                        
                                                slideContainer.find('div.admin-body').find('div').html('').append($(newUl));
                                                // Variable neu anlegen
                                                var adminNav = slideContainer.find('ul#admin-nav');
                                            }
                                            // ul leeren
                                            adminNav.html('');
                                           
                                            // a#id für Ansichts Change aktualisieren
                                            slideContainer.find('a.change_view').attr('id', 'change_' + changeV + '_' + pageId);
                                            slideContainer.find('a.change_desc').attr('id', 'change_' + changeD + '_' + pageId);
                                            
                                            // dann falls nötig sichtbar machen
                                            slideContainer.find('a.change_desc').show();
                                            slideContainer.find('a.change_view').show();                                        
        
                                            // neue Links in adminNav packen
                                            var neueLinks = $(aLinks);
        
                                            neueLinks.each(function() {
                                                var ClearDiv = '<div class=\"clear\"></div>';
                                                
                                                $(this).appendTo(adminNav);
                                                
                                                if (viewType == 2 || viewType == 4) {
                                                    $(ClearDiv).appendTo(adminNav);
                                                }
                                            });
        
                                            // Content wieder anzeigen
                                            msgBox.fadeOut('fast', function() {
                                                slideContainer.slideDown(500);
        
                                                // falls es einen aktiven Link gibt Klasse normalisieren
                                                var curActive = $('ul#admin-ul-nav').find('.active');
                                                if (curActive.length > 0) {
                                                    var curActiveSplit = curActive.attr('id').split('_'),
                                                        curActivePageID = curActiveSplit[2];
        
                                                        curActive.attr('class', 'admin-ul-nav-' + curActivePageID);
                                                }
        
                                                // Geklickten Link aktivieren
                                                $('ul#admin-ul-nav li#nav_li_' + pageId).attr('class', 'active admin-ul-nav-active-' + pageId);
                                                msgBox.html('').css(cssDefaultMap);
                                            });
                                        } // in ajax success
                                    } // if data.links != null                                    
                                } // ajax success
                            }); // Ajax                                                                                       
                        }); // slideDown
            }); // slideUp                                   
        }

        function renderLink(oLink, tplCont, viewType, aid) {
            if (viewType == 2 || viewType == 4) {
                var tpl = {
                    link : 'a',
                    img : 'div.admin-icons img.icon',
                    title : 'div.admin-desc h3',
                    desc : 'div.admin-description',
                };
                var LinkTpl = tplCont.find('div#tpl1').find('li').clone();
            } else {
                var tpl = {
                    link : 'a',
                    img : 'img',
                    title : 'div',
                    desc : 'a',
                };
                var LinkTpl = tplCont.find('div#tpl2').find('li').clone();
            }

            LinkTpl.find(tpl.link).attr('href', PATH_ADMIN + oLink.admin_link + aid);
            LinkTpl.find(tpl.img).attr('src', oLink.admin_image);
            LinkTpl.find(tpl.title).text(oLink.admin_title);

            if (viewType == 2) {
                LinkTpl.find(tpl.desc).html(oLink.admin_description);
            } else if (viewType == 3) {
                LinkTpl.find(tpl.desc).attr('title', oLink.admin_description).attr('class', 'tip').tipsy({gravity: 'sw'});
            }

            return LinkTpl;
        }
        
        function updateBreadCrumb(container, text) {        
            var CrumbUl     = container.find('ul'),
                CrumbLiList = CrumbUl.find('li'),
                countCrumbs = CrumbLiList.length,
                newText     = text;

            // breadCrumbs ggf. erweitern
            if (countCrumbs == 2) {
                var curText = CrumbLiList.eq(1).text(),
                    newLi   = '<li>' + newText + '</li>';
                // zweites li linken! und tipsy binden!
                CrumbLiList.eq(1)
                            .html('<a title=\"' + curText + '\" href=\"'
                                    + PATH_ADMIN + 'index.php' + AID_LINK
                                    + '\">' + curText + '</a>')
                            .find('a')
                            .tipsy({gravity: 'nw'});

                // neues li anhängen
                CrumbUl.append($(newLi));
            } else {
                // wenn nicht erweitert werden muss, dann ersetzen
                CrumbUl.find('li:last').text(newText);
            }                    
        }                 
    }); // on DOM-Ready
})(jQuery);