var numPics, picWidth, strPicContainer;
var workPos = 0;
var currentWorkPos = 0;
var autoPlay= true;
var delayTime= 6000;
var speed= 500;
var idSelTab= null;
varboolSEF= false;
window.log = function () {
	log.history = log.history || [];
	log.history.push(arguments);
	if (this.console) {
		console.log(Array.prototype.slice.call(arguments));
	}
};

window.addEvent('domready', function() {
	if($defined($('home_pics'))) {
		numPics = $$('.home_pic').length;
		picWidth = 1000;
		strPicContainer= "home_pics_inner";
		nextWork.delay( delayTime );
	}
	if($defined($('tech_pics'))) {
		numPics = $$('.tech_pic').length;
		if( numPics && $$('.tech_pic')[0].getChildren().length ) {
			$('tech_pics').setStyle( 'height', $$('.tech_pic')[0].getChildren()[0].getStyle( 'height' ));
		}
		if( numPics <= 1 ) {
			$('tech_thumbs').setStyle( 'display', 'none' );
		}
		else {
			picWidth= 465;
			strPicContainer= "tech_pics_inner";
			nextWork.delay( delayTime );
		}
	}
	
	if($defined($('subnav')) && $('subnav').getChildren().length ) {
		$('top_container').setStyle( 'height', '155px' );
	}
	
	if($defined($$('.tabs')) && $$('.tabs').length ) {
		var tabCount = 0;
		$$('.tab').each(function( el ) {
			if( el.get( 'text' ) == "" ) {
				el.setStyle( 'display', 'none' );
			} else {
				el.addEvent( 'click', function( e ) {
					e.stop();
					var id = "";
					if($defined( this.get( 'id' )))
					id = this.get( 'id' ).split( 'tab_' ).join('');
					addTabContent( this, id );
				});
				tabCount++;
			}
		});
		if( tabCount ) addTabContent($('tab_1'), 1 );
		if( tabCount == 1 ){
			$('tab_1').removeEvents( 'click' );
			$('tab_1').addEvent( 'click', function( e ) {
				e.stop();
			});
		}
	}
	

	
	if($defined($$('.csc-sitemap')) && $$('.csc-sitemap').length ) {
		$('tech_col_1').grab($$('.csc-sitemap')[0] );
	}
	
	
	if($defined($('sales_division_top_content'))) {
		var divisions = {};
		var divisionsArray = [];
		var divisionOptions = '<option value="">'+$('sales_division_drop_down').get('text')+'</option>';
		var initialHeight = parseInt($('content_inner').getStyle( 'height' ), 10 );
		$$('.sales_division').each( function( el ) {
			title = el.getElements('h2').get( 'text' );
			divisions[title] = el;
			divisionsArray.push( title );
		});
		divisionsArray.sort();
		for( i=0; i < divisionsArray.length; i++ ) {
			divisionOptions += '<option value="'+divisionsArray[i]+'">'+divisionsArray[i]+'</option>';
		}
		divisionOptions = '<select id="sales_divistion_select">'+divisionOptions+'</select>';
		$('sales_division_drop_down').set( 'html', divisionOptions );
		$('sales_division_btn').getElement('input').addEvent( 'click', function( e ) {
			$$('.sales_division').setStyle( 'display', 'none' );
			if($('sales_divistion_select').value != '' ) {
				divisions[$('sales_divistion_select').value].setStyle( 'display', 'inline-block' );
				$('content_inner').setStyle( 'height', (initialHeight+parseInt(divisions[$('sales_divistion_select').value].getStyle( 'height' ),10))+'px' );
			} 
		});
	}

	// if($defined($('download_bottom' ))) {
	// 	$$('.download_items').each( function( el ) {
	// 		download_item = el.getElements('.download_item');
	// 		ct = 0;
	// 		download_item.each( function( el2 ) {
	// 			if( ct==0 || (ct%3)==0 ) {
	// 				div = new Element( 'div', { 'class':'download_item_row' } );
	// 				el.grab( div );
	// 			}
	// 			div.grab( el2 );
	// 			ct++;
	// 		});
	// 	});
	// }


	// MooTools chokes in IE9 on "new Element(...)" - let's just use jQuery instead...
	if(jQuery('.download_bottom' )) {
		jQuery('.download_items').each( function( i ) {
			var el = jQuery(this);
			ct = 0;
			el.find('.download_item').each( function( j ) {
				if( ct==0 || (ct%3)==0 ) {
					div = jQuery('<div class="download_item_row"></div>');
					el.append( div );
				}
				div.append( $(this) );
				ct++;
			});
		});
	}
	$$('.btn_wide').each( function( el ) {
		el.getElements('span.btn_bg').setStyle( 'width', (el.getStyle( 'width' ).toInt()) + 'px' );
	});
	
	// if( window.location.href.toURI().getData('boolIsMinimal') == "1" ) {
	// 	$('container').setStyle( 'display', 'none' );
	// 	$('content_container').setStyle( 'padding-top', '20px' );
	// 	$('content_container').setStyle( 'width', '770px' );
	// 	$('footer_container').setStyle( 'display', 'none' );
	// 	$('content').setStyle( 'width', '770px' );
	// 	$('content').set( 'html', '<div style="position:absolute; right:0; z-index:9999;"><div class="home_pic_learn btn_wide_small" ><p><a href="#" onclick="javascript:parent.Mediabox.close(); return false;" rtekeep="1"><span class="btn_bg"><span class="btn_copy">X</span></span></a></p></div></div>' + $('content').get('html'));
	// }
	registerQA() ;
});

