$(document).ready(function(){
	// horoscope
	if( $('.horoscope').length ){
		$('.horoscope > .menu_row > .menu').css('cursor','pointer');
		$('.horoscope > .menu_row > .menu').click(function(){
			location.href = $('.menu_content > .c3 > h2 > a', $(this) ).attr('href');
		});
	}

	// dus
	if( $('.dus').length ){
		$('#dus_name').change(function(){
			window.location = '?dus='+ this.value;
		});

		$('#dus_table tr').mouseover(function(){
			$(this).addClass('MouseOver');
		});

		$('#dus_table tr').mouseout(function(){
			$(this).removeClass('MouseOver');
		});

		$('#Charts > ul > li > a').click(function(){
			$('#Charts > ul > li.selected').removeClass('selected');
			$('#Charts > div.open').hide();
			$('#Charts > div.open').removeClass('open');

			$(this).parent().addClass('selected');
			$('#'+ $(this).attr('name') ).show();
			$('#'+ $(this).attr('name') ).addClass('open');
		});

	}

	// comments
	if( $('.comments').length ){
		var object_id = $('#object_id').val();

		// submit
		$('.comment_submit').click(function(){
			var comment = $('#comment').val();
			var name = $('#name').val();
			$('#comment, .comment_submit').attr('disabled', 'disabled');
			$.ajax({
				type: 'POST',
				url: './?comment_add',
				data: 'id='+ object_id +'&comment='+ comment +'&name='+name,
				beforeSend: function(){
					if($.cookie('commentOn') == null){
						$.cookie('commentOn', 1, { path: '/' });
						//alert('ok');
					}
					$('#loading').show();
				},
				success: function(xmlData){
					$('#comment').val('');
					comment_load(object_id);
					$('#comment, .comment_submit').removeAttr('disabled');
					$('#loading').hide();
					// alert('Paldes! ( vajag smukāku logu / ikonu, kur ladejas komentars )' + xmlData);
				}
			});
		});
	}


	// rates
	if( $('.rating_vote').length ){
		var object_id = $('#object_id').val();

		$('.rating > div').css('cursor', 'pointer');
		$('.rating > div').mouseenter(function(){
			var ths = $('div', $(this).parent() );
			var star = ths.index(this) + 1;
			var star_off = star;
			ths.each(function( inx ){
				if(star > inx){
					if($(this).hasClass('star_off')){
						$(this).removeClass('star_off');
						$(this).addClass('star');
					}
				}else{
					if($(this).hasClass('star')){
						$(this).removeClass('star');
						$(this).addClass('star_off');
					}
				}
			});
		});
		$('.rating > div').click(function(){
			var star = $('.rating > div').index(this) + 1;
			$.ajax({
				type: 'POST',
				url: './?rating_add',
				data: 'id='+ object_id +'&rate='+ star,
				beforeSend: function(){
					$('.rating').html('<img src="/img/indicator2.gif" alt="" style="vertical-align:middle" />');
				},
				success: function(xmlData){
					$('.rating').html( xmlData );
					/*
					var e = $('.rating span');
					e.fadeIn();
					e.queue(function(){
						setTimeout(function(){
							e.dequeue();
						}, 1000 );
					});
					e.fadeOut('fast');
					*/
				}
			});
		});

	}



	// dvd
	if( $('.DVD').length ){
		if($('#x1').val() == ''){
			$('#x1').val('Nosaukums, atslēgas vārds...');
			$('#x1').focus(function(){ this.value = '' });
		}
		$('#x1').focus(function(){ $(this).addClass('onclick') });

		if($('.single').length == 0){
			$('.DVD .movie_box .rating').css('cursor', 'pointer');
			$('.DVD .movie_box .rating').click(function(){
				location.href = $('h2 > a', $(this).parent().parent() ).attr('href');
			});
		}
	}


	// kino
	if( $('.Kino').length ){
		if($('#x1').val() == ''){
			$('#x1').val('Nosaukums, atslēgas vārds...');
			$('#x1').focus(function(){ this.value = '' });
		}
		$('#x1').focus(function(){ $(this).addClass('onclick') });

		$('#KinoX').change(function(){
			window.location = '?cinema='+ this.value;
		});

		if($('.single').length == 0){
			$('.Kino .movie_box .rating').css('cursor', 'pointer');
			$('.Kino .movie_box .rating').click(function(){
				location.href = $('h2 > a', $(this).parent() ).attr('href');
			});
		}

		// Reminder
		$('li.Reminder a').click(function(){
			try{
				var msg = $(this).attr('title');
				OpenSendSmsForm(userPhone, msg, unixtime, module);
			}catch(e){
				alert('Atvainojamies, bet šobrīd SMS atgādinājumu nav iespējams nosūtīt!');
			}
			return false;
		});
	}


	// tv
	if( $('.TV').length ){
		if($('#x1').val() == ''){
			$('#x1').removeClass('onclick');
			$('#x1').val('Nosaukums..');
			$('#x1').focus(function(){ $(this).val(''); $(this).addClass('onclick'); });
		}
		$('.SearcherContent form').submit(function(){
			if($('#x1').val() == 'Nosaukums..'){
				$('#x1').val('');
			}
			return true;
		});

		$('.more, .close').css('cursor', 'pointer');
		$('.more').click(function(){
			$('.channels_menu').toggle();
		});
		$('.close').click(function(){
			$('.channels_menu').toggle();
		});

		$('.popup_content ul').bind("mouseenter mouseleave", function(e){
			$(this).toggleClass('hover');
   		});

		// Reminder
		$('a.Reminder').click(function(){
			var data = $('span', this).text().split('@');
			OpenSendSmsForm(userPhone, data[0], data[1], module);
			return false;
		});

		$('td > p').toggle(function(){
			$('> span', this).removeClass('closeInfo');
			$('> span', this).addClass('openInfo');
		}, function(){
			$('> span', this).removeClass('openInfo');
			$('> span', this).addClass('closeInfo');
		});

		// style-2 next
		$('.timelineview .prev a').click(function(){
			$('.timelineview ul').css('left', function(i, val){
				if(parseFloat(val) == 0){
					return;
				}
				return parseFloat(val) + 180;
			});
			$('.real-time').css('marginLeft', function(i, val){
				if(parseFloat(val) > 0){
					return;
				}
				return parseFloat(val) + 180;
			});
		});
		$('.timelineview .next a').click(function(){
			$('.timelineview ul').css('left', function(i, val){
				if(parseFloat(val) < -4500){
					return;
				}
				return parseFloat(val) - 180;
			});
			$('.real-time').css('marginLeft', function(i, val){
				if(parseFloat(val) < -4500){
					return;
				}
				return parseFloat(val) - 180;
			});
		});

		if( $('.timelineview').length ){
			window.setInterval(tvRealTime, 20000); // 20sec
		}
	}

	var realTime_offset = 0;
	function tvRealTime(){
		$('.real-time').css('margin-left', parseInt( $('.real-time').css('margin-left') )+1 );

		realTime_offset += 0.34; // 20sec = 0.34x3 = 1min
		var realTime = $('.real-time > .time-top').text();
		var time = realTime.split(':');
		timeH = parseInt(time[0], 10);
		timeM = parseInt(time[1], 10);
		// add minute
		if(Math.floor(realTime_offset) == 1){
			timeM += 1;
			realTime_offset = 0;
		}
		// next hour
		if(timeM == 60){
			timeH += 1;
			timeM = '0';
			realTime_offset = 0;
		}
		// next hour
		if(timeH == 24){
			timeH = 0;
		}
		// set zero before one number
		if(timeM < 10){
			timeM = '0'+ timeM;
		}
		// $('#x1').val( real_time +'-'+  parseInt( time[1] )  +'-'+ real_time_offset +'-'+ Math.floor(real_time_offset) );

		realTime = timeH +':'+ timeM;
		$('.real-time > .time-top,.time-bottom').text(realTime);
	}


	// vacancy
	if( $('.vakances').length ){
		if($('#x1').val() == ''){
			$('#x1').removeClass('onclick');
			$('#x1').val('Amats, nozare vai uzņēmums...');
			$('#x1').focus(function(){ $(this).val(''); $(this).addClass('onclick'); });
		}
	}

});


