var formCount = 0;
$(document).ready(function(){

	$('#popup_close').hover(function(){
			$(this).css('cursor','pointer');
			$(this).addClass('bold');						 
				});
	$('#popup_close').mouseout(function(){
			$(this).removeClass('bold');						 
				});
	$('#popup_close').click(function(){
			$('#javascript_popup').hide();
			 $('#popup_content').hide();
				});
});

function popup_position(){
	var bwidth = $(window).width()/2;
	var bheight = ($(window).height()/2)+$(window).scrollTop();
	var wid = $("body").width();
	var hei = $("body").height();
	var top = bheight-($('#javascript_popup').height()/2);
	var left = bwidth-($('#javascript_popup').width()/2);
	if(top < 50)
		top = 50;
	$('#javascript_popup_mask').width(wid);
	$('#javascript_popup_mask').height(hei);
	$('#javascript_popup').css({top:top,left:left});
}

function ajaxLoadPage(page,$do,id,nav,width,extra){
	$("#popupContainer__").html($("#popupHtml__").html());
	var loadingID = id;
	var action = $do;
	$('#close_button').show();
	$('#submit_buttons').hide();
	var submitValue = 'Submit';
	
	if($do=='Add' || $do=='add' )
		submitValue = 'Add';
	if($do=='Edit' || $do=='edit')
		submitValue = 'Update';
	if($do=='Delete' || $do=='delete' )
		submitValue = 'Delete';
	if($do=='Login' || $do=='login' )
		submitValue = 'Login';	
		
	$('#popup_submit').attr('value',submitValue);
	$('#popup_content').text('');
	$('#popup_content').hide();
	$('#popup_submit').show();
	$('#popup_loading').show();
	$('#popup_title').html('AndMine&trade; '+$do); // '+var1+' Banner');
	$('#popup_title').show();
	popup_position();
	//alert(page);
	$('#javascript_popup_mask').css('opacity',0);
	$('#javascript_popup_mask').show();
	$('#javascript_popup').show();
	$.ajax({
	   type: "GET",
	   url: "cms/page.php?pg="+page+"&do="+$do+"&id="+id+"&"+nav+'&width='+width+extra,
	   success: function(msg){
		 if(msg == 'Page Not Found.')
		 	$('#popup_submit').hide();
		 $('#popup_loading').hide();
		 $('#popup_content').hide();
		 $('#popup_content').text('');
		 $('#popup_content').html(msg);
		 $('#popup_content').show();
		 $('#close_button').hide();
		 $('#submit_buttons').show();
		 initEditor(); 	
			//if($do == "Edit"){
				  $('#andminecms').ajaxForm({ 
					//target identifies the element(s) to update with the server response 
					//target: '#popup_message',
					dataType: 'json',
					// success identifies the function to invoke when the server response 
					// has been received; here we apply a fade-in effect to the new content 
					url: 'cms/lib/action.php',
					type: 'POST',
					//contentType: "application/json; charset=utf-8",
					/*beforeSend: function(x) {
						if(x && x.overrideMimeType) {
							x.overrideMimeType("application/json; charset=UTF-8");
						}
					},*/

					success: function(data) {
						//alert(data); 
						if(data.error){	 
							$('#popup_message').addClass('cms-text');
							$('#popup_message').css('color','red');
							$('#popup_message').html(data.error);
							$('#popup_loading').hide();	
							$('#popup_content').show();
							$('#close_button').hide();
							$('#submit_buttons').show();
							popup_position();
							initCms();
						}
						else if(data.success){
								$('#popup_content').addClass('cms-text');
								$('#popup_content').css('color','green');
								$('#popup_content').html(data.success);
								$('#popup_loading').hide();
								$('#popup_content').show();
								popup_position();
								
								//$('#'+loadingID+'_title').html(data.title);
								if(page == "Login"){
									//$("#javascript_logout_popup").show();
									$('#popup_submit').attr('value','Continue').click(function(){window.location = "index.php"; return false;});
									$('#submit_buttons').show();
									$('#popup_cancel').hide();
									$('#close_button').hide();
									
								}else
									setTimeout("hide_popup()",3000);
									
									
								//if(page == "Welcome" || page == "Special Events" || page == "Contact-test" || page == "Contact-test-footer")
	if(page == "Welcome" || page == "Special Events"  || page == "Booking-Contact" || page == "Booking-Contact-footer" || page == "Contact-test" || page == "Contact-test-footer")
									$('#'+loadingID).html(data.content);
								else if(page == "Staff"){
									$('#'+loadingID).html(data.content);
									initCms();
									//$('#staffimage').attr('src',data.image);
								}
								else if(page == "Community Night"){
									
									if(action == "Edit"){
										$('#'+loadingID).html(data.content);
										initCms();
									}else if(action == "Delete" || action == "Add"){
										$.ajax({
											   url:'cms/CommunityList.php',
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										initCms();
									}
									initCms();
								}
								else if(page == "Gallery"){
									if(action == "Add"){
										$.ajax({
											   url:'cms/GalleryList.php?pg='+data.pg+'&mainimage='+data.content,
											   success:function(htmls){ $('#gallery').append(htmls);initCms();}
											   });
									}else if(action == "Delete"){
										$('#'+data.image_id).hide();
										initCms();
									}
									initCms();
								}
								else if(page == "Menus" ){
									$.ajax({
										   url:'cms/MenusList.php',
										   success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
										   });
								}
								else if(page == "Membership"){
//									alert(data.type);
									$.ajax({
										   url:'cms/MembersList.php?pg='+data.pg+'&type='+data.type+'&column='+data.column,
										   success:function(htmls){ $('#'+loadingID).html(htmls);initCms();}
										   });
								}
								
								initCms();
							}
					} ,
					error:function(data){
						alert('Error:'+data.error);
					}
					/*error: function(xhr, ajaxOptions, thrownError){
                                if (xhr.status == 200) {
                                        alert("Error code 200");
                                }
                                else {
                                        currentData = {};
                                        alert(xhr.status);
                                }
                        }
*/
				});
			
		   popup_position();
	   },
	   error: function(msg){
		 //$('#popup_loading').hide();
	   }
	 });

}

function hide_popup(){
	$('#javascript_popup_mask').hide();
	$('#javascript_popup').hide();
	$('#popup_content').html('');
	$('#popup_content').text('');
}

function submit_form(){
	$('#popup_content').hide();
	$('#submit_buttons').hide();
	$('#close_button').show();
	$('#popup_loading').show();	
	popup_position();
}
