﻿// Script functions used by the ImagePicker controls and Telerik RAD editor commands

// Open the dialog for image upload and finding
function OpenWCWImagePickerDialog(url)
{
    var HeightVindu = screen.availHeight - 90;
    var WidthVindu = screen.availWidth;
    var xVindu = WidthVindu/2-500;
    var retVal = window.showModalDialog(url,'','resizable:yes;scroll:yes;dialogWidth:1000px;dialogHeight: ' + HeightVindu + 'px;dialogLeft:'+ xVindu +'; dialogTop=0');
    return retVal;
}

// Function that starts the wcwImagePickerDialog and write values to the fields in the 
//
// url - Url to call the pickerdialog with
// imageID - ClientID for the image to write the picked images url to
// HiddenFieldID - ClientIDfor the image to write the picked images url to
 //clearButtonID  - ClientID for the clearbutton
function StartWCWImagePickerDialogFromPageLayoutAndWriteValues(url,imageID,hiddenFieldID,clearButtonID)
{
    var retVal = OpenWCWImagePickerDialog(url);
    if(retVal != '' && retVal != null)
    {
        var image = document.getElementById(imageID);
        image.src = retVal;              
        var hiddenField = document.getElementById(hiddenFieldID);
        hiddenField.value = retVal;
        var clearButton = document.getElementById(clearButtonID);
        clearButton.disabled = false;  
    }
}


// Function that clears the wcwimagefieldcontrol 
//
// url            - Url to call the pickerdialog with
// imageID        - ClientID for the image to write the picked images url to
// HiddenFieldID  - ClientIDfor the image to write the picked images url to    
// clearButtonID  - ClientID for the clearbutton
// altTextID      - ClientID for the alternate text box
function ClearWCWImagePickerValues(imageID,previewImageUrl, hiddenFieldID,clearButtonID,altTextID)
{
    var image = document.getElementById(imageID);
    image.src = previewImageUrl;              
    var hiddenField = document.getElementById(hiddenFieldID);
    hiddenField.value = '';
    var altText = document.getElementById(altTextID);
    altText.value = '';              
    var clearButton = document.getElementById(clearButtonID);
    clearButton.disabled = true;     
}

//***** END Functions used by the  WCW - ImagefieldControl ***********
  
//Gets the relative URL for a web based on the current document
function GetRelativeWebUrl()
{
	var relativeUrl = document.URL.toLowerCase();
	relativeUrl = relativeUrl.substring(0, relativeUrl.indexOf("/pages/"));
	relativeUrl = relativeUrl.substring(relativeUrl.indexOf("//") + 2);
	relativeUrl = relativeUrl.substring(relativeUrl.indexOf("/"));

	return relativeUrl;  
}

function GetImageUrl()
{
	var relativeWebUrl = GetRelativeWebUrl();
	var imagePickerUrl = relativeWebUrl + "/_layouts/sh/ImagePicker.aspx?ListUrl=" + relativeWebUrl + "/PublishingImages";
    var imageUrl = OpenWCWImagePickerDialog(imagePickerUrl);
	
	return imageUrl;
}

function GetImageUrlAndSize()
{
	var relativeWebUrl = GetRelativeWebUrl();
	var imagePickerUrl = relativeWebUrl + "/_layouts/sh/ImagePicker.aspx?ListUrl=" + relativeWebUrl + "/PublishingImages&GetSize=1";
    var imageUrlAndSize = OpenWCWImagePickerDialog(imagePickerUrl);
	
	return imageUrlAndSize;
}

function SplitImageUrlAndSize(imageUrlAndSize, imageUrl, imageSize)
{
    var items = imageUrlAndSize.split("|");
    imageUrl = items[0];
    
    if (items.length > 1)
        imageSize = items[0];
}

//Determines if the radEditor spans 3 columns
function isContentWide()
{
    return (document.getElementById("RadEWrapperctl00_PlaceHolderMain_Content_ctl00").style.width == "711px"); //TODO Hardcoded size
}

//Returns true if an image is "big" by inspecting the class of the surrounding div
function isImageDivBig(divElement)
{
    return (divElement.className.indexOf("artImgWide") >= 0 || divElement.className.indexOf("artImgBig") >= 0);
}

//Returns true if an image size is "big" (468 x 195)
function isImageSizeBig(sizeStr)
{
    return (sizeStr == "468 x 195"); //TODO Hardcoded size
}

