var $j = jQuery.noConflict();


function showLoading() {
	$j.fancybox.showActivity();
}

function hideLoading() {
	$j.fancybox.hideActivity();
}

$j(document).ready(function() {
	timelineLayout();

	if($j('#shipyardWeeks')) {
		var height = $j("#shipyardWeeks").height();
		if(height>0) {
			$j('#topic').height(height+265);
		}
	};

	curvyWindows();
})

$j(window).load(function() {
	curvyForms();

	$j('#upper_username').css({'paddingLeft':'18px', 'width':'92px'});
	$j('#upper_password').css({'paddingLeft':'18px', 'width':'92px'});

	$j('.scrollbarpaper-green').scrollbarPaper();

	$j(".radio").dgStyle();
	$j(".checkbox").dgStyle();

  /*var galleries = $j('.ad-gallery').adGallery();*/

	/*old height 100/138*/
	/*old width 723/513*/
	$j('#autoKeywords').slideshow({
		height:138,
		width:475,
		effecttime:2000,
		time:2000,
		panel:false,
		title:false,
		playframe:false,
		imgzoom:false
	}).playSlide();

	$j('#invitationCesaGeneralAssemblyOsloSlideshow').slideshow({
		height:390,
		width:225,
		effecttime:3000,
		time:5000,
		panel:false,
		title:false,
		playframe:false,
		imgzoom:false
	}).playSlide();

	/*old height 319/482*/
	$j('#autoSlideshow').slideshow({
		height:319,
		width:960,
		effecttime:3000,
		time:5000,
		panel:false,
		title:false,
		playframe:false
	}).playSlide();

	$j('#contactSlideshow').slideshow({
		height:384,
		width:205,
		effecttime:2000,
		time:2000,
		panel:false,
		title:false,
		playframe:false
	}).playSlide();

	$j('#shipsPeopleSlideshow').slideshow({
		height:459,
		width:205,
		effecttime:2000,
		time:4000,
		panel:false,
		title:false,
		playframe:false
	}).playSlide();

	$j('#shipyardWeek2011Slideshow').slideshow({
		height:847,
		width:604,
		effecttime:0,
		time:5000,
		panel:false,
		title:false,
		playframe:false
	}).playSlide();

	var startFeedback = 0;
	if($j('#feedbackSlideshow').length) {
		var max = $j('#feedbackSlideshow div').length-1;
		var min = 0;
		var random = Math.random();
		startFeedback = Math.floor(random * (max - min + 1)) + min;
	}

	$j('#feedbackSlideshow').slideshow({
		height:120,
		width:933,
		index:startFeedback,
		effecttime:2000,
		effect:'scrollDown',
		time:15000,
		panel:false,
		title:false,
		playframe:false
	}).playSlide();

	/*
	$j('#agendaSlideshow').slideshow({
		height:152,
		width:193,
		effecttime:2000,
		effect:'scrollUp',
		time:15000,
		panel:false,
		title:false,
		playframe:false,
		callback:function(i) {
			if($j('#agendaSlideshow li').length>1) {
				$j('#agendaSlideshowIndex').html((i+1)+ '/' + ($j('#agendaSlideshow li').length));
			}
        },
		controls :{         // show/hide controls elements
			'hide':true,    // show controls bar on mouse hover
			'first':false,   // goto first frame
			'prev':true,    // goto previouse frame (if it first go to last)
			'play':true,    // play slideshow
			'next':true,    // goto next frame (if it last go to first)
			'last':false,    // goto last frame
			'help':false,    // show help message
			'counter':false  // show slide counter
		}
	}).playSlide();


	$j('#newsSlideshow').slideshow({
		height:152,
		width:230,
		effecttime:2000,
		effect:'scrollUp',
		time:15000,
		panel:false,
		title:false,
		playframe:false,
		callback:function(i) {
			if($j('#newsSlideshow li').length>1) {
				$j('#newsSlideshowIndex').html((i+1)+ '/' + ($j('#newsSlideshow li').length));
			}
        },
		controls :{         // show/hide controls elements
			'hide':true,    // show controls bar on mouse hover
			'first':false,   // goto first frame
			'prev':true,    // goto previouse frame (if it first go to last)
			'play':true,    // play slideshow
			'next':true,    // goto next frame (if it last go to first)
			'last':false,    // goto last frame
			'help':false,    // show help message
			'counter':false  // show slide counter
		}
	}).playSlide();
	*/

	if($j(".agendaScroll").attr('id')) {
		$j(".agendaScroll").vTicker({
		   speed: 1500,
		   pause: 3000,
		   showItems: 2,
		   mousePause: true,
		   height: 0,
		   direction: 'up'
		});
	}

//	$j(".agendaScroll").jCarouselLite({
//		vertical: true,
//		visible: 2,
//		auto:1000,
//		speed:3000
//	});

	/* viewport_width:932*/
	$j("#pppBanner img").panorama({
		speed: 150000,
		viewport_width: 945,
		direction: 'right',
		start_position: 2490,
		control_display: 'no'
	});

	$j('.tooltip').tooltip({
		effect: 'slide',
		offset: [40, 0],
		relative: true,
		events: {
			def: 'click, blur'
		}
	});

	$$('body').each(function(object) {
		object.setStyle({"visibility":"visible"});
	});
});

