﻿//=====================================================
// H G   P I X
//=====================================================

//
// create closure
//
(function($) {

 	//=====================================================
 	// plugin definition
	//
	$.fn.hgPix = function(options) {

		//debug(this.size());

		// build main options before element iteration
		//var opts = $.extend({}, $.fn.hgMenu.defaults, options);

		//=====================================================
		// iterate and reformat each matched element
		return this.each(function(i) {
			var $hgPix = $(this);
			var sPixId = $hgPix.attr('id');

			var $imgs = $('div.img', $hgPix).children();
			if ($imgs.size() > 1)
				$imgs.each( function(j) {
						   $(this).click( function() { setCurrentPix( sPixId, (j + 1) % $imgs.size() ); } );
						   $(this).css( 'cursor', 'pointer' );
					} );

			//alert(sPixId + ': ' +  $imgs.size());

			//.each( function(j) { (i == j) ? $(this).removeClass('hidden') : $(this).addClass('hidden'); } );


			$('.nav a', this).each( function(i) {
					var $this = $(this);
					if ($this.attr('href') != 'javascript:void(0);')
					{
						$this.attr('href', 'javascript:void(0);');
						$this.click( function() { setCurrentPix( sPixId, i ); } );
					}
				});
		});

	};


	//=====================================================
 	// plugin private methods definition
	//
	function setCurrentPix(sPixId, i)
	{
		$('#'+sPixId).each( function() {
			$('div.nav', this).children().each( function(j) { (i != j) ? $(this).removeClass('current') : $(this).addClass('current'); } );
						   $('div.img', this).children().each( function(j) { (i == j) ? $(this).fadeIn(300) : $(this).css('display', 'none'); } );
			$('div.txt', this).children().each( function(j) { (i == j) ? $(this).fadeIn(300) : $(this).css('display', 'none'); } );
			//$('div.img', this).children().each( function(j) { (i == j) ? $(this).removeClass('hidden') : $(this).addClass('hidden'); } );
			//$('div.txt', this).children().each( function(j) { (i == j) ? $(this).removeClass('hidden') : $(this).addClass('hidden'); } );
		});
	}


 	//=====================================================
 	// private function for debugging
	//
	function debug(o, msg) {
		var s = '['+(typeof o) + "]:\n", v;
		if (typeof o == 'object') {
			for( k in o ) {
				switch(typeof (o[k])) {
				case 'object':
				case 'function':
					v = '['+(typeof (o[k]))+']';
					break;
				case 'string':
					v = o[k].length > 50 ? o[k].substr(0, 46) + ' ...' : o[k];
					break;
				default:
					v = o[k];
				}
				s += k + ': ' + v + "\n";
			}
		}
		else
			s += o;

		alert( (msg ? msg + "\n" : '' ) + s );
	};


//
// end of closure
//
})(jQuery);


//
// run on document ready
//
jQuery(function($){
	   $('.hgPix').hgPix();
});


