/* 
 * This file contains functions used by the Image Editor 
 */


 
//this function helps to show object images
function image_editor_showObjectImages(input)
 {   
    var delim =',';
    var object_type =parseInt(getTokenAt(input,0,delim,3));//object type
    var object_id=parseInt(getTokenAt(input,1,delim,3));//object id
    var div_show_object_images_id=getTokenAt(input,2,delim,3);//div id

    var action_file_path = "image_editor/index.php";
    var actioncmd = "?show_object_images_request=1"+
                    "&&object_type="+object_type+
                    "&&object_id="+object_id+
                    "&&div_show_object_images_id="+div_show_object_images_id;

    var url = action_file_path+actioncmd;
    url=url+"&sid="+Math.random();

  //get the content to display using ajax
    $.ajax({
            method: "get",url:url,data: "page",
            beforeSend: function()
            {
            // showSpinner(div_show_object_images_id);
            },
            complete: function(){}, 
            success: function(html)
            {
            $("#"+div_show_object_images_id).html(html);          
            }
        }); //close $.ajax()

 }//image_editor_showObjectImages
 

///////////////////////////////////////////////////////////////////////////////
  function image_editor_showUploadImagesForm(input)
 {    // alert(input);
       var delim =',';               
       var object_type =parseInt(getTokenAt(input,0,delim,4));
       var object_id=parseInt(getTokenAt(input,1,delim,4));
       var current_image_count=parseInt(getTokenAt(input,2,delim,4));
       var div_upload_image=getTokenAt(input,3,delim,4);

       var action_file_path = "image_editor/index.php";
       var actioncmd = "?show_upload_image_form_request=1"+
                        "&&object_type="+object_type+
                        "&&object_id="+object_id+
                        "&&current_image_count="+current_image_count;

        var url = action_file_path+actioncmd;
        url=url+"&sid="+Math.random();

     //get the content to display using ajax
        $.ajax({
                method: "get",url:url,data: "page",
                beforeSend: function()
                {
                    showSpinner(div_upload_image);
                },
                complete: function(){}, 
                success: function(html)
                { 
                $("#"+div_upload_image).html(html); 
                }
            }); //close $.ajax()
 }//image_editor_showUploadImageForm()




	function _add_more(input)
        { //alert(input);
        var delim =',';
        var div_added_file_counter = getTokenAt(input,0,delim,4);
        var div_upload_counter=getTokenAt(input,1,delim,4);
        var divFile=getTokenAt(input,2,delim,4);
        var div_addable_file_counter=getTokenAt(input,3,delim,4);

        var addable_file_counter = parseInt(document.getElementById(div_addable_file_counter).value);
        var added_file_counter = parseInt(document.getElementById(div_added_file_counter).value);

        if(added_file_counter<addable_file_counter)//addable_images is the max allowed
            {
            //alert(added_file_counter+" is less than "+addable_file_counter);
            var txt = "<br><input type=\"file\" name=\"item_file[]\">";
            document.getElementById(divFile).innerHTML += txt;

            added_file_counter = parseInt(document.getElementById(div_added_file_counter).value);
            added_file_counter +=1;
            document.getElementById(div_added_file_counter).value=added_file_counter;
            }
          else
            {
              //alert(added_file_counter+" is NOT less than "+addable_file_counter);
              document.getElementById(div_upload_counter).innerHTML = "Max reached";
            }
            
	}//_add_more()


	function image_editor_validate(f)
        {
		var chkFlg = false;
                
		for(var i=0; i < f.length; i++)
                {
			if(f.elements[i].type=="file" && f.elements[i].value != "")
                        {
                            chkFlg = true;
			}
		}
		if(!chkFlg)
                {
			alert('Please browse/choose at least one file');
			return false;
		}

		f.pgaction.value='upload';
                
		return true;
	}



////////////////////////////////////////////////////////////////////////////////
//this function helps to show the form for editing photos
function image_editor_showConfirmDeleteImageForm(input)
{     //alert(input);
       var delim = ',';
       var object_type =parseInt(getTokenAt(input,0,delim,5));
       var object_id =parseInt(getTokenAt(input,1,delim,5));
       var image_id =parseInt(getTokenAt(input,2,delim,5));
       var div_delete_image_id = getTokenAt(input,3,delim,5);
       var div_show_object_images_id = getTokenAt(input,4,delim,5);

       var action_file_path = "image_editor/index.php";
       var actioncmd = "?delete_tour_image_request=1"+
                        "&&object_type="+object_type+
                        "&&object_id="+object_id+                  
                        "&&image_id="+image_id+
                        "&&div_delete_image_id="+div_delete_image_id+
                        "&&div_show_object_images_id="+div_show_object_images_id;

        var url = action_file_path+actioncmd;
        url=url+"&sid="+Math.random();

          //get the content to display using ajax
            $.ajax({
                method: "get",url:url,data: "page",
                beforeSend: function()
                {
                    showSpinner(div_delete_image_id);
                },
                complete: function(){},
                success: function(html)
                { 
                $('#'+div_delete_image_id).html(html); 
                }
     }); //close $.ajax()


}//image_editor_showConfirmDeleteImageForm()



