var $ = jQuery.noConflict();

$(document).ready(function($){
	
	if($('.alertBox').length > 0){
		$('.alertBox').show();
	}
	
	$('#blockLeft').height($('#blockMain').height()-$('#blockTop').height()-$('#blockBeforeStart').height()-120);
	if($('.elementMarketInfo').length > 0){
	  xOffset = 10;
    yOffset = 20;
		
		jQuery(".elementMarketInfo .performance").hover(function(e){
		  var title = jQuery(this).next().text();
		
		  jQuery("body").append('<p id="tooltip">'+ title +'</p>');
		  jQuery("#tooltip")
		   .css("top",(e.pageY - xOffset) + "px")
		   .css("left",(e.pageX + yOffset) + "px")
		   .fadeIn("fast");
		  },
		  function(){
		    var title = jQuery(this).next().text();
		    jQuery("#tooltip").remove();
		  });
		  jQuery(".elementMarketInfo .performance").mousemove(function(e){
		  jQuery("#tooltip")
		   .css("top",(e.pageY - xOffset) + "px")
		   .css("left",(e.pageX + yOffset) + "px");
		  });
  }
	
	$('body').delegate('a.closeEmbed', 'click', function(){
 	  $(this).parents('.navLevel1Embed').hide();
    $(this).parents('.navLevel1Embed').parent().removeClass('active');
	});
	
	$('body').delegate('a.embed', 'click', function(e){
	  e.preventDefault();
  	if(typeof wEditor == 'object')
      return false;
    
    var id			  = $(this).attr('id').replace('navpoint_', '');
    var nav_point = $('#navpoint_' + id);
    var nav_embed = $('#navEmbed_' + id);
    var width = (nav_embed.parent().parent().parent().attr('id') == 'navigationMeta') ? 521 : 720;
    var url = $(this).attr('href'); 
    
    if($('ul.navLevel1Container > li.active').length > 0){
      $('ul.navLevel1Container > li.active').each( function(i, e){
        if($('a.navLevel1', this).attr('id') != 'navpoint_'+id){
          $('div.navLevel1Embed', this).hide();
          $(this).removeClass('active');
        }
      })
    }
   
    if(nav_point.data('hasLoaded') == '1'){             
      if(nav_point.parent('li').hasClass('active') === true){
        nav_embed.hide();
        nav_point.parent().removeClass('active');
      }  
      else{
        nav_point.parent().addClass('active');
        nav_embed.show();                                 
      }               
    }
    else{        
      if(nav_point.parent().hasClass('active')){
        nav_embed.hide();
        nav_point.parent().removeClass('active');
      }    
      else{
        url = url.indexOf('?') == -1 ? url + '?viewmode=blank' : url + '&viewmode=blank';
        url = url.replace('[URL]', location.pathname + location.search);
        nav_point.data('hasLoaded', '1'); 
				nav_point.parent().addClass('active');
			
        $.ajax({
      		url: url,
      		type: 'GET',
      		timeout: 1500,
      		dataType: 'html',
      		beforeSend: function(){
      		  var left = width/2;
      		  $('<img src="/global/wGlobal/layout/images/icons/ajax-loader.gif" alt="loading..." style="margin: 20px 0 0 '+left+'px;"/>').appendTo('#navEmbed_' + id + ' .navLevel1EmbedInner');
      		  nav_embed.show();
      		},
      		error: function() {
      			//alert('Error loading html data!');
      			return false;
      		},
      		success: function(data){      		  
      		  
      		  $('#navEmbed_' + id + ' .navLevel1EmbedInner > img').remove();      		  
      		  $(data).appendTo('#navEmbed_' + id + ' .navLevel1EmbedInner');
      		}
      	});
      }
    }
  });
  
});



////////////////////////////////////////////////////////////////
//
// @method void wOpenURL(url)
//
// @desc Öffnet eine Seite
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenURL(url){
  if(location.pathname.indexOf('/weblication/grid5/') != 0){
    top.location.href = url;
  }
}

////////////////////////////////////////////////////////////////
//
// @method void wOpenPopupURL(url, width, height)
//
// @desc Öffnet eine Seite in einem Popup
//
// @return void
//
////////////////////////////////////////////////////////////////

function wOpenPopupURL(url, width, height){

  var name = '';
  
  if(width == '' || width == ' '){
    width = 786;
  }
  if(height == '' || height == ' '){
    height = 677;
  }
  
  url = url.replace(/&amp;/g, '&');
  options = ',location=no, menubar=no,toolbar=no,resizable=no,scrollbars=yes,status=no';
  width   = parseInt(width);
  height  = parseInt(height);

  var posLeft = (screen.width / 2)  - (width  / 2);
  var posTop  = (screen.height / 2) - (height / 2);
  window.open(url, name, 'top='+posTop+',left='+posLeft+',width='+width+',height='+height+',' + options);
}

////////////////////////////////////////////////////////////////
//
// @method void wSendMailNospam(data)
//
// @desc Öffnet eine E-Mail zum Versenden
//
// @return void
//
////////////////////////////////////////////////////////////////

function wSendMailNospam(data){
  if(data.indexOf('|') > 0) {
	location.href = 'mailto:' + data.replace('|', '@');
	}
	else {
	location.href = 'mailto:' + data + '@banklinth.ch';
	}
}

////////////////////////////////////////////////////////////////
//
//	Metanavigation Funktionen
//
////////////////////////////////////////////////////////////////

function ToggleMetaNavi(id){
	document.getElementById('OnlineSchalter').style.display = 'none';
	document.getElementById('Standorte').style.display = 'none';
	document.getElementById('Ueberuns').style.display = 'none';
	document.getElementById('Kontakt').style.display = 'none';
	if(document.getElementById(id).style.display == 'block'){
		document.getElementById(id).style.display = 'none';
	}else{
		document.getElementById(id).style.display = 'block';
	}
}

function ToggleHauptNavi(id){
	document.getElementById('Private').style.display = 'none';
	document.getElementById('Firmen').style.display = 'none';
	document.getElementById('Jugendliche').style.display = 'none';
	document.getElementById('Banking').style.display = 'none';
	if(document.getElementById(id).style.display == 'block'){
		document.getElementById(id).style.display = 'none';
	}else{
		document.getElementById(id).style.display = 'block';
	}
}


function loadContent(file, action, rechner){
jQuery.ajax({
		url: file,
		data: action,
		type: 'GET',
		timeout: 500,
		dataType: 'html',
		success: function(data){
        jQuery('#'+rechner).html(data);
		}
	})
}

////////////////////////////////////////////////////////////////
//
// Beschreibung: Zeigt Seite empfehlen an an
//
////////////////////////////////////////////////////////////////

function wTellAFriend(filepath){  
  var url = location.pathname;
  var anchor = location.hash;

  url = url.replace(anchor, '');
  
  if(url.indexOf('?') >= 0){
    url += '&viewmode=blank';
  }
  else{
    url = url.replace('?', '&');
    url += '&viewmode=blank';    
  }
  
  openWindow(filepath+'?page='+url, 'wTellAFriend', 'width=325,height=520,scrollbars=no');
}

function NavShortHide(punkt){

  if(jQuery('ul li a.embed').length > 0){
      jQuery('ul li a.embed').each( function(i, e){
        if(jQuery(this).text() == punkt){
          jQuery(this).trigger('click');
        }
      })
    }

}