// dvd
function dvd(){
	if($('#x1').val() == 'Nosaukums, atslēgas vārds...'){
		$('#x1').val('');
	}
	return true;
}
// kino
function kino(){
	if($('#x1').val() == 'Nosaukums, atslēgas vārds...'){
		$('#x1').val('');
	}
	return true;
}
// comment load 2
function comm(page){
	var object_id = $('#object_id').val();
	comment_load(object_id, page);
}
// comment load
function comment_load(id, page){
	var object_id = $('#object_id').val();
	if( page == undefined ){ var page = 1; }
	$.ajax({
		type: 'GET',
		url: './?comment_get&id='+ object_id + '&page='+ page,
		dataType: 'html',
		success: function(data){
			$('ul.comments').html(data);
		}
	});
};

/*
// dus
var map_points = new Array();
var buf_ids = '21,22,23,24,25,26';

function getPoints(ids){
	var len = map_points.length;
	for( var i = 0; i < len; i++){
		map.deleteMarker( map_points[i] );
	}
	map_points = new Array();

	var url = '';
	var cor_type = 'lks';
	if( map.mapType == 'gmap' ){
		cor_type = 'geo';
	}
	var cor = map.MapCorners(cor_type);
	var x = cor.x_min;
	var x2 = cor.x_max;
	var y = cor.y_min;
	var y2 = cor.y_max;
	// alert(ids +'-'+ buf_ids +'#'+ typeof(ids));
	if(typeof(ids) == 'undefined'){
		ids = buf_ids;
	}else{
		buf_ids = ids;
	}
	// alert(ids +'-'+ buf_ids +'#'+ typeof(ids));
	url += '&ids='+ ids;

	$.ajax({type:'GET',
		url: '/karte/bankomati/?action=points&map='+ map.mapType +'&x='+ x +'&x2='+ x2 +'&y='+ y +'&y2='+ y2 + url,
		dataType: 'json',
		beforeSend: function(){ $('#loading').show(); },
		success: function(data){
			if(data.p){
				$.each(data.p, function(i,item){
					var image_id = map.addMarker(item.x, item.y, item.name, item.type);
					map_points.push( image_id );
				});
			}
			$('#loading').hide();
		}
	});
}
*/

// vacancy
function vacancy(){
	if($('#x1').val() == 'Amats, nozare vai uzņēmums...'){
		$('#x1').val('');
	}
	return true;
}
