﻿var gorigional;

function SendMessage()
{
    new Ajax.Request('/services/EmailMember.ashx',
        { 
            parameters: {AID: $('AID').value, body: $('EmailMessage').value},
            onSuccess: function(response) {
            var messageBack ='<div style="margin:30px;font-size:16px;">'+response.responseText+'</div>';
            messagewin.setContent(messageBack)
              
            }
    });

}

function AddComment()
{

    new Ajax.Request('/services/AddComment.ashx',
        {   method:'get',
	        asynchronous:false,
            parameters: {IMID: $F('IMID'), comment: $('NewComment').value.trim() },
            onSuccess: function(response) {
                        window.location.href = window.location.href;
            }
            , onFailure: function(response) {
				Util.log('add comment failed ' + response.responseText);                      
         
             }
    });
}

function DeleteComment(that, member, MemberCommentID)
{
    that = $(that).up().previous().previous();
    var resp = confirm('Really???');
    if(!resp)
        return;

    new Ajax.Request('services/AddComment.ashx',
        {   method:'get',
	        asynchronous:false,
            parameters: {IMID: $F('IMID'), comment: that.innerHTML , IsActive:"false", MemberId:member, MemberCommentID:MemberCommentID},
            onSuccess: function(response) {
                        window.location.href = window.location.href;
            }
            , onFailure: function(response) {
                 Util.log('add comment failed ' + response.responseText );                      
             }
    });
}


function AddFan(that)
{
that = $(that);
if(that.hasClassName('btnIsFavorite'))
{
    var removeOK = confirm('Are you sure you want to remove this image from your favorites?')
    if(!removeOK)
    return;
}

    new Ajax.Request( '../../../services/image.ashx',
        { 
        method:'get',
        parameters: {Type:'Fan', IMID: $F('IMID'), a:escape(Date())},
        onSuccess: function(response) {
		
		var isFav = response.responseText;
		
		if(isFav == 1)
		{
		try
		{
			that.removeClassName('btnAddAsFavorite');
			that.addClassName('btnIsFavorite');
			//$E('div', 'divSeeAll').show();
			$E('div', 'divSeeAll').style.display = 'block';
			
			//Post this as a feed to Facebook (only non-adult images)
			if ($('adultRating').value != "R")
				AddFBFeed_FavImage();
		}catch(e){Util.log(e)}
		}
		else if(isFav == 0)
		{
			that.removeClassName('btnIsFavorite');
			that.addClassName('btnAddAsFavorite');
			//$E('div', 'divSeeAll').hide();
			$E('div', 'divSeeAll').style.display = 'none';
		}
		else if(isFav == -1)
		{
			window.location.href ='/login.aspx?ReturnUrl=%2fshowartwork.aspx%3fIMID%3d' + $F('IMID')
		}
		          
        }
        , onFailure: function(response) 
        {
		  Util.LogAjaxError('add comment show artwork failed ' + response)
        }
    });
      
}
function ShowDescription(show)
{
    if (show)
    {
        $('fulldescription').show();
        $('shortdescription').hide();
    } else {
        $('fulldescription').hide();
        $('shortdescription').show();
    }
}

function ToggleEmbed(id, name)
{


messagewin = new Window({className:  "alphacube",
  width:  705, 
               height: 600,
               recenterAuto: false
              
             
              });
    
    messagewin.setURL('/usercontrols/embed.aspx?IMID=' + id + '&Name=' + escape(name))
    messagewin.setZIndex(2)

  //  messagewin.setLocation(5, Left)

	messagewin.showCenter();

}
function showShare()
{
$('divShareAnImage').toggle()

}        
function ToggleDiv(which, pin, height, width, origional, inputBox)
{

if(typeof(origional) == 'undefined' || origional == null)
{
	var text = $(which).innerHTML.replace(/txtNewComment/g,'NewComment')
	var text = text.replace(/txtEmailMessage/g,'EmailMessage')
}
else
{
	$(which).show(); 
	text = $(which)
}

closeWindow();
var leftOffset = (document.documentElement.clientWidth - 1000) / 2

	messagewin = new Window({className:  "alphacube",
               width:  width, 
               height: height,
               minimizable:false,
               maximizable:false
              });
    messagewin.setHTMLContent(text)
    var loc = $(pin).positionedOffset()
    var Top = loc[1]
    var Left = loc[0]
	messagewin.setZIndex(3)
	messagewin.setLocation(Top + 43, Left + leftOffset)
	messagewin.show();


if(inputBox != null)
{
inputBox = $(inputBox);
inputBox.innerHTML = '&nbsp;'
//inputBox.focus();
}

}

