/* Author: 

*/

function randomColor () {
	return 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
}


	$(function() { 
	  var theTable = $('table.filtered')
	
	  theTable.find("tbody > tr").find("td:eq(1)").mousedown(function(){
	    $(this).prev().find(":checkbox").click()
	  });
	
	  $("#filter").keyup(function() {
	    $.uiTableFilter( theTable, this.value );
	  })
	   $("#filter").change(function() {
	    $.uiTableFilter( theTable, this.value );
	  })
	
	  $('#filter-form').submit(function(){
	    theTable.find("tbody > tr:visible > td:eq(1)").mousedown();
		//highlightAlt();
	    return false;
	  }).focus(); //Give focus to input field
	}); 




$(document).ready(function () {
	
//	var clockOptions = {
//        format: '%A, %d %B %Y, %H:%M'
	var clockOptions = {
        format: ' %d %b %Y, %H:%M'

      }
	$('#curent-time').jclock(clockOptions);
	
	$('form.validate').validate();

    
	$('#header .news').infiniteCarousel().bind('mouseenter',function () {
			$(this).data('stopped',1);
   
		}).bind('mouseleave', function () {
			$(this).data('stopped',0);
		});
   
	setInterval(function() {
		if (!$('#header .news').data('stopped')){
			$('#header .news .forward').click();
		}
	}, 7000);


	$('.tabs .menu a').click(function(e) {
		e.preventDefault();
		$(this).parent().parent().find('li').removeClass('active').parent().parent().find('.tab').hide();
			$(this.hash).show();
			$(this).parent().addClass('active');
			
	
		}).each(function() {
			$(this.hash).addClass('tab').hide();
			if ($(this).parent().hasClass('active')) {
				$(this).click();
			}
		
		});
	
	

	$('.accordion .head').click(function() {
		$(this).next().toggle('fast');
		if ($(this).hasClass('active')) {
			$(this).removeClass('active');
		} else { 
			$(this).addClass('active');
		}
		return false;
	}).each(function () {
		$(this).html('<i></i><span class="pseudo">' + $(this).html() + '</span>');
		if (!$(this).hasClass('active')) {
			$(this).next().hide()
		}
	}).css({'cursor':'pointer'});



	$('#sitemap .level2>ul').each(function () {
		$(this).hide().parent().addClass('parent closed').append('<i>+</i>').find('i').click(function() {
			$(this).parent().find('ul:first').toggle('fast');
			if ($(this).html()=='+') {
				$(this).html('-');
			} else {
				$(this).html('+');
			}
		});
	});
	
	

	$('.sitemap h1').after('<span class="switch"><span class="short"><small>коротка</small></span> <span class="long"><small>повна</small></span></span>');
	
	$('.sitemap .switch .long').click(function (e) {
		e.preventDefault();
		$('#sitemap i').html('-');
		$('#sitemap .level2>ul').show('fast');
		$('.sitemap .switch span').removeClass('active');
		$(this).addClass('active');
	})
	
	$('.sitemap .switch .short').click(function (e) {
		e.preventDefault();
		$('#sitemap i').html('+');
		$('#sitemap .level2>ul').hide('fast');
		$('.sitemap .switch span').removeClass('active');
		$(this).addClass('active');
		
	}).click();
	

	$("#markets i").each(function () {
	var col = randomColor();
	$(this).animate({
		backgroundColor: col
	}, 600 );
	$(this).data("original", col);

	});

	$('#markets a').hover(function() { //mouseover
		var col = randomColor();
		//alert (jQuery.data($(this).find('i'), "original"));
		$(this).find('i').animate({
			'background-color': col,
			'top': '-6px'
		},200);
		},function() { //mouseout
			$(this).find('i').animate({
				'background-color': $(this).find('i').data("original"),
				'top': '0'
		},200);
	});




		if ( $('#gallery').length) {	
 
				// Initially set opacity on thumbs and add
				// additional styling for hover effect on thumbs
				var onMouseOutOpacity = 0.67;
				$('#thumbs ul.thumbs li, div.navigation a.pageLink, .nav-controls .prev, .nav-controls .next').opacityrollover({
					mouseOutOpacity:   onMouseOutOpacity,
					mouseOverOpacity:  1.0,
					fadeSpeed:         'fast',
					exemptionSelector: '.selected'
				});
				

				
				// Initialize Advanced Galleriffic Gallery
				var gallery = $('#thumbs').galleriffic({
					delay:                     2500,
					numThumbs:                 1200,
					preloadAhead:              10,
					enableTopPager:            false,
					enableBottomPager:         false,
					maxPagesToShow:            8,
					imageContainerSel:         '#slideshow',
					controlsContainerSel:      '#controls',
					captionContainerSel:		'#caption',
					loadingContainerSel:		'#loading',
					renderNavControls:			true,
					renderSSControls:			false,
					playLinkText:              'Включить слайдшоу',
					pauseLinkText:             'Выключить слайдшоу',
					prevLinkText:              'Предыдущее фото',
					nextLinkText:              'Следующее фото',
					nextPageLinkText:          '&rsaquo;',
					prevPageLinkText:          '&lsaquo;',
					enableHistory:             false,
					autoStart:                 false,
					syncTransitions:           true,
					defaultTransitionDuration: 500,
					onSlideChange:             function(prevIndex, nextIndex) {
						// 'this' refers to the gallery, which is an extension of $('#thumbs')
						this.find('ul.thumbs').children()
							.eq(prevIndex).fadeTo(0, onMouseOutOpacity).end()
							.eq(nextIndex).fadeTo(0, 1.0);
							
					},
					onPageTransitionOut:       function(callback) {
						this.fadeTo(0, 0.0, callback);
					},
					onPageTransitionIn: function() {
						var prevPageLink = this.find('a.prev').css('visibility', 'visible');
						var nextPageLink = this.find('a.next').css('visibility', 'visible');
						prevPageLink.hide();
						nextPageLink.hide();
						
						// Show appropriate next / prev page links
						if (this.displayedPage > 0) {
							//prevPageLink.css('visibility', 'visible');
							prevPageLink.show();
						}

						var lastPage = this.getNumPages() - 1;
						if (this.displayedPage < lastPage) {
							nextPageLink.show();
							//nextPageLink.css('visibility', 'visible');
							}

						this.fadeTo(0, 1.0);
					}

				});
				
				gallery.find('a.prev').click(function(e) {
					gallery.previousPage();
					e.preventDefault();
				});

				gallery.find('a.next').click(function(e) {
					gallery.nextPage();
					e.preventDefault();
				});

	}	



});

