/*
  Only functions that need to be present in the page and therefor can not be in dinmat_footer
*/


// use: dmToolbarPopup.add('#link_id', '#box_id');
var dmToolbarPopup = {
    current: false,
    settings: false,
    target: { opacity: 0, display: 'none' },
    add: function( link, box, onFirstShow )
    {
        link = ez.$$( link );
        box = ez.$$( box );
        link.forEach(function(o, i)
        {
            o.addEvent( 'click', ez.fn.bind( dmToolbarPopup.show, o, box[i], onFirstShow ) );
            o.show();
            o.el.href = 'JavaScript:void(0);';
            var url = '' + window.location, hash, close = ez.$$('a.dm-toolbar-close', box[i]);
            if ( close.length )
            {
                close[0].addEvent( 'click', ez.fn.bind( dmToolbarPopup.show, o, box[i], onFirstShow ) );
                close[0].el.href = 'JavaScript:void(0);';
            }
            if ( url.indexOf('#') !== -1 )
            {
                hash = url.split('#')[1];
                if ( box[i].el.id == hash ) dmToolbarPopup.show.call( o, box[i], onFirstShow );
            }
        });

    },
    show: function( b, onFirstShow )
    {
        if ( dmToolbarPopup.settings === false )
        {
            dmToolbarPopup.settings = { duration: 200, transition: ez.fx.sinoidal };
        }
        if ( dmToolbarPopup.current !== false )
        {
            dmToolbarPopup.current.hide( dmToolbarPopup.settings, dmToolbarPopup.target);
        }
        if ( dmToolbarPopup.current !== b )
        {
            if ( onFirstShow.call !== undefined && b.el.style.position !== 'absolute' )
            {
                onFirstShow.call( this, b );
            }
            b.setStyles({
                position: 'absolute'
            });
            dmToolbarPopup.current = b;
            dmToolbarPopup.current.show( dmToolbarPopup.settings, dmToolbarPopup.target);
        }
        else
        {
            dmToolbarPopup.current = false;
        }
    }
};

var dmFasettMenu = {
    url: '',
    rootUrl: '/',
    init: function( url, siteaccess, rootUrl )
    {
        this.url = (url + '/').replace('//', '/').replace( '/' + siteaccess + '/' + siteaccess, '/' + siteaccess );
        if ( rootUrl !== undefined ) this.rootUrl = (rootUrl + '/').replace('//', '/').replace( '/' + siteaccess + '/' + siteaccess, '/' + siteaccess );
    },
    swap: function( boxId, selectedId )
    {
        var box = ez.$$( '#' + boxId + ' div.fasettmenu-mc' ), content = ez.$( 'js_fasettmenu_' + selectedId );
        if ( box && box.length )
        {
            box[0].el.innerHTML = content ? content.el.innerHTML : '<ul></ul>';
            dmScrollAnimation.init( ez.$$( '#' + boxId + ' div.fasettmenu-box' )[0] , true );
        }   
    },
    regex: {
        tid: /\(tid\)\/(\w+)?\//,
        land: /\(land\)\/(\w+)?\//,
        tema: /\(tema\)\/(\w+)?\//,
        ingredienser: /\(ingredienser\)\/(\w+)?\//
    },
    add: function( link, type, value, multimode )
    {
	    value = '/' + value + '/';
	    var li = ez.$( link.parentNode.parentNode );
	    if ( this.url.indexOf( value ) !== -1 )
	    {
	        this.url = this.url.replace( value, '/' );
	        if ( !this.url.match( this.regex[ type ] ) )
	           this.url = this.url.replace( '/(' + type + ')/', '/' );
	        li.removeClass('selected');
	    }
	    else
	    {
	        if ( this.url.match( this.regex[ type ] ) )
	        {
	            var oldValue = RegExp.$1, oldLi;
	            if ( multimode )
	            {
	               this.url = this.url.replace('(' + type + ')/' + oldValue + '/', '(' + type + ')/' + oldValue + value );
	            }
	            else
	            {
	               this.url = this.url.replace('(' + type + ')/' + oldValue + '/', '(' + type + ')' + value );
	               if ( oldLi = ez.$('dm_' + type + '_' + oldValue) )
	                   oldLi.removeClass('selected');
	            }
	        }
	        else
	        {
	           this.url = this.url + '(' + type + ')' + value;
	        }
	        li.addClass('selected');
	    }
	    return false;
	},
	path: function( link, newPath, id, lvl)
	{
	    var oldPath = this.url.split('(')[0], li = ez.$( link.parentNode.parentNode ), ul = li.el.parentNode, oldLi;
	    newPath = (newPath + '/').replace('//', '/')
	    if ( li.hasClass('selected') )
	    {
	        if ( lvl === 1 || lvl === 2 )
	            id = 0;
	         if ( lvl === 3 || lvl === 2 )
	         {
	           var newPathArr = newPath.split('/');
	           newPathArr.pop();
	           newPathArr.pop();
	           newPath = newPathArr.join('/') + '/';
	         }
	         else
	         {
	           newPath = this.rootUrl;
	         }
	         li.removeClass('selected'); 
	    }
	    else
	    {
	       if ( oldLi = ez.$$('li.selected', ul) )
                oldLi.callEach('removeClass', 'selected' );
           li.addClass('selected');
        }

        this.url = this.url.replace( oldPath, newPath );

        if ( lvl === 1 )
        {
            dmFasettMenu.swap( 'fasett_menu_sub_type', id );
            dmFasettMenu.swap( 'fasett_menu_sub_sub_type', 0 );
        }
        else if ( lvl === 2 )
        {
            dmFasettMenu.swap( 'fasett_menu_sub_sub_type', id );
        }
	    return false;
	}
};


function dmGenerateRelationLinks( r, id, titleText, noRelationText, coreUrl, contentUrl)
{
    var data = eval( r.responseText ), html = '', el = document.getElementById( id );
    if ( !data || !data.length ) return el.innerHTML = '<p class="alternative_color">' + noRelationText + '<\/p>';
    for ( i = 0, l = data.length; i< l; i++)
    {
        html += '<a href="' + contentUrl + '/' + ( data[i].node_id || data[i].main_node_id )+ '">' + data[i].name + '</a>';
        if ( data[i].is_related )
             html += ' <a href="' + coreUrl +'/unrelate/'+ data[i].contentobject_id + '/recipe_relation/' + data[i].related_to_id +'" onclick="this.style.display=\'none\'">[Fjern]<\/a><br \/>';
        else
            html += ' <a href="' + coreUrl +'/relate/'+ data[i].contentobject_id + '/recipe_relation/' + data[i].related_to_id +'" onclick="this.style.display=\'none\'">[Legg til]<\/a><br \/>';
    }
    el.innerHTML = '<h6>'+ titleText + ':<\/h6>' + html;
    return 0;
}