/**
 * NobleProfile Slider
 */
Phx.namespace('Phx.Application.Modules.NobleProfileVideoList', new function() {
    
	var _popupLinkOnClick = function (ev) {
        Phx.Event.stop(ev);
        var $popupLink = $(this);
        var href = $popupLink.attr('href');
        var params = { width: 600, height:600 };
        var windowName = '_blank';
        var validParams = {
            dependent : 'boolean', 
            height      : 'number',
            hotkeys     : 'boolean',   
            innerHeight : 'number',
            innerWidth  : 'number',
            left        : 'number',
            location    : 'boolean', 
            menubar     : 'boolean', 
            resizable   : 'boolean', 
            screenX     : 'number',
            screenY     : 'number', 
            scrollbars  : 'boolean', 
            status      : 'boolean', 
            toolbar     : 'boolean',   
            top         : 'number',  
            width       : 'number',
            name        : 'windowName'
            };
 
        var posSharp = href.lastIndexOf('#') 
        if (posSharp >= 0) {
            var str = href.substring(posSharp+1);
            href = href.substring(0, posSharp);
            var els = str.split(',');
            var i;
            for (i = 0; i < els.length; i++) {
                var pair = els[i].split('=');
                if(pair.length === 2) {
                    var valid = validParams[pair[0]];
                    if (valid === 'number') {
                        var intVal = parseInt(pair[1]);
                        if (!isNaN(intVal)) {
                            params[pair[0]] = intVal;
                        }
                    }
                    else if ( valid === 'boolean') {
                        if (pair[1] === 'yes' || pair[1] === 'no') {
                            params[pair[0]] = pair[1];
                        }
                    }
                    else if ( valid === 'windowName') {
                        windowName = pair[1];
                    }
                }
            }
             
        }
        
        var paramString = '';
        var item;
        for (item in params) {
            paramString = (paramString === '')? paramString : paramString + ',';
            paramString = paramString + item + '=' + params[item];
        }
        
        var win = window.open(href, windowName, paramString);
        try {
            win.focus();
        }
        catch(except) {
            
        }
    };
    
    var _registerPopupLink = function($popupLink) {
        $popupLink.unbind('click').bind('click', _popupLinkOnClick);
    };
    
    var _registerVideoList = function($playerTag) {
        var _htmlCodeCache = {};
        var _currentVideo = 0;
        
		$playerTag.find('.videolist li').each(function(index) {
            $(this).unbind('click').bind('click', function(ev) {
				Phx.Event.stop(ev);
                if (index === _currentVideo) {
                    return;
				}
                var $currentVideoContainer = $playerTag.find('.monitor .nobleprofile-video-'+_currentVideo); 
                $currentVideoContainer.addClass('hidden');
                var $listTags = $playerTag.find('.videolist li');
                var htmlCode = $currentVideoContainer.html(); 
                if (htmlCode.indexOf('sevenload') < 0 && htmlCode.indexOf('myvideo') < 0 ) {
                    _htmlCodeCache[_currentVideo] = htmlCode; 
                    $currentVideoContainer.html(''); 
                }
                $($listTags[_currentVideo]).removeClass('selected');
                _currentVideo = index;
                $currentVideoContainer = $playerTag.find('.monitor .nobleprofile-video-'+_currentVideo);
                if (_htmlCodeCache[_currentVideo]) {
                    $currentVideoContainer.html(_htmlCodeCache[_currentVideo]);
                }
                $currentVideoContainer.removeClass('hidden');
                $($listTags[_currentVideo]).addClass('selected');
			});
		});
	};

	/**
	 * initializes the module
	 * 
	 * @return {void}
	 */
	this.init = function() {
		$('#Wahlzentrale-StartPagelet .np-popuplink, #NobleProfile_Content .np-popuplink').each(function() {
                    _registerPopupLink($(this));
                });
        $('.videoplayer').each(function() {
					_registerVideoList($(this));
				});
	};

}());

