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,title){


	$("#popupContainer__").html($("#popupHtml__").html());
	var loadingID = id;
	var action = $do;
	$('#close_button').show();
	$('#submit_buttons').hide();
    $('#login_now').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';	
        

	if(($do=='Add' || $do=='add') && (page=="StoreMail"||page=="ContactMail"))
		submitValue = 'Submit';	
        
	$('#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');
    if(($do=='Add' || $do=='add') && (page=="StoreMail"||page=="ContactMail"||page=="StoreRequest"))
    {
        if(page=="StoreMail")
            $('#popup_title').html('Email Us');
        if(page=="ContactMail")
            $('#popup_title').html('Contact Us');
        if(page=="StoreRequest")
            $('#popup_title').html('Add Your Store');
        
        
        
        $('#popfooter1').html('');
        $('#popfooter2').html('');
    }
        
     if(title!='')
        $('#popup_title').html(title);
        
	$('#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();
         
      
         if(page=="Directory"||page=="Store"||page=="StoreRequest")
            initEditor("","270px","100px");
         else if(page=="ContactMail")	
            initEditor("bold,italic,underline","","");
         else
            initEditor();
            
   	   //fix for IE to activate the tinyMCE controller
		try{
    		  if (typeof tinyMCE != "undefined") {
    		   for(id in tinyMCE.editors){
    			   tinyMCE.execCommand('mceFocus', false, id);
    		   }
    		   $('form:not(.filter) :input:visible:first').focus() ;
    
    		 }
         }catch(e){};
         
			//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: "action.php",
					type: 'POST',
					//contentType: "application/json; charset=utf-8",
					/*beforeSend: function(x) {
						if(x && x.overrideMimeType) {
							x.overrideMimeType("application/json; charset=UTF-8");
						}
					},*/

					
					beforeSend: function(x) {
						//alert(this.url);
					},
					success: function(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();
								
								if(page == "Login"){
									//$("#javascript_logout_popup").show();
                                   
                                    if(data.storealias!="")
    									$('#popup_submit').attr('value','Continue').click(function(){window.location =  data.storealias; return false;});
                                    else
                                        $('#popup_submit').attr('value','Continue').click(function(){window.location ="index.php"; return false;});
									$('#submit_buttons').show();
									$('#popup_cancel').hide();
									$('#close_button').hide();
                                    $('#login_now').hide();
									
								}
                                else if(page == "ForgotPassword")
                                {
                                       $('#submit_buttons').hide();
									   $('#popup_cancel').hide();
                                       $('#login_now').show(); 
                                       
                                }else
                                {
                                    $('#login_now').hide();
                                    setTimeout("hide_popup()",3000);
                                }
									
                               
                                if(page=="Directory")
                                {
                                   
                                        if(data.category)
                                             extra=extra+"&updatecategory_id="+data.category;
                                    
										$.ajax({
											
												url:'ajaxpageContent.php?page='+page+extra,
											    success:function(htmls){
											     $('#'+loadingID).html(htmls);initCms();
                                                 fnResizeStoreList();
                                                 }
											   });
										initCms();
                                }
                              
                                else if(page=="Store")
								{
								   
										$.ajax({
											   url:'ajaxpageContent.php?page='+page+extra,
											    success:function(htmls){
											     
											         $('#'+loadingID).html(htmls);
                                                     initCms();
                                                     //to update the page title
                                                 	 $.ajax({
        											    url:'ajaxpageContent.php?page=Storetitle'+extra,
        											    success:function(htmls){$('#pageTitle').html(htmls);}
        											   });
                                                     
                                                     }
											   });
										initCms();
								}
                                
                                else if(page=="WebBanner")
								{
								    	$.ajax({
											
												url:'ajaxpageContent.php?page='+page+extra,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();fnInitSliderCMSActive();}
											   });
										initCms();
                                }
								else if(extra=="")
								{
								   
										$.ajax({
											   url:'ajaxpageContent.php?page='+page,
											    success:function(htmls){
											     if(loadingID)
											         $('#'+loadingID).html(htmls);initCms();
                                                }
											   });
										initCms();
								}
                                else if(page=="Blog") {

                                        $.ajax({
										   url:'ajaxpageContent.php?page='+page,
										    success:function(htmls){
										     if(loadingID)
										         $('#'+loadingID).html(htmls);initCms();fixBlogList(true);
                                            }
										});
                                        fixBlogList(true);
										initCms();
                                }
								else
								{
								   
										$.ajax({
											
												url:'ajaxpageContent.php?page='+page+extra,
											    success:function(htmls){
											     if(loadingID)
                                                    $('#'+loadingID).html(htmls);initCms();
                                                 }
											   });
										initCms();
								}	
									
							
								initCms();
							}
					} ,
					error:function(data){
					   /*var output = "";
                       for(var i in data) {
                            output += i+" = "+data[i]+"\n";
                       }
                       alert(output);*/
					   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();
    
    
    //fix to save the tinyMCE content
    try
    {
        if (typeof tinyMCE != "undefined") {
		   for(id in tinyMCE.editors){
		   		tinyMCE.editors[id].save();
					
		   }
		 }
    }catch(e){};
}


