/* ----------------------------------------------------------
	init functions on page load
------------------------------------------------------------- */
jQuery(document).ready(function(){
	
	//add class to body tag for js-hides and shows
	jQuery("body").addClass("vc_jq");
	
	//site features
	vc_initTotop();
	vc_initSlideshow();
	vc_initToggleNews();
	vc_initMailSpamProtect();
	
	//construct functions
	vc_initConstructMainInfoScrollbarSlide();
	vc_initConstructBottomScrollbarSlide();
	vc_initConstructShowTitle();
	//vc_initConstructGetInfos();
});


/* ------------------------------------------------------------------------------------------------------------
	toplink
------------------------------------------------------------------------------------------------------------ */
function vc_initTotop(){
	jQuery(".vc_link_top").click(function() {
		jQuery.scrollTo(0, 600);
		return false;
	});
}

/* ------------------------------------------------------------------------------------------------------------
	printlink
------------------------------------------------------------------------------------------------------------ */
function vc_initPrint(lang){
	if (lang == 'de') {
		title = 'Seite drucken';
	} else if (lang == 'en') {
		title = 'print page';
	}
	document.write('<a href="javascript:print();" class="vc_link_print" title="' + title + '"><span>' + title + '</span></a>');
}

/* ------------------------------------------------------------------------------------------------------------
	load style from styleswitcher
------------------------------------------------------------------------------------------------------------ */
function vc_initStyleswitcher(lang) {
	if (lang == 'de') {
		title = 'Schriftgröße anpassen';
	} else if (lang == 'en') {
		title = 'adjust fontsize';
	}
	document.write('<a href="#" onclick="return false;" class="vc_link_fontsize" title="' + title + '"><span>' + title + '</span></a>');
	
	vc_loadStyle();
	
	
	jQuery("a.vc_link_fontsize").click(function(){
		if(vc_style == 'small-fontsize') {
			vc_setStyle("standard-fontsize");
		} else if(vc_style == 'standard-fontsize') {
			vc_setStyle("large-fontsize");
		} else if(vc_style == 'large-fontsize') {
			vc_setStyle("small-fontsize");
		}
	});
	
}

/* ------------------------------------------------------------------------------------------------------------
	scrollbar in vc_construct_main_info
------------------------------------------------------------------------------------------------------------ */
function vc_initConstructMainInfoScrollbarSlide() {
	//set css
	jQuery("div.vc_construct_main_info_content").css("overflow", "hidden");
	jQuery("div.vc_construct_main_info_content").css("height", "auto");
	jQuery("div.vc_construct_main_info_content").css("top", "0");
	
	// add scrollbar
	jQuery("div.vc_construct_main_info_scrollbar").html('<a href="#" onclick="return false;" class="vc_jq_scroll_up vc_jq_scroll_up_disabled" title="nach oben"><span>nach oben</span></a><br /><a href="#" onclick="return false;" class="vc_jq_scroll_down vc_jq_scroll_down_disabled" title="nach unten"><span>nach unten</span></a>');
	
	if(jQuery("div.vc_construct_main_info_content").height() - jQuery("div.vc_construct_main_info_wrapper").height() > 0) {
		jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_down").removeClass("vc_jq_scroll_down_disabled");
	} else {
		jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_down").hide();
		jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_up").hide();
	}		
	
	//functions	
	function checkScrollState() {
		var div_scrolllength = jQuery("div.vc_construct_main_info_content").height() - jQuery("div.vc_construct_main_info_wrapper").height();
		var div_position = jQuery("div.vc_construct_main_info_content").position();
		
		if ( div_position.top < 0 && div_position.top > (div_scrolllength * -1) ) {				
			jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_up").removeClass("vc_jq_scroll_up_disabled");
			jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_down").removeClass("vc_jq_scroll_down_disabled");
		} else if ( div_position.top == (div_scrolllength * -1) ) {			
			jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_up").removeClass("vc_jq_scroll_up_disabled");
			jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_down").addClass("vc_jq_scroll_down_disabled");
		} else if ( div_position.top == 0 ) {			
			jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_up").addClass("vc_jq_scroll_up_disabled");
			jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_down").removeClass("vc_jq_scroll_down_disabled");
		}
	}
	
	function scrollDownStart(){
		var time = 800 //for 100px;		
		var div_scrolllength = jQuery("div.vc_construct_main_info_content").height() - jQuery("div.vc_construct_main_info_wrapper").height();
		var div_position = jQuery("div.vc_construct_main_info_content").position();
		var scrolltime = (div_scrolllength/100) * time;
		var act_scrolltime = (scrolltime/div_scrolllength) * (div_scrolllength + div_position.top); // + because of - - = +
		
		jQuery("div.vc_construct_main_info_content").animate({"top": div_scrolllength * -1}, act_scrolltime, checkScrollState);
	}
	
	function scrollUpStart(){
		var time = 800 //for 100px;
		var div_scrolllength = jQuery("div.vc_construct_main_info_content").height() - jQuery("div.vc_construct_main_info_wrapper").height();
		var div_position = jQuery("div.vc_construct_main_info_content").position();
		var scrolltime = (div_scrolllength/100) * time;
		var act_scrolltime = (scrolltime/div_scrolllength) * (0 - div_position.top); // - because of - - = +
		
		jQuery("div.vc_construct_main_info_content").animate({"top": 0}, act_scrolltime, checkScrollState);
	}

	function scrollStop() {
		jQuery("div.vc_construct_main_info_content").stop();
		checkScrollState();
	}
	
	// events
	jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_down").hover(function() {
		scrollDownStart();
	}, function(){
		scrollStop();
	});
	
	jQuery("div.vc_construct_main_info_scrollbar a.vc_jq_scroll_up").hover(function() {
		scrollUpStart();
	}, function(){
		scrollStop();
	});
}

