<?php if(!isset($sumfingIncl)) {header("HTTP/1.0 403 Forbidden"); die();} ?>
<script type="text/javascript">
$(document).ready(function() {
	
	$('#ticket_info').css('cursor','pointer').popover();
	
	<?php if($status === 'OVER') {?>
	$('.winclick').css('cursor','pointer').popover();
	$('.madeclick').popover();
	
	<?php
	}
	if(isset($set->show_start_timer) && ($set->select_counter == "time") && ($status == "NOT STARTED")) {
		$willstart = strtotime($set->starts) - time();?>
		countDownFunc(<?php echo $willstart;?>, 'start_counter');
	<?php }
	
	if(isset($set->select_counter) && ($set->select_counter == "time") && ($status == "ON")) {?>
	countDownFunc(<?php echo $count_time;?>,'counter'); 	
	<?php }
	 if(isset($set->select_counter) && ($set->select_counter == "like") && ($status == "ON")) {?>
		countLikeFunc('<?php echo $data['page']['id'];?>','like_counter',30000,<?php echo $set->start_likes;?>,<?php echo $set->end_likes;?>);
		clear_like_int = setInterval(function(){
				countLikeFunc('<?php echo $data['page']['id'];?>','like_counter',30000,<?php echo $set->start_likes;?>,<?php echo $set->end_likes;?>);
		},30000);
	<?php }?>
	<?php 
	 if(isset($set->select_counter) && ($set->select_counter == "like") && ($status == "NOT STARTED")) {?>
		countLikeFunc('<?php echo $data['page']['id'];?>','start_counter',30000,<?php echo $set->start_likes;?>,<?php echo $set->end_likes;?>);
		clear_like_int = setInterval(function(){
				countLikeFunc('<?php echo $data['page']['id'];?>','start_counter',30000,<?php echo $set->start_likes;?>,<?php echo $set->end_likes;?>);
		},30000);
	<?php }?>
	
	//load leaderboard
	$('#lboard').click(function () {
		$("#tab2").load("incl/ticketleader.php");
	});
	
	//load prizes
	$('#lprizes').click(function () {
		$("#tab5").load("incl/prizes.php");
	});

	//load user tickets
	$("#totaltickets").load("incl/mytickets.php?user=<?php echo $user;?>");
	setInterval(function(){
		$("#totaltickets").load("incl/mytickets.php?user=<?php echo $user;?>");
	}, 30000);
	
	
	<?php if(($set->homepage_show_users_thumbs != '') || ($set->homepage_show_users_thumbs > 0)) { ?> 
		//load participans and tickets count
		$("#users-joined").load("incl/participans.php?check=<?php echo $set->homepage_show_users_thumbs;?>&abc=<?php echo time();?>");
	<?php }
	
	
	// Loading next scripts only for admin
	if( $admin === 1 ) { ?>
		
	//admin posting to users timeline
	$('#savePostUserWall').click(function () {
		$(".load").show();
		$.post('incl/makepost.php', $('#postUserWall').serialize(),
		function(data) {
			$('.admin-responce').html(data).fadeIn();
			$(".load").hide();		
			setTimeout(function() {
			$('.admin-responce').fadeOut();
			}, 5000);
			
		});
	});	
	
	$("#loto-settings").click(function() {
		$("#winners-cont,#users-cont").hide();
		$("#lottery-cont").show();
	});
	
	$("#user-settings").click(function() { 
		$("#winners-cont,#lottery-cont").hide();
		$("#users-cont").load("incl/users.php?check=<?php echo $userdata->fake_id;?>").show();
	});
	
	$("#winners-settings").click(function() { 
		$("#users-cont,#lottery-cont").hide();
		$("#winners-cont").load("incl/admin_winners.php?check=<?php echo $userdata->fake_id;?>").show();
	});
	
	// Sets of settings
	$(".set_cont").hide();
	$(".set_cont:first").show(); 
	$(".set_btn").click(function() {
		$(".set_cont").hide();
		var activeSet = $(this).attr("rel"); 
		$("#"+activeSet).fadeIn(); 
				
	});
	
	$('.nav-tabs').button(); 
	
	$(".upload").click(function() {
		click_id = this.id;
		popupOver("incl/uploader/uploader.php", 800, 500, click_id, "<?php echo sha1($user);?>");
	});
	
	// counter options
	var count_onload = $('input[name=select_counter]:checked', '#save-lottery').val();
	$(".count_field").hide();
	$("#" + count_onload + "_cnt").show();
	$(".count_sel").click(function(){
		var a = this.id.split("_");
		$(".count_field").hide();
		$("#" + a[1] + "_cnt").show();
	});
	
	//save admin form
	$("#save_admin").click(function() {
		$(this).html('<?php echo _("Wait...");?>').attr("disabled", "disabled");
		$.post('incl/savedata.php?check=<?php echo sha1('admin_'.$user);?>', $('#save-lottery').serialize(),
		function(data) {
			if(data) {
				$("#save_admin").html('<?php echo _("Save Settings");?>').removeAttr("disabled", "disabled");	
				$('#adm_save_succ').show();
				setTimeout(function() {
					$('#adm_save_succ').slideUp(800);
				}, 5000);
				
			} else {
				$("#save_admin").html('<?php echo _("Save Settings");?>').removeAttr("disabled", "disabled");
				$('#adm_save_err').show();	
				setTimeout(function() {
					$('#adm_save_err').slideUp(800);
				}, 5000);
			}
	   });
	});
	
	// slot timeline options
	if($("#slot_timeline_toggle").is(":checked")) {
		$("#slot_timeline").show();	
	}
	$("#slot_timeline_toggle").click(function() {
		$("#slot_timeline").toggle();
	});
	
	// slot fanpage options
	if($("#slot_app_page_toggle").is(":checked")) {
		$("#slot_app_page").show();	
	}
	$("#slot_app_page_toggle").click(function() {
		$("#slot_app_page").toggle();
	});
	
	// fangate options
	if($("#toggle_fangate").is(":checked")) {
		$("#fan_gate_form").show();	
	}
	$("#toggle_fangate").click(function() {
		$("#fan_gate_form").toggle();
	});
	
	// security field options 
	if($("#toggle_secform").is(":checked")) {
		$("#sec_form_field").show();	
	}
	$("#toggle_secform").click(function() {
		$("#sec_form_field").toggle();
	});
	
	// sharing field options
	if($("#toggle_share_tickets").is(":checked")) {
		$("#share_tickets_field").show();	
	}
	$("#toggle_share_tickets").click(function() {
		$("#share_tickets_field").toggle();
	});
	
	// request options field
	if($("#use_app_requests").is(":checked")) {
		$("#request_options_field").show();	
	}
	$("#use_app_requests").click(function() {
		$("#request_options_field").toggle();
	});
	
	// use slot machine
	if($("#use_slot").is(":checked")) {
		$("#slot_options_field").show();	
	}
	$("#use_slot").click(function() {
		$("#slot_options_field").toggle();
	});
	
	// ref email
	if($("#toggle_ref_email").is(":checked")) {
		$("#ref_email_fields").show();	
	}
	$("#toggle_ref_email").click(function() {
		$("#ref_email_fields").toggle();
	});
	
	// win email
	if($("#toggle_win_email").is(":checked")) {
		$("#win_email_fields").show();	
	}
	$("#toggle_win_email").click(function() {
		$("#win_email_fields").toggle();
	});
	
	// end email
	if($("#toggle_end_email").is(":checked")) {
		$("#end_email_fields").show();	
	}
	$("#toggle_end_email").click(function() {
		$("#end_email_fields").toggle();
	});
	
	// new event email
	if($("#toggle_new_email").is(":checked")) {
		$("#new_email_fields").show();	
	}
	$("#toggle_new_email").click(function() {
		$("#new_email_fields").toggle();
	});
	
	// new event email
	if($("#toggle_bulk_email").is(":checked")) {
		$("#bulk_email_fields").show();	
	}
	$("#toggle_bulk_email").click(function() {
		$("#bulk_email_fields").toggle();
	});
	
	// timeline post fields
	if($("#toggle_post_timeline").is(":checked")) {
		$("#timeline_post_fields").show();	
	}
	$("#toggle_post_timeline").click(function() {
		$("#timeline_post_fields").toggle();
	});
	
	// smtp options  
	if($("#smtp_emails_toggle").is(":checked")) {
		$("#smtp_fields").show();	
	}
	$("#smtp_emails_toggle").click(function() {
		$("#smtp_fields").toggle();
	});
	
	$("#add_sec_field").click(function() {
		var divcount = $(".sec_inp_div").length;
		var next_id = divcount + 1;
		if(next_id>2) {
			$(".remove").hide();
		}
		if(next_id>4) {
			$("#addfieldbtn").hide();
		}
		var new_elem = "<div id='sec_form_input_"+next_id+"' class='form-inline sec_inp_div' style='margin-top:8px;'>" +
						"<input type='text' name='txt_"+next_id+"' id='inp_"+next_id+"' class='input-large' placeholder='<?php echo _("Field name");?>'> " +
						"<label class='checkbox'>" +
                        " <input type='checkbox' name='txt"+next_id+"_mand' value='mandatory'> <?php echo _("Mandatory");?>" +
                        "</label>" +
						"&nbsp;&nbsp;<button type='button' id='remove_"+next_id+"' class='btn btn-danger btn-mini remove'><?php echo _("Remove");?></button>" +
                        "</div>";
		$('#sec_wrap').append(new_elem);
		
		$('.remove').click(function(){
			var btnId = ($(this).attr('id'));
			var nowclick=btnId.split("_");
			var clickedBtn=	nowclick[1];
			$("#sec_form_input_" + clickedBtn).remove();
							
			var last_remove = $('.sec_inp_div:last').attr('id');
			var lst_rmv_id = "#"+last_remove;		
			$(lst_rmv_id + " .remove").show();
			if(next_id<5) {
				$("#addfieldbtn").show();
			}				
		});
		
	});
	
	// more
	$('.remove').click(function(){
		var btnId = ($(this).attr('id'));
		var nowclick=btnId.split("_");
		var clickedBtn=	nowclick[1];
		$("#sec_form_input_" + clickedBtn).remove();
						
		var last_remove = $('.sec_inp_div:last').attr('id');
		var lst_rmv_id = "#"+last_remove;		
		$(lst_rmv_id + " .remove").show();
		if(next_id<5) {
			$("#addfieldbtn").show();
		}			
	});
	
	$("#counter_classes").change( function() {
		var addnewclass = $(this).val();
		var crntclass = $("#sample_counter").attr("title");
	    $("#sample_counter").removeAttr('class').attr('class', 'btn btn-large ' + addnewclass).attr('title', addnewclass);	  
	});
	$("#counter_classes_ch").change( function() {
		var addnewclass = $(this).val();
		var crntclass = $("#sample_counter_change").attr("title");
	    $("#sample_counter_change").removeAttr('class').attr('class', 'btn btn-large ' + addnewclass).attr('title', addnewclass);	  
	});
	$("#share_btn_color").change( function() {
		var addnewclass = $(this).val();
		var crntclass = $("#share_btn_sample").attr("title");
	    $("#share_btn_sample").removeAttr('class').attr('class', 'btn btn-large ' + addnewclass).attr('title', addnewclass);	  
	});
	$("#req_btn_color").change( function() {
		var addnewclass = $(this).val();
		var crntclass = $("#req_btn_sample").attr("title");
	    $("#req_btn_sample").removeAttr('class').attr('class', 'btn btn-large ' + addnewclass).attr('title', addnewclass);	  
	});
	$("#slot_btn_color").change( function() {
		var addnewclass = $(this).val();
		var crntclass = $("#slot_btn_sample").attr("title");
	    $("#slot_btn_sample").removeAttr('class').attr('class', 'btn btn-large ' + addnewclass).attr('title', addnewclass);	  
	});
	
	$('#reset_btn').click(function() {
		var res = confirm('<?php echo _("Are you sure? All previous tickets will be deleted. Make sure that you have made changes on starting and ending times. Press OK to proceed with reset.");?>');
		if(res==true) {
			$('#reset_btn').html('<?php echo _("Wait...");?>');
		   $.post('incl/reset.php',{uid: '<?php echo $userdata->fake_id; ?>', func: 'adm_reset', url: '<?php echo urlencode($app_url);?>', },function(resp) {
				if(resp && resp != "") {
					top.location.href='<?php echo $app_url;?>';	
				}
		   });
		} else {
		  // Do nothing for example
		}
	});
	
	$('#timeline_send').click(function() {
		
		$(this).hide();
		$('#timeline_send_fake').show();
		$('#timeline_info_msg').hide();
		
		var tmln_title = $('#timeline_title').val(),
			tmln_msg = $('textarea#timeline_message').val(),
			tmln_img = $('img#timeline_thumb').attr('src'),
			tmln_amnt = $('#timeline_amount').val();
		
		$.ajax({
			url: "incl/tmln_post.php",
			type: "POST",
			data: {url: '<?php echo urlencode($app_files_url);?>',uid: '<?php echo $userdata->fake_id; ?>', title: tmln_title, msg: tmln_msg, img: tmln_img, amnt: tmln_amnt },
			success: function(resp) {
			if(resp) {
				$('#timeline_info_msg').html(resp).show();
				$('#timeline_send_fake').hide();
				$('#timeline_send').show();
			}
		  },
			error: function() {
				
				$('#timeline_info_msg').html('<?php echo _("Error! Try again later.");?>').show();
				$('#timeline_send_fake').hide();
				$('#timeline_send').show();
			}
		});
		
		
		return false;
	});
	
	// Bulk email sending
	$('#bulk_email_send').click(function() {
		
		$(this).hide();
		$('#bulk_email_info_msg').hide();
		
		var email_subject = $('#bulk_email_subject').val(),
			email_body = $('textarea#bulk_email_body').val();
		
		$.ajax({
			url: "incl/bulk_email.php",
			type: "POST",
			data: { uid: '<?php echo $userdata->fake_id; ?>', subject: email_subject, emailbody: email_body },
			success: function(resp) {
			if(resp && resp != "") {
				$('#bulk_email_info_msg').html(resp).show();
				$('#bulk_email_send').show();
			}
		  },
			error: function() {
				
				$('#bulk_email_info_msg').html('<?php echo _("Error! Try again later.");?>').show();
				$('#bulk_email_send').show();
			}
		});
		
		
		return false;
	});
	
	<?php // Admin scripts end
	}?>
	
	// Ticket click
	<?php if($status === 'OVER') {?>
	$('.winclick').click(function() {
		var ticket_id = $(this).attr('id');
		$('#'+ticket_id).hide();
		$.post('incl/claimwin.php',{uid: '<?php echo $userdata->fake_id; ?>', ticket_id: ticket_id, },
			function(resp) {
			    if(resp != '') {
					alert('<?php echo _("Thanks! Your request was successfully sent. We will be in touch to you ASAP.");?>');
				}
		});
		
	});
	<?php }?>
	
	//save sec form
	$("#sec_form_btn").click(function() {
		$(".alert").hide();
		var washtml = $(this).html();
		$(this).html('<?php echo _("Wait...");?>').attr("disabled", "disabled");
		$.post('incl/savedata.php', $('#sec_form').serialize(),
		function(data) {
			if(data == 1) {
				$("#mand_err").show();
				$("#sec_form_btn").html(washtml).removeAttr("disabled", "disabled");
			}
			else if(data == 0){
				$("#succ_sec").show();
				$("#sec_form_btn").hide();
				setTimeout(function() {
					
					top.location.href='<?php echo MY_APP_URL;?>';
				}, 3000);
			}
			else {
				$("#exists_err").html(data + " <?php echo _(' already exists in our database!');?>").show();
				$("#sec_form_btn").html(washtml).removeAttr("disabled", "disabled");
			}
	   });
	});
	
	
	<?php 
	if($status != 'ON') { ?>
		$('#more_tickets_opt').hide();
	<?php }
	
	if(isset($set->homepage_show_stats)){ ?>
    	$('#joined_and_tickets').load("incl/userntickets.php?stats=<?php echo sha1($user);?>").show();
		setInterval(function(){
			$('#joined_and_tickets').load("incl/userntickets.php?stats=<?php echo sha1($user);?>");
		}, 30000);
    <?php }
	
	if($userdata->slot_int > time()) {?>
	$('#rp_slot_btn').show().attr('disabled','disabled');
	countDownFunc(<?php echo ($userdata->slot_int - time());?>,'rp_slot_btn');
	<?php }
	
	
	if($userdata->share_int > time()) {?>
	$('#rp_sharethis_btn').show().attr('disabled','disabled');
	countDownFunc(<?php echo ($userdata->share_int - time());?>,'rp_sharethis_btn');
	<?php }
	
		if($set->request_limit <= $reqcount) {?>
		$('#requests_btn').remove();
		$('#rp_req_btn').show().attr('disabled','disabled').html('<?php echo _("Requests limit reached");?>');
		
		<?php }
	
		if($userdata->req_int > time()) {?>
		$('#rp_req_btn').show().attr('disabled','disabled');
		countDownFunc(<?php echo ($userdata->req_int - time());?>,'rp_req_btn');
		<?php }
		
		if(isset($set->use_app_requests) && ($userdata->req_int < time())) { // App requests?>
		
			$('#requests_btn').click(function() {
				sendRequest();
			});
		<?php }?>
		
		
		$("#user_show_tickets").click(function() {
			$("#tickets_panel").toggle();
			if($('#user_show_tickets').hasClass('icon-chevron-down')) {
				$('#user_show_tickets').removeClass('icon-chevron-down').addClass('icon-chevron-up');
			} else {
				$('#user_show_tickets').removeClass('icon-chevron-up').addClass('icon-chevron-down');
			}
			
		});
	
	$('#disclaimer').click(function() {
		var btn_id = $(this).attr('id');
		popupOver('incl/terms.php', 700, 500, btn_id,'');
	});
	
	$('#terms').click(function() {
		var btn_id = $(this).attr('id');
		popupOver('incl/terms.php', 700, 500, btn_id,'');
	});
	
});//document ready ends here

