<?php
$server_host = $_SERVER['HTTP_HOST'];
if(str_ireplace('www.', '', $server_host) != $ultra_domainname) {
	$server_host .= "/$ultra_addonname";
}

// Load the required JS and/or CSS
$optres['js'][] = "$path" . "js/dimensions.js";
$optres['js'][] = "$path" . "js/jquery-ui.custom.js";
//$optres['js'][] = "$path" . "js/jquery.dynatree.js";
//$optres['css'][] = "$path" . "css/ui.dynatree.css";
//$optres['js'][] = "$path" . "js/jquery.cookie.js";

$optres['css'][] = "$path" . "css/jqModal.css";
$optres['js'][] = "$path" . "js/jqModal.js";

$show_icons = (isset($module_show_icons) && intval($module_show_icons) == 1) ? true : false;
$show_expander = (isset($module_show_expander) && intval($module_show_expander) == 1) ? true : false;

if(!function_exists("show_article_breadcrumbs")) {
	function show_article_breadcrumbs($parent_id){
		$display_bread = false;
		$ret = "";
		$newparent = $parent_id;
		$root = $newparent;
		$i=0;
		$go_up = true;
		$query = "SELECT * FROM main_article_categ WHERE cat_id = '$parent_id'";
		$res = mysql_query ($query);
		$line = mysql_fetch_array($res);
		${"root$i"}=$line["title_en"];
		${"friendly$i"}=$line["friendly_url"];
		${"link$i"}=$line["cat_id"];
		if($line['is_root'] == 1) {
			$go_up = false;
			$display_bread = true;
		}
		while($go_up && $root>0){
			$newparent=$line["parent_id"];
			$query = "SELECT * FROM  main_article_categ WHERE cat_id = '$newparent'";
			$result = mysql_query ($query);
			$line = mysql_fetch_array($result);
			$i++;
			${"root$i"}=$line["title_en"];
			${"friendly$i"}=$line["friendly_url"];
			${"link$i"}=$line["cat_id"];
			$root=$newparent;
			
			if($line['is_root'] == 1) {
				$display_bread = true;
				${"root$i"} = "[home]" . stripslashes(trim($line["title_en"]));
				$go_up = false;
			}
		}
		for($j=($i);$j>=0; $j = $j-1){
			if($j>=0){
				if($j != $i) $ret .= '<strong> &raquo; </strong>';
				$class= "blue";
				if(strpos(${"root$j"},"[home]") !== false) {
					${"root$j"} = str_replace("[home]", "", ${"root$j"});
					$class = "blue_home";
				}
				
				$ret .= '<a href="articles/'.${"friendly$j"} . ".html" .'" class="' . $class . '">' . ${"root$j"} . '</a>';
			}
			
		}
		
		if($display_bread) return $ret;
		else return false;
	}
}

function categGetSubs($cat_id) {
	$ret = array();
	$q = "select * from main_article_categ where parent_id='$cat_id' and active='yes' order by order_seq asc;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r)>0) {
		while ($row = mysql_fetch_assoc($r)) {
			$ret [] = array(
				"categ_id" 					=> intval($row['cat_id']),
				"categ_title" 				=> trim(stripslashes($row["title_en"])),
				"categ_friendly_url" 		=> trim(stripslashes($row["friendly_url"])),
				"categ_url"					=> str_replace("http://", "", trim(stripslashes($row['url']))),
				"categ_blank" 				=> trim($row['_blank']),
				"categ_parent_id"			=> intval($row['parent_id']),
				"small_logo"				=> trim($row['small_logo']),
				"abstract"					=> trim(stripslashes($row['short_desc_en']))
			);
		}
	}
	
	if(count($ret)) return $ret;
	
	return false;
}

