var debugging = true; // or true
if (typeof console == "undefined") var console = { log: function() {} };
else if (!debugging || typeof console.log == "undefined") console.log = function() {};


$(document).ready(function(){

  //Hide (Collapse) the toggle containers on load
	$(".news-list-content-container").hide(); 

	//Switch the "Open" and "Close" state per click
	$("div.news-trigger").toggle(
		function(){
			$(this).addClass("active");
		}, function () {
			$(this).removeClass("active");
		}	
	);
	
	//Switch the "Open" and "Close" state per click
	$("div.jobs-trigger").toggle(
		function(){
			$(this).addClass("active");
		}, function () {
			$(this).removeClass("active");
		}
	);

	//Slide up and down on click
	$("div.news-trigger").click(function(){
		$(this).next(".news-list-content-container").slideToggle("slow");
	});
	
	//Slide up and down on click
	$("div.jobs-trigger").click(function(){
		$(this).next(".news-list-content-container").slideToggle("slow");
	});
	
	// NEWS SINGLE VIEW
	
	$("div.news-single-trigger").addClass("active");
  
	//Switch the "Open" and "Close" state per click
	$("div.news-single-trigger").toggle(
		function(){
			$(this).removeClass("active");
		}, 
		function() {
			$(this).addClass("active");
		}
	);

	//Slide up and down on click
	$("div.news-single-trigger").click(function(){
		$(this).next(".news-list-content-single-container").slideToggle("slow");
	});
	
	// Scroll Panes
  	setupScrollPane("atom", "div.atom_items", "span.atom_item_content");
	setupScrollPane("youtube", "li", "");
  	setupScrollPane("wttwitter", "div.twitter-container", "");
	setupScrollPane("news", "div.news-latest-item", "");
  	

	// SOCIAL BOOKMARKS
	$(".icons").hover(
    	function() {
      		var tit = $(this).attr('title');
      		$("#bookmarks-title").html(tit);
    	},
    	function() {
			$("#bookmarks-title").html('');
		}
	);
	
	// LANGUAGE
	$("#language-box").hover(  	
		function() {
			$(".language-button-hidden").show();
		},
		function() {
			$(".language-button-hidden").hide();
		}
	);
   
 	// FEED WIDGET
	$(".atom_item_content").hide();
	$(".atom_pager").hide();

	var feed_pos = 0;
	
	$(".atom_item_header").toggle(
		function(){
			var header = $(this);			
			var item = header.parent();		
			var content = header.next();
	
			feed_pos = $("#atom .wgtContent").css('top');
			$("#atom .wgtContent").css('top', '0');
	
			$(".atom_items").not(item).hide();
			$(".atom_feed .button").hide();
			content.show();

			$("#atom").height('auto');
			$(".atom_pager").show();

			return false;
		},
		function() {
			var header = $(this);
			var item = header.parent();		
			var content = header.next();

			content.slideUp('fast');		
			$(".atom_feed .button").show();
			$(".atom_items").not(item).show();
			$("#atom .wgtContent").css('top', feed_pos);

			$("#atom").height('22em');
			$(".atom_pager").hide();

			return false;
		}
	);

	
	$("#atom .close-button").click(function() {
		$(".atom_item_header:visible").trigger('click');
	});



	$("#atom .atom_prev").click(function() {
		var item = $(".atom_items:visible");
		var prev_item = item.prev();

		item.find(".atom_item_header").trigger('click');
		
		if(prev_item.attr('class') == 'atom_items') {
			prev_item.find(".atom_item_header").trigger('click');
		}
	});
   
	$("#atom .atom_next").click(function() {
		var item = $(".atom_items:visible");
		var next_item = item.next();

		item.find(".atom_item_header").trigger('click');
		
		if(next_item.attr('class') == 'atom_items') {
			next_item.find(".atom_item_header").trigger('click');
		}
		
	});



	$(".quality-image img").fadeTo("fast", 0.2); // This sets the opacity of the thumbs to fade down to 60% when the page loads
  
	$(".quality-image img").hover(
		function(){
			$(this).fadeTo("fast", 1.0); // This should set the opacity to 100% on hover
	  	},
	  	function(){
			$(this).fadeTo("slow", 0.2); // This should set the opacity back to 60% on mouseout
	  	}
  	);
  
  	$(".quality-image img").fadeTo("fast", 0.2); // This sets the opacity of the thumbs to fade down to 60% when the page loads
  

	// REFERENCES FISHEYE (DOCK EFFECT)
	$('#reference-content-line1').Fisheye({
		maxWidth: 60,
		items: 'a',
		itemsText: 'span',
		container: '.fisheyeContainer',
		itemWidth: 78,
		proximity: 97.5,
		halign: 'center',
		pone : 0.5,
		itemHeight: 35
	});


	$('#reference-content-line2').Fisheye({
		maxWidth: 60,
		items: 'a',
		itemsText: 'span',
		container: '.fisheyeContainer',
		itemWidth: 78,
		proximity: 97.5,
		halign: 'center',
		pone : -0.4,
		itemHeight: 35
	});
	
	
	// LOGIN
	$("#loginform").validate({
		rules: {
			username: "required",
			password: "required"
		},
		messages: {
			username: "",
			password: ""
		},
		errorContainer: ".error-login",
		submitHandler: function(form) {
			$("#error-logincheck").hide();
			$(form).ajaxSubmit({
/*				beforeSubmit: function() {
					$("#error-logincheck").hide();
				},
*/
				url: '/proxy.php',
				type: 'post',
				dataType: 'xml',
				success: function(xml){
					var failure = $('failure', xml).text();
					var success = $('success', xml).text()
			
					if(failure) {
						//$("#login #error-logincheck").html(failure).show();
						$("#login .error-login").show();
						$("#login input#username").addClass('error');

						if($("#login span[htmlfor=username]").length > 0) {
							$("span[htmlfor=username]").show();
						} else {
							$("#login p.username").append('<span htmlfor="username" generated="true" class="error" style="display: block;"></span>');
						};
							
						$("#login input#password").addClass('error');						
	
						if($("#login span[htmlfor=password]").length > 0) {
							$("span[htmlfor=password]").show();
						} else {
							$("#login p.password").append('<span htmlfor="password" generated="true" class="error" style="display: block;"></span>');
						};

					} else {
						$("#login .error-login").hide();
						form.submit();
					}
				} 
			});		
		}
	});
	
	
	// LINK FIX
	// Twitter
	$(".twitter-info p>a").each(function() {
		$(this).attr('target', '_blank');
	});
	
	// News
	$(".browseLinksWrap a").each(function(){
		var target = $(this).attr('href');

		if(target.search(/detailansicht/) != -1) {
			var new_target = target.replace(/detailansicht/, "uebersicht").split('/').slice(0,4).join('/');
			$(this).attr('href', new_target + ".html");
		}
		
	}); 
	
});



