var play_video_redirect;
var last_date_range = null;
function get_videos(id_category, type, offset_page){
	if(last_date_range == null){
		last_date_range = 'all_time';
	}
	$.ajax({
			url: URL_BASE + "?page=ajax",
			type: "GET",
			data: ({
				"action" : 'get_videos',
				"id_category" : id_category,
				"offset_page" : offset_page,
				"type" : type,
				"date_range" : last_date_range
			}),
			dataType: "html",
			success: function(data){
				$('#video-archive-ajax').html(data);
			}
		}
    )
}

$(function(){
	if(location.hash != undefined && location.hash.length > 1){
		var hash_function = location.hash.split('-');
		if(hash_function[0] == '#play_video'){
			current_id_video = hash_function[2];
			play_video(hash_function[2], hash_function[1]);
		}
	}
});

function video_arhive_tab(n){
	$('#video-archive-categories .selected').removeClass('selected');
	$('#video-archive-categories ul li:eq(' + n + ')').addClass('selected');
}

var current_id_video = -1;

function play_video( id_video, id_category ){
	if(play_video_redirect != undefined && play_video_redirect == true){
		document.location.href = '?page=video&cat=' + id_category + '&video=' + id_video;
		return;
	}
	select_video(id_video);
	$.ajax({
			url: URL_BASE + "?page=ajax",
			type: "GET",
			data: ({
				"action" : 'get_play_video_info',
				"id_video" : id_video
			}),
			dataType: "json",
			success: function(data){
				$('#video-big').show();
				jwplayer("video").load({
					file: data.file, 
					image: data.photo
				});
				$('#video-big-bottom-left h1').html(data.title);
				$('#video-big-details .date').html(data.date);
				$('head title').html('Info TV - ' + data.title);
				_video_rating = data.rating;
				reset_rating_stars();
				current_id_video = id_video;
				scrollTo(0, 0);
				document.location.href = '#play_video-' + id_category + '-' + id_video;
			}
		}
    )
	
}

function select_video(id_video){
	$('.video-list-active').removeClass('video-list-active');
	$('.video-list-ajax-selected').removeClass('video-list-ajax-selected');
	$('#video-list-' + id_video).addClass('video-list-active');
	$('#video-list-ajax-' + id_video).addClass('video-list-ajax-selected');
}

var _video_rating = 3;

function reset_rating_stars(){
	for(var i=1; i<=5; i++){
		if(i<=_video_rating){
			$('#rating-star-' + i).attr('src', URL_BASE + 'media/img/ico-star-full.png');
		}else{
			$('#rating-star-' + i).attr('src', URL_BASE + 'media/img/ico-star-empty.png');
		}
	}
}

$(document).ready(function() {
    $('#flash-news-container').cycle({
		fx: 'fade',
		timeout:  7000
	});
	$('.rating-star').mouseover(function(){
		var _rating = $(this).attr('id').split('-')[2];
		for(var i=1; i<=5; i++){
			if(i<=_rating){
				$('#rating-star-' + i).attr('src', URL_BASE + 'media/img/ico-star-full.png');
			}else{
				$('#rating-star-' + i).attr('src', URL_BASE + 'media/img/ico-star-empty.png');
			}
		}
	});
	$('#rating-stars').mouseleave(function(){
		reset_rating_stars();
	});
	$('.rating-star').click(function(){
		$.ajax({
			url: URL_BASE + "?page=ajax",
			type: "GET",
			data: ({
				"action" : 'rate_video',
				"id_video" : current_id_video,
				"rating" : $(this).attr('id').split('-')[2]
			}),
			dataType: "html",
			success: function(data){
				alert('Glas je bil oddan');
			}
		});
	});
});