function closeReport()
{
var report = $('reportInappropriate'); 
report.toggle();

}

function ToggleMail(imageid, pin, GID, IMID, ImageName, BestName, ArtistName, GalleryName, ArtistId,  SenderMemberIDShort,ImageIDShort)
{
    closeWindow();
    htm='<iframe src="../usercontrols/SendEmails.aspx?IMID='+IMID+'&GID='+GID+'&ImageName='+ImageName+'&BestName='+BestName+'&ArtistName='+ArtistName+'&GalleryName='+GalleryName+'&ArtistId='+ArtistId+'&SenderMemberIDShort='+SenderMemberIDShort+'&ImageIDShort='+ImageIDShort+'" frameborder="0" width=540 height=560 style="background-color:white; margin-top:1px; margin-left:1px;"></iframe>'

    messagewin = new Window({className:  "alphacube",
               width:  575, 
               height: 575,
               minimizable:false,
               maximizable:false
              });
    messagewin.setHTMLContent(htm)
    var loc = $(pin).positionedOffset()
    var Top = loc[1]
    var Left = loc[0]
	messagewin.setLocation(Top + 25, Left + 150)
	messagewin.show();
}

function report(imageid, pin)
{
htm='<iframe src="../usercontrols/ReportImage.aspx?IMID='+imageid+'" frameborder="0" width=230 height=210 style="background-color:white; margin-top:1px; margin-left:1px;"></iframe>'

var report = $('reportInappropriate'); 
report.toggle();
report.innerHTML = htm;
report.show();
}


function closeWindow()
{
		
   		if(typeof(messagewin) != 'undefined')
		{
			messagewin.destroy();
			 
		}
 }

function OnEmailToFocus(objThis)
{
    if (objThis.value == 'Please enter email addresses separated by commas')
    {
        objThis.value = '';
    }
	document.getElementById("divInvalid").style.display = 'none';
}