function categDisplaySubs($ar, $level=-1, $max_level=10, $to_show_icons=true) {
	
	$show_icons = ($to_show_icons == true) ? 'null' : 'false';
	$cur_level = 0;
	$sub_level = 0;
	$class_level = "";
	if($level >= 0) {
		$class_level = "level_" . $level;
		$cur_level = $level;
		$sub_level = $cur_level + 1;
	}
	if(!is_array($ar) || count($ar) == 0) return false;
	foreach ($ar as $i=>$categ) {
		$disp_url = ($categ['categ_url'] != "") ? ("http://" . str_ireplace("http://", "", trim($categ['categ_url']))) : ('articles/' . $categ['categ_friendly_url'] . ".html");
		$disp_target = ($categ['categ_blank'] == "yes") ? '_blank' : '_self';
		$disp_node = "node_" . $categ['categ_id'];
		$disp_title = mysql_real_escape_string($categ['categ_title']);
		$abstract = $categ['abstract'];
		
		$disp_li = '<li>';
		$disp_li .= '<h2><a href="'. $disp_url .'" target="'. $disp_target.'" title="'. $disp_title.'" class="article_folder">'. $disp_title. '</a></h2>';
		if($abstract != "") {
			$disp_li .= '<div class="categ_abstract"><p>' . $abstract . '</p><div style="clear: both;"></div></div>';
		}
		
		$subs = categGetSubs($categ['categ_id']);
		if($subs && count($subs)) {
			//$disp_li = str_replace("jstree-closed", "jstree-open", $disp_li);
			echo $disp_li;
			if($max_level > 0) {
				echo '<ul>';
				categDisplaySubs($subs, $sub_level, $max_level-1, $to_show_icons);
				echo '</ul>';
			}
			else {
				echo '<ul>';
				categDisplaySubs($subs, $sub_level, $max_level-1, $to_show_icons);
				echo '</ul>';
			}
			echo '</li>';
			
			$arts = categGetArticles($categ['categ_id']);
			if($arts) {
				categDisplayArticles($arts, $cur_level, $to_show_icons);
			}
			
		}
		else {
			$arts = categGetArticles($categ['categ_id']);
			if($arts) {
				
				//$disp_li = str_replace('class="folder"', 'class="lazy folder"', $disp_li);
				//$disp_li = str_replace('expand:true', 'expand:false', $disp_li);
				echo $disp_li;
				echo '<ul class="sub_arts">';
				categDisplayArticles($arts, $cur_level, $to_show_icons);
				echo '</ul>';
				echo '</li>';
			}
			
			
		}
		
		
	}
}

function categGetArticles($cat_id) {

	$ret = array();
	$q = "select * from main_article where parent_id='$cat_id' and active='yes' order by order_seq asc;";
	$r = @mysql_query($q);
	if($r && mysql_num_rows($r)>0) {
		while ($row = mysql_fetch_assoc($r)) {
			$ret [] = array(
				"article_id" 				=> intval($row['article_id']),
				"article_title" 			=> trim(stripslashes($row["title_en"])),
				"article_friendly_url" 		=> trim(stripslashes($row["friendly_url"])),
				"article_url"				=> str_replace("http://", "", trim(stripslashes($row['url']))),
				"article_blank" 			=> trim($row['_blank']),
				"article_parent_id"			=> intval($row['parent_id']),
				"small_photo"				=> $row['small_photo'],
				"abstract"					=> $row['short_descr_en']			
			);
		}
	}
	
	if(count($ret)) return $ret;
	
	return false;
}

function categDisplayArticles_($ar, $level=-1, $to_show_icons=true) {
	global $full_uri, $full_addon_uri;
	
	$show_icons = ($to_show_icons == true) ? 'null' : 'false';
	$class_level = "level_article";
	//if($level >= 0) {
	//	$class_level = "level_" . $level;
	//}

	if(!is_array($ar) || count($ar) == 0) return false;
	foreach ($ar as $i=>$article) {
		$disp_url = ($article['article_url'] != "") ? ("http://" . $article['article_url']) : "";
		$disp_href = ($article['article_friendly_url'] . ".html");
		
		$href = $disp_url != "" ? $disp_url : $disp_href;
		
		$disp_target = ($article['article_blank'] == "yes") ? '_blank' : '_self';
		$disp_node = "";
		$disp_title = $article['article_title'];
		
		echo '<li>';
		echo '&raquo; <a href="', ($disp_url == "" ? $full_addon_uri . $disp_href : $disp_url) ,'" target="', $disp_target, '" title="', $disp_title,'" class="article_article">', $disp_title, '</a>';
		echo '</li>';
	}
}