// POPUPS
TopUp.addPresets({
  "#youtube a": {
    type: "iframe",
    width: "800",
    height: "600",
    layout: "dashboard",
    modal: 1,
    shaded: 1,
    effect: "fade",
    resizable: 0,
    overlayClose: 1
  },
  
   ".poll a": {
    type: "iframe",
    width: "800",
    height: "600",
    layout: "dashboard",
    modal: 1,
    shaded: 1,
    effect: "fade",
    resizable: 0,
    overlayClose: 1
  },
  
  
  ".callback-service a": {
    type: "iframe",
    width: "309",
    height: "377",
    layout: "dashboard",
    modal: 1,
    shaded: 0,
    effect: "fade",
    resizable: 0,
    overlayClose: 1
  },
  
   "#aktion-widget a": {
    type: "iframe",
    width: "800",
    height: "400",
    layout: "dashboard",
    modal: 1,
    shaded: 1,
    effect: "fade",
    resizable: 0,
    overlayClose: 1
  },
  
  "a.navipunkt11": {
    type: "iframe",
    width: "296",
    height: "306",
    layout: "dashboard",
    x: 600,
    y: 50,
    modal: 1,
    shaded: 0,
    effect: "fade",
    resizable: 0,
    overlayClose: 1
  },
  
  "a.navipunkt413": {
    type: "iframe",
    width: "296",
    height: "306",
    layout: "dashboard",
    x: 600,
    y: 50,
    modal: 1,
    shaded: 0,
    effect: "fade",
    resizable: 0,
    overlayClose: 1
  },
  
  "a.popup800": {
    type: "iframe",
    width: "800",
    height: "600",
    layout: "dashboard",
    modal: 1,
    shaded: 1,
    effect: "fade",
    resizable: 0,
    overlayClose: 1
  },
  
  "a.popup480": {
    type: "iframe",
    width: "480",
    height: "320",
    layout: "dashboard",
    modal: 1,
    shaded: 1,
    effect: "fade",
    resizable: 0,
    overlayClose: 1
  },
  
  "a.popup640": {
    type: "iframe",
    width: "640",
    height: "480",
    layout: "dashboard",
    modal: 1,
    shaded: 1,
    effect: "fade",
    resizable: 0,
    overlayClose: 1
  }
  
});