//Registers custom commands for handling images in the radEditor
function RegisterWCWRadEditorCommands()
{
    //Adjusts the class of the div surrounding the image to align the image left
    //Big images should only be changed if the radeditor spans 3 columns
    RadEditorCommandList["WCWImageLeft"] = function(commandName, editor, oTool)
    {
        var selection = editor.GetSelection();
        var selectedElement = selection.GetParentElement();

        if (selectedElement != null && selectedElement.tagName == "IMG")
        {
            var divElement = selectedElement.parentElement;
            
            if (divElement != null && divElement.tagName == "DIV")
            {
            	if (isImageDivBig(divElement))
                {
					if (isContentWide())
					{
                        divElement.className = "artImgInline artImgWideLeft";
                    }
                }
                else
                {
                    divElement.className = "artImgInlineLeft";
                }
            }
        }
     };

    //Adjusts the class of the div surrounding the image to align the image right
    //Big images should only be changed if the radeditor spans 3 columns
    RadEditorCommandList["WCWImageRight"] = function(commandName, editor, oTool)
    {
        var selection = editor.GetSelection();
        var selectedElement = selection.GetParentElement();

        if (selectedElement.tagName == "IMG")
        {
            var divElement = selectedElement.parentElement;
            
            if (divElement.tagName == "DIV")
            {
            	if (isImageDivBig(divElement))
                {
					if (isContentWide())
					{
                        divElement.className = "artImgInline artImgWide floatRight";
                    }
                }
                else
                {
                    divElement.className = "artImgInline";
                }
            }
        }
    };

    //Inserts an image with a surrounding div, and a dummy image caption and photographer text
    //If image is big other classes are used
    RadEditorCommandList["WCWInsertImage"] = function(commandName, editor, oTool)
    {
    	var imageUrlAndSize = GetImageUrlAndSize();

        var items = imageUrlAndSize.split("|");
        var imageUrl = items[0];
        
        if (items.length > 1)
            var imageSize = items[1];

        if (imageUrl != null && imageUrl.length > 0)
        {
            var className = "artImgInline";

            if (isImageSizeBig(imageSize))
            {
				if (isContentWide())
				{
                    className = "artImgInline artImgWide floatRight";
				}
				else
				{
            	    className = "artImgInline artImgBig";
            	}
            }

            var imageText = "Lorem ipsum dolor sit amet.";
            var photographer = "(Foto: Lorem Ipsum)";

			var html = "<div class='{className}'><img alt='Bilde' src='{imageUrl}'><p class='artImgtext'>{imageText}</p><p class='artImgPhotographer'>{photographer}</p></div>";
			html = html.replace("{className}", className);
			html = html.replace("{imageUrl}", imageUrl)
			html = html.replace("{imageText}", imageText)
			html = html.replace("{photographer}", photographer);
            	
            editor.PasteHtml(html);
        }
    };

    //Replaces the image by letting the user selected a new image in the image picker dialog
    RadEditorCommandList["WCWReplaceImage"] = function(commandName, editor, oTool)
    {      
        var selection = editor.GetSelection();
        var selectedElement = selection.GetParentElement();

        if (selectedElement.tagName == "IMG")
        {
        	var imageUrl = GetImageUrl();

            if (imageUrl != null && imageUrl.length > 0)
            {
                selectedElement.src = imageUrl;
            }
        }
    };

    //Removes an image and its surroundings from the editor
    RadEditorCommandList["WCWRemoveImage"] = function(commandName, editor, oTool)
    {      
        var selection = editor.GetSelection();
        var selectedElement = selection.GetParentElement();

        if (selectedElement.tagName == "IMG")
        {
            var divElement = selectedElement.parentElement;
            
            if (divElement.tagName == "DIV")
            {
                divElement.removeNode(true);
            }
        }
	};

    //Prompts the user for a new alt text for an image, and applies it to the image
    RadEditorCommandList["WCWEditAlternateText"] = function(commandName, editor, oTool)
    {      
        var selection = editor.GetSelection();
        var selectedElement = selection.GetParentElement();

        if (selectedElement.tagName == "IMG")
        {
            var existingAltText = selectedElement.alt;
            var newAltText = prompt("Alternate Text", existingAltText);
            
            if (newAltText != null)
            {
                selectedElement.alt = newAltText;
            }
        }
	};
}
    
/* function for switching classes in general*/
/* specifically in use on the multilang switch*/
function switchClass(id, myClass) {
	var x = document.getElementById(id);
	x.className = myClass;
}
