<?php
/*
 **************************************************************************************************
 *    Copyright (c) 2010-2012 Just Another Video script
 *
 * 	  This file is part of Just Another Video Script.
 *    Version 1.4.3
 *    info@justanothervideoscript.com
 **************************************************************************************************
 *    Come to website for support or buying additional plugins/modules.
 *    http://justanothervideoscript.com/
 **************************************************************************************************
 *    By using this software you agree that you have read and acknowledged our End-User License
 *    Agreement available at http://justanothervideoscript.com/license and can be bound by it.
 **********************************************************************************
 */
include_once "includes/common.inc" ;
global $db,$sitepath,$rootpath,$template,$templateimagepath,$loggedIn;

//moet blijven
$vrw = quote_smart($_GET['vrw']) ; //title of video with -
$tabfav = strpos($vrw, '/addfav');
$tabflag = strpos($vrw, '/flagmedia');

if ($tabfav !== false) {
	$addfav = 1 ;
	$vrw = str_replace("/addfav", "", $vrw);
}
if ($tabflag !== false) {
	$addflag = 1 ;
	$vrw = str_replace("/flagmedia", "", $vrw);
}

$videotitle = str_replace("-", " ", $vrw); //video title without the - 


$pagelink = $sitepath."play/" ; 
if ($vrw != "") {
	$pagelink .= str_replace(" ", "-", $vrw) ; //important for rating or comment i forgot lol
	$_SESSION['session_pagelink'] = $pagelink ; //maybe I use this
}

