/*
 * Jquery scripts for www.scada.lv
 * Copyright (c) 2009 SIA SCADA
 * Requires: jQuery v1.3+
*/

$(document).ready(function() {		

	$("a.lightbox").fancybox({
		'zoomOpacity'			: true,
		'overlayShow'			: true,
		'zoomSpeedIn'			: 500,
		'zoomSpeedOut'			: 500

	});

	$("a.framebox").fancybox({
		'zoomOpacity'			: true,
		'overlayShow'			: true,
		'zoomSpeedIn'			: 500,
		'zoomSpeedOut'			: 500,
		'frameWidth'			: 494,
		'frameHeight'			: 466,
		'hideOnContentClick'	: false

	});

	$('#left-block-link').click(function() {
		document.location.href = $(this).attr('href');
	});

	$('#right-block-link').click(function() {
		document.location.href = $(this).attr('href');
	});

	/*
	$('.portfolio-list-img').hover(function() {			
		$(this).stop();
		$(this).animate({width: 200, height: 145}, 100);		
	}, function() {		
		$(this).stop();
		$(this).animate({width: 210, height: 155}, 300);
	});
	*/

	$(document).keydown(function(e){		
		if (!e) var e = window.event;

		if (e.ctrlKey)
		{
			var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : null;
			
			if(keyCode==39) {				
				if($('#right-block-link').is(':visible'))
					$('#right-block-link').trigger('click');
			}

			if(keyCode==37) {								
				if($('#left-block-link').is(':visible'))
					$('#left-block-link').trigger('click');
			}
		}
	});

	$.update_timeline = function(rel) {
		$.ajax({
				type: 'GET',
				url: '/ajax/history-data/',
				data: {hid: rel},
				async: true,
				dataType: 'json',
				success: function(data) {
					if(data.result) {
						$('#timeline-table').find('td').removeClass('on');
						$('#timeline-table').find('td[rel=' + rel + ']').addClass('on');						

						if(data.next !== false) {
							$('#history-next-btn').attr('href', '#'+data.next);
							$('#history-next-btn').show();
						} else 
							$('#history-next-btn').hide();

						if(data.prev !== false) {
							$('#history-prev-btn').attr('href', '#'+data.prev);
							$('#history-prev-btn').show();
						} else 
							$('#history-prev-btn').hide();					 
						$('#history-content').html(data.content);

						$('.lightbox').lightBox();									
						$('#timeline').css('background-image', $('#timeline-table').find('td[rel=' + rel + '] > div:first').css("background-image"));

					}				
				}
		});		
	}



	$.update_timeline_noajax = function(rel) {
		if(!$('#bc-' + rel).is(':visible')) {
			$('#timeline-table').find('td').removeClass('on');
			$('#timeline-table').find('td[rel=' + rel + ']').addClass('on');			
			var bg = $('#timeline-table').find('td[rel=' + rel + '] > div:first').css("background-image");
			$('#timeline').css('background-image', bg);
			$('#history-container').css({'background-image': bg});			
			$('.history-block').hide();			
			$('#history-container').animate({'height': $('#bc-' + rel).height()}, 100, function(){			
				
				$('#bc-' + rel).fadeIn(500);
				$('#timeline-table').find('td[rel!=' + rel + ']').removeClass('on');						
			});
		}
	}

	$('#timeline-table').find('td').live('click', function(){
		
		var rel = $(this).attr('rel').replace('#', '');		
		if(rel) {
			
			$.update_timeline_noajax(rel);
		}		
		window.location.href= '#' + rel;
	});

	$('.history-control').click(function() {
	  
	  var rel = $(this).attr('href').replace('#', '');
	  if(rel) {		
		$.update_timeline_noajax(rel);
	  }  	  
	});
		

	var current_page = self.document.location.hash;

	if(current_page) {
		$('td[rel='+current_page.replace(/#/, '')+']').queue(function() {$(this).trigger('click') });
		
	}

	$('<div id="time-tuner">&nbsp;</div>').insertAfter('#timeline-table');
	
	$('#timeline-table').hover(function(){
		$('#time-tuner').show();			
	}, function(event){
		if($(event.relatedTarget).attr('id')!='time-tuner')
			$('#time-tuner').hide();		
	});

	$('#timeline-table').mousemove(function(e){
		$('#time-tuner').css({left: e.pageX});
	});

	$('#time-tuner').mouseout(function(event){
	  if($(event.relatedTarget).attr('id')!='timeline-table')
		$('#time-tuner').hide();		
	});



	$.animateServiceBlocks = function(el) {
		$('.service-block:visible').fadeOut(300, function(){$(el).fadeIn(1000);});		
	};

	$('.service-control').click(function() {
		var href = $(this).attr('href');
		$('#services-top-text a').removeClass('on');
		$('#services-top-text a[href=' + href + ']').addClass('on');
		$.animateServiceBlocks('#service-block-' + href.replace(/^#/, ''));	   
	});

	$('#services-top-text').find('a').click(function() {
		var href = $(this).attr('href');		
		if(href.match(/^#/)) {
			var rel = href.replace(/^#/, '');
			var elem  = '#service-block-' +  rel
			if($(elem).attr('id') == 'service-block-' + rel) {
				$('#services-top-text a').removeClass('on');
				$(this).addClass('on');
				$.animateServiceBlocks(elem);
			}			
		}
		
	});

	if(current_page) {		
		var elem  = '#service-block-' + current_page.replace(/^#/, '');
		$('#services-top-text a').removeClass('on');
		$('#services-top-text a[href=' + current_page + ']').addClass('on');

		if($(elem).attr('id') == 'service-block-' + current_page.replace(/^#/, '') && !$(elem).is(':visible')) {

			$.animateServiceBlocks(elem);			
		}
	}




	$('#search-form').submit(function() {
		if($('#searchterm-field').val().length > 0 && $('#searchterm-field').val()!=document.getElementById('searchterm-field').defaultValue) {
			$(this).attr('action', $(this).attr('action') + '?'+ encodeURIComponent($('#searchterm-field').val())); 
			return true;
		} else 
			return false;
	});

	$('#search-form-main').submit(function() {
		if($('#searchterm-field-main').val().length > 0) {
			$(this).attr('action', $(this).attr('action') + '?'+ encodeURIComponent($('#searchterm-field-main').val())); 
			return true;
		} else 
			return false;
	});
	
	var bank_details_height = $('#bank-details').height() + parseFloat($('#bank-details').css('padding-top')) + parseFloat($('#bank-details').css('padding-bottom'));		

	$('#bank-details-link').click(function() {		
		
		
		if($('#bank-details').is(':visible')) {			
			$('#bank-details').animate({top: bank_details_height}, 500, function(){$(this).hide();});
		} else 	{				
			$('#bank-details').css({opacity: 0.8, top: bank_details_height}).show().animate({top: 0}, 500);
		}
		return false;
	});


	$('#vac-inner-div-diz, #vac-inner-div-cod').hover(function(){
		$(this).addClass('on');
	}, function(){
		$(this).removeClass('on');
	});

	$('#vac-inner-div-diz, #vac-inner-div-cod').click(function() { 
		window.location.href = $(this).find('a').attr('href'); 
	});

	
});