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: '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) {
					
						//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();
									
								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 == "News" ){
									
									$.ajax({
											    url:'pageContent.php?page='+page,
											    success:function(htmls){
											    	
													$('#'+loadingID).html(Linkify(htmls));initCms();
													$('a:contains("arcone")').each(function(){$(this).attr("target","");});
												}
											   });
										initCms();
								}	
                                else if( page == "Artist_CuriculumVitae" || page == "Artist_Biography")	{
									
									$.ajax({
											   	url:'pageContent.php?page='+page+extra,
											    success:function(htmls){
											    	
													$('#'+loadingID).html(Linkify(htmls));initCms();
													$('a:contains("arcone")').each(function(){$(this).attr("target","");});
												}
											   });
										initCms();
								}	
								else if(extra=="")
								{
										$.ajax({
											   url:'pageContent.php?page='+page,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										initCms();
								}
								else
								{
								   
										$.ajax({
											
												url:'pageContent.php?page='+page+extra,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										initCms();
								}	
									
							/*	if(page == "Artists"){
										$.ajax({
											   url:'pageContent.php?page='+page,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										initCms();
								}
								else if(page == "ArtWork"){
									$.ajax({
											
												url:'pageContent.php?page='+page+extra,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										initCms();
								}
								else if(page == "Exhibition_Artwork"){
									$.ajax({
											   url:'pageContent.php?page='+page+extra,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										initCms();
								}
								else if(page == "Profile_Artwork"){
									$.ajax({
											   url:'pageContent.php?page='+page+extra,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										initCms();
								}
								else if(page == "Exhibition"){
									$.ajax({
											   url:'pageContent.php?page='+page,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										initCms();
								}
								else if(page == "HomePageImage"){
									
									$.ajax({
											   url:'pageContent.php?page='+page,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										initCms();
								}		
								else if(page == "News"){
									
									$.ajax({
											    url:'pageContent.php?page='+page,
											    success:function(htmls){$('#'+loadingID).html(htmls);initCms();}
											   });
										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();
}

function fnChaneArtworkOrder(loadingID,awid,extra,page)
{


	newOrder=$('#Artwork_Order_'+awid).val();
	$.ajax({
		
		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
	
	
		type: 'GET',
		url: 'changeorder.php?loadingID='+loadingID+'&awid='+awid+'&oid='+newOrder,
	
		success: 
			function(data) {
				
								if(data.error){	 
									alert('error');
								}
								else if(data.success)
								{
										$.ajax({
													
											    url:'pageContent.php?page='+page+extra,
											    success:function(htmls){
											    	
													$('#'+loadingID).html(Linkify(htmls));initCms();
													
												}
											   });
										initCms();
								}
							},
		error:
			function(data){alert('Error:'+data.error);}
	});
							
}
