{include file="$gentemplates/$header_tpl"}
<h2>{$lang.modules.hot_or_not.name}</h2>
<div style="overflow: hidden; padding: 10px 0;">
	<div class="skip_div p_b_5 float_left" style="padding: 6px 0; margin-top: 0px;">
		<a class="arr-left1-icon" href="{$gallery_link}" title="{$lang.hotornot.gallery}">&nbsp;</a>&nbsp;<a href="{$gallery_link}">{$lang.hotornot.gallery}</a>
	</div>
	{if $user_info}
	<div class="skip_div p_b_5 float_right" style="padding: 6px 0; margin-top: 0px;">
		<a href="javascript:void(0);" onclick="javascript: changeView(1);">{$lang.general.next}</a>&nbsp;<a class="arr-right1-icon" href="javascript:void(0);" onclick="javascript: changeView(1);" title="{$lang.general.next}">&nbsp;</a>
	</div>
	{/if}
</div>
<div id="sort_table">
<div class="hotornot" id="hotornot">
	{*if $err}<div class="error_msg">* {$err}</div>{/if*}
	<!--vote buttons-->
	{if $user_info}
		{include file="$gentemplates/hot_or_not_userinfo.tpl"}		
	{/if}
        {if $empty}
            <div class="p-b5" style="position: relative; margin-top:0;overflow: hidden;">		
		<div class="float_right" style="padding-top: 7px;">
	        <form name="sort_form" action="{$filename}" method="POST" style="margin:0px; text-align: center;">
                    <input type="hidden" name="sel" value="sort_photo">
                    <span>{$lang.hotornot.show_me}&nbsp;</span>
                    <select style="width:150px;" id="show_me" name="show_me" onchange="SortUserType(this.options[this.selectedIndex].value);">
                         <option value="0" {if $show_me eq '0'}selected{/if}>All</option>
                        {foreach key=key item=item from=$user_types}
                            <option value="{$key}" {if $show_me eq $key}selected{/if}>{$item}</option>
                        {/foreach}
                    </select>
                    &nbsp;
                    <span>{$lang.hotornot.age}&nbsp;</span><select id="min_age" name="min_age" style="width:50px;" onchange="document.sort_form.submit();">
                        {foreach item=item from=$age_min}
                                <option value="{$item}" {if $min_age_sel eq $item}selected{/if}>{$item}</option>
                        {/foreach}
                    </select>&nbsp;
                    <select id="max_age" name="max_age" style="width:50px;" onchange="document.sort_form.submit();">
                        {foreach item=item from=$age_max}
                                <option value="{$item}" {if $max_age_sel eq $item}selected{/if}>{$item}</option>
                        {/foreach}
                    </select>&nbsp;
		</form>
		</div>
            </div>
            <div class="error" style="margin:10px 0; padding: 10px 0;">
                <font class="error_msg">*&nbsp;{$lang.hotornot.empty}</font><br/>
            </div>
        {/if}
</div>
	<div>
		{if $user_info}<div id="preview_td">{include file="$gentemplates/hot_or_not_preview.tpl"}</div>{/if}
		<div id="postview_td" class="overflohidden"></div>
		<div id="tops">{include file="$gentemplates/hot_or_not_tops.tpl"}</div>
		{*<div id="user_stats">{include file="$gentemplates/hot_or_not_stats.tpl"}</div>*}
		<div class="disp_none" id="info_td"></div>
	</div>
</div>
{if $user_info}
<div id="tmp"></div>
<script type="text/javascript">
{literal}
//initial
user_td_obj = document.getElementById('user_td');
hotornot_td_obj = document.getElementById('hotornot');
show_photos_str_obj = document.getElementById('show_photos_str');
all_photos_obj = document.getElementById('all_photos');
sort_table_obj = document.getElementById('sort_table');
firs_photo_path_obj = document.getElementById('firs_photo_path');
tops_obj = document.getElementById('tops');

icon_url_obj = document.getElementById('icon_url');
main_image_obj = document.getElementById('main_image');
img_width = '{/literal}{$user_info.width}{literal}';

user_area_obj = document.getElementById('user_area');

this.preview_arr = new Array();
{/literal}
{foreach from=$preview item=item name=preview}
	preview_obj_{$item.array_index} = document.getElementById('preview_{$item.array_index}');
	this.preview_arr[{$item.array_index}] = new Array();
	this.preview_arr[{$item.array_index}]['id'] = {$item.id};
	this.preview_arr[{$item.array_index}]['upload_url'] = "{$item.upload_url}";
	count_preview = {$item.array_index};
{/foreach}
{literal}