/* ------------------------------------------------------------------------------------------------------------
	scrollbar in vc_construct_bottom_wrapper_content
------------------------------------------------------------------------------------------------------------ */
function vc_initConstructBottomScrollbarSlide() {
	//set css
	//jQuery("div.vc_construct_bottom_wrapper_content ul").css("left", "0");
	
	//set width for scrolling
	width = (jQuery("div.vc_construct_bottom_wrapper_content ul li").outerWidth() * jQuery("div.vc_construct_bottom_wrapper_content ul li").length ) - (jQuery("div.vc_construct_bottom_wrapper_content ul li").outerWidth() - jQuery("div.vc_construct_bottom_wrapper_content ul li").width());
	jQuery("div.vc_construct_bottom_wrapper_content ul").width(width);
	
	// add scrollbar
	jQuery("div.vc_construct_bottom_wrapper_scrollbar").append('<a href="#" onclick="return false;" class="vc_jq_scroll_left vc_jq_scroll_left_disabled" title="nach links"><span>nach links</span></a><br /><a href="#" onclick="return false;" class="vc_jq_scroll_right vc_jq_scroll_right_disabled" title="nach rechts"><span>nach rechts</span></a>');
	
	if(jQuery("div.vc_construct_bottom_wrapper_content ul").width() - jQuery("div.vc_construct_bottom_wrapper_content").width() > 0) {
		jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_right").removeClass("vc_jq_scroll_right_disabled");
	} else {
		jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_right").hide();
		jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_left").hide();
	}	
	
	//functions	
	function checkScrollState() {
		var div_scrolllength = jQuery("div.vc_construct_bottom_wrapper_content ul").width() - jQuery("div.vc_construct_bottom_wrapper_content").width();
		var div_position = jQuery("div.vc_construct_bottom_wrapper_content ul").position();
		
		if ( div_position.left < 0 && div_position.left > (div_scrolllength * -1) ) {				
			jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_left").removeClass("vc_jq_scroll_left_disabled");
			jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_right").removeClass("vc_jq_scroll_right_disabled");
		} else if ( div_position.left == (div_scrolllength * -1) ) {			
			jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_left").removeClass("vc_jq_scroll_left_disabled");
			jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_right").addClass("vc_jq_scroll_right_disabled");
		} else if ( div_position.left == 0 ) {			
			jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_left").addClass("vc_jq_scroll_left_disabled");
			jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_right").removeClass("vc_jq_scroll_right_disabled");
		}
	}
	
	function scrollRightStart(){
		var time = 800 //for 100px;
		var div_scrolllength = jQuery("div.vc_construct_bottom_wrapper_content ul").width() - jQuery("div.vc_construct_bottom_wrapper_content").width();
		var div_position = jQuery("div.vc_construct_bottom_wrapper_content ul").position();
		var scrolltime = (div_scrolllength/100) * time;
		var act_scrolltime = (scrolltime/div_scrolllength) * (div_scrolllength + div_position.left); // + because of - - = +
		
		jQuery("div.vc_construct_bottom_wrapper_content ul").animate({"left": div_scrolllength * -1}, act_scrolltime, checkScrollState);
	}
	
	function scrollLeftStart(){
		var time = 800 //for 100px;
		var div_scrolllength = jQuery("div.vc_construct_bottom_wrapper_content ul").width() - jQuery("div.vc_construct_bottom_wrapper_content").width();
		var div_position = jQuery("div.vc_construct_bottom_wrapper_content ul").position();
		var scrolltime = (div_scrolllength/100) * time;
		var act_scrolltime = (scrolltime/div_scrolllength) * (0 - div_position.left); // - because of - - = +
		
		jQuery("div.vc_construct_bottom_wrapper_content ul").animate({"left": 0}, act_scrolltime, checkScrollState);
	}

	function scrollStop() {
		jQuery("div.vc_construct_bottom_wrapper_content ul").stop();
		checkScrollState();
	}
	
	// events
	jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_right").hover(function() {
		scrollRightStart();
	}, function(){
		scrollStop();
	});
	
	jQuery("div.vc_construct_bottom_wrapper_scrollbar a.vc_jq_scroll_left").hover(function() {
		scrollLeftStart();
	}, function(){
		scrollStop();
	});
}


