var jqwipe_id = "head_images";
var jqwipe_pics = "../images/head_image_02.jpg,../images/head_image_03.jpg,../images/head_image_04.jpg,../images/head_image_05.jpg,../images/head_image_06.jpg,../images/head_image_07.jpg";
var jqwipe_line = 6;
var jqwipe_linedelay = 200;
var jqwipe_delay = 6000; // slideshow delay

var jqw_pic = 0;
var jqw_line = 1;
var jqw_preloadimg = [];

function initWipe(){
	var e = $( '#' + jqwipe_id )[0];
	var line = '';
	var picname = $( e ).css( 'background-image' ); 
	picname = picname.substr( 4, picname.length - 5 );
	picname = picname.replace(/\"/g,''); //" 
	jqwipe_pics = picname + ',' + jqwipe_pics;
	jqwipe_pics = jqwipe_pics.split(",");

	for ( i = 1; i < jqwipe_pics.length; i++ ) {
		jqw_preloadimg[ i ] =	new Image();
		jqw_preloadimg[ i ].src = jqwipe_pics[ i ]; 
	}
	
	var fullwidth = $( e ).width();
	var fullheight = $( e ).height();
	var lineheight = parseInt (fullheight / jqwipe_line );
	var lineheightmod = fullheight % jqwipe_line;
	$( e ).empty();

//	$( e ).css( "background", "white" );
	$( '#' + jqwipe_id ).css({ 
		backgroundImage: "url( '" + picname + "' )",
		backgroundRepeat: "no-repeat",
		backgroundPosition: ( "0px 0px" )
	});
	var offset = 0;

	for ( i = 0; i < jqwipe_line; i++ ) {
		$( e ).append( '<div id="jqw_box' + i + '"/>' );
		$( '#jqw_box' + i ).css({ 
			padding: 0,
			margin: 0,
			width: ( fullwidth ),
			height: ( lineheight + ( i < lineheightmod  ? 1 : 0 ) )
		});
		$( '#jqw_box' + i ).append( '<div id="jqw_line' + i + '"/>' );
		$( '#jqw_line' + i ).css({ 
			backgroundImage: "url( '" + picname + "' )",
			backgroundRepeat: "no-repeat",
			backgroundPosition: ( (0) + "px " + ( offset ) + "px" ),
			padding: 0,
			margin: 0,
			width: ( fullwidth ),
			height: ( lineheight + ( i < lineheightmod  ? 1 : 0 )  )
		});
		offset -= ( lineheight + ( i < lineheightmod  ? 1 : 0 )  );
	}
	wipe();
}

var oldpicname = newpicname; 
var newpicname = jqwipe_pics[jqw_pic];
function wipe(){
	oldpicname = newpicname; 
	newpicname = jqwipe_pics[jqw_pic];
	$( '#' + jqwipe_id ).css({ backgroundImage: "url( '" + newpicname + "' )" });
	for ( var i = 0; i < jqwipe_line; i++ ){
		$( '#jqw_line' + i ).show();
		$( '#jqw_line' + i ).css({	backgroundImage: "url( '" + oldpicname + "' )" });
		setTimeout("$( '#jqw_line' + " + i + " ).slideUp( 'slow' )", i * jqwipe_linedelay );
	}
	for ( var old_jqw_pic = jqw_pic; old_jqw_pic == jqw_pic; ) { jqw_pic = getRandom( 0, jqwipe_pics.length - 1 ); }
//	jqw_pic += 1;	if ( jqw_pic >= jqwipe_pics.length ) { jqw_pic = 0; } 
	setTimeout("wipe()", jqwipe_delay );
}

function getRandom( minNum , maxNum )
{
  if (Math.random && Math.round){
		var ranNum = Math.round( Math.random() * ( parseFloat(maxNum) - parseFloat(minNum) ) );
		ranNum += minNum;
		return ranNum;
  } else  {
	  var today= new Date();
	  var hours= today.getHours();
	  var mins=   today.getMinutes();
	  var secn=  today.getSeconds();
	  if (hours==19) hours=18;
	  var ranNum= (((hours+1)*(mins+1)*secn)%(parseFloat(maxNum) - parseFloat(minNum)))+parseFloat(minNum);
	  return ranNum;
  }
}

$( document ).ready( function(){ initWipe() });
