function setAdvancedSearchOnLoad()
{
        var radioPDF= document.getElementById('radioPdfOrArticleBibliotek');
        var radioArticle = document.getElementById('radioPdfOrArticleArtikler');
        if ( !radioPDF.checked && !radioArticle.checked )
        {
                radioPDF.checked = true;
        }
        this.setAdvancedSearch( null );
}

function loadBuildingComponent(articlesurl,buildingcomponenturl){
        show("articlesMenu");
        sendAsyncRequest("#content",buildingcomponenturl);
        sendAsyncRequest("#articlesMenu",articlesurl);
}

/**
 * Used to set the search type article or PDF. Also
 * disabling the searchfields not needed when changing search type.
 * @author Kim Terp ...(kim@icn.dk)...
 */
function setAdvancedSearch( _caller )
{
 
        var searchType = '';
        if ( _caller != null )
        {
                if( _caller.id == 'radioPdfOrArticleArtikler' )
                {
                        searchType = 'Artikler';
                }
                else
                {
                        searchType = 'Bibliotek';
                }
        }
        else
        {
                if ( document.getElementById('radioPdfOrArticleArtikler').checked )
                {
                        searchType = 'Artikler';
                }
                else
                {
                        searchType = 'Bibliotek';
                }
        }

        if ( searchType == 'Artikler' )
        {
					$("#advancedSearchForm_advancedSearch_author").attr("disabled","disabled");
					$("#advancedSearchForm_advancedSearch_author").addClass("disable");
					$("#advancedSearchForm_advancedSearch_article_buildingComponent_identity").removeAttr("disabled");
					$("#advancedSearchForm_advancedSearch_article_buildingComponent_identity").removeClass("disable");
        }
        else
        {
					$("#advancedSearchForm_advancedSearch_article_buildingComponent_identity").attr("disabled","disabled");
					$("#advancedSearchForm_advancedSearch_article_buildingComponent_identity").addClass("disable");
					$("#advancedSearchForm_advancedSearch_author").removeAttr("disabled");
					$("#advancedSearchForm_advancedSearch_author").removeClass("disable");
        }
        document.getElementById('advancedSearchType').value = searchType;
}

function sendAsyncRequest(idToReplace,requestUrl){
    if (window.stopLoadContent && idToReplace == "#content") return;
    if (requestUrl.indexOf("timeStamp")==-1){
        var timeStamp = new Date().getTime();
        if (requestUrl.indexOf("?")!=-1){
            requestUrl += "&timeStamp=" + timeStamp;
        }
        else{
            requestUrl += "?timeStamp=" + timeStamp;
        }
    }
    jQuery(idToReplace).load(requestUrl, function() {
    	if ($.browser.msie) {    		
      	$('a[href$=#Top], a[href$=#welcomeLink]').click(function(e) {      		
      		scrollToEl(this, e);      		
      		return false;    	
      	});
    	}
    });
}


function toggleVisibility(id, isClick) {
	if (isClick) {
    var e = $('#' + id);    
    if (e.is(':hidden')) {
      e.show();
    } else {
    	e.hide();
    }
	}
}
function hide(id) {
  var e = document.getElementById(id);
  e.style.display = 'none';
}
function show(id) {
  var e = document.getElementById(id);
  e.style.display = 'block';
}    
//---------------ARTICLE RELATED FUNCTIONS---------------------//
/**
 * Used to vhange elements when uploading PDF-files. Reacting on the isParent checkbox.
 * @author Kim Terp ...(kim@icn.dk)... | 05/07/2008 ~ 15.00.11 | 
 */
function selectDefaultValueForArticleType()
{
	/*
	alert( "inde" );
	var elementName = "create_article_articleType_identity";
	var element = document.getElementById( elementName );
	for ( var i = 0; i < element.length; i++ )
	{
		if ( element[i].name == "STANDARD_TEKST" )
		{
			element[i].selected = true;
		}
	}
	*/
}

function articleTypeSelectBoxChanged()
{
	/*
	//Element ArticleType SelectList
	var elementNameArticleType = "create_article_articleType_identity";
	var elementArticleType = document.getElementById( elementName );
	//Element BuildingComponent Selectlist
	var elementNameBuildingComponent = "create_article_buildingComponent_identity";
	var elementBuildingComponent = document.getElementById( elementName );
	//Element Structure Selectlist
	var elementNameStructure = "create_article_structure_identity";
	var elementStructure = document.getElementById( elementName );
	
	if( elementArticleType.value == "BYGNINGSDELS_TEKST" )
	{
		elementBuildingComponent.disabled = false;
		elementStructure.disabled = false;
	}
	else
	{
		elementBuildingComponent.disabled = true;
		elementStructure.disabled = true;
	}
	*/
}

//---------------PDF RELATED FUNCTIONS---------------------//

/**
 * Used to vhange elements when uploading PDF-files. Reacting on the isParent checkbox.
 * @author Kim Terp ...(kim@icn.dk)... | 05/07/2008 ~ 14.58.11 | 
 */
function pdfParentCheckboxChange()
{
	if ( document.getElementById( "pdfForm_pdf_parent" ).checked )
	{
		document.getElementById( "pdfForm_pdf_parentPDF_id" ).disabled = true;
	}
	else
	{
        document.getElementById( "pdfForm_pdf_parentPDF_id" ).disabled = false;
		
	}
}//End pdfParentCheckboxChange()