function trim01 (str) 
{
    return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function validateEmailRegularExp(elementValue)
{
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; 
    return emailPattern.test(elementValue); 
}

function ValidateEmail(which)
{
    var sEmails = $(which).value;
    sEmails = trim01(sEmails);
    sEmails = sEmails.replace(/;/g, ',');
    sEmails = sEmails.replace(/ /g, ',');
    var Emails = sEmails.split(',');
    var aEmail;
    var n;
    var bReturn = false;
    var k = 0;
    document.getElementById("divInvalid").style.display = 'block';

    for (var i = 0, n = Emails.length; i < n; i++) 
    {  

	    aEmail = Emails[i];
    			    
	    if (aEmail.length > 0)
	    {
	        if (validateEmailRegularExp(aEmail))
	        {
	            bReturn = true;
	        }
	        else
	        {
	            document.getElementById("divInvalid").innerHTML = "<span style='color:Red; '>Please enter valid email addresses separated by commas.</span>"; 
	            return false;
	        }
	        k++;
	    }
    }
    
    if (k > 5)
    {
        document.getElementById("divInvalid").innerHTML = "<span style='color:Red; '>Please enter no more than 5 email addresses.</span>";
        return false;
    }
    
    if (bReturn)
    {
       window.setTimeout('messagewin.close()',2000)
          
        document.getElementById("divInvalid").innerHTML = "<span style='color:Green; '>Email(s) sent.</span>";
    }
    else
    {
        document.getElementById("divInvalid").innerHTML = "<span style='color:Red; '>Please enter email addresses separated by commas.</span>";
    }
    return bReturn;
}

function textCounter(textarea, maxLen) 
{    
    if (textarea.value.length > maxLen)
    {
        textarea.value = textarea.value.substring(0,maxLen);
    }
}
function inviteToGroup(that)
{
    var comment = $('NewComment')
    var text = $(that).options[$(that).selectedIndex].innerHTML
    var value =   $(that).options[$(that).selectedIndex].value
    if(value == "-1") return;

	comment.value += '<br />Your image would be a great addition to the '+ text +' Group here at Imagekind. Joining is a good way to get exposure for your art at Imagekind and to meet artists with similar interests. You can join at <u><a href="http://groups.imagekind.com/'+value+'">http://groups.imagekind.com/'+value+'</a></u>. I look forward to seeing you there! <br /><br />'
}
		
function fixImage(that, fix)
{
	that = $(that)
	if(that.height < 450 && that.width < 450)
	{
		that.src = fix
	}
}

function brokeImage(that, fix)
{
	that = $(that)
	that.src = fix;
}

function SwitchTab(tabname)
{
    if (tabname == "p")
    {
        $('imgTabPrint').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_print_active.gif';
        $('imgTabCanvas').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_canvas_off.gif';
        if ($('imgTabGreetingCards') != null) { $('imgTabGreetingCards').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_gc_off.gif'; }
        if ($('imgConsiderPrintFraming') == null)
            $E('div', 'tcBuyingOptions_body').style.height = "90px";
        else
            $E('div', 'tcBuyingOptions_body').style.height = "400px";
    }
    else if (tabname == "c")
    {
        $('imgTabPrint').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_print_off.gif';
        $('imgTabCanvas').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_canvas_active.gif';
        if ($('imgTabGreetingCards') != null) { $('imgTabGreetingCards').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_gc_off.gif'; }
        if ($('imgConsiderCanvasFraming') == null)
            $E('div', 'tcBuyingOptions_body').style.height = "90px";
        else
            $E('div', 'tcBuyingOptions_body').style.height = "400px";
    }
    else if (tabname == "gc")
    {
        $('imgTabPrint').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_print_off.gif';
        $('imgTabCanvas').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_canvas_off.gif';
        $('imgTabGreetingCards').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_gc_active.gif';
        $E('div', 'tcBuyingOptions_body').style.height = "100px";
    }
}

function SwitchImageTab(tabname)
{
    if (tabname == "c")
    {
        $('imgTabComments').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_comments_active.gif';
        $('imgTabStats').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_stats_inactive.gif';
    }
    else if (tabname == "s")
    {
        $('imgTabComments').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_comments_inactive.gif';
        $('imgTabStats').src = 'http://d3br5i6ykfuol.cloudfront.net/showartwork/tab_stats_active.gif';
    }
}


var retryAttempt = 0;
function GetFramedThumb(that, galleryid, imid, maxWidth, maxHeight, materialID)
{
    if (Util.getElementByEndingId("img", "largeImage").src == "http://www.imagekind.com/images/banners/missing_image.gif")
    {
        that.src = "http://www.imagekind.com/images/banners/missing_image.gif";
        return;
    }

    new Ajax.Request('/services/frame_engine_save.ashx', {method:'get', parameters: {IMID: imid, maxWidth: 150, maxHeight: 150, materialID: materialID }}); 

    var tryURL = that.src;
    if(tryURL.indexOf('?') == -1)
        tryURL += '?1'
    else
        tryURL += '&1'

    if(typeof(that.style.maxHeight) != 'undefined')
    {
        that.style.maxHeight = maxHeight;
        that.style.maxWidth = maxWidth;
    }
    that.src = "http://d3br5i6ykfuol.cloudfront.net/showartwork/loading.gif";
    if (retryAttempt <= 3)
        window.setTimeout(function(){if(/loading/.test(that.src)) { that.src = tryURL; retryAttempt += 1;} },1000)
    else
    {
        if (imid.indexOf('_' == -1))
            that.src = "http://thumbs.imagekind.com/member/" + galleryid + "/uploadedartwork/150X150/" + imid + ".jpg";
        else
            that.src = "http://thumbs.imagekind.com/museum/150X150/" + galleryid + "/JPGS/" + imid + ".jpg";
    }
 }
 
function FramePopup(frameID) {
    var popup = window.open('FrameDetail.aspx?FID=' + frameID, 'FrameDetail', 'scrollbars,resizable,width=515,height=750');
    popup.focus();
}

function UpdatePrintPrice()
{
    if ($E('select', 'lstPrintSizes') != null)
    {
        var size = $F($E('select', 'lstPrintSizes'));
        $E('input', 'hdnPrintSizeID').value = size;
        var material = $F('materialID');
        if (material >= 5 && material <= 12)
            material = 1;

        new Ajax.Request('/services/PricingDetails.aspx',
        {
            parameters: {imid: $F('IMID'), sizeId: size, materialId: material, command: 'total'},
            onSuccess: function(response) {
                            $E('span', 'lblPrintUnframedPrice').innerHTML = response.responseText;
                       },
            onFailure: function(response) {
                            alert(response.responseText);
                        }
        })
    }
}



function UpdateCanvasPrice()
{
    if ($E('select', 'lstCanvasSizes') != null)
    {
        var size = $F($E('select', 'lstCanvasSizes'));
        $E('input', 'hdnCanvasSizeID').value = size;
        var material = $F('materialID');
        if (material < 5 || material > 12)
            material = 5;

        new Ajax.Request('/services/PricingDetails.aspx',
        {
            parameters: {imid: $F('IMID'), sizeId: size, materialId: material, command: 'total'},
            onSuccess: function(response) {
                            $E('span', 'lblCanvasUnframedPrice').innerHTML = response.responseText;
                       },
            onFailure: function(response) {
                            alert(response.responseText);
                        }
        })
    }        
}