self.curvyForms = function() {
	var settings = {
		tl: {radius: 3},
		tr: {radius: 3},
		bl: {radius: 3},
		br: {radius: 3},
		antiAlias: true
	}

	$$('.date_selectbox select').each(function(object) {
			var div = new Element('div');
			div.addClassName('rounded');
			div.addClassName('selectbox');
			if(object.getStyle('width')) {
				var width = parseInt(object.getStyle('width'))+5;
				div.setStyle({
					'width': width+'px'
				});
			}
			
			Element.insert(object, {before: div});
			div.appendChild(object);
			object.addClassName('selectbox');
			if(!object.style.width&&!object.getStyle('width')) {
				object.setStyle({'width': '100%'});
			}
			curvyCorners(settings, div);
	});

	$$('input', 'textarea', 'select', 'a.button').each(function(object) {
		if(object.hasClassName('button')) {
			//var div = new Element('div', { 'class': 'rounded button'});
			var div = new Element('div');
			div.addClassName('rounded');
			if(object.hasClassName('orange_button')) {
				div.addClassName('button');
				div.addClassName('orange_button');
			} else if(object.hasClassName('green_button')) {
				div.addClassName('button');
				div.addClassName('green_button');
			} else {
				div.addClassName('button');
				div.addClassName('green_button');
			}
		} else if(object.hasClassName('text')) {
			//var div = new Element('div', { 'class': 'rounded text'});
			var div = new Element('div');
			div.addClassName('rounded');
			div.addClassName('text');
			if(object.getStyle('width')) {
				var width = parseInt(object.getStyle('width'));
				div.setStyle({'width': width+'px'});
			}
		} else if(object.hasClassName('selectbox')) {
			var div = new Element('div');
			div.addClassName('rounded');
			div.addClassName('selectbox');
			if(object.getStyle('width')) {
				var width = parseInt(object.getStyle('width'))+5;
				div.setStyle({'width': width+'px'});
			}
		} else {
			 var div = new Element('div');
		}

		if(object.hasClassName('button')||object.hasClassName('text')||object.hasClassName('selectbox')) {
			Element.insert(object, {before: div});
			div.appendChild(object);

			if(!object.style.width&&!object.getStyle('width')) {
				object.setStyle({'width': '100%'});
			}
			curvyCorners(settings, div);
		}
	});
};

