﻿/// <reference path="jquery.sg.lightbox.js" />

var sURL = unescape(window.location.pathname);
if (window.location.search.substring(1).length > 0)
     sURL += '?' + window.location.search.substring(1);
     
var currentimage;
var currentimagewidth;
var currentimageheight;
var currentimagenodeurl;
var currentimagexpath;
var currentimageurl;

function refresh()
{
    window.location.href = sURL;
}

function setContent(id, content)
{
    //save edit button to tack on after content
    var editlink = $(id).find(".contentlightbox").clone();
    //set content to new content
    $(id).html(content);
    //tack on edit button again
    $(id).append(editlink);    
}

function browseImages(src, width, height, nodeurl, xpath)
{
	var finder = new CKFinder() ;
	finder.BasePath = '/components/ckfinder/';

	finder.FolderType = 'Images';
	finder.SelectFunction = setSelectedImage;
	finder.Popup();
	
	currentimage = $('#' + src );
	currentimagewidth = width;
	currentimageheight = height;
	currentimagenodeurl = nodeurl;
	currentimagexpath = xpath;
}

function setSelectedImage(fileUrl)
{
    currentimageurl = fileUrl;
    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "/services/wsImage.asmx/getSize",
      data: "{ 'file':'" + fileUrl + "'}",
      dataType: "json",
      success: function(msg){ 
        //split out width and height
        width = msg.d.split("x")[0];
        height = msg.d.split("x")[1];

        if (width == currentimagewidth && height == currentimageheight)
            setNewImage();
        else
            resizeImage(width, height);        
      }
    });    
}

function setNewImage(deleteOldImage)
{
    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "/services/wsImage.asmx/saveImage",
      data: "{ 'file':'" + currentimageurl + "', 'nodeurl':'"+currentimagenodeurl+"', 'xpath':'"+currentimagexpath+"', 'deleteOldImage':'"+deleteOldImage+"'}",
      dataType: "json",
      success: function(msg){ 
        $(currentimage).attr('src', currentimageurl);     
      },
      error: function(XMLHttpRequest, textStatus, errorThrown){
        if (textStatus == 'error')
           alert('status error: ' + XMLHttpRequest.responseText);
        else
           alert('error: ' + errorThrown);
      }
    });    
}

function newImage(file, deleteOldImage)
{
    currentimageurl = file;
    setNewImage(deleteOldImage);
    SGDialog.close();
}

function resizeImage(newimgwidth, newimageheight)
{
    //open resize window
    var greaterHeight = newimageheight;
    if (parseFloat(currentimageheight) > parseFloat(newimageheight))
        greaterHeight = currentimageheight;

    var greaterWidth = 700;
    if ((parseFloat(newimgwidth) + parseFloat(currentimagewidth)) > greaterWidth)
        greaterWidth = parseFloat(newimgwidth) + parseFloat(currentimagewidth);

    lightbox('/cms/popups/ImageSize.aspx?imageurl=' + currentimageurl + '&node=' + currentimagenodeurl + '&xpath=' + currentimagexpath + '&targetwidth=' + currentimagewidth + '&targetheight=' + currentimageheight, 'Edit Image')
}

function editImageAttr(src, nodeurl, xpath)
{
    currentimagenodeurl = nodeurl;
	currentimagexpath = xpath;
	currentimage = $('#' + src );

	lightbox('/cms/popups/ImageAttributeEditor.aspx?page=' + nodeurl + '&xpath=' + xpath, 'Edit Image Information');
}

function setImageAttr(alternateText, title) {
    currentimage.attr('alt', alternateText);
    currentimage.attr('title', title);
}

function InsertObjectToDocument(toDocument, insertObject) {
    toDocument.write(insertObject);
}