function next_photo() {
	id = parseInt(document.getElementById('id_user').value);
	min = 9;
	max = 18;
	//$("#next-photo").click(function(){
		$.ajax({
			url: SEO_SITE_ROOT + '/hot_or_not.php',
			type: 'POST',
			data: {sel: "get_photo", id_user:id, min:min, max:max},
			success: function (data) {
				RunJS(all_photos_obj,data);
			}
		});
	//});
}
if (document.images)
{
	var mark_show=new Image;
	mark_show.src="{/literal}{$site_root}{$template_root}{$template_images_root}/vote_icon_1.gif{literal}"
	var mark_hide=new Image;
	mark_hide.src="{/literal}{$site_root}{$template_root}{$template_images_root}/vote_icon_0.gif{literal}"
}

function marks(id,type)
{
	if (!document.images) return false;
	for (i=1; i<=id; i++){
		if (type=="show"){
			$("#mark"+i).removeClass("rate_heart_inactive");
			$("#mark"+i).addClass("rate_heart_active");
		}
		else if (type=="hide"){
			$("#mark"+i).removeClass("rate_heart_active");
			$("#mark"+i).addClass("rate_heart_inactive");
		}
	}
	return;
}

function changeView(array_index, next_prev, array_id){
	if (!getMainPart(array_index, next_prev, array_id)) return;
	return true;
}

function getTops(){
	$(tops_obj).html('{/literal}{$lang.ajax.tmp_text|replace:"'":"\'"}{literal}');
	$.ajax({
		url: SEO_SITE_ROOT + '/hot_or_not.php',
		type: 'POST',
		data: {sel: "return_tops"},
		success: function (data) {
				RunJS(tops_obj,data);
			}
	});
}

function getUserStats(){
	$(user_stats_obj).html('{/literal}{$lang.ajax.tmp_text|replace:"'":"\'"}{literal}');
	$.ajax({
		url: SEO_SITE_ROOT + '/hot_or_not.php',
		type: 'POST',
		data: {sel: "return_stats"},
		success: function (data) {
				RunJS(user_stats_obj,data);
			}
	});
}

function drawLoader(elem){
	$(elem).html('<div style="width: 100%; text-align: center;">\n\
		<img src="{/literal}{$site_root}{$template_root}{$template_images_root}/ajax-loader.gif{literal}">\n\
	</div>');
}

function getMainPart(array_index, next_prev, array_id){
	if (this.preview_arr.length == 1){
		alert('{/literal}{$lang.hotornot.end_reached|escape:javascript}{literal}');
		location.href='{/literal}{$site_root}/{$seo.top_users}{literal}';
		return false
	}	
	show = document.getElementById('show_me').value;
	age_mi = document.getElementById('min_age').value;
	age_ma = document.getElementById('max_age').value;
	id = this.preview_arr[array_index]["id"];
	str = 'sel=return_main_part&id_user='+id+'&show_me='+show+'&min_age='+age_mi+'&max_age='+age_ma;
	asinch = false;
	hotornot_td_obj = document.getElementById('hotornot');
	drawLoader(hotornot_td_obj);
	$.ajax({
		url: SEO_SITE_ROOT + '/hot_or_not.php',
		type: 'POST',
		data: {sel: "return_main_part", id_user: id, show_me: show, min_age: age_mi, max_age: age_ma},
		success: function (data) {
				RunJS(hotornot_td_obj,data);
				init();
				preview_arr.reverse();
				last_id = preview_arr[0]['id'];
				preview_arr.reverse();
				from_id = last_id;
				getAdditionPhotos(array_index, from_id, next_prev, array_id);
			}
	});
	return true;
}

function getAdditionPhotos(array_index, from_id, next_prev, array_id){
	addition_preview_data_obj = document.getElementById('addition_preview_data');
	if(next_prev==1){
		count = array_index+1;
	}else{
		count = array_id;
	}
	$.ajax({
		url: SEO_SITE_ROOT + '/hot_or_not.php',
		type: 'POST',
		data: {sel: "get_addition_preview", from_id: from_id, count: count, show_me: show, min_age: age_mi, max_age: age_ma},
		success: function (data) {
			RunJS(addition_preview_data_obj,data);
			count_prev = parseInt(document.getElementById('count_prev').value);
			fillPreviewArray();
			if(next_prev==0){
				deleteOldElements(count_prev-1);
			} else {
				prevPhoto(array_index);
			}
			assignArrayToPreview();
			getTops();
		}
	});
}

