﻿/// <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 = $(".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()
{
    $.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "/services/wsImage.asmx/saveImage",
      data: "{ 'file':'" + currentimageurl + "', 'nodeurl':'"+currentimagenodeurl+"', 'xpath':'"+currentimagexpath+"'}",
      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)
{
    currentimageurl = file;
    setNewImage();
    modalWindow.close();
}

function resizeImage(newimgwidth, newimageheight)
{
    //open resize window
    lightbox('/cms/popups/ImageSize.aspx?imageurl=' + currentimageurl + '&node=' + currentimagenodeurl + '&xpath=' + currentimagexpath + '&targetwidth=' + currentimagewidth + '&targetheight=' + currentimageheight, parseFloat(newimgwidth) + parseFloat(currentimagewidth) + 100, parseFloat(newimageheight) + 20, false, true)
}

function editImageAttr(src, nodeurl, xpath)
{
    currentimagenodeurl = nodeurl;
	currentimagexpath = xpath;
	currentimage = $('#' + src );
	
	editImg = '<div id="imgAttrWrapper"><div id="imgAttrEdit" class="imgAttrEdit">';
	editImg += '<label for="imgAlt">Alternate Text</label><input type="text" name="imgAlt" id="imgAlt" value="'+$(currentimage).attr("alt")+'" /><br>';
	editImg += '<label for="imgTitle">Title</label><input type="text" name="imgTitle" id="imgTitle" value="'+$(currentimage).attr("title")+'" /><br>';
	editImg += '<div style="float: right;margin-right: 50px;"><input type="button" name="imgSave" id="imgSave" value=" Save " /> <input type="button" name="imgCancel" id="imgCancel" value=" Cancel " /><div>';
	editImg += '</div></div>';
	$("body").append(editImg);
	
	lightbox('imgAttrWrapper', 400, 170, false, false, function() { $("#imgCancel").click(function() { modalWindow.close(); } ); });
	$("#imgAttrWrapper").remove();

	
}

function setImageAttr(nodeurl, xpath)
{
    
}
