function myRandom( min, max )
{
    return min + Math.floor( ( max - min + 1 ) * Math.random() );
}

function myParseInt( i )
{
    var ret = parseInt( i );
    return isNaN( ret ) ? 0 : ret;
}

function poszikeClick() {
    for( i = 0; i < myRandom( 20, qMaxCount ); i++ ) {
        putQ( myRandom( 0, pageSize[2] ), myRandom( 0, pageSize[3] ) );
    }
    objBody.style.cursor = 'crosshair';
    window.clearInterval( qAnimTimerId );
    qAnimTimerId = window.setInterval( "animateQ();", 25 );
}

function resetBackground() {
    var objPage = document.getElementById("page");
    objBody.style.backgroundColor = 'black';
    objPage.style.backgroundColor = 'transparent';
    objBody.style.backgroundImage = 'url(/wp-content/themes/k2/styles/hullahaz/back.jpg)';
}

function killQ( obj ) {
    qKilled++;
    document.getElementById('divQHitCounter').innerHTML = 'Body Count: ' + qKilled;
    obj.parentNode.removeChild( obj );
    var objPage = document.getElementById("page");
    objBody.style.backgroundColor = 'red';
    objBody.style.backgroundImage = 'none';
    objPage.style.backgroundColor = 'red';
    window.setTimeout( 'resetBackground();', 10 );
    if( qKilled == qCount ) {
	document.getElementById('divQHitCounter').innerHTML = 'ZUPAGUT';
	qCount = 0;
    }
}

function putQ( x, y ) {
    if( ( qCount == 0 ) && ( !document.getElementById("divQHitCounter") ) ) {
	var divQHitCounter = document.createElement("div");
	divQHitCounter.setAttribute('id','divQHitCounter');
	divQHitCounter.style.display = 'block';
	divQHitCounter.style.position = 'absolute';
	divQHitCounter.style.top = "0px";
	divQHitCounter.style.left = "0px";
	divQHitCounter.style.zIndex = '89';
	divQHitCounter.innerHTML = 'Body Count: 0';
	divQHitCounter.setAttribute( 'onclick', 'javascript:initHeaderClick();' );
	objBody.insertBefore(divQHitCounter, objBody.firstChild);
    }

    qCount++;
    var divQ = document.createElement("div");
    divQ.setAttribute('id','divQ_'+qCount);
    divQ.style.display = 'block';
    divQ.style.position = 'absolute';
    divQ.style.top = y + "px";
    divQ.style.left = x + "px";
    divQ.style.zIndex = '88';
    divQ.style.width = '171px';
    divQ.style.height = '181px';
    divQ.setAttribute( 'onclick', 'javascript:killQ(this);' );
    if( myRandom( 0, 1 ) ) {
	divQ.style.backgroundImage = 'url(http://www.hullahaz.hu/wp-content/themes/k2/styles/hullahaz/q1.png)';
    } else {
	divQ.style.backgroundImage = 'url(http://www.hullahaz.hu/wp-content/themes/k2/styles/hullahaz/q2.png)';
    }
    objBody.insertBefore(divQ, objBody.firstChild);
}

function animateQ() {
    if( qCount == 0 ) {
	var divQHitCounter = document.getElementById('divQHitCounter');
	if( ( myParseInt( divQHitCounter.style.left ) > 100 ) || ( myParseInt( divQHitCounter.style.left ) < 0 ) ) {
	    switch( qHitCounterDirection ) {
		case 0: qHitCounterDirection = 1; break;
		case 1: qHitCounterDirection = 0;
	    }
	}
	switch( qHitCounterDirection ) {
	    case 0:
		divQHitCounter.style.left = myParseInt( divQHitCounter.style.left ) + qSpeed + "px";
		break;
	    case 1:
		divQHitCounter.style.left = myParseInt( divQHitCounter.style.left ) - qSpeed + "px";
	}

	return
    }
    for( i=1; i < qCount + 1; i++ ) {
	var currQ = document.getElementById( 'divQ_' + i );
	if( !currQ ) { continue; }
	switch( myRandom( 0, 3 ) ) {
	    case 0:
		currQ.style.left = myParseInt( currQ.style.left ) + myRandom( 1, qSpeed ) + "px";
		break;
	    case 1:
		currQ.style.left = myParseInt( currQ.style.left ) - myRandom( 1, qSpeed ) + "px";
		break;
	    case 2:
		currQ.style.top = myParseInt( currQ.style.top ) + myRandom( 1, qSpeed ) + "px";
		break;
	    case 3:
		currQ.style.top = myParseInt( currQ.style.top ) - myRandom( 1, qSpeed ) + "px";
	}
    }
}

function initHeaderClick() {
    window.clearInterval( qAnimTimerId );
    for( i=1; i < qCount + 1; i++ ) {
	var currQ = document.getElementById( 'divQ_' + i );
	if( !currQ ) { continue; }
	currQ.parentNode.removeChild( currQ );
    }
    var divQHitCounter = document.getElementById('divQHitCounter');
    if( divQHitCounter ) {
	divQHitCounter.parentNode.removeChild( divQHitCounter );
	objBody.style.cursor = qOldCursor;
    }
    qCount = 0;
    qKilled = 0;
    qHitCounterDirection = 0;
}

objBody = document.getElementsByTagName("body").item(0);
qOldCursor = objBody.style.cursor;
qSpeed = 10;
qMaxCount = 100;
qAnimTimerId = -1;
qHitCounterDirection = 0;
qCount = 0;
qKilled = 0;
pageSize = getPageSize();
initHeaderClick();