var qaLastOn;
var initialFaq;

function registerQA() {
	var toggle, title, content;
	initialFaq = $('content_inner').getStyle( 'height' ).toInt();
	$$('.faq').each( function( el ) {
		toggle = el.getChildren()[0];
		title = el.getChildren()[1];
		toggle.addEvent( 'click', function( toggle, title ) {
			$$('.faq').each( function( el2 ) {
				el2.getChildren()[0].removeClass( 'faq_q_on' );
				el2.getChildren()[1].removeClass( 'faq_a_on' );
			});
			toggle.toggleClass( 'faq_q_on' );
			title.toggleClass( 'faq_a_on' );
			// if( Browser.Engine.trident ) {
			// 	$('content_inner').setStyle( 'height', ( initialFaq + title.getComputedSize().height ) + 'px' );
			// }
		}.pass( [ toggle, title ] ));
	});
}

function setLang( strLang ) {
	$('top_link_lang_sel').set( 'html', '<img src="fileadmin/templates/main/images/triangle_black.png" />' + strLang );
	$('top_link_lang_options').setStyle( 'display', 'none' );
}

function addTabContent( el, id ) {
	el.addClass( 'tab_selected' );
	$('tab_content_' + id).setStyle( 'display', 'inline-block' );
	removeTabContent();
	idSelTab = id;
}

function removeTabContent() {
	if( idSelTab != null ) {
		$('tab_' + idSelTab).removeClass( 'tab_selected' );
		$('tab_content_' + idSelTab).setStyle( 'display', 'none' );
	}
}

function nextWork() {
	if( autoPlay ) {
		currentWorkPos = workPos;
		workPos++;
		if( workPos >= numPics ) workPos = 0;
		setThumbs( currentWorkPos, workPos );
		showWork( currentWorkPos, workPos );
	}
}

function setThumbs( i, next ) {
	if($defined($('project_thumb_' + i))) {
		$('project_thumb_' + i).removeClass('selected');
		$('project_thumb_' + next).addClass('selected');
	}
}

function showWork( i, next ) {
	$( strPicContainer ).set('tween', {
		duration: speed, 
		onComplete: function(){
			$( strPicContainer ).set('tween', {onComplete: null});
			$( strPicContainer ).setStyle( 'left', '-' + (next * picWidth) + 'px' );
			$( strPicContainer ).tween( 'opacity', 1 );
			nextWork.delay( delayTime );
		}
	});
	$( strPicContainer ).tween( 'opacity', 0 );
}

function showWorkClick( i ) {
	autoPlay = false;
	setThumbs( workPos, i );
	showWork( workPos, i );
	workPos = i;
}

function showWorkPrev() {
	autoPlay = false;
	currentWorkPos = workPos;
	workPos--;
	if( workPos < 0 ) workPos = numPics-1;
	setThumbs( currentWorkPos, workPos );
	showWork( currentWorkPos, workPos );
}

function showWorkNext() {
	autoPlay = false;
	currentWorkPos = workPos;
	workPos++;
	if( workPos >= numPics ) workPos = 0;
	setThumbs( currentWorkPos, workPos );
	showWork( currentWorkPos, workPos );
}

//Calls up ColorBox on download links
jQuery(document).ready(function($) {
	jQuery('.download_title').each(function() {
		var lightbox_content = jQuery(this).next();
		jQuery(this).colorbox({
			width:"50%", 
			inline:true, 
			href: lightbox_content, 
			onCleanup: function(){
				jQuery('.download_links').hide(); 
			}
		});
	});
	jQuery('.download_title').click(function() {
		jQuery(this).next().show();
	});
});
