<?php
require('config.php');
require('fn.php');
require('ga-tracking.php');

$device = '';
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($ua,'android') !== false) {
    $device = 'android';
} else if((stripos($ua,'iPhone') !== false) || (stripos($ua,'iPod') !== false) || (stripos($ua,'Pad') !== false)) {
    $device = 'iOS';
}

$demo = false;
if(isset($_GET['mode'])) {if($_GET['mode'] == 'demo') $demo = true;}

$ga = (isset($_GET['ga']) && $_GET['ga'] == true && isset($_GET['tr_id']));
$url = $_GET['url'];
$url_decoded = urldecode($_GET['url']);

$show = check_url_cache($url_decoded,$supported);
if(isset($_GET['bg_style']) && isset($_GET['show_text'])) {
    $bg = urldecode($_GET['bg_style']);
    $txt = urldecode($_GET['show_text']);
    $ui = (in_array($bg, $ui_styles)) ? $bg : 'light';
    $text = (in_array($txt, array('yes','no'))) ? $txt : 'yes';
} else {
    $ui = 'light';
    $text = 'yes';
}

function check_url_cache($url_string,$supported) {
	if(stripos($url_string,'youtube.com') !== false) {
		return true;
	}
    $url_sha1 = sha1($url_string);
    // 4 - supported, 5 - unsupported
    $playable = false;
    //check if url is in cache
    ////if($a = apc_fetch($url_sha1) !== false) {
        // url is in cache, check if it's a supported format
       // ($a == 4) ? $playable = true : $playable = false;
    ////} else {
        // url is not in cache, check format and store
        $type = get_url_mime_type($url_string);
        if(in_array($type, $supported)) {
            // supported, write as 4
            ////apc_store($url_sha1, 4, $cache_ttl);
            $playable = true;
        } else {
            // not supported write as 5
           //// apc_store($url_sha1, 5, $cache_ttl);
            $playable = false;
        }
    ////}
    return $playable;
}
?>
<!DOCTYPE html>
<html lang="en" id="ims_widget">
<head>
<meta charset="utf-8" />
<title>iMediaShare - &quot;Watch on TV&quot; Button Widget</title>
<script></script>
<style>
body{font-size: 11px;font-family:"lucida grande",tahoma,verdana,arial,sans-serif;color:#333;line-height:1.28;margin:0;padding:0;text-align:left;direction:ltr;unicode-bidi:embed}
h1,h2,h3,h4,h5,h6{font-size: 13px;color:#333;margin:0;padding:0}
h1{font-size: 14px}
h4,h5{font-size: 11px}
p{margin:1em 0}
a{cursor:pointer;color:#3b5998;-moz-outline-style:none;text-decoration:none}
a:hover{text-decoration:underline}
img{border:0}
td,td.label{font-size: 11px;text-align:left}
dd{color:#000}
dt{color:#777}
ul{list-style-type:none;margin:0;padding:0}
abbr{border-bottom:none}
hr{background:#d9d9d9;border-width:0;color:#d9d9d9;height:1px}
.clearfix:after{clear:both;content:".";display:block;font-size:0;height:0;line-height:0;visibility:hidden}
.clearfix{zoom:1}
.datawrap{word-wrap:break-word}
.word_break{display:inline-block}
wbr:after{content:"\00200B"}
.ellipsis{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
<?php if($text == 'yes') { ?>
    <?php if($ui == 'dark') { ?>
        #ims_button{background:url(./iMS_buttons_text.png) -246px 0px no-repeat;width:122px;height:30px}
        #label_text{font-size:15px;color:#fff;margin:0 0 0 34px;padding:5px 0 0 0;font-family:tahoma,sans-serif;display:block}
    <?php  } else if($ui == 'medium') { ?>
        #ims_button{background:url(./iMS_buttons_text.png) -123px 0px no-repeat;width:122px;height:30px}
        #label_text{font-size:15px;color:#fff;margin:0 0 0 34px;padding:5px 0 0 0;font-family:tahoma,sans-serif;display:block}
    <?php  } else { // light - default UI ?>
        #ims_button{background:url(./iMS_buttons_text.png) 0px 0px no-repeat;width:122px;height:30px}
        #label_text{font-size:15px;color:#000;margin:0 0 0 34px;padding:5px 0 0 0;font-family:tahoma,sans-serif;display:block}
    <?php } ?>
<?php } else { ?>
    <?php if($ui == 'dark') { ?>
        #ims_button{background:url(./iMS_buttons_notext.png) -77px 0px no-repeat;width:37px;height:38px}
        #label_text{font-size:11px;color:#fff;margin:3px 0 0 32px;padding:4px 0 0 0;font-family:tahoma,sans-serif;display:block}
    <?php  } else if($ui == 'medium') { ?>
        #ims_button{background:url(./iMS_buttons_notext.png) -39px 0px no-repeat;width:37px;height:38px}
        #label_text{font-size:11px;color:#fff;margin:3px 0 0 32px;padding:4px 0 0 0;font-family:tahoma,sans-serif;display:block}
    <?php  } else { // light - default UI ?>
        #ims_button{background:url(./iMS_buttons_notext.png) 0px 0px no-repeat;width:37px;height:38px}
        #label_text{font-size:11px;color:#000;margin:3px 0 0 32px;padding:4px 0 0 0;font-family:tahoma,sans-serif;display:block}
    <?php } ?>
<?php } ?>
</style>
<script>
<?php if($ga) { ?>
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', '<?php echo $_GET['tr_id'] ?>']);
	_gaq.push(['_setDomainName', 'imediashare.tv']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
<?php } ?>

	function openAndroid() {
<?php if($ga) { ?>
		trackPlayEvent("Watch on TV", "Play", "<?php echo $url ?>");
<?php } ?>
		setTimeout('openAndroidImpl()', 100);
	}

	function openAndroidImpl() {
		var imsUrl = 'ims://play?url=<?php echo urlencode($url) ?>';

		var iframe = document.createElement('iframe');

		iframe.style.visibility = 'hidden';
		iframe.onload = function noapp() { parent.window.location = "http://<?php echo $_SERVER['SERVER_NAME'];?>/w/play.php?url=<?php echo urlencode($url) ?>";};
		iframe.src = imsUrl;
		document.body.appendChild(iframe);
	}

	// not used
	// function openiOS() {
// <?php if($ga) { ?>
		// trackPlayEvent("Watch on TV", "Play", "<?php echo $url ?>");
// <?php } ?>
		// setTimeout('openiOSImpl()', 100);
	// }

	// function openiOSImpl() {
		// var imsUrl = 'ims://play?url=<?php echo urlencode($url) ?>';

    // var w = window.location(imsUrl);
    // var time = (new Date()).getTime();
    // setTimeout(function(){
		// w.close();
        // var now = (new Date()).getTime();

        // if((now-time)<400) {
            // if(confirm('iMediaShare app plays online videos on your connected TV screen without any hardware, cables or TV apps. Click OK to download iMediaShare and play selected internet videos on any connected TV.')){
// window.location = 'http://itunes.apple.com/app/id302026401';
            // }
         // }
    // }, 300);
// }

	function trackPlayEvent(category, action, label) {
		_gat._getTrackerByName()._trackEvent(category, action, label);
	}

</script>
</head>
<body>
<?php if($demo) { ?>
    <?php if(!$show) { ?>
        <div id="error_msg">Unsupported video format</div>
    <?php } else { ?>
        <a style="text-decoration:none" target="_parent" href="http://<?php echo $_SERVER['SERVER_NAME'];?>/w/play.php?url=<?php echo urlencode($url) ?>"><div id="ims_button">
            <?php if($text == 'yes') { ?>
            <div id="label_text">watch on TV</div>
            <?php } ?>
        </div></a>
    <?php } ?>
<?php } else { ?>
    <?php if(!$show) { ?>
        <div id="error_msg">Unsupported video format</div>
    <?php } else { ?>
        <?php if($device == 'android') { ?>
            <a style="text-decoration:none"  href="#" onclick="openAndroid();"><div id="ims_button">
                <?php if($text == 'yes') { ?>
                <div id="label_text">watch on TV</div>
                <?php } ?>
            </div></a>
        <?php } ?>
        <?php if($device == 'iOS') { ?>
            <a id="watch-link" style="text-decoration:none" target="_parent" href="#"><div id="ims_button">
                <?php if($text == 'yes') { ?>
                <div id="label_text">watch on TV</div>
                <?php } ?>
            </div></a>
        <?php } ?>
    <?php } ?>
	<?php 			// not the best place to put it; better after </html>
		flush();	// flush output buffer
		ga_trackPageview($_SERVER['SCRIPT_URL'], 'iMediaShare - &quot;Watch on TV&quot; Button Widget');
	?>
<?php } ?>

<script>
		document.getElementById('watch-link').onclick = function(ev) {
			var imsUrl = 'ims://play?url=<?php echo urlencode($url) ?>';

    		window.location = imsUrl;
			var time = (new Date()).getTime();
    		setTimeout(function(){
				var now = (new Date()).getTime();

        		if((now-time)<400) {
            		if(confirm('iMediaShare app plays online videos on your connected TV screen without any hardware, cables or TV apps. Click OK to download iMediaShare and play selected internet videos on any connected TV.')){
						window.location = 'http://itunes.apple.com/app/id302026401';
            		}
				}
    		}, 300);
			ev.preventDefault();
		};
</script>
</body>
</html>