// SCROLLPANE
function setupScrollPane(widget_id, selector, exclude) {
	var widget = $("#"+widget_id);
	var upBtn = widget.children(".upBtn:first");
	var downBtn = widget.children(".downBtn:first");
	var content = widget.children(".wgtContent:first");
	
	var cHgt = widget.height() - upBtn.height() - downBtn.height()-80;
	//alert(cHgt);

	content
	.find(selector)
	.each(function(i) {
		//console.log(exclude);
		//item = $(this).clone().children().remove(exclude).end();
		//console.log($(this).clone().children().remove(exclude).end().height());
		itemHeight = $(this).outerHeight(true);
		
		if(exclude) {
			itemHeight -= $(this).find(exclude).height()-13;
		}
		
		//console.log(widget_id+" item "+i+": "+itemHeight);
	});
	//console.log(widget_id+" inizialized");


	upBtn.click(function(){
		if(parseInt(content.css("top")) < -cHgt) {
			content.animate({"top": "+="+cHgt+"px"}, "slow");
		} else {
			content.animate({"top": "0"}, "slow");		
		}
	});
		
	downBtn.click(function(){		
		if(parseInt(content.css("top"))*(-1) < content.height()-2*cHgt ) {
			content.animate({"top": "-="+cHgt+"px"}, "slow");
		} else {
			content.animate({"top": "-"+(content.height()-cHgt)+"px"}, "slow");		
		}
	});
}

// SOCIAL WIDGET

// OPEN LAYER WITH FLASH

function flashCall640(url)
{	
	var c = {
    	type: "iframe",
	    title: "Exelution",
	    width: "640",
	    height: "480",
	    layout: "dashboard",
	    modal: 1,
	    shaded: 1,
	    effect: "fade",
	    resizable: 0,
	    overlayClose: 1
    };
	TopUp.display(url, c);
}

function flashCall800(url)
{	
	var c = {
	    type: "iframe",
	    title: "Exelution",
	    width: "800",
	    height: "600",
	    layout: "dashboard",
	    modal: 1,
	    shaded: 1,
	    effect: "fade",
	    resizable: 0,
	    overlayClose: 1
    };
	TopUp.display(url, c);
}


function flashCall480(url)
{	
    var c = {
 	type: "iframe",
	width: "480",
	height: "320",
	layout: "dashboard",
	modal: 1,
	shaded: 1,
	effect: "fade",
	resizable: 0,
	overlayClose: 1
    };
	TopUp.display(url, c);
}