function fillPreviewArray(){
	this.count_added = parseInt(document.getElementById('count_added').value);
	y=0;
	start = count_preview+1;
	end = count_preview+this.count_added;
	for (i=start; i<=end; i++){
		this.preview_arr[i] = new Array();
		this.preview_arr[i]['id'] = parseInt(document.getElementById('addition_preview_id_'+y).value);
		this.preview_arr[i]['upload_url'] = document.getElementById('addition_preview_upload_url_'+y).value;
		y++;
	}
}

function deleteOldElements(array_index){

	this.preview_arr.splice(array_index,1);
	count_listed = 0;
	document.getElementById('preview_div_'+this.preview_arr.length).style.display='none';
	prevNextPhoto(array_index);
}

function assignArrayToPreview(){
	i=0;
	for(el in this.preview_arr){
		document.getElementById('preview_'+i).src = this.preview_arr[i]["upload_url"];
		i++;
    }
}

function prevNextPhoto(array_index){
		if(array_index==0)
			prev_id =0;
		else
			prev_id =1;
		$("#preview_"+array_index).css("border","1px solid #CCCDCB");
		$("#preview_"+prev_id).css("border","3px solid #D00C0C").css("margin-top","-5px").css("padding","5px");
}

function prevPhoto(array_index){
	count_listed = 0;
	var prev_id = array_index;
	for(i=array_index+1;i<this.preview_arr.length;i++){
		div_index = i;
		last_id = div_index;
		$("#preview_"+last_id).css("border","1px solid #CCCDCB");
		$("#preview_"+prev_id).css("border","3px solid #D00C0C").css("margin-top","-5px").css("padding","5px");
	}
	for(i=0;i<array_index+1-this.count_added;i++){
		div_index = array_index - count_listed-i;
		last_id = div_index;
		$("#preview_"+last_id).css("border","1px solid #CCCDCB");
		$("#preview_"+prev_id).css("border","3px solid #D00C0C").css("margin-top","-5px").css("padding","5px");
	}
	
	
}

function VoteUser(estim){
	postview_td_obj = document.getElementById('postview_td');
	id = parseInt(document.getElementById('id_user').value);
	hotornot_td_obj = document.getElementById('hotornot');
	asinch = true;
	str = 'sel=rate&id_user='+id+'&estim='+estim;

	$.ajax({
		url: SEO_SITE_ROOT + '/hot_or_not.php',
		type: 'POST',
		data: {sel: "rate", id_user: id, estim: estim},
		success: function (data) {
				array_id = 0;
				changeView(1, 0, array_id);
			}
	});


}


function showPhotosBar(){
	show_photos_str_obj.style.display = 'none';
	$("#all_photos").removeClass("disp_none");
	showPhoto(firs_photo_path_obj.value, 'small_photo_0');
}

function showPhoto(path, id){
	small_photo_object = document.getElementById(id);
	photo_count = document.getElementById('photo_count_id').value;
	small_icon_object = document.getElementById('small_icon');
	small_icon_object.style.border='';
	for(i=0; i<photo_count; i++)document.getElementById('small_photo_'+i).style.border='';
	small_photo_object.style.border = '1px solid #000000';
	main_image_obj.src = path;
}

//////////////////////////////////
function init(){
	show_photos_str_obj = document.getElementById('show_photos_str');
	all_photos_obj = document.getElementById('all_photos');
	firs_photo_path_obj = document.getElementById('firs_photo_path');
	icon_url_obj = document.getElementById('icon_url');
	main_image_obj = document.getElementById('main_image');
	main_image_td_obj = document.getElementById('main_image_td');

	client_width = $(user_area_obj).width();
	main_image_obj.src = document.getElementById('img_url').value;
	if(main_image_obj.src==SERVER + '/hot_or_not.php') window.location.reload();
	$(document).ready(function () {
		if (main_image_obj.width > client_width && client_width > 100) {
			main_image_obj.width = client_width;
		}
	});
}

$(window).load(function () {
  init();
});

setInterval(function(){ajaxRequest(SEO_SITE_ROOT + '/hot_or_not.php', 'qwe=1', '', '', true);},1000*60*45); //session not die;
{/literal}
</script>
{/if}
<script type="text/javascript">
{literal}
function SortUserType(type){
    document.sort_form.submit();
}
{/literal}
</script>  
{include file="$gentemplates/$footer_tpl"}
