(function($)
{
	$.site = {
		hudHtml: '',
				virbDomain: 'virb.com',
		reloads: {},
		init: function()
		{
			this.buildHud();
			
			$("#virbHud #settings a").click(function()
		    {
				if ($("#virbHud iframe").css('top').replace('px','') < 0)
				{
					$.site.showCustomize();
				}
				else
				{
					$.site.hideCustomize(false);
				}
		
				return false;
		    });
		
			$('body').click(function()
			{
				$.site.hideCustomize(false);
			});
		
			$('#followSite').click(function()
			{
				$('#virbHud #hudTitle').html('Saving...');
				$("#virbHud iframe").attr('src','http://'+$.site.virbDomain+'/services/ajax/get?data[Relationship][action]=followSite&data[Relationship][id]='+$.site.id);
			});
		},
		hideCustomize: function(args)
		{
			$("#virbHud #hudMenu").removeClass('active');
			$("#virbHud iframe").slideUp(function()
			{
				$(this).css({top: "-5000px"});
			});									
		},
		showCustomize: function()
		{
			$("#virbHud #hudMenu").addClass('active');
			$("#virbHud iframe").hide().css({top: "52px"}).slideDown();	
		},
		addScript: function(url)
		{		
			var scriptTag  = document.createElement('script');
			scriptTag.type = 'text/javascript';
			scriptTag.src  = url;

			$('head').prepend(scriptTag);

			return true;
		},
		reloadScript: function(url)
		{
			if (!this.reloads[url])
			{
				this.reloads[url] = 1;
			}   
			else
			{
				this.reloads[url]++;
			}
			
			if (this.reloads[url] <= 10)
			{
				setTimeout(function()
				{
					$('script[href^='+url+']').remove();

					$.site.addScript(url+'&cacheb='+Math.random());									
				}, 500);
			}			
		},
		buildHud: function()
		{
			$('body').append(this.hudHtml);
		},
		parseHash: function(hash)
		{
			try
			{
				hashObj = $.parseJSON(hash);
			}			
			catch(error)
			{
				hash = hash.replace(/%22/g, '"').replace(/%20/g, ' ');				
				hashObj = $.parseJSON(hash);
			}

			this[hashObj.callback](hashObj.args);
		},
		firstResizeIframe: function(args)
		{
			this.resizeIframe(args);

				
		},
		resizeIframe: function(args)
		{
			$('#virbHud').css({"visibility": "visible"});
			$("#virbHud iframe").attr({"height":args.height,"width":args.width}).css({"overflow": "hidden", "top": "-5000px", "visibility": "visible"});	
		},
		updateIframe: function()
		{
			$("#virbHud iframe").slideUp(function()
			{
				$(this).css({top: "-5000px"});
			});						
		},
		finishUpdateIframe: function(args)
		{
			$('#virbHud').css({"visibility": "visible"});
			$("#virbHud iframe").attr({"height":args.height,"width":args.width}).css({"overflow": "hidden", "visibility": "visible"});	
			
			$("#virbHud iframe").slideDown();	
		},
		updateText: function(args)
		{
			$(unescape(decodeURIComponent(args.selector))).html(unescape(decodeURIComponent(args.html)));
		},
		updateFollow: function(args)
		{
			if (args.status == 'ok')
			{
				$('#virbHud #hudTitle').html(args.message);
			}

			// alert('update follow');
			// window.console.log(args);
		},
		updateImage: function(args)
		{	
			var selector = unescape(decodeURIComponent(args.selector));
			var url = unescape(decodeURIComponent(args.url));

			$(selector + ' .virb_customize_loading').css({"display":"block"});

			// create an image object
			objImage = new Image();
			// preload the image file
			objImage.src=url;
			
			// set what happens once the image has loaded
			objImage.onload = function()
			{
				
				if ($(selector+' img').length > 0)
				{
					$(selector+' img').attr({"src":url});   
				}                                                      
				else
				{
					$(selector).html('<img src="'+url+'" />');
				}
				
				$(selector + ' .virb_customize_loading').css({"display":"none"});				
			};
		},
		updateBackstretch: function(args)
		{
			var url = unescape(decodeURIComponent(args.url));
			
			// $('#backstretch-wrap img').attr('src', url);
			$.backstretch.init(url);
		},
		deleteImage: function(args)
		{	
//			window.console.log('deleteImage');
			
			if (args.backstretch)
			{
				// window.console.log('backstretch');
				// $('#backstretch-wrap img').attr('src', args.url);
				$.backstretch.init(args.url);				
			}
			else
			{
				// window.console.log('regular');
				$('#virb_customize_'+args.selector).html('<span class="virb_customize_loading"></span>'+args.url);	
			}
		},
		updateCss: function(args)
		{		
			$('link[href^=http://judo.virb.com/css]').attr('href', 'http://judo.virb.com/css?'+Math.random());
		},
		updatePageSiteCss: function(args)
		{
			var $styles = $('#customizeSiteStyles');

			if ($styles.length > 0)
			{
				$styles.remove();
				$('body').append('<style id="customizeSiteStyles">'+unescape(decodeURIComponent(args.site_css))+'</style>');
				// window.console.log('updating styles');
			}
			else
			{
				// window.console.log('appending styles');
				$('body').append('<style id="customizeSiteStyles">'+unescape(decodeURIComponent(args.site_css))+'</style>');
			}
			
			var $pageStyles = $('#customizePageStyles');
			if ($pageStyles.length > 0)
			{
				$pageStyles.remove();
				$('body').append('<style id="customizePageStyles">'+unescape(decodeURIComponent(args.page_css))+'</style>');
			}
			else
			{
				// window.console.log('appending styles');
				$('body').append('<style id="customizePageStyles">'+unescape(decodeURIComponent(args.page_css))+'</style>');
			}		
						
			$('link[href^=http://judo.virb.com/welcome/css]').remove();	
		},
		attachAudio: function(args)
		{
			if (!$.audio)
			{
				this.addScript('http://virb.com/_assets/_js/media.js');
				this.addScript('http://virb.com/_assets/_js/audio.js');
			}
		}
	};
	
	$(document).ready(function()
	{
		$.site.init();
	});
	  
})(jQuery);

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-4266223-3']);
  _gaq.push(['_setDomainName', 'none']);
  _gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
