<?php

Class Js {

	/**
	 * @param $f3 \Base
	 * @param $params
	 */
	public function getAd($f3, $params){

		$url = '//'.$f3->get('HOST').$f3->get('BASE').'/index.php?in&a&i=';
		$code = $f3->get('PARAMS.code');
		if (!is_numeric($code)) $code = 1;

		if ($f3->get('GET.v') == '2'){

			if ($code == 3){
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mav1_3").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&loc="+t.attr("data-l")+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			} else {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mav1_1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			}

		} else {

			if ($code == 3){
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mav1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&loc="+t.attr("data-l")+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			} else {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mav1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			}

		}

		header('Content-Type: text/javascript; charset=UTF-8');
		echo $js;
	}

	/**
	 * @param $f3 \Base
	 * @param $params
	 */
	public function getGroup($f3, $params){

		$js = $this->_getGroupingCode($f3, 'g');

		header('Content-Type: text/javascript; charset=UTF-8');
		echo $js;
	}


	/**
	 * @param $f3 \Base
	 * @param $params
	 */
	public function getSale($f3, $params){

		$js = $this->_getGroupingCode($f3, 's');

		header('Content-Type: text/javascript; charset=UTF-8');
		echo $js;
	}


	/**
	 * @param $f3 \Base
	 * @param $params
	 */
	public function getKeywordAd($f3, $params){

		$url = '//'.$f3->get('HOST').$f3->get('BASE').'/index.php?in&ka&k=';
		$code = $f3->get('PARAMS.code');
		if (!is_numeric($code)) $code = 1;

		if ($f3->get('GET.v') == '2'){

			if ($code == 3){
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.makv1_3").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&loc="+t.attr("data-l")+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			} else {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.makv1_1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			}

		} else {

			if ($code == 3){
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mav1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&loc="+t.attr("data-l")+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			} else {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mav1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			}

		}

		header('Content-Type: text/javascript; charset=UTF-8');
		echo $js;
	}


	/**
	 * @param $f3 \Base
	 * @param $params
	 */
	public function getKeywordGroup($f3, $params){

		$url = '//'.$f3->get('HOST').$f3->get('BASE').'/index.php?in&kg&k=';
		$code = $f3->get('PARAMS.code');
		if (!is_numeric($code)) $code = 1;

		if ($f3->get('GET.v') == '2'){

			if ($code == 2){
				$js = 'if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mgkv1_2").each(function(){function c(n){u=true;e(t).hide().html(n[0].Code).fadeIn("slow").delay(s-1e3).fadeOut("slow");var r=0;setInterval(function(){e(t).hide().html(n[r].Code).fadeIn("slow").delay(s-1e3).fadeOut("slow");r++;if(r>=n.length){r=0;h()}},s)}function h(){e.getJSON(l,function(e){if(!u)c(e)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_")}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s=t.attr("data-i");var o=true;var u=false;var a=100;if(s<2e3){s=2e3}var f=0;var l="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";h()})})})(jQuery)}';
			} elseif ($code == 3){
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mgkv1_3").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&loc="+t.attr("data-l")+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			} else {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mgkv1_1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			}

		} else {

			if ($code == 2){
				$js = 'if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mgrv1").each(function(){function c(n){u=true;e(t).hide().html(n[0].Code).fadeIn("slow").delay(s-1e3).fadeOut("slow");var r=1;setInterval(function(){e(t).hide().html(n[r].Code).fadeIn("slow").delay(s-1e3).fadeOut("slow");r++;if(r>=n.length){r=0;h()}},s)}function h(){e.getJSON(l,function(e){if(!u)c(e)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_")}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s=t.attr("data-i");var o=true;var u=false;var a=100;if(s<2e3){s=2e3}var f=0;var l="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";h()})})})(jQuery)}';
			} elseif ($code == 3){
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mgv1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&loc="+t.attr("data-l")+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			} else {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mgv1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-k")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			}

		}
		header('Content-Type: text/javascript; charset=UTF-8');
		echo $js;
	}


	/**
	 * @param $f3 \Base
	 * @param $type string g for group, s for sale
	 */
	private function _getGroupingCode($f3, $type){
		$url = '//'.$f3->get('HOST').$f3->get('BASE').'/index.php?in&'.$type.'&i=';
		$code = $f3->get('PARAMS.code');
		if (!is_numeric($code)) $code = 1;
		$class = $type == 'g' ? 'mgv1_' : 'mgsv1_';

		if ($f3->get('GET.v') == '2'){

			if ($code == 2){
				$js = 'if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.'.$class.'2").each(function(){function fadeContent(a,b){e(t).html(a[b].Code).fadeIn("slow").delay(s-1e3).fadeOut("slow",function(){e(t).html(a[b].Code),b++,b>=a.length&&(b=0,h()),fadeContent(a,b)})}function c(n){u=true;var r=0;fadeContent(n, r);}function h(){e.getJSON(l,function(e){if(!u)c(e)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_")}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s=t.attr("data-i");var o=true;var u=false;var a=100;if(s<2e3){s=2e3}var f=0;var l="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";h()})})})(jQuery)}';
			} elseif ($code == 3) {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.'.$class.'3").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&loc="+t.attr("data-l")+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			} else {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.'.$class.'1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			}

		} else {

			if ($code == 2){
				$js = 'if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mgrv1").each(function(){function c(n){u=true;e(t).hide().html(n[0].Code).fadeIn("slow").delay(s-1e3).fadeOut("slow");var r=1;setInterval(function(){e(t).hide().html(n[r].Code).fadeIn("slow").delay(s-1e3).fadeOut("slow");r++;if(r>=n.length){r=0;h()}},s)}function h(){e.getJSON(l,function(e){if(!u)c(e)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_")}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s=t.attr("data-i");var o=true;var u=false;var a=100;if(s<2e3){s=2e3}var f=0;var l="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";h()})})})(jQuery)}';
			} elseif ($code == 3) {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mgv1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&loc="+t.attr("data-l")+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			} else {
				$js ='if(typeof jQuery!="undefined"){(function(e){e(function(){e("span.mgv1").each(function(){function o(){e.getJSON(s,function(e){t.html(e.Code)})}var t=e(this);var n="";if(document.referrer){n=document.referrer.split("/")[2].replace(":","_");}var r="";if(e(location).attr("href")){r=e(location).attr("href").split("/")[2].replace(":","_")}var i=e(window).width();var s="'.$url.'"+t.attr("data-id")+"&c="+"'.$code.'"+"&w="+i+"&h="+r+"&rf="+n+"&callback=?";o()})})})(jQuery)}';
			}

		}

		return $js;
	}

}