function categDisplayArticles($ar, $level=-1, $to_show_icons=true) {
	global $full_uri, $full_addon_uri, $server_host;
	
	$show_icons = ($to_show_icons == true) ? 'null' : 'false';
	$class_level = "level_article";
	//if($level >= 0) {
	//	$class_level = "level_" . $level;
	//}

	if(!is_array($ar) || count($ar) == 0) return false;
	foreach ($ar as $i=>$article) {
		$disp_url = ($article['article_url'] != "") ? ("http://" . $article['article_url']) : "";
		$disp_href = "article/" . $article['article_friendly_url'] . ".html";
		
		$href = $disp_url != "" ? $disp_url : $disp_href;
		
		$disp_target = ($article['article_blank'] == "yes") ? '_blank' : '_self';
		$disp_node = "";
		$disp_title = $article['article_title'];
		
		echo '<li>';
		?>
		<div class="article_list_item" itemscope="itemscope" itemtype="http://schema.org/Article">
			<?php 
			echo '&raquo; <a href="', ($disp_url == "" ? $full_addon_uri . $disp_href : $disp_url) ,'" target="', $disp_target, '" title="', $disp_title,'" class="article_article" itemprop="url"><span itemprop="name">', $disp_title, '</span></a>';
			$have_photo = false;
			$have_abstract = ($article['abstract'] != "") ? true : false;
			if($article['small_photo'] != "") {
				$article_photo_path = "images/articles/" . $article['article_id'] . "/" .$article['small_photo'];
				$article_photo = dirname(__FILE__) . "/../../$article_photo_path";
				
				if(file_exists($article_photo)) {
					$have_photo = true;
				}
			}
			
			if($have_abstract) {
				?>
				<div class="article_abstract" itemprop="description">
					<?php
					if($have_photo) {
						//echo $article_photo;
						?>
						<img src="http://<?php echo $_SERVER['HTTP_HOST'] . "/$article_photo_path"; ?>" alt="http://<?php echo $server_host . "/$article_photo_path"; ?>" itemprop="image">
						<?php
					}
					
					echo '<p>' . $article['abstract'] . '</p>';
					?>
					<div style="clear: both;"></div>
				</div>
				<?php
			}
			?>
		</div>
		
		<?php
		echo '</li>';
	}
}