function image_editor_deleteObjectImage(input)
{
        var delim =',';
        var object_type =parseInt(getTokenAt(input,0,delim,4));
        var object_id =parseInt(getTokenAt(input,1,delim,4));
        var object_image_id =parseInt(getTokenAt(input,2,delim,4));
        var div_show_object_images_id =getTokenAt(input,3,delim,4);

       var action_file_path = "image_editor/index.php";
       var actioncmd = "?delete_image_confirm=1"+
                        "&&object_type="+object_type+
                       "&&object_image_id="+object_image_id+
                        "&&object_id="+object_id;

        var url = action_file_path+actioncmd;
        url=url+"&sid="+Math.random();

         $.ajax({
                    method: "get",url:url,data: "page",
                    beforeSend: function()
                    {
                    showSpinner(div_show_object_images_id);
                    },
                    complete: function()
                    {
                    //$(div_show_object_images_id).html("");
                    }, 
                    success: function(link)
                    {
                   // alert(link);
                    //reloadSite(link);
                    }
         }); //close $.ajax()
}//image_editor_deleteObjectImage_tabPanel()



//this function helps to select an image  to be made the main for the object
function image_editor_setObjectImageMainPhoto(input)
{
       var delim = ',';
       var object_type =parseInt(getTokenAt(input,0,delim,4));
       var object_id =parseInt(getTokenAt(input,1,delim,4));
       var image_id =parseInt(getTokenAt(input,2,delim,4));
       var div_main_image_note =getTokenAt(input,3,delim,4);

      var action_file_path = "image_editor/index.php";
      var actioncmd = "?set_object_main_image_request=1"+
                        "&&object_type="+object_type+
                         "&&object_id="+object_id+
                         "&&image_id="+image_id;

        var url = action_file_path+actioncmd;
        url=url+"&sid="+Math.random();

             //get the content to display using ajax
                $.ajax({
                    method: "get",url:url,data: "page",
                    beforeSend: function()
                    {
                    showSpinner(div_main_image_note);
                    }, 
                    complete: function()
                    {                 
                    }, //stop showing loading when the process is complete
                    success: function(html)
                    { 
                        $("#"+div_main_image_note).html(html); //show the html inside .content div
                    }
         }); //close $.ajax()

}//image_editor_setObjectImageMainPhoto()



//this function helps to show the form for getting a message
function image_editor_showFullImage(input)
{
    var delim =',';
    var div_email_us_box_id=getTokenAt(input,0,delim,2);
    var btn_email_us_box_id=getTokenAt(input,1,delim,2);

     $.fx.speeds._default = 1000;

       $(function()
       {
		// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
		$( "#dialog:ui-dialog" ).dialog( "destroy" );

		$( "#"+div_email_us_box_id ).dialog({
			autoOpen: false,
                        width: 750,
			modal: true,
                        draggable: false,
                        resizable:false,
                        show: "fade",
                        hide: "fade"
		});

		$( "#"+btn_email_us_box_id )
                .click(function(e)
                {
                    e.preventDefault();
                    $( "#"+div_email_us_box_id ).dialog( "open" );

                });
	});
}//image_editor_showFullImage()


//this function helps to show object images
function image_editor_showEditImageDescripForm(input)
 {//alert(input);
    var delim =',';
    var image_id =parseInt(getTokenAt(input,0,delim,3));
    var object_type=getTokenAt(input,1,delim,3);
    var div_show_text_editor_edit_image_descrip=getTokenAt(input,2,delim,3);

    var action_file_path = "image_editor/index.php";
    var actioncmd = "?show_text_editor_edit_image_descrip_request=1"+
                    "&&image_id="+image_id+
                    "&&object_type="+object_type;

    var url = action_file_path+actioncmd;
    url=url+"&sid="+Math.random();

  //get the content to display using ajax
    $.ajax({
            method: "get",url:url,data: "page",
            beforeSend: function()
            {
            // showSpinner(div_show_text_editor_edit_image_descrip);
            },
            complete: function(){},
            success: function(html)
            {
            $("#"+div_show_text_editor_edit_image_descrip).html(html);
            }
        }); //close $.ajax()

 }//image_editor_showEditImageDescriptionForm