//now get the right video from database
	$db->query("SELECT * FROM media WHERE title = '".$videotitle."'") ;
	$videores = $db->fetch() ;
	
	$poster = $videores['poster']; //poster of the video
	$vidid = $videores['id']; //related colum for user vids
	$tags = $videores['tags']; //related colum for suggestions
	$title = $videores['title']; //title above the video
	
	//personal ad creating
	if ($videores['vidad'] != '') {
		$personalvideoad = '<div id="adsecond468x60">'.$videores['vidad'].'</div>' ;
	}
	
	// update views
	$db->query("SELECT checkday,checkweek,checkmonth FROM media WHERE id=".$vidid) ;
	$dres = $db->fetch() ;
	$last_day = $dres['checkday'] ;
	$todays_day = mktime(0, 0, 0, date("m"), date("d"), date("Y")) ;
	if ($todays_day != $last_day) {
		$db->query("UPDATE media SET checkday='".$todays_day."',dayviews=0 WHERE id=".$vidid) ;
	}
	$last_week = intval($dres['checkweek']) ;
	$todays_week = intval(date("W")) ;
	if ($todays_week != $last_week) {
		$db->query("UPDATE media SET checkweek='".$todays_week."',weekviews=0 WHERE id=".$vidid) ;
	}
	$last_month = intval($dres['checkmonth']) ;
	$todays_month = intval(date("m")) ;
	if ($todays_month != $last_month) {
		$db->query("UPDATE media SET checkmonth='".$todays_month."',monthviews=0 WHERE id=".$vidid) ;
	}
	$db->query("UPDATE `media` SET allviews=allviews+1,monthviews=monthviews+1,weekviews=weekviews+1,dayviews=dayviews+1 WHERE `id` = '".$vidid."'") ;

	//update lastviewed
	$db->query("UPDATE media SET lastviewed =".time()." WHERE id=".$vidid);

	//get category under the video
	$db->query("SELECT name FROM category WHERE id = ".$videores['category']) ;
	$catres = $db->fetch() ;
	$fullcategorypath = url_encode($catres['name']) ;	
	$category = '<a href="'.$sitepath.'category/'.$fullcategorypath.'">'.str_replace("-", " ", $fullcategorypath).'</a> ' ;
	
	//get tags under the video
	
	$videotags = strtolower($videores['tags']) ;
	
	if (str_replace(" ", "", $tags) != '') {
		$tagwords = explode(",", $tags) ;
		$taglinks = '' ;
			foreach ($tagwords as $tagword) {
				if (trim($tagword) != '') {
					$taglinks .= "<a href='".$sitepath."search/".urlencode(trim($tagword))."'>$tagword</a>," ;
				}
			}
	} else {
		$taglinks = '' ;
	}
	
	
	
	
	
	
	if (getSetting("showdesc", $db) == '1') {
		$description = $videores['description']; //description above the video if set in admin panel
	}

	//addthis is easy to use and people can share your stuff easy
	if (getSetting("showsocial", $db) == '1') {
		$showsocial = '<div class="addthis_toolbox addthis_32x32_style addthis_default_style">   
			<a class="addthis_button_facebook"></a>    
			<a class="addthis_button_twitter"></a>   
			<a class="addthis_button_email"></a>    
			<a class="addthis_button_google"></a>    
			<a class="addthis_button_myspace"></a>
			<a class="addthis_button_digg"></a>
			<a class="addthis_button_delicious"></a>
			<a class="addthis_button_reddit"></a>
			<a class="addthis_button_stumpleupon"></a>
			<a class="addthis_button_live"></a>
			<a class="addthis_button_favorites"></a>
			<a class="addthis_button_email"></a>
			<a class="addthis_button_google_plusone"></a>  
			<a class="addthis_button_compact"></a>
			</div>' ;	
	}


	//now get the video or embed code in flowplayer of jwplayer for now

	$mediaPlayer = "" ;
	if ($videores['mediatype'] == "video") {
			if (getSetting("player", $db) == 'flow') { 
			
				$mediaPlayer = '<div id="player" style="height:480px;">
						<a href="'.$sitepath.'uploads/vids/'.$videores['fileid'].'/'.$videores['fileid'].'.flv" style="display:block;width:600px;height:480px;" id="flowplayer"></a> 
						 <script type="text/javascript">
						var options = {          
						    // use the first frame of the video as a "splash image" 
						    clip: { 
						        autoPlay: true, 
						        autoBuffering: true,
						    }, 
						     
						    // controlbar settings 
						    plugins:  { 
						        controls: {             
						             all:false, 
						            scrubber:true, 
						            play:true, 
						            mute:true,
						            fullscreen:true, 
						            autoHide: "always",             
						            width: "98%",  
						            bottom: 5, 
						            left: "50%", 
						            borderRadius: 15, 
						         } 
						    } 
						};
						options.plugins.controls.bufferColor=buffcol;
						options.plugins.controls.progressColor=progcol;
						options.plugins.controls.buttonColor=butcol;
						options.plugins.controls.buttonOverColor=butovercol;
						options.plugins.controls.tooltipColor=tooltipcol;
						$f("flowplayer", "'.$sitepath.'player/flow/flowplayer-3.2.7.swf",  	options);					
						</script>
						</div>											
						';
				$medialink = $pagelink;		
			} 
			else if (getSetting("player", $db) == 'jwplayer') {
				$skinname = getSetting("skinjw", $db) ;
				if ($videores['hd'] == "1") {
					$hd = '&plugins=hd-1&hd.file='.$sitepath.'uploads/vids/'.$videores['fileid'].'/'.$videores['fileid'].'.mp4' ;
				} else {
					$hd = '' ;
				}
				$mediaPlayer = '
				<div id="player">This will be replaced with video</div>

				<script type="text/javascript">
				var so = new SWFObject("'.$sitepath.'player/jwplayer/player.swf","mpl","600","480","9");
				so.addParam("allowscriptacces","always");
				so.addParam("allowfullscreen","true");
				so.addParam("wmode","opaque");			
				so.addParam("flashvars","file='.$sitepath.'uploads/vids/'.$videores['fileid'].'/'.$videores['fileid'].'.flv'.$hd.'&amp;skin='.$sitepath.'player/jwplayer/skin/'.$skinname.'&amp;autostart=true&amp;dock=false&amp;provider=http&amp;stretching=false");
				so.write("player");
				</script>'; 

				$medialink = $pagelink;	 
			} 

	}
	else if ($videores['mediatype'] == "embed") {
		$mediaPlayer = stripslashes($videores['embed']) ;
	}	


	//now get the vars that only show or changed when user is loggedin
	
	//flag
	if ($loggedIn) {
		if ($videores['flagged'] == '0') {
			$flagvidlink = '<a href="#" id="play_flag" title="Flag this Video"></a>' ;
		}

		$db->query("SELECT id FROM member WHERE username = '".$_SESSION['session_username']."'") ;
		$userres = $db->fetch() ;
		$userid = $userres['id'] ;	
		$db->query("SELECT * FROM favorite WHERE userid=".$userid." AND vidid=".$vidid) ;

		if ($db->numRows() == 0) {
			$addfavlink = '<a href="#" id="play_addfav" title="Added to Favorites"></a>' ;
		}
		$Jdownload = '<a href="#" id="play_down" title="Download Video"></a>' ;
	} 
	if (!$loggedIn) {
		if ($videores['flagged'] == '0') {
			$flagvidlink = '<a href="'.$sitepath.'login">Login to Flag</a>' ;
		}
		$Jdownload = '<a href="#" id="play_down" title="Download Video"></a>' ;
	}
?>