/* ------------------------------------------------------------------------------------------------------------
	show title in vc_construct_bottom_wrapper_scrollbar
------------------------------------------------------------------------------------------------------------ */
function vc_initConstructShowTitle(){
	
	//show vc_active title
	jQuery("div.vc_construct_bottom_wrapper_scrollbar span.vc_construct_bottom_wrapper_scrollbar_title").html(jQuery("div.vc_construct_bottom_wrapper_content ul li.vc_active a").attr("title"));
	
	// change on hover or focus
	jQuery("div.vc_construct_bottom_wrapper_content ul li a").vc_hoverFocus(function(){
		jQuery("div.vc_construct_bottom_wrapper_scrollbar span.vc_construct_bottom_wrapper_scrollbar_title").html(jQuery(this).attr("title"));
	}, function() {
		jQuery("div.vc_construct_bottom_wrapper_scrollbar span.vc_construct_bottom_wrapper_scrollbar_title").html(jQuery("div.vc_construct_bottom_wrapper_content ul li.vc_active a").attr("title"));
	});
}

/* ------------------------------------------------------------------------------------------------------------
	show infos from ajax in vc_construct_main_info_content
------------------------------------------------------------------------------------------------------------ */
var vc_initConstructGetInfosAnimate = 0;
var vc_initConstructGetInfosHtml = '';

function vc_initConstructGetInfos(){
	
	// change icon state
	jQuery("div.vc_construct_bottom_wrapper_content ul li a").click(function(){
		jQuery(this).parent().parent().find("li.vc_active").removeClass("vc_active");
		jQuery(this).parent().addClass("vc_active");
	});
	
	//load info-content via ajax
	jQuery("div.vc_construct_bottom_wrapper_content ul li a").click(function(){
		if (jQuery(this).hasClass("vc_jq_follow_link")) {			
			window.location.href = jQuery(this).attr("href");
		} else {			
			jQuery("div.vc_construct_main_info_content").html('<div class="vc_construct_main_nocontent"><img alt="Logo - Heinke . Skribe + Partner Rechtsanwälte GmbH" src="/fileadmin/template/img/construct/logo.png"></div>');
			jQuery("div.vc_construct_main_info").stop();
			vc_initConstructGetInfosAnimate = 1;
			jQuery("div.vc_construct_main_info").animate({'right': '-30em'} , 800, function(){ vc_initConstructGetInfosAnimate = 0 });
			
			url = jQuery(this).attr("href");
			if( jQuery(this).parent().hasClass("vc_jq_act") ) {
				datastring = 'ajax=2';
			} else {
				datastring = 'ajax=1';
			}
				
			jQuery.manageAjax.create('ajaxManagerPerson', {
				queue: 'false',
				abortOld: 'true',
				maxRequests: 1
			});
			jQuery.manageAjax.add('ajaxManagerPerson', {
				type: "GET",
				url: url,
				data: datastring,
				success: function(data){
					vc_initConstructGetInfosHtml = jQuery("div.vc_ajax_content", data).html();
					vc_initConstructGetInfosWritter();
				}
			});
		}
	});
}

