/* ROOM Javascript page handling 
 *
 * ae nov.2009
*/

$(document).ready(function() {

/* ------- disable right click menu ------- */

//$(document).bind("contextmenu",function(e) {
//	return false;
//});

/* ------- handle menu logo ------- */

$('#menu18 a').css({'background-color' : 'red', 'color' : 'white', 'border' : '1px solid white', 'margin-top' : '2px', 'padding' : '1px 1px 1px 3px', 'line-height' : '12px'});
$('#menu18 a').hover(function() {
$(this).css({'background-color' : 'white', 'color' : 'red', 'border' : '1px solid red'});} , function() {
$(this).css({'background-color' : 'red', 'color' : 'white', 'border' : '1px solid white'});}
);

// remove menu click through 
$("#menu ul > li a").not("#menu ul li ul a").slice(1,5).click(function(){
	$('#menu ul li a').blur();
	return false;
});

/* ------- handle grid ------- */

// remove href from grid, size of grid -1
var num_items = $('#qd_BR div.grid').length; // was > a
//$('#qd_BR div.grid > a').slice(0,num_items-1).attr( { href: '#' } );

// aesthetic fix outer borders
$.each( [2,4,8], function(i,n) {
	$('#qd_BR div.grid:eq(' + n + ')').css({'border-right' : '0px solid white', 'width' : '75px'});
});
$.each( [9,10,11], function(i,n) {
	$('#qd_BR div.grid:eq(' + n + ')').css({'border-bottom' : '0px solid white', 'height' : '75px'});
});
$('#qd_BR div.grid:eq(12)').css({'border-bottom' : '0px solid white','border-right' : '0px solid white'});

// add label div
$('#qd_BR').append('<div id="label"> </div>');

// populate first cell 
$('#qd_BR img:first').attr({ src: $('#qd_BR img:eq(1)').attr('src'), id: $('#qd_BR img:eq(1)').attr('id') });
$('#qd_BR a:first').attr({ href: $('#qd_BR a:eq(1)').attr('href') });
$('#qd_BR div.meta_data:eq(0)').html( $('#qd_BR img:eq(1)').parent().next().html() );
$('#qd_BR div.prod_images:eq(0)').html( $('#qd_BR img:eq(1)').parent().next().next().html() );
$('#label').text( $('#qd_BR img:eq(1)').attr('alt'));

// mouse over
$('#qd_BR div.grid img.first_image').mouseover(function() {
	$('#qd_BR img:first').attr({ src: $(this).attr('src'), id: $(this).attr('id') });
	$('#qd_BR a:first').attr({ href: $(this).parents('a').attr('href') });
	$('#qd_BR div.meta_data:eq(0)').html( $(this).parent().next().html() );
	$('#qd_BR div.prod_images:eq(0)').html( $(this).parent().next().next().html() );
	$('#label').html($(this).attr('alt'));
}); 


// grid click, get ajax data
$('#qd_BR img').click(function(){
	row_clicked = -1; // clear row index store
	// $('#qd_UL img').attr({ src: $(this).attr('src') });		// show image
	
	// load images in slide show 
	//$('#slides').html('<div class="slide first"></div>');
	//$('<img/>').attr({ src: $(this).attr('src') }).appendTo('#slides .slide');
	//$('#slides .prod_image_slide').addClass('active'); // makes sure first image starts on IE
	$('#slides').html('');
	// get images and captions
	$('#slides').append($(this).parent().next().next().html());
	
	$('#prod_description').html( $(this).parent().next().html() );	// load meta data
	prod_descr = $('#prod_description p').html();
	$('#prod_stockitems').text("Loading..");
	$('#progressIndicator').show();

	// get ajax stock data 
	$('#qd_BL').load("repo/ajaxGet",
			{ditto_parents: $(this).attr('id')},
			function() { // callback
//					$("#qd_UL img").attr({ src: $('#ajax_data_img').text() });
//					$('#prod_description').html($('#ajax_data').html());
				$('#progressIndicator').hide(); // done!
			});
			
/*		$("#prod_description").load("ajaxGet2",
			{ditto_documents: $(this).attr('id')},
			function() { // callback]
			prod_descr = $('#prod_description').html();
			$('#progressIndicator').hide(); // done!
			
			}); */
			
		$('#qd_BR a').blur();
		return false; // to prevent click thru
});

/* ------- handle table click + hover ------- */

// save product description
var prod_descr = $('#prod_description p').html();
var row_clicked = -1;

	$('#qd_BL .stockTable_head tr')
		.livequery('click',function() {
			if ( row_clicked == $(this).parent().children().index($(this)) ) {
				$('#prod_description p').html(prod_descr);
				row_clicked = -1;
			} else {
				$('#prod_description p').html($(this).children('td:last').html());
				$('#prod_description table').show();
				row_clicked = $(this).parent().children().index($(this));
			}
	
			// remove ugly focus outline
			$(this).blur();
	});
	
	// table row hightlight
	$('#qd_BL .stockTable_head tr')
		.livequery('mouseover', function() {
			$(this).addClass('highlight');
			$(this).children('td:last-child').addClass('row_over');
		})
		.livequery('mouseout', function() {
			$(this).removeClass('highlight');
			$(this).children('td:last-child').removeClass('row_over');
		});

/* ------- slideshow ------- */

// By http://fifthmammoth.co.uk/slideshow/

function slideSwitch() {
		if ( $('#slides DIV.slide').length == 1 ) return; // only one image: out.
		var current = $('#slides' + ' .slide:eq(0)');
		var next = $('#slides' + ' .slide:eq(1)');
		$('#slides').append(current);
		next.fadeIn(1000);
		current.fadeOut(1000);
	}

	var playSlideshow = setInterval( slideSwitch, 6000 );

	$('#slides').hover(	
		function() {
		clearInterval(playSlideshow);
		},
		function() {
		playSlideshow = setInterval( slideSwitch, 6000 );
	});	

/*
By http://jonraasch.com/blog/a-simple-jquery-slideshow/

	function slideSwitch() {
		// only one image: out.
		if ( $('#prod_slides DIV').length == 1 ) return; 

		var $active = $('#prod_slides DIV.active');
		if ( $active.length == 0 ) $active = $('#prod_slides DIV:last');
	
		var $next =  $active.next().length ? $active.next()
			: $('#prod_slides DIV:first');
	
		$active.addClass('last-active');
	
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
	}
	
*/

}); // end $(document).ready(function() 
