function disableButton(button) {
	button.value='bitte warten';
	button.disabled = true;
}

function limitInputLength(input, limit) {
	if (input.value.length == limit) {
		input.readOnly = true;
	}
}

function addLoader(container) {
	var oContainer = document.getElementById(container);
	
	oContainer.innerHTML='<img src="/html/img/loadingAnimation.gif">';
	
	return true;
}

/**
 * Removes leading whitespaces
 * 
 * @param value - string
 */
function LTrim(value) {
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

/**
 * Removes ending whitespaces
 * 
 * @param value - string
 */
function RTrim(value) {
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

/**
 * Removes leading and ending whitespaces
 * 
 * @param value - string
 */
function trim(value) {
    return LTrim(RTrim(value));
}

function showPreview(id) {
	popup('/content/default/VPaperTest.php?imd='+id+'&host=vpaper', 'lala', 400,270);
}

/**
 * sets the visibility of a container on (1) or off (0)
 * 
 * @param value - 1 toggle visible, 0 toggle unvisible
 * @param container to be toggled
 */
function setVisibility(value, container) {
    if (value == 1) {
        container.style.display = 'block';
    } else if (value == 0) {
        container.style.display = 'none';
    }
    
    return true;
}

/**
 * like toggleContainer but it changes the display style on its own
 * 
 * @param container-id to be toggled
 */
function toggleDisplay(container) {
	var oContainer = document.getElementById(container);
	if (oContainer.style.display == 'block') {
		oContainer.style.display = 'none';
	}
	else if (oContainer.style.display == 'none') {
		oContainer.style.display = 'block';
	}
}

function showRecommend(template, width, height, link) {
	if (typeof(link) != "undefined" && link != null) {
		document.getElementById('re_link').value=link;
	}
    tb_show('vPaper-Nachricht','#TB_inline?height=' + height + '&width=' + width + '&inlineId=' + template + '&modal=true', '');
}

function showToc(template, width, height) {
    tb_show('Inhaltsverzeichnis','#TB_inline?height=' + height + '&width=' + width + '&inlineId=' + template + '&modal=true', '');
}		

/**
 * confirmDelete displays a given template in vpaper CI
 * in a modal window
 *
 * example template:
 * <div id="confirm_delete" style="display: none; background: #1a1a1a;">
 *    <table cellspacing="0" cellpadding="0" width="100%">
 *        <tr>
 *            <td class="popup_header" valign="middle" width="40">
 *                <img src="img/icon_info.jpg">
 *            </td>
 *            <td class="popup_header" valign="middle">vPaper Nachricht</td>
 *            <td class="popup_header" valign="middle" width="100"><a href="#" onClick="tb_remove();" id="TB_closeWindowButton" ignore="true" title="Close">SCHLIESSEN</a></td>
 *        </tr>
 *    </table>
 *    <div class="popup_content">
 *        <div class="popup_message" id="confirm_body"></div>
 *        <br />
 *        <div id="interaction_area">
 *        <a href="#" id="delete_link" onClick="getElementById('interaction_area').style.display='none'; getElementById('waiting_delete').style.display='block';" ><img src="img/button_ok.gif" border="0" /></a>
 *        <img src="img/button_cancel.gif" border="0" onClick="tb_remove();" style="cursor:pointer" />
 *        </div>
 *        <div id="waiting_delete" style="display:none;"><img src="img/loadingAnimation.gif" /></div>
 *    </div>
 *</div>
 * @param template - id of html snippet used for the modal window
 * @param message_body - id of html element used for the displayed text
 * @param message - text to show in the window
 * @param link_body - id of a-tag used for redirecting, if button ok was pressed
 * @param redirect_link - link to redirect, after pressing the ok button
 * @param width - width of the window
 * @param height - height of the window
 */
function showConfirm(template, message_body, message, link_body, redirect_link, width, height) {
    document.getElementById(link_body).href = redirect_link;
    document.getElementById(message_body).innerHTML = message;

    tb_show('vPaper-Nachricht','#TB_inline?height=' + height + '&width=' + width + '&inlineId=' + template + '&modal=true', '');
}

/**
 * showInfo displays a given template in vpaper CI
 * in a modal window
 *
 * example template:
 * <div id="info_msg" style="display: none; background: #1a1a1a;">
 *     <!-- Header -->
 *     <table cellspacing="0" cellpadding="0" width="100%">
 *         <tr>
 *             <td class="popup_header" valign="middle" width="40">
 *                 <img src="img/icon_info.jpg">
 *             </td>
 *             <td class="popup_header" valign="middle">vPaper Nachricht</td>
 *             <td class="popup_header" valign="middle" width="100"><a href="#" onClick="tb_remove();" id="TB_closeWindowButton" ignore="true" title="Close">SCHLIESSEN</a></td>
 *         </tr>
 *     </table>
 *     <!-- Body -->
 *     <div class="popup_content">
 *         <div class="popup_message" id="message_body"></div>
 *         <br /><img src="img/button_ok.gif" border="0" onClick="tb_remove();" style="cursor:pointer" />
 *     </div>
 * </div>
 * @param template - id of html snippet used for the modal window
 * @param message_body - id of html element used for the displayed text
 * @param message - text to show in the window
 * @param width - width of the window
 * @param height - height of the window
 */
function showInfo(template, message_body, message, width, height) {
    document.getElementById(message_body).innerHTML = message;
	tb_show('vPaper-Nachricht','#TB_inline?height=' + height + '&width=' + width + '&inlineId=' + template + '&modal=true', '');
}

var visiblePages = 15;
var visiblePagesInList = 5;
function showDetail (template, width, height, url, path, imd) {
	document.getElementById('searchlist').innerHTML='';
	$('#match_count').html('');
	$(template).ready(function(){
		$.get(url, function (data){
			var result = eval ( '(' + data + ')' );
			//get element with id mylist
			
			//document.getElementById('match_count').innerHTML = '&nbsp;&nbsp;' + result.match_list.length;
			$('#match_count').html('&nbsp;&nbsp;' + result.match_list.length);
			if (result.match_list.length==0){
				var error_message = '<strong>Es sind keine Treffer gefunden.</strong>';
				$('#searchlist').html(error_message);
				return false;
			}
			
			var searchList = $('#searchlist'); 
	    	var searchListItems = '<div id="up" class="scroll" />';  /*arrow up*/ 
	    	searchListItems += '<div id="search_wrap">';
			searchListItems += '<table id="search_table" class="search_table">';
			
			
			
			for(var row in result.match_list) {
				searchListItems += '<tr id="'+ result.match_list[row].page +'">';
				searchListItems += '<td class="search_list_first_col"> Seite '+ result.match_list[row].page + '</td>';
				searchListItems += '<td>' + result.match_list[row].content + '</td>';
				searchListItems += '</tr>';
				
			}
			
			searchListItems += '</table>';
			searchListItems += '<img id="search_result_preview" src=""/>';
			searchListItems += '<div style="clear:both;"/></div>';/*clear float property & close "search_wrap" div */
			searchListItems += '<div id="down" class="scroll" />'; /*arrow down*/
			
			
			/*build pager*/
			searchListItems += '<table id="search_pager"><tr>';
		
			searchListItems += '<td id="pager_to_first"><a href="#" class="pager_controls">&lt;&lt;</a></td>';
			searchListItems += '<td id="pager_to_prev" ><a href="#" class="pager_controls">&lt;</a></td>';
			for (var i=1; i<result.pages_total;i++) {
				searchListItems += '<td id="search_page_'+i+'"><a href="#" >' + i +'</a></td>';
			}			

			searchListItems += '<td id="pager_to_next"><a href="#" class="pager_controls">&gt;</a></td>';
			searchListItems += '<td id="pager_to_last"><a href="#" class="pager_controls">&gt;&gt;</a></td>';

			searchListItems += '</tr></table>';
			
			/*add to DOM*/
			searchList.html(searchListItems);
			
			 /* show first 5 page*/
			$('#search_table tr:gt('+ (visiblePagesInList-1)+')').hide();
			$("#search_table tr").click(function(){
				$('#search_result_preview').attr('src', '/FlashViewer,imd='+imd+',axw=image,ip='+this.id+',axm=mini');
				return false;
			});	

			/*underline matched*/
			var pages = $("#search_table tr");
			for (var i=0; i<pages.length; i++) {
				$('#search_pager td#search_page_'+pages[i].id+' a').addClass('selected');
				$('#search_pager td#search_page_'+pages[i].id).addClass('selected');
			}

			
		    /* pager click*/
		    $("#searchlist td[id^='pager_to_']").click(function(){
		    	switch (this.id) {
		    	case 'pager_to_next':
		    		var len = $('#search_pager td[id^="search_page_"]').length ;
		    		if ($("#search_pager td[id^='search_page_']:visible:last").attr('id') == ('search_page_'+len)) return false;
		    		$("#search_pager td[id^='search_page_']:visible:last").next().show();
		    		$("#search_pager td[id^='search_page_']:visible:first").hide();
		    	break;
		    	case 'pager_to_prev':
					if ($("#search_pager td[id^='search_page_']:visible:first").attr('id') == 'search_page_1') return false;
		    		$("#search_pager td[id^='search_page_']:visible:first").prev().show();
		    		$("#search_pager td[id^='search_page_']:visible:last").hide();
    	
		    	break;
		    	case 'pager_to_last':
		    		var len = $('#search_pager td[id^="search_page_"]').length - 1 ;
		    		$('#search_pager td[id^="search_page_"]').hide();
		    		$('#search_pager td[id^="search_page_"]:gt('+ (len - visiblePages) + ')').show();
		    	break;
		    	case 'pager_to_first':
		    		$('#search_pager td[id^="search_page_"]').hide();
		    		$('#search_pager td[id^="search_page_"]:lt('+ visiblePages + ')').show();		    	
		    	break;
		    	}
		    	return false;
		    });
		    
		    
		    /* page click*/
		    $("#search_pager td[id^='search_page_']").click(function(){
		    	/*prevent possible scroll background page*/
		    	return false;
		    });
		    
		    /* selected page click*/
		    $("#search_pager td[id^='search_page_'].selected").click(function(){
		    	var id = $('#'+this.id + ' a:first-child').text();
		    	$('#search_table tr').hide();
		    	$('#search_table tr#'+id).show().click()
		    		.next().show()
		    		.next().show()
		    		.next().show()
		    		.next().show();
		    	
		    	/*handle scroll arrows */
		    	if ($('#search_table tr#'+id).prevAll().length > 0) {
		    		$('#up').addClass('scroll_up');
		    	}else {
		    		$('#up').removeClass('scroll_up');
		    	}
		    	if ($('#search_table tr:visible').nextAll().length >= visiblePagesInList) {
		    		$('#down').addClass('scroll_down');
		    	}else {
		    		$('#down').removeClass('scroll_down');
		    	}
		    	return false;
		    });
		    
			/*work with pager*/
			var first_page = parseInt($('#search_table tr:first').attr('id')) - 1 ;
			if ((result.pages_total - first_page)< visiblePages) {
				/* 
				   visiblePages = 15
				   show last 15 pages, when first match result within last 15
				 */
				first_page = result.pages_total - visiblePages - 1;
			}
			$('#search_pager td[id^="search_page_"]:lt('+first_page+')').hide(); /*hide first*/
			$('#search_pager td[id^="search_page_"]:gt('+(parseInt(first_page)+visiblePages)+')').hide(); /*hide last*/
			
			
			/*scroll handler*/
			$('#searchlist .scroll').click(function(){
				if (this.id=='down') {
					if ($('#search_table tr:last').attr('id') == $('#search_table tr:visible:last').attr('id')) {
						/*$('#down').removeClass('scroll_down');*/
						return false;
					}
					
					$('#up').addClass('scroll_up');
					$('#search_table tr:visible:first').hide();
					$('#search_table tr:visible:last').next().show();
					if ($('#search_table tr:last').attr('id') == $('#search_table tr:visible:last').attr('id')) {
						$('#down').removeClass('scroll_down');
					}
				}else { /*up*/
					if ($('#search_table tr:first').attr('id') == $('#search_table tr:visible:first').attr('id')) {
						/*$('#up').removeClass('scroll_up');*/
						return false;
					}
					
					$('#search_table tr:visible:first').prev().show();
					if ($('#search_table tr:visible').length > visiblePagesInList) {
						$('#search_table tr:visible:last').hide();
						
						$('#down').addClass('scroll_down');
					}
					if ($('#search_table tr:first').attr('id') == $('#search_table tr:visible:first').attr('id')) {
						$('#up').removeClass('scroll_up');
					}					
				}
				return false;
			});
			/*show down arrow if needed*/
			if (result.match_list.length > visiblePagesInList) {
				$('#down').addClass('scroll_down');
			}
			
			/*color to odd entry*/
			$('#search_table tr:odd', searchList).addClass ('search_list_item_odd');
			/*show preview for first entry*/
			$('#search_table tr:first').click();
		});
	})
	tb_show('show_detail','#TB_inline?height=' + height + '&width=' + width + '&inlineId=' + template + '&modal=true', '');
	return false;
}

function create_my_items (items){
    $('#pager a').click(function(){return false;})
    if (!items.length) {
        document.getElementById('pager').style.display = 'none';
        document.getElementById('my_media_list').innerHTML='';
        return false;
    }
    var list =$('#my_media_list');
    var list_items = '<ul id="media_list">';
    var counter = 1;
    var display_none = ' style="display:none;"';
    for (var obj in items) {
        var o = items[obj];
        var display = counter>visible_items ? display_none : '';
        list_items+='<li id="'+o.id+'" class="img_list"'+display+'><img src="./../html/img/thumbnails/'+o.path+'/repository/paper/cat/1.jpg" border="0">';
        list_items+='<div align="center">'+o.magazine+'<br />'+o.exp+'/'+o.year+'</div>';
        list_items+='<form id="issue_'+o.id+'" action="/content/default/VPaper.php" method="POST" target="_blank">';
        list_items+='<input type="hidden" name="mode" value="0" />';
        list_items+='<input type="hidden" name="imd" value="'+o.id+'"/>';
        list_items+='</form>';
        list_items+='</li>';
        counter++;
    }
    list_items += '</ul>';
    list.html(list_items);
    var li = $('#media_list li');
    li.mouseover(function (){this.style.cursor='pointer'});
    li.mouseout(function (){this.style.cursor='default'});
    li.click(function(){
        document.getElementById('issue_'+this.id).submit();
    });
    if (items.length <= visible_items) {
        document.getElementById('pager').style.display = 'none';
    }else {
        document.getElementById('pager').style.display = 'block';
    }
}
