<?php
session_start();

if($_SESSION['my_id']==1 || $_SESSION['my_id']==3) error_reporting(E_ALL);
	else error_reporting(0);

require_once('conf/definitions.php');
require_once('conf/modules.php');
require_once('conf/objs.php');
require_once('conf/functions.php');

$dba = new db;

$vote_actions = array('image', 'melody', 'game', 'video', 'theme', 'sms');

(key_exists('id', $_GET) && $_GET['id']<>'') ? $id=$_GET["id"] : $id = 0;
(key_exists('hlas', $_GET) && $_GET['hlas']<>'') ? $hlas=$_GET["hlas"] : $hlas = 0;
(key_exists('action', $_GET) && $_GET['action']<>'') ? $action=$_GET["action"] : $action = 0;



if($id!=0 && $hlas>0 && $hlas<=5 && in_array($action, $vote_actions))
{
    if(session_is_registered('my_id'))
    {	
        if(!session_is_registered('votes'))
        {
            $_SESSION['votes'] = array();
        }
        if(!session_is_registered('hls'))
        {
            session_register('hls');
            $_SESSION['hls'] = 0;
        }
        else 
        {
        	if(!in_array($_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'], $_SESSION['votes']))
        	{
        		$_SESSION['hls']++;
        		$_SESSION['votes'][] = $_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
        	}
        	if($_SESSION['hls'] >= $hlasov_na_kredit)
        	{
        		fnPridajKred($_SESSION['my_id'], 1);
        		$_SESSION['hls'] = 0;
        		$_SESSION['votes'] = array();
        	}
        }
    }
    
	if($action=='image') $session_name='mobile_image_'.$id;
	if($action=='melody') $session_name='mobile_melody_'.$id;
	if($action=='game') $session_name='mobile_game_'.$id;
	if($action=='video') $session_name='mobile_video_'.$id;
	if($action=='theme') $session_name='mobile_theme_'.$id;
	if($action=='sms') $session_name='mobile_sms_'.$id;
	
	
	if($action=='image'){$query="SELECT hlasov, sucet FROM mobile_pozadia WHERE ID='$id'";}
	if($action=='melody'){$query="SELECT hlasov, sucet FROM mobile_melody WHERE ID='$id'";}
	if($action=='game'){$query="SELECT hlasov, sucet FROM mobile_games WHERE ID='$id'";}
	if($action=='video'){$query="SELECT hlasov, sucet FROM mobile_video WHERE ID='$id'";}
	if($action=='theme'){$query="SELECT hlasov, sucet FROM mobile_themes WHERE ID='$id'";}
	if($action=='sms'){$query="SELECT hlasov, sucet FROM mobile_sms WHERE ID='$id'";}
	
	if($query<>'')
	{
		$result = $dba->GetResult($query);
		$row = mysql_fetch_array($result);
		$hlasov = $row["hlasov"] + 1;
		$sucet = $row["sucet"] + $hlas;
		$priemer = round($sucet/$hlasov,2);
	}
		
	$query='';
	if($action=='image' && !session_is_registered($session_name)){$query="UPDATE mobile_pozadia SET hlasov='$hlasov', sucet='$sucet', priemer='$priemer' WHERE ID='$id' limit 1"; session_register($session_name);}
	if($action=='melody' && !session_is_registered($session_name)){$query="UPDATE mobile_melody SET hlasov='$hlasov', sucet='$sucet', priemer='$priemer' WHERE ID='$id' limit 1"; session_register($session_name);}
	if($action=='game' && !session_is_registered($session_name)){$query="UPDATE mobile_games SET hlasov='$hlasov', sucet='$sucet', priemer='$priemer' WHERE ID='$id' limit 1"; session_register($session_name);}
	if($action=='video' && !session_is_registered($session_name)){$query="UPDATE mobile_video SET hlasov='$hlasov', sucet='$sucet', priemer='$priemer' WHERE ID='$id' limit 1"; session_register($session_name);}
	if($action=='theme' && !session_is_registered($session_name)){$query="UPDATE mobile_themes SET hlasov='$hlasov', sucet='$sucet', priemer='$priemer' WHERE ID='$id' limit 1"; session_register($session_name);}
	if($action=='sms' && !session_is_registered($session_name)){$query="UPDATE mobile_sms SET hlasov='$hlasov', sucet='$sucet', priemer='$priemer' WHERE ID='$id' limit 1"; session_register($session_name);}
	if($query<>'')
	{
		$dba->ExecuteQuery($query);
	}
}

header("location: ".$_SERVER['HTTP_REFERER']);

 ?>