include_once("$general_path" . "functions.php");
$query = "SELECT * FROM main_article_categ WHERE cat_id='$middle_parent_id'";
//echo $query;
$result = mysql_query ($query);
if($result && mysql_num_rows($result) > 0) {
	$line = mysql_fetch_assoc($result);
	
	$category_title = trim(stripslashes($line['title_en']));
	$category_abstract = trim(stripslashes($line['short_desc_en']));
	$category_meta_description = trim(stripslashes($line['meta_description_en']));
	$category_meta_keywords = trim(stripslashes($line['meta_keywords_en']));
	$category_friendly_url = trim($line['friendly_url']);
	
	?>
	<div id="article_reader">
		<div id="open_article_jqm" class="jqmWindow article_jqm" style="display:none;">
			<img alt="Close" title="Close Window" class="jqmClose" src="<?php echo $path;?>images/jqmodal/close_pop.png">
			<div id="jqm_header">
				<?php
				/*
				<div class="bookmarkus_container">
					<a href="" title="<?php echo $bookmarkus_title;?>" id="middle_article_bookmark" class="bookmarkus_link jqbookmark">Bookmark This Article</a>
				</div>
				*/
				?>
			</div>
			<div id="jqm_target"></div>
		</div>
	</div>
	<?php
	if($line['is_root'] != 1) {
		$breads = show_article_breadcrumbs($line['cat_id']);
		if($breads) {
		?>
		<div id="article_breadcrumbs" itemprop="breadcrumb">
			<?php
			echo $breads;
			?>
		</div>
		<?php
		}
	}
	?>
	<div class="middle_module_container article_list_middle_container">
		<?php
		if($module_show_parent_title == 1 && $line['is_root'] == 1) {
			?>
			<div class="middle_module_title_container article_list_middle_title_container">
				<div class="middle_module_title article_list_middle_title">
					<?php
						if($line['url'] != "") {
							$ahref = return_friendly_link('main_article_categ', $line["cat_id"], $line['_blank'], $line['url']);
							?>
							<p><a class="article_list_middle_main" href="<?php echo $ahref['link'];?>" target="<?php echo $ahref['target'];?>"><?php echo $line["title_en"];?></a></p>
							<?php
						}
						else {
							echo "<h1>".$line["title_en"]."</h1>"; 
						}
					?>
				</div>
				<div class="middle_module_title_image article_list_middle_title_image"></div>
			</div>
			<?php
		}

		$nr_sub_categs = 0;
		$q = "SELECT * FROM main_article_categ WHERE cat_id='$middle_parent_id' and active='yes'";
		$sub_categs = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
		if($sub_categs && mysql_num_rows($sub_categs)>0) {
			$nr_sub_categs = mysql_num_rows($sub_categs);
		}
		
		$root_nodes = array();
		
		if($nr_sub_categs > 0) {
			while ($categ = mysql_fetch_assoc($sub_categs)) {
				$categ_title = trim(stripslashes($categ["title_en"]));
				$categ_friendly_url = trim($categ['friendly_url']);
				$categ_id = $categ['cat_id'];
				$categ_url = "http://" . str_replace("http://", "", trim($categ['url']));
				$categ_blank = trim($categ['_blank']);
				$categ_short_desc = trim(stripslashes($categ["short_desc_en"]));
				
				$root_nodes [$categ_id] = "root_" . $categ_id;
				
				$have_list = false;
				$subs = categGetSubs($categ_id);
				$arts = categGetArticles($categ_id);
				
				if($subs || $arts) $have_list = true;
				
				if($have_list) {
					if($line['is_root'] != 1) {
						?>
						<div class="middle_module_title_container article_list_middle_title_container">
							<div class="middle_module_title article_list_middle_title">
								<?php
									if($line['url'] != "") {
										$ahref = return_friendly_link('main_article_categ', $line["cat_id"], $line['_blank'], $line['url']);
										?>
										<p><a class="article_list_middle_main" href="<?php echo $ahref['link'];?>" target="<?php echo $ahref['target'];?>"><?php echo $line["title_en"];?></a></p>
										<?php
									}
									else {
										echo "<h1>".$line["title_en"]."</h1>";
									}
								?>
							</div>
							<div class="middle_module_title_image article_list_middle_title_image"></div>
						</div>
						<?php 
					}
					?>
					<div class="middle_module_content article_list_middle_content">
					<table cellpadding="0" cellspacing="0" border="0" width="100%" class="article_list_table">
						<?php
						if($line['is_root'] != 1) {

							if($categ_short_desc != "") {
								?>
								<tr>
									<td align="left"><p class="categ_abstract"><?php echo $categ_short_desc;?></p></td>
								</tr>
								<?php
							}
						}
						else {
							if($module_show_parent_title == 1) {

								if($categ_short_desc != "") {
									?>
									<tr>
										<td align="left"><p class="categ_abstract"><?php echo $categ_short_desc;?></p></td>
									</tr>
									<?php
								}
							}
						}
						?>
						<?php
							if(is_array($subs)) {
								echo '<tr><td align="left">';
								echo '<div id="root_',$categ_id, '" class="sub_article_tree"><ul class="article_list">';
								categDisplaySubs($subs, 0, 10, $show_icons);
								if(is_array($arts)) {
									categDisplayArticles($arts, 0, $show_icons);
								}
								echo '</ul></div>';
								echo '</td></tr>';
							}
							else {
								echo '<tr><td align="left">';
								echo '<div id="root_',$categ_id, '" class="sub_article_tree"><ul class="article_list">';
								categDisplayArticles($arts, 0, $show_icons);
								echo '</ul></div>';
								echo '</td></tr>';
							}
						?>
					</table>
					</div>
					<?php
				}
			}
		}
		else {
			
			$arts = categGetArticles($middle_parent_id);
			if(is_array($arts) && count($arts)>0) {
				?>
				<div class="middle_module_content article_list_middle_content">
					<table cellpadding="0" cellspacing="0" border="0" width="100%" class="article_list_table">
					<?php
						/*
						?>
					<?php
					$categ_short_desc = trim(stripslashes($line["short_desc_en"]));
					if(!isset($module_show_parent_title)) {
						?>
						<th align="left"><?php echo trim(stripslashes($line["title_en"]));?></th>
						<?php
					}
					
					if($categ_short_desc != "") {
						?>
						<tr>
							<td align="left"><?php echo $categ_short_desc;?></td>
						</tr>
						<?php
					}
					
					*/
					
					
					echo '<tr><td align="left">';
					echo '<div id="root_',$middle_parent_id, '" class="sub_article_tree"><ul class="article_list">';
					categDisplayArticles($arts, 0, $show_icons);
					echo '</ul></div>';
					echo '</td></tr>';
					
					?>
					</table>
				</div>
				<?php
			}
		}
		?>
	</div>
	<?php
}
?>
<script type="text/javascript">

	function bookmark_article(article_url) {
		
		$('#middle_article_bookmark').attr("href", article_url);
		
		if(window.opera) {
			if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
				$("a.jqbookmark").attr("rel","sidebar");
			}
		}
	
		$("a.jqbookmark").click(function(event){
			event.preventDefault(); // prevent the anchor tag from sending the user off to the link
			var url = this.href;
			var title = this.title;
	
			if (window.sidebar) { // Mozilla Firefox Bookmark
				window.sidebar.addPanel(title, url,"");
			} else if( window.external ) { // IE Favorite
				window.external.AddFavorite( url, title);
			} else if(window.opera) { // Opera 7+
				return false; // do nothing - the rel="sidebar" should do the trick
			} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
				 alert('Unfortunately, this browser does not support the requested action,'
				 + ' please bookmark this page manually.');
			}
	
		});
	}

	$(document).ready(function(){
		
		<?php
		/*
		if(count($root_nodes) > 0) {
			$main_server_domainname = "lc.cypruswebsitescom.local";
			//$main_server_domainname = "cypruswebsites.com";
			
			if($on_main_server && (strtolower($_SERVER['HTTP_HOST']) != "www.$main_server_domainname" && strtolower($_SERVER['HTTP_HOST']) != "$main_server_domainname")) {
				$article_path = "http://" . str_replace("//", "/", $_SERVER['HTTP_HOST'] . "/" );
			}
			elseif($on_main_server && (strtolower($_SERVER['HTTP_HOST']) == "www.$main_server_domainname" || strtolower($_SERVER['HTTP_HOST']) == "$main_server_domainname")) {
				$article_path = "http://" . str_replace("//", "/", $_SERVER['HTTP_HOST'] . "/" . $real_addonname . "/");
			}
			elseif($sales_view) {
				$article_path = "http://" . str_replace("//", "/", $_SERVER['HTTP_HOST'] . "/" . $real_addonname . "/");
			}
			else {
				$article_path = $full_uri . "";
			}
			?>
			
			<?php
			foreach ($root_nodes as $id=>$root_id) {
				?>
				$("#<?php echo $root_id;?>").dynatree({
					onClick: function(node) {
						if(!node.isFolder && node.data.url ) {
							if(node.data.target == "_blank") {
								//console.log(node.data.url);
								window.open(node.data.url);
							}
							else {
								
								//console.log('<?php echo $article_path; ?>' + node.data.url);
								
								//$('#article_jqm').remove();
								//$('#article_reader').empty();
								//$('#article_reader').append('<div id="article_jqm" class="jqmWindow" style="display: none;"></div>');
								//alert('<?php echo $full_addon_uri; ?>' + node.data.url + '?page=1&module_page=main_article&' + new Date().getTime());
								$('#open_article_jqm').jqm({ajax: '<?php echo $article_path; ?>' + node.data.url + '?page=1&' + new Date().getTime() , modal: false, target : "#jqm_target", toTop: true});
								$('#open_article_jqm').jqmShow();
								bookmark_article('<?php echo $article_path; ?>' + node.data.url);
							}
						}
					},
					onLazyRead: function(node){
				        node.appendAjax({
				        	url: "<?php echo ($on_main_server) ? "http://" . $_SERVER['HTTP_HOST'] : "http://" . $_SERVER['HTTP_HOST'];?>/ajax/get_article_list.php",
				            data: {
				            	"id": node.data.key ? node.data.key.replace("node_","") : node.data.key, 
								"show_icons": "<?php echo ($show_icons == true) ? 1 : 0;?>",
								"class_level" : node.data.addClass
								},
							success: function(node) {
							   // Called after nodes have been created and the waiting icon was removed.
							   // 'this' is the options for this Ajax request
							   },
							error: function(node, XMLHttpRequest, textStatus, errorThrown) {
							   // Called on error, after error icon was created.
							   },
							cache: false // Append random '_' argument to url to prevent caching.
				        });
				    },
				    classNames : {
				    	<?php
				    	if(isset($show_expander) && $show_expander == false) {
				    		?>
				    		expander : "no-expander",
				    		vline :	"no-vline",
				    		connector : "no-connector"
				    		<?php
				    	}
				    	?>
				    },
				    debugLevel : 2
				});
				
				//$("#<?php echo $root_id;?>").dynatree("option",  "autoExpand", true);
				<?php 
			}
		}
		*/
		?>
		
	});
</script>
