$(document).ready(function(){
	resizeGradientBoxContent();
	makeRedLineLong();
	registerTabContainers();
	addJwClickEvents();
	addArrowClassForSubMenus();
	addArrowClassToLinks();
});


// Gradient Box Heading-Checker and left-/right resizer
function resizeGradientBoxContent() {
	$(".gradient_box").each(function(i){
		var headerWidth = $(this).find("h1").width();
		if (headerWidth > 50){
			$(this).find(".col_1_rightContent").width(168);
		}
	});	
}


// Following codes makes the vertical line always go to the bottom of the site
// no matter if the left content or the right content needs more height
function makeRedLineLong() {
	var col1 = $("#content_box #col_1");
	var col2 = $("#content_box #col_2");
	var col1h = (col1.height());
	var col2h = (col2.height());
	if (col1h > col2h){
		//col1 is higher so we place the red line as right border of it
		//and remove the border of col2 by giving adding class 'noborder'
		col1.css({'border-right': '3px solid #D9000B'})
		col2.addClass('noborder');
	}
	/*
	 else if (col1h < col2h) {
		//col2 is higher so we place the red line as left border of it
		col2.css({'border-left': '4px solid #D9000B'})
	} else {
		//both cols have the same height, lets say we place it left to col2
		col2.css({'border-left': '4px solid #D9000B'})		
	}
	*/
}

function registerTabContainers(){
	var allTabs = jQuery(".tab-container ul.tabs");
	if(allTabs != null){
		
		allTabs.each(function(index){
			currentContainer = jQuery(".tab-container:eq(" + index + ")");
			registerTabContainer(currentContainer, index);
		});
	}
}

function registerTabContainer(container, containerIndex){
	//copy title-attribute from panes as a TAB-Header-Div
	container.children('.panes').children('div').each(function(index){
		tabHeaderElement = (container.children('.panes').children('div:eq(' + index + ')').children('h1'));
		tabHeader = tabHeaderElement.html();//attr('title'))
		tabHeaderElement.remove();

		//tab-Header is null? Then probably a tab-container was used where the header is somewhere different
		if (tabHeader == null || tabHeader == undefined){
			tabHeaderElement = (container.children('.panes').children('div:eq(' + index + ')').children('.paragraph-container'));
			tabHeader = tabHeaderElement.attr('header');
		}
		
		//TabHeader Should only be null now, if it was not set in the Backend; -> Selber Schuld! ;)
		if (tabHeader == null || tabHeader == undefined){
			tabHeader = "...";
		}
		container.children('.tabs').append('<li class="tab"><a href="#">' + tabHeader + '</a></li>');
	});
	
	
	//remove administrator edit links from tabs as they destroy the container
	jQuery('.tab-container .frontEndEditIconLinks').remove();
	
	
	//Activate Tabbing
	currentTabs = container.children("ul.tabs");	
	currentTabs.tabs(".tab-container:eq(" + containerIndex  + ") div.panes > div");
}


//For opening a JW-Player-Movie in a Fancybox with Background set to dark
function addJwClickEvents() {
	$(".jwPlayMovie").click(function() {
		this.href = $(this).attr('moviePlayerUrl') + "?file=" + $(this).attr('movieUrl') + "&" + $(this).attr('movieOptions');
    	$.fancybox({
     		'autoscale' : false,
     	    'transitionIn' : 'none',
     	    'transitionOut': 'none',
     	    'padding' : 0,
     	    'title'  : this.title,
     	    'width'  : $(this).attr('movieWidth'),
     	    'height' : $(this).attr('movieHeight'),
     	    'type'    : 'swf',
     	    'href'    : this.href,
			'overlayColor'	: "#000000", 
			'overlayOpacity': "0.85", 
     	    'swf'   : { 'wmode':'transparent', 'allowfullscreen':'true' }
  		});
     	  return false;
   	});
}

function addArrowClassForSubMenus() {
	$(".main_menu_sub1 li ul").each(function(i){
		$(this.parentNode).addClass("subMenuArrow");
	});
}

function addArrowClassToLinks() {
	var hasImageElements = $(".teaser_big a:has(img), .gradient_box a:has(img), .teaser_small a:has(img), .div.csc-default div.col_1_rightContent a:has(img)");
	$(".teaser_big a, .teaser_small a, .gradient_box a, div.csc-default div.col_1_rightContent a").each(function(i){
		if(hasImageElements.index($(this)) == -1){
			$(this).addClass("teaserLinkArrow");						
		}
	});
}
