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

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

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

function ajaxLoadPage(page,jQuerydo,id,dir_index,width){
	jQuery("#popupContainer__").html(jQuery("#popupHtml__").html());
	var loadingID = id;
	var action = jQuerydo;
	jQuery('#close_button').show();
	jQuery('#submit_buttons').hide();
	var submitValue = 'Submit';
	if(jQuerydo=='login' || jQuerydo=='Login' )
		submitValue = 'Login';
	if(jQuerydo=='Add' || jQuerydo=='add' )
		submitValue = 'Add';
	if(jQuerydo=='Edit' || jQuerydo=='edit')
		submitValue = 'Update';
	if(jQuerydo=='Delete' || jQuerydo=='delete' )
		submitValue = 'Delete';
		
	jQuery('#popup_submit').attr('value',submitValue);
	jQuery('#popup_content').text('');
	jQuery('#popup_content').hide();
	jQuery('#popup_submit').show();
	jQuery('#popup_loading').show();
	jQuery('#popup_title').html('AndMine&trade; '+jQuerydo); // '+var1+' Banner');
	jQuery('#popup_title').show();
	popup_position();
	//alert(page);
	jQuery('#javascript_popup_mask').css('opacity',0);
	jQuery('#javascript_popup_mask').show();
	jQuery('#javascript_popup').show();
	jQuery.ajax({
	   type: "GET",
	   url: "page.php?pg="+page+"&do="+jQuerydo+"&id="+id+"&dir_index="+dir_index+'&width='+width,
	   beforeSend: function(){ 
	   				//alert(this.url)
	   				},
	   success: function(msg){
		 if(msg == 'Page Not Found.')
		 	jQuery('#popup_submit').hide();
		 jQuery('#popup_loading').hide();
		 jQuery('#popup_content').hide();
		 jQuery('#popup_content').text('');
		 jQuery('#popup_content').html(msg);
		 jQuery('#popup_content').show();
		 jQuery('#close_button').hide();
		 jQuery('#submit_buttons').show();
				  jQuery('#uploadimage').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: 'ajax.php',
					type: 'POST',
					//beforeSend: function(msg){alert(this.url)},
					success: function(data) {
						//alert(data); 
						if(data.error){	 
							jQuery('#popup_message').addClass('cms-text');
							jQuery('#popup_message').css('color','red');
							jQuery('#popup_message').html(data.error);
							jQuery('#popup_loading').hide();	
							jQuery('#popup_content').show();
							jQuery('#close_button').hide();
							jQuery('#submit_buttons').show();
							popup_position();
							initCms();
						}
						else if(data.success){
								jQuery('#popup_content').addClass('cms-text');
								jQuery('#popup_content').css('color','green');
								jQuery('#popup_content').html(data.success);
								jQuery('#popup_loading').hide();
								jQuery('#popup_content').show();
								popup_position();
								
								if(page == 'login'){
									jQuery('#popup_submit').attr('value','Continue').click(function(){window.location = "index.php?nav1=Gallery"; return false;});
									jQuery('#popup_submit').show();
									jQuery('#submit_buttons').show();
									jQuery('#popup_cancel').hide();
									jQuery('#close_button').hide();
					
									//window.location = 'index.php?nav1=Gallery';
								}else	
									setTimeout("hide_popup()",3000);
								
								if(loadingID == "Video"){
									jQuery.ajax({
										   url:'VideoList.php?frm_ajax=1',
										   success:function(htmls){ jQuery('#'+loadingID).html(htmls);initCms();}
										   });
									initCms();
								}
								else if(loadingID == "Gallery"){
									jQuery.ajax({
										   url:'WallpaperList.php?frm_ajax=1',
										   success:function(htmls){ jQuery('#'+loadingID).html(htmls);initCms();}
										   });
									initCms();
								}
								
								/*if(data.nav1)
								setTimeout(window.location='index.php?nav1='+data.nav1,3000);*/
								
								//jQuery('#'+loadingID+'_title').html(data.title);
							}
					} ,
					error:function(data){
						alert('Error:'+data.error);
					}
				});
			
		   popup_position();
	   },
	   error: function(msg){
		 //jQuery('#popup_loading').hide();
	   }
	 });

}

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

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