﻿IK.ArtistMenu = 
{	parentContainer:'',
	parentContainerEl:'',
	artistMenu:'',
	isFan:'',
	fanItem:'',
	fanImg:'',
	id:'',
	Show:function(that, name, id, fanOf, screenName, hasStore)
	{
	this.parentContainer = $(that);
	
	
	if(this.parentContainerEl == that & $(IK.ArtistMenu.artistMenu) != null)
	{
	$(IK.ArtistMenu.artistMenu).toggle();
	return;
	}
	this.id = id;
	this.parentContainerEl = that;
	
	
	if($('RSiframe') != null)
	$('RSiframe').hide();
			
		//Initialize Elements
		if(this.artistMenu == '')
		{
			this.artistMenu = $(document.createElement("div"))
			this.artistMenu.className = 'MenuBox';
			this.artistMenu.innerHTML =  '<div id="closeMenu" title="close"></div>'
			this.artistMenu.innerHTML += '<div id="memberItem"><img id="avatarImg"  /><a id="memberName" ></a></div>' ;
			this.artistMenu.innerHTML += '<div id="fanItem"><img id="isFan"></img><span id="addFan"></span></div>'
			this.artistMenu.innerHTML += '<div><img src="http://static.imagekind.com/community/16x16-SeeGalleries.png" /><a id="SeeGallery" >See Galleries</a></div>'
			this.artistMenu.innerHTML += '<div><img src="http://static.imagekind.com/community/16x16-SendMessage.png" /><a id="SendMessage" >Send Message</a></div>'
			this.artistMenu.innerHTML += '<div id="invite"><img src="http://static.imagekind.com/community/16x16-InviteToGroup.png" /><span >Invite To Group</span></div>'
			this.artistMenu.innerHTML += '<div id="store"><img src="http://static.imagekind.com/community/16x16-ShopStore.png" /><a id="MyStore" >Shop My Store</a></div>'
			
			//Attach to document	
			document.body.appendChild(this.artistMenu)
			Event.observe(window, "resize", this.positionMenu);	
			
			//Assign Elements to Vars			
			this.fanImg = $('isFan');
			this.fanItem = $('fanItem');
			this.avatar = $('avatarImg');
			this.Name = $('memberName');
			this.Store = $('MyStore');
			this.Message = $('SendMessage');
			this.Gallery = $('SeeGallery');
		
			//Close Button	
			$('closeMenu').observe('click', function()
			{
				IK.ArtistMenu.artistMenu.hide();
			});
		}

		this.Gallery.href= '/MemberProfileGalleries.aspx?MID='+id;
		this.Name.innerHTML = name;
		this.Name.href='/MemberProfile.aspx?MID='+id
		this.avatar.src = '/services/avatar.ashx?MID=' + id + '&size=20'
		this.positionMenu();
		this.artistMenu.show();
		this.Store.href='http://'+screenName.toLowerCase()+'.imagekind.com/store';
		this.Message.href='/member/compose.aspx?screenname='+screenName;
		if(fanOf == "true")
		{
		this.MemberFan(id, 'get')
		}
		else if(fanOf == "hide")
		{
		this.fanItem.hide();
		$('invite').hide();
		}
		else
		{
		   $('isFan').src = 'http://static.imagekind.com/community/16x16-AddedImageAsFavorite.png';
           $('addFan').innerHTML = 'Be a fan';
           $('addFan').observe('click',function()
           {
           window.location.href = '/login.aspx?ReturnUrl=' + escape(window.location.pathname)
           });
		}
		
		if (hasStore == "true")
		    $('store').show();
		else
		    $('store').hide();
	} 
	,positionMenu:function()
	{
		var pagewidth =  (window.document.documentElement.clientWidth - $('contentwrapper').getWidth()) / 2
		var width = (200 - IK.ArtistMenu.parentContainer.getWidth()) / 2;
		var pos = IK.ArtistMenu.parentContainer.positionedOffset();
		IK.ArtistMenu.artistMenu.setStyle({left: (pos.left + pagewidth - width) + 'px', top: (pos.top + 21)+ 'px', position:'absolute'})
	}
	,MemberFan: function(id,type)
	{
		IK.ArtistMenu.fanItem.show();
		
		if(typeof(type) != 'string')
			type = 'set';
	 
		if(/remove/.test($('addFan').innerHTML) && type == 'set')
		{
			var  goahead = confirm('Are you sure you want to remove this artist from your favorites?')
 			if(goahead == false)
				return;
		}
	

	
	
	 new Ajax.Request('/services/ArtistMenu.ashx',
                {   method:'get',
                    parameters: {ArtistID: IK.ArtistMenu.id, type:type, num:escape(Date())},
                    onSuccess: function(response) {
                   
                    var resp = response.responseText;
                     if(resp == "1")
                     {
                       $('isFan').src = 'http://static.imagekind.com/community/16x16-AddedImageAsFavorite.png';
                 
                      $('addFan').innerHTML = 'I’m a fan (remove)';               
 
                     
                     }
                     if(resp == "0")
                      {
 
                     
                     $('addFan').innerHTML = 'Be a fan';
                      $('isFan').src = 'http://static.imagekind.com/community/17x17-AddFavArtist.png';
 
 
                     }
                     
                     this.fanItem.show();
                     this.isFan.show();
                      }
                  ,onFailure:function()
                  {
                 
                  // that.setStyle({backgroundColor:'red'})
                   
                  }
                  });
                  
                  
           
            new Ajax.Request('/services/ArtistInvite.ashx',
                {   method:'get',
                    parameters: {ArtistID: IK.ArtistMenu.id, type:'invite', num:escape(Date())},
                    onSuccess: function(response) {
                   
                    var resp = response.responseText;
                     if(resp == "1")
                     {

                     $('invite').show();
                    $('invite').stopObserving('click')
                     $('invite').observe('click',IK.ArtistMenu.showInvite);
            
                   
                     }
                     if(resp == "0")
                     {
						$('invite').hide();
                     }
                     
                     this.fanItem.show();
                     this.isFan.show();
                      }
                  ,onFailure:function()
                  {
                 
                  // that.setStyle({backgroundColor:'red'})
                   
                  }
                  });
                  
           
           
           
                  
                  
                  
                  
                  
					$('addFan').stopObserving('click')
                     $('addFan').observe('click',IK.ArtistMenu.MemberFan);
                     
	
	}
	, showInvite:function()
	{

		el = IK.ArtistMenu.parentContainer;
	
	if($('RSiframe') == null)
	{
		var tempiframe=document.createElement('iframe');
		tempiframe.setAttribute('id','RSiframe');
		tempiframe.style.border='0px';
		tempiframe.style.width='210px';
		tempiframe.style.height='270px';
		tempiframe.style.overflow = 'hidden';
		el.appendChild(tempiframe);
	}
	$('RSiframe').src='/usercontrols/InviteToGroup.aspx?MID=' + IK.ArtistMenu.id +"&divId=" + IK.ArtistMenu.id + 'RSiframe';
	IK.ArtistMenu.artistMenu.hide();
	pos = el.positionedOffset();
	var loco = window.location;
	if(/showartwork/i.test(loco))
	{
	 pos.left -= 50;
	 pos.top -= 238;
	}
	$('RSiframe').setStyle({left: (pos.left ) + 'px', top: (pos.top  )+ 'px', position:'absolute'})
	$('RSiframe').show();		
}
	
	
	
}
function HideInvite()
{
	document.location.href = document.location.href
}