<?php	if(isset($set->use_app_requests)) { // App requests
		if($userdata->req_int < time()) {?>
		//friend request to get tickets

function sendRequest() {
        FB.ui({
            method: 'apprequests',
            message: '<?php echo $set->title;?>',
           
        },
        function (response) {
            if (response.request && response.to) {
                var request_ids = [];
                for(i=0; i<response.to.length; i++) {
                    var temp = response.request + '_' + response.to[i];
                    request_ids.push(temp);
                }
                var requests = request_ids.join(',');
				
 				$.post('incl/requests.php',{uid: '<?php echo $userdata->fake_id; ?>', request_ids: requests, },function(resp) {
                    
					var rq_btn_orign = $('#requests_btn').html();
					var resp_param = resp.split("_");
					if(resp_param[1] == 'over') {
						$('#requests_btn').html(resp_param[0] + ' <?php echo _("over limit. Failed!");?>');
						setTimeout(function() {
							$('#requests_btn').html(rq_btn_orign);
						}, 3000);
					}
					if(resp_param[1] == 'tot') {
						$('#requests_btn').html('<?php echo _("Success");?>').show();
						setTimeout(function() {
							$('#requests_btn').html(rq_btn_orign);
							$('#req_limit-show').text(resp_param[0] + '/<?php echo $set->request_limit;?>');
						}, 3000);
						
						if(resp_param[0] == <?php echo $set->request_limit;?>) {
							$('#requests_btn').remove();
							$('#rp_req_btn').show().attr('disabled','disabled').html('<?php echo _("Requests limit reached");?>');	
						}
						
					}
					<?php if($set->request_ticket_int > 0) {?>
					if(resp_param[1] == 'int') {
						$('#requests_btn').remove();
						$('#rp_req_btn').show().attr('disabled','disabled');
						countDownFunc(resp_param[0],'rp_req_btn');
					}
					<?php }?>
					
                });
            } 
        });
        return false;
    }
//requests end
		
<?php }
}?>

