function initPNGfix(imgList) {
	if ( $('html').hasClass('msie6') ) {		// execute in ie6 only
		for (x in imgList) {
			if ( imgList[x].length ) {
				DD_belatedPNG.fix(imgList[x]);
			};
			x++;
		}		
	}
}

function rotateAsideImg() { 						/* ie only, proper browsers use css3 */
	$('.rotateP15 img').transform({rotate: 15});
	$('.rotateN15 img').transform({rotate: -15});
	$('.rotateP05 img').transform({rotate: 5});	
	
	/* offset correction for IE6/7 */
	var computedMargin = (parseInt($('.offsetMod img').css('margin-top')) - 75 )+ 'px';
	$('.msie6 .offsetMod img,'+
	  '.msie7 .offsetMod img').css('margin-top', computedMargin);
}

function popupHint() {
    $('.popupHint, .popupHint .close').click(function() { $(this).children('.hContent').toggle() });
    $('.popupHint.start-open').children('.hContent').toggle();
}

function formProgress(_classname,_classnameLast) {
	$('.formProgress ol').each(function() {
		if ( $(this).children('li').last().hasClass(_classname) ) {
			$(this).addClass(_classnameLast);
		}	
	})
}

function expandTables() {
	$('.uniForm .tableWrapper').hover(function() {
		var tableInnerWidth = $("table", this).width() + 10;
		if (tableInnerWidth > 470) {
			$(this)
				.css({
					height: $("table", this)[0].offsetHeight + "px"
				});
			$("table", this)
				.stop(true, false)
				.css({
					zIndex: "100",
					position: "absolute"
				});
			}
	}, function() {
		$(this).stop(true, false).css({
			height: "auto"
		});		
		$("table", this).stop(true, false).css({
			position: "static"
		});
	});
}

function coverFlip() {
	var imgList = [];
	var imgCounter = 0;

	$('#flip .imgWrap').each(function(i) {
		imgList[i] = [
			$(this).children('.imgData').attr('data-href'), 
			$(this).children('.imgData').attr('data-label')
		];
	});
	
	$('#flip .imgWrap').each(function(i) {
		$(this).html('<img src="'+ imgList[i][0]
			+'" alt="'+ imgList[i][1]
			+'" title="'+ imgList[i][1]
			+'" />');
	});
	
	$('#flip .imgWrap img').each(function() {
		$(this).load(function() {
			imgCounter++;
			if ( imgCounter === imgList.length ) { /* wait until the last img element is loaded */
				$('.preloader').css('display','none');	/* hide the preloader */
				$('.carousel').css('display','block'); /* show the fully loaded carousel*/

				$( '#flip' ).jcoverflip({	/* initialize coverflip on the loaded elements */
				  current: parseInt(($( '#flip li' ).length) / 2),
				  beforeCss: function( el, container, offset ){
					var offsetMod = (2 - offset);	  
					return [
					  $.jcoverflip.animationElement( el, { 
							left: ( container.width( )/2 - 209 - 50*offset + 20*offset )+'px', 
							top:  Math.min(160,58+25*offset*offset)+'px' 
						}, 
						{ 
							0: {'z-index': offsetMod } 
						}),
					  $.jcoverflip.animationElement( el.find( 'img' ), { 
							width: Math.max(10,160-50*offset*offset) + 'px' }, {} )
					];
				  },
				  afterCss: function( el, container, offset ){
					var offsetMod = (2 - offset);	  
					return [
					  $.jcoverflip.animationElement( el, { 
							left: ( container.width( )/2 + 49 + 80*offset )+'px', 
							top: Math.min(160,58+25*offset*offset)+'px' 
						}, 
						{ 
							0: {'z-index': offsetMod } 
						}),
					  $.jcoverflip.animationElement( el.find( 'img' ), { 
							width: Math.max(10,160-50*offset*offset) + 'px' }, {} )
					];
				  },
				  currentCss: function( el, container ){
					return [
					  $.jcoverflip.animationElement( el, { 
							left: ( container.width( )/2 - 131 )+'px', 
							top: 0 }, 
					  { 
							0: {'z-index': '3' } 
					  }),
					  $.jcoverflip.animationElement( el.find( 'img' ), { 
							width: '262px' }, { } )
					];		
				  },
				  change: function( el, event, ui ){}
				});
				
				$('#flipNext').click(function() { $('#flip').jcoverflip('next'); });
				$('#flipPrev').click(function() { $('#flip').jcoverflip('previous'); });
				
				$( '#flip' ).disableTextSelect();
			};
		});
	});
	
};

function courseSubmit() {
    $('.searchPane #CourseId').change(function() {
        $(this).parents('form').submit();
    });
}

function setupPrintLink() {
    $('p#print-help').hide();
    $('span#jqPrintLink').html('<a href="#print">Print</a>');
    $('span#jqPrintLink a').click(function() {
        window.print();
        return false;
    });
}

function initSpinnerBox() {
	if ( $('html').hasClass('iphone') == false &&  $('html').hasClass('ipad') == false ) {
		$('#spinnerList').jScrollPane({
			showArrows : true,
			arrowSize : 1,
			scrollbarWidth : 13,
			dragMinHeight : 21,
			dragMaxHeight : 21
		}); 
	};
}

$(document).ready(function() {
    var PNGimageList = [
		'.main h1 a',
		'.mainHz a',
		'#langSelector a',
		'#langSelector span',
		'.searchResults li',
		'.pagination .prev',
		'.pagination .next',
		'.formProgress em',
		'.formProgress ol',
		'.formProgress ol.lastActive',
		'.popupHint',
		'.hContent .top',
		'.hContent .bottom',
		'#flipPrev',
		'#flipNext',
		'#login-box h1',
		'#login-box',
		'#login-box-bottom',
		'.spinnerContainer',
		'.fDP .completed a',
		'#login-box .login-lang a.active',
		'#login-box .login-lang a.active span'		
	];
    initPNGfix(PNGimageList);

    var bgiframeList = [
		'.hContent'
	];
    initBgiframe(bgiframeList);

    rotateAsideImg();

    $('form').uniform();

    popupHint();

    formProgress('active', 'lastActive');
    formProgress('completed', 'lastCompleted');

    //	expandTables();

    courseSubmit();

    $('.spinnerWrapper').focus();

    setupPrintLink();
	
	initSpinnerBox();
		
});

$(window).load(function() {
    coverFlip();
});

