﻿window.addEvent('domready', function(){
var dwProgressBar = new Class({
	
	//implements
	Implements: [Options],

	//options
	options: {
		container: $('head_kep'),
		boxID:'',
		percentageID:'',
		displayID:'',
		startPercentage: 0,
		displayText: false,
		speed:10
	},
	
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
		//create elements
		this.createElements();
	},
	
	//creates the box and percentage elements
	createElements: function() {
		var box = new Element('div', { id:this.options.boxID });
		var perc = new Element('div', { id:this.options.percentageID, 'style':'width:0px;' });
		perc.inject(box);
		box.inject(this.options.container);
		if(this.options.displayText) { 
			var text = new Element('div', { id:this.options.displayID });
			text.inject(this.options.container);
		}
		this.set(this.options.startPercentage);
	},
	
	//calculates width in pixels from percentage
	calculate: function(percentage) {
		return ($(this.options.boxID).getStyle('width').replace('px','') * (percentage / 100)).toInt();
	},
	
	//animates the change in percentage
	animate: function(to) {
		$(this.options.percentageID).set('morph', { duration: this.options.speed, link:'cancel' }).morph({width:this.calculate(to.toInt())});
		if(this.options.displayText) { 
			$(this.options.displayID).set('text', to.toInt() + '%'); 
		}
	},
	
	//sets the percentage from its current state to desired percentage
	set: function(to) {
		this.animate(to);
	}
	
});
if($('head_kep') != undefined) {
	/* progress bar */
	var progressBar = new dwProgressBar({
		container: $('progress-bar'),
		startPercentage: 0,
		speed:750,
		boxID: 'box',
		percentageID: 'perc',
		displayID: 'text',
		displayText: true
	});
	
	/* preloading */
	var images = ['/images/head-fooldal-001.jpg',
    '/images/head-fooldal-001.jpg',
    '/images/head-fooldal-002.jpg',
    '/images/head-fooldal-003.jpg',
    '/images/head-fooldal-004.jpg',
    '/images/head-fooldal-005.jpg',
    '/images/head-fooldal-006.jpg',
    '/images/head-fooldal-007.jpg',
    '/images/head-fooldal-008.jpg',
    '/images/head-fooldal-009.jpg',
    '/images/head-fooldal-010.jpg',
    '/images/head-fooldal-011.jpg',
    '/images/head-fooldal-012.jpg',
    '/images/head-fooldal-013.jpg',
    '/images/head-fooldal-014.jpg',
    '/images/head-fooldal-015.png',
    '/images/head-fooldal-016.png',
    '/images/head-fooldal-017.png',
    '/images/head-fooldal-018.png',
    '/images/head-fooldal-019.png'
    ];
	var loader = new Asset.images(images, {
		onProgress: function(counter,index) {
			progressBar.set((counter + 1) * (100 / images.length));
		},
		onComplete: function() {
			$('progress-bar').setStyle('display', 'none');
		}
	});
}
});