// Centered popup window
function popupOver(pageURL, w, h, btn_id,user) {
	var left = (screen.width/2)-(w/2);
	var top = 100;
	var targetWin = window.open (pageURL+"?id="+btn_id+"&token="+user, "_blank", 'toolbar=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
}

// Like counter
function countLikeFunc(pageid,forelement,interv,starts,ends) {
	//load participans and tickets count
	var url = "incl/likes.php?id=" + pageid + "&starts=" + starts + "&ends=" + ends + "&rnd=" + new Date().valueOf();
	
	$("#" + forelement).load(url, function(response) {
		var idto = this.id;
		//console.log(response);
		if(parseInt(response) < (<?php echo $set->xbefore_end;?> + 1)) {
			$("#" + idto).removeClass('<?php echo $set->counter_classes;?>').addClass('<?php echo $set->counter_classes_ch;?>');
		}
		if(response == 0) {
			clearInterval(clear_like_int);
			top.location.href='<?php echo MY_APP_URL;?>';
		}
	});
}

//counting time on elements
function countDownFunc(counttime,forelement) {
		
		var timeToCount = counttime;
		var makechange = forelement;
	
		timeToCountdownPost();
	
	function timeToCountdownPost() {
		if((timeToCount- 1) >= 0){
			
			var day = Math.floor(timeToCount / (24*3600));
			var differ = timeToCount - (day * (24*3600));
			var hour = Math.floor(differ / 3600);
				differ = timeToCount - (day * (24*3600)) - (hour * 3600);
			var minute = Math.floor(differ / 60);
				differ = timeToCount - (day * (24*3600)) - (hour * 3600) - (minute * 60);
			var second = differ;
			
			if(day < 10) { day = '0' + day; }
			if(hour < 10) { hour = '0' + hour; }
			if(minute < 10) { minute = '0' + minute; }
			if(second < 10) { second = '0' + second; }
		
		
			var currTime = day + ':' + hour + ':' + minute + ':' + second;
			
			if(day == 0 && hour == 0 && minute < <?php echo $set->xbefore_end;?>) {
				$("#" + makechange).removeClass('<?php echo $set->counter_classes;?>').addClass('<?php echo $set->counter_classes_ch;?>');	
			}
		
			timeToCount = timeToCount - 1;
			$("#" + makechange).html(currTime);
			 
			// Different var for every element
			cntdwnTimer = setTimeout(timeToCountdownPost, 1000);
		} else {
			$("#" + makechange).html("<span style='color:#EEE'>00:00:00:00</span>");
			if(timeToCount == 0) {
				
				setTimeout(function() {
					top.location.href='<?php echo MY_APP_URL;?>';
				}, 3000);	
				
			}
		}
	}
	
}


<?php if(isset($set->use_share_tickets)) {
		if($userdata->share_int < time()) {?>
function sthisCallbackFunction (event,service){
	if(event == "click" && service != "") {
		$.post("incl/ticketstep.php", { step: 1, user: '<?php echo $userdata->fake_id;?>' })
			.done(function(data) {
			if(data != '') {
				<?php if($set->share_ticket_int > 0) {?>
				$('#st_sharethis_btn').remove();
				$('#rp_sharethis_btn').show().attr('disabled','disabled');
				countDownFunc(data,'rp_sharethis_btn');
				<?php }?>
			}
		});
	}
}
stLight.subscribe("click",sthisCallbackFunction); //register the callback function with sharethis 
<?php 
  }
}?>

</script>
<!-- Javascripts -->