function vc_initConstructGetInfosWritter(){
	if(vc_initConstructGetInfosAnimate == 0){
		jQuery("div.vc_construct_main_info_content").html(vc_initConstructGetInfosHtml);
		jQuery("div.vc_construct_main_info_content").prepend("<h2>Sie haben die Wahl:</h2>");
		jQuery("div.vc_construct_main_info").stop();					
		jQuery("div.vc_construct_main_info").css({'right': '-30em'});
		jQuery("div.vc_construct_main_info").animate({'right': '-0.01em'} , 800);
		vc_initConstructMainInfoScrollbarSlide();
	} else {
		setTimeout("vc_initConstructGetInfosWritter()", 10);
	}
}


/* ------------------------------------------------------------------------------------------------------------
	extend jquery with own helper functions
------------------------------------------------------------------------------------------------------------ */
jQuery.fn.extend({
	/* checks if an jquery-object has at least one dom-element */
	vc_exists: function() {
	 	if(this.length != 0) { return true; } else { return false; }
	 },
	 
	 /* clear input fields on focus */
	vc_clearInput: function() {
		var searchlabeltext = clearObject.val();		
		this.focus(function(){	if(this.value==searchlabeltext) { this.value=''; } });
		this.blur(function(){ if(this.value=='') { this.value=searchlabeltext; } });
	},
	
	/* generate hover and focus/blur event at one time  */
	vc_hoverFocus: function(fnIn, fnOut) {
		return this.bind('mouseenter', fnIn).bind('mouseleave', fnOut).bind('focus', fnIn).bind('blur', fnOut);
	},
	
	/* generate focus/blur event at one time */ 
	vc_focusBlur: function(fnIn, fnOut) {
		return this.bind('focus', fnIn).bind('blur', fnOut);
	}
});

/* ------------------------------------------------------------------------------------------------------------
	Slideshow
------------------------------------------------------------------------------------------------------------ */
function vc_initSlideshow() {
  // define images to use
  var imageArray=new Array('/fileadmin/template/img/image/slideshow/header01.jpg', '/fileadmin/template/img/image/slideshow/header02.jpg', '/fileadmin/template/img/image/slideshow/header03.jpg', '/fileadmin/template/img/image/slideshow/header04.jpg', '/fileadmin/template/img/image/slideshow/header05.jpg');
  
  // define id of slidewhow list
  slideshowID = 'vc_slideshow';
  
  // start slideshow
  if (jQuery('#' + slideshowID).find("li").length > 1) {
	jQuery('#' + slideshowID).innerfade({
  		animationtype: 'fade',
  		speed: 3000,
  		timeout: 5000,
  		type: 'sequence',
  		containerheight: '27em'
  	});
  }
}

/* ------------------------------------------------------------------------------------------------------------
	toggle news
------------------------------------------------------------------------------------------------------------ */
function vc_initToggleNews() {
  //close boxes
  jQuery("div.vc_t3_news div.vc_t3_news_item div.vc_t3_news_item_content").hide();
  
  // add link
  jQuery("div.vc_t3_news div.vc_t3_news_item h2").wrapInner('<a href="#" title="öffnen/schließen"></a>');
  
  // toggle function
  jQuery("div.vc_t3_news div.vc_t3_news_item h2 a").toggle(function(){
  	jQuery(this).parent().next().show();
  }, function() {
  	jQuery(this).parent().next().hide();
  });
}

/* ------------------------------------------------------------------------------------------------------------
	spamprotect
------------------------------------------------------------------------------------------------------------ */
function vc_initMailSpamProtect(){
	jQuery("a.vc_link_mail").each(function() {
		text = jQuery(this).text();
		text = text.replace(/\(at\)/, '@');
		jQuery(this).text(text);
	});
}