function alertObject(obj,comment) {
  var str = (comment)? comment+"\n":'{';
  for(var el in obj) {
    var value = obj[el];
    if ( typeof value == "function" ) {
      value = "[<FUNCTION>]";
    }
    str += el + " : "+ value +" , ";
  }
  str +=" }"
  alert(str);
}

function scrollToEl(target, e) {
  var toElement = $(target).attr('href');  
  toElement = toElement.split('#');
  if (toElement && toElement.length == 2) {
  	toElement = toElement[1];
  	toElement = $('[name=' + toElement + ']');
  	if (toElement.length == 1) {
  	  $.scrollTo(toElement[0], 300, {offset:-15, axis: 'y'});
  	}
  }  
  e.preventDefault();
}

$(document).ready(function() {
	
	$('a[href^=#]').live('click',function(e) {		
    scrollToEl(this, e);
    return false;
  })
  
  $.address.init(function(event) {
    $('form').address();    
    $('#buildingComponentList').prevAll('a:first').click(function() {    	
      toggleVisibility('buildingComponentList', true);
    });    
    
    $('#topmenu a, a.orange:gt(0)').click(function() {    	
      $('#buildingComponentList').hide();    
    })
    
  }).change(function(event) {      
  	if (event.value == '/') {
  		sendAsyncRequest('#content', homePage);
  	} else {
  		if (event.value.indexOf('/') > -1) {
  			if (event.value.indexOf('/search/') > -1) {  				
  				if (event.value != '/') {
  					var currForm = $('form[action*=' + event.value.split('?')[0] + ']');
  					if (currForm.length > 0) {
    					currForm.ajaxSubmit({
                  target: '#content'
              });
  					}
          } 
  			} else {
      		var link;
  				if (event.value.split(',').length > 1) {  					
  					var linksUrl = event.value.split(',');
  					var currLink = $('[href*=' + linksUrl[linksUrl.length - 1] + ']');  					
  					if (currLink.length == 1) {
  						link = currLink; 
  					} else if (currLink.length == 0) {
    					window.stopLoadContent = true;
    					jQuery.ajaxSetup({async: false});
    					for (var i = 0; i < linksUrl.length; i++) {    						
    						if (i == linksUrl.length - 1) {
    							window.stopLoadContent = false;
                  jQuery.ajaxSetup({async: true});
                  sendAsyncRequest('#content', linksUrl[i]);
    						} else {
    						
      						var currLink = $('[href*=' + linksUrl[i] + ']');
      						if (currLink.length == 0) {
      							sendAsyncRequest('#content', homePage);
      							return;
      						}      						
      						if (currLink[0] && currLink[0].onclick) {         
                    currLink[0].onclick.call(link[0]);
                  }
                  var jsCode = currLink.attr('href');
                  if (jsCode) {
                    if (currLink.is(':hidden')) {
                      currLink.parents('ul:last').show();
                    }                  
                    eval(jsCode);
                  }
    						}
    					}   					
    					return;
  					}
  				} else {
  					link = $('[href*=' + event.value + ']');
  					if (link.length == 0) {
  						sendAsyncRequest('#content', homePage);
  						return;
  					}
  					link = $(link[0]);
  				}
      			
    		  if (link[0] && link[0].onclick) {  		  	
    		    link[0].onclick.call(link[0]);
    		  }
    		  var jsCode = link.attr('href');      		  
    		  if (jsCode) {
    		  	if (link.is(':hidden')) {
    		  		link.parents('ul:last').show();
    		  	}
    		    eval(jsCode);
    		  }
      		
  			}
  		}
  		
  	}
  }).externalChange(function(event) {
  	var link = $('[href*=' + event.value + ']');
  	if (link.length == 1) {
  		if (link.attr('href').indexOf("toggleVisibility('buildingComponentList')") > -1) {
  			toggleVisibility('buildingComponentList', true);
  		}
  	}  	
  })
  
  function parseLocationHref(partNum) {
  	if (window.location.href.indexOf('/search/') > -1) return '';
  	
  	var result = window.location.href.split('#');
  	if (result.length == 2) {  		
  		result = result[1].split(',');
  		var res = '';
  		for (var i = 0; i < result.length; i++) {
  			if (result.length > partNum - 1 && i == result.length - 1) break;
  			res += res != '' ? ',' : '';
  			res += result[i];
  		}
  		result = res; 
  	} else {
  		result = '';
  	}
  	return result;   
  }
  
	$('a[href^=javascript]').address(function() {		
		var url = $(this).attr('href').split(';');
		url = url[url.length-1] ? url[url.length-1] : url[url.length-2];
		url = url.match(/(\/.*)'/gi);		
		
		
		if (!url) return false; 
		
		url = url.length > 0 ? url[0].substring(0, url[0].length - 1) : homePage;		
		if (url.split(',').length > 1) {
			url = url.split(',');			
		  url = url[url.length - 1].substring(1, url[url.length - 1].length - 1);		  
		}
		if ($(this).parents('#articlesMenu').length > 0) {			
			var addonUriPart = parseLocationHref(2);
  		if (addonUriPart) {
  			url = addonUriPart + ',' + url; 
  		}
		}		
		
		if ($(this).parents('#content').length > 0) {      
      var addonUriPart = parseLocationHref(3);
      if (addonUriPart) {
        url = addonUriPart + ',' + url; 
      }
    }        
    return url;		
  });  
});