self.curvyWindows = function() {
	$$('.window').each(function(object) {
		if(!object.down('.content')) {
			if(object.down('.box')&&object.down('.container')&&object.down('.container').getStyle('display')=='none') {
				object.down('.container').setStyle({display: 'block'});
			}

			var hide = false;
			if(object.getStyle('display')=='none') {
				hide = true;
				object.setStyle({'display':'block'});
			}

			var content = new Element('div');
			content.addClassName('content')

			var top_left = new Element('div');
			top_left.addClassName('top_left')

			var top = new Element('div');
			top.addClassName('top')

			var top_right = new Element('div');
			top_right.addClassName('top_right')

			var bottom_left = new Element('div');
			bottom_left.addClassName('bottom_left')

			var bottom = new Element('div');
			bottom.addClassName('bottom')

			var bottom_right = new Element('div');
			bottom_right.addClassName('bottom_right')


			// Move the body's children into this wrapper
			while (object.firstChild)
			{
				content.appendChild(object.firstChild);
			}
			// Append the wrapper to the body
			object.appendChild(content);

			new Insertion.Top(object, top_right);
			new Insertion.Top(object, top);
			new Insertion.Top(object, top_left);

			new Insertion.Bottom(object, bottom_left);
			new Insertion.Bottom(object, bottom);
			new Insertion.Bottom(object, bottom_right);

			if(object.down('.top_left')&&object.down('.top_right')) {
				var corner = parseInt(object.down('.top_left').getStyle('width'))+parseInt(object.down('.top_right').getStyle('width'));
				//var corner = object.down('.top_left').offsetWidth)+object.down('.top_right').offsetWidth;
			}

			/* changed for the Filetree
			var width = object.offsetWidth?object.offsetWidth:parseInt(object.getStyle("width"));
			*/
			var width = object.getStyle("width")?parseInt(object.getStyle("width")):object.offsetWidth;

			var height = object.offsetHeight;

			if(object.down('.content'))
				var paddingWidth = parseInt(object.down('.content').getStyle('paddingLeft'))+parseInt(object.down('.content').getStyle('paddingRight'));
			if(object.down('.head')&&object.down('.foot'))
				var paddingHeight = object.down('.head').offsetHeight+object.down('.foot').offsetHeight;
			if(object.down('.top'))
				object.down('.top').setStyle('width:'+(width-corner)+'px');
			if(object.down('.content')) {
				object.down('.content').setStyle('width:'+(width-paddingWidth)+'px');
				if(parseInt(object.down('.content').getStyle('height'))<parseInt(object.style.height)) {
					//parseInt(object.down('.content').getStyle('height'));
					object.down('.content').setStyle({height: (height-8)+'px'});
				}
			}
			if(object.down('.bottom'))
				object.down('.bottom').setStyle('width:'+(width-corner)+'px');
			if(object.down('.scrollbarpaper-green')) {
				object.down('.scrollbarpaper-green').setStyle('height:'+(height-paddingHeight)+'px');
				object.down('.scrollbarpaper-green').setStyle('width:'+(width-paddingWidth)+'px');
			}

			if(hide) {
				object.setStyle({display:'none'});
			}
		}
	});

	$$('.toggle tfoot', '.toggle tbody').each(function(object) {
		if(!object.hasClassName('open'))
			object.setStyle('display:none');
	});

	$$('.box .container').each(function(object) {
		if(!object.hasClassName('open'))
			object.setStyle('display:none');
		else {
			object.removeClassName('open');
			object.setStyle('display:block');
		}
	});
}

function trim (zeichenkette) {
  // Erst f?hrende, dann Abschlie?ende Whitespaces entfernen
  // und das Ergebnis dieser Operationen zur?ckliefern
  return zeichenkette.replace (/^\s+/, '').replace (/\s+$/, '');
}

function implode (glue, pieces) {
    // Joins array elements placing glue string between items and return one string
    //
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/implode    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Waldo Malqui Silva
    // +   improved by: Itsacon (http://www.itsacon.net/)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: implode(' ', ['Kevin', 'van', 'Zonneveld']);    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: implode(' ', {first:'Kevin', last: 'van Zonneveld'});
    // *     returns 2: 'Kevin van Zonneveld'
    var i = '', retVal='', tGlue='';
    if (arguments.length === 1) {pieces = glue;
        glue = '';
    }
    if (typeof(pieces) === 'object') {
        if (pieces instanceof Array) {return pieces.join(glue);
        }
        else {
            for (i in pieces) {
                retVal += tGlue + pieces[i];tGlue = glue;
            }
            return retVal;
        }
    } else {
        return pieces;
    }
}

