<?php
defined( '_JEXEC' ) or die( 'Restricted index access' );

// set default vars
if (!empty($stylesList[$preset_style])) {
	$fontstyle = "f-" . $stylesList[$preset_style][2];
	$fontfamily = $stylesList[$preset_style][2];
	$tstyle = $stylesList[$preset_style][0];
	$bgstyle = $stylesList[$preset_style][1];
} else {
	$fontstyle = "f-" . $font_family;
	$fontfamily = $font_family;
	$tstyle = $primary_style;
	$bgstyle = $bg_style;
}
$mtype = $menu_type;
$thisurl = $this->base . rebuildQueryString($template_properties);
$my_session = JFactory::getSession();

//array of properties to look for and store
foreach ($template_properties as $tprop) {

    if (isset($_REQUEST['reset-settings'])) {
        $my_session->set($cookie_prefix.$tprop,"");
        setcookie ($cookie_prefix. $tprop, "", $cookie_time, '/', false);  
    } elseif ($my_session->get($cookie_prefix.$tprop)) {
        $$tprop = $my_session->get($cookie_prefix.$tprop);
    } elseif (isset($_COOKIE[$cookie_prefix. $tprop])) {
    	$$tprop = htmlentities(JRequest::getVar($cookie_prefix. $tprop, '', 'COOKIE', 'STRING'));
    }    
}

if ($preset_style != $tstyle) {
    rebuildColorParams($this, $preset_style);
}

// rebuild color params from preset array
function rebuildColorParams(&$template, &$tstyle) {
    global $stylesList, $primary_style, $body_style, $font_family, $fontstyle, $bg_style, $fontfamily;
    
	if (!empty($stylesList[$tstyle])) {
    	$style = $stylesList[$tstyle];
       	$primary_style = $style[0];
    	$bg_style = $style[1];
		$fontfamily = $style[2];
	}
	$fontstyle = "f-" . $fontfamily;
    $font_family = $fontfamily;
    //var_dump ($template->params);
}

// rebuild the querystring when needed
function rebuildQueryString($template_properties) {

  if (!empty($_SERVER['QUERY_STRING'])) {
      $parts = explode("&", $_SERVER['QUERY_STRING']);
      $newParts = array();
      foreach ($parts as $val) {
          $val_parts = explode("=", $val);
          if (!in_array($val_parts[0], $template_properties)) {
            array_push($newParts, $val);
          }
      }
      if (count($newParts) != 0) {
          $qs = implode("&amp;", $newParts);
      } else {
          return "?";
      }
      return "?" . $qs . "&amp;"; // this is your new created query string
  } else {
      return "?";
  } 
}
?>