// ----------------------------------------------------------------------------
// show/hide
// ----------------------------------------------------------------------------

// show an element
function show(id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		el.style.display = "block";
	}
}
// hide an element
function hide(id) {
	if (document.getElementById) {
		var el = document.getElementById(id);
		el.style.display = "none";
	}
}

// ----------------------------------------------------------------------------
// forms
// ----------------------------------------------------------------------------

// clear default value in forms
function doClear(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = "";
	}
}
// undo clear of default value
function undoClear(theText) {
	if (theText.value == "") {
		theText.value = theText.defaultValue;
	}
}

// ----------------------------------------------------------------------------
// links and popups
// ----------------------------------------------------------------------------

// open external links in a new window ("target" is not valid XHTML strict)
function externallinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		anchor.tabindex = i;
		if (anchor.getAttribute("href")) {
			if (anchor.getAttribute("rel") == "external")
				anchor.target = "_blank";
			else if (anchor.getAttribute("rel") == "parent")
				anchor.target = "_parent";
			else if (anchor.getAttribute("rel") == "top")
				anchor.target = "_top";
			else if (anchor.getAttribute("rel") == "popup")
                $('a[rel="popup"]').click(function(){return $.dbPopWin( $(this).attr('href'));}
				);	
		}
	}
}
jQuery.fn.popupwindow = function(p)
{
	var profiles = p || {};
	return this.each(function(index){
		var settings, parameters, mysettings, b, a;
		// for overrideing the default settings
		mysettings = (jQuery(this).attr("rel") || "").split(",");
		settings = {
			height:600, // sets the height in pixels of the window.
			width:600, // sets the width in pixels of the window.
			toolbar:0, // determines whether a toolbar (includes the forward and back buttons) is displayed {1 (YES) or 0 (NO)}.
			scrollbars:0, // determines whether scrollbars appear on the window {1 (YES) or 0 (NO)}.
			status:0, // whether a status line appears at the bottom of the window {1 (YES) or 0 (NO)}.
			resizable:1, // whether the window can be resized {1 (YES) or 0 (NO)}. Can also be overloaded using resizable.
			left:0, // left position when the window appears.
			top:0, // top position when the window appears.
			center:0, // should we center the window? {1 (YES) or 0 (NO)}. overrides top and left
			createnew:1, // should we create a new window for each occurance {1 (YES) or 0 (NO)}.
			location:0, // determines whether the address bar is displayed {1 (YES) or 0 (NO)}.
			menubar:0 // determines whether the menu bar is displayed {1 (YES) or 0 (NO)}.
		};
		// if mysettings length is 1 and not a value pair then assume it is a profile declaration
		// and see if the profile settings exists
		if(mysettings.length == 1 && mysettings[0].split(":").length == 1)
		{
			a = mysettings[0];
			// see if a profile has been defined
			if(typeof profiles[a] != "undefined")
			{
				settings = jQuery.extend(settings, profiles[a]);
			}
		}
		else
		{
			// overrides the settings with parameter passed in using the rel tag.
			for(var i=0; i < mysettings.length; i++)
			{
				b = mysettings[i].split(":");
				if(typeof settings[b[0]] != "undefined" && b.length == 2)
				{
					settings[b[0]] = b[1];
				}
			}
		}
		// center the window
		if (settings.center == 1)
		{
			settings.top = (screen.height-(settings.height + 110))/2;
			settings.left = (screen.width-settings.width)/2;
		}
		parameters = "location=" + settings.location + ",menubar=" + settings.menubar + ",height=" + settings.height + ",width=" + settings.width + ",toolbar=" + settings.toolbar + ",scrollbars=" + settings.scrollbars  + ",status=" + settings.status + ",resizable=" + settings.resizable + ",left=" + settings.left  + ",screenX=" + settings.left + ",top=" + settings.top  + ",screenY=" + settings.top;
		jQuery(this).bind("click", function(){
			var name = settings.createnew ? "PopUpWindow" + index : "PopUpWindow";
			window.open(this.href, name, parameters).focus();
			return false;
		});
	});
};
// jumpmenu (macromedia)
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/* TextLimit - jQuery plugin for counting and limiting characters for input and textarea fields
 * pass '-1' as speed if you don't want slow char deletion effect. (don't just put 0)
 * Example: jQuery("Textarea").textlimit('span.counter',256)
 * $Version: 2007.10.24 +r1
 * Copyright (c) 2007 Yair Even-Or
 */
jQuery.fn.textlimit=function(counter_el, thelimit, speed) {
	var charDelSpeed = speed || 15;
	var toggleCharDel = speed != -1;
	var toggleTrim = true;	
	var that = this[0];
	updateCounter();	
	function updateCounter(){jQuery(counter_el).text(thelimit - that.value.length);};	
	this.keypress (function(e){ if( this.value.length >= thelimit && e.charCode != '0' ) e.preventDefault() })
	.keyup (function(e){
		updateCounter();
		if( this.value.length >= thelimit && toggleTrim ){
			if(toggleCharDel){
				// first, trim the text a bit so the char trimming won't take forever
				that.value = that.value.substr(0,thelimit+100);
				var init = setInterval
					(function(){if(that.value.length <= thelimit){ init = clearInterval(init); updateCounter()}
					else{that.value=that.value.substring(0,that.value.length-1); jQuery(counter_el).text('trimming...  '+(thelimit - that.value.length)); };
						} ,charDelSpeed 
					);}
			else this.value = that.value.substr(0,thelimit);}
	});	
};

/* Common functions */

//PAGES
function pageOpen() {
	// popup windows
	$(".popupwindow").popupwindow();
	// subnavigation
	$(".subnav").slideUp('slow'); /* hide subnav if js is on */
	$("#nav a").hover(function() {
		$(this).parent().find(".subnav").stop().slideDown('fast').show('slow', function(){$(this).height("auto");});
		$(this).parent().hover(function() {}, function(){$(this).parent().find(".subnav").stop().slideUp('slow');});
		// TODO: any way to simplify/clean up?
	})
	// follow us
	$('#followus').attr({openstate:'closed'});
	$('#trigger').click(function() {
		if ($('#followus').attr('openstate')=='closed'){
			$('#followus').animate({top: 0}, 500).attr({openstate:'open'});
			$('#trigger').addClass('close');
		}
		else {
			$('#followus').animate({top:'-300px'}, 500).attr({openstate:'closed'});
			$('#trigger').removeClass('close');
		};
	});
};
function pageClose() {
    Cufon.replace('#nav li a',{hover:true});
	Cufon.replace('h2');
	Cufon.replace('h3',{hover:true});
	Cufon.replace('h4');
	Cufon.replace('.button',{hover:true});
	Cufon.now();
	$('.thumb').fadeTo(0,0.8);
	$('.thumb').hover(function(){$(this).stop().fadeTo(0,1)},function(){$(this).stop().fadeTo(500,0.8)});
	$('#share').bookmark({});
};
// POPUPS
function pageOpenPopup() {};
function pageClosePopup() {
	Cufon.replace('h2');
	Cufon.replace('h3',{hover:true});
	Cufon.replace('h4');
	Cufon.replace('.button',{hover:true});
	Cufon.now();
	$('#share').bookmark({});
};
// SPLASH
function pageOpenSplash() {};
function pageCloseSplash() {
    Cufon.replace('h2');
	Cufon.replace('#venues dt',{hover:true});
    Cufon.now();
};

// switch bed sheets issue (issuu)
function switchIssue() {
    var flashvars = {mode: "embed",layout:"http%3A%2F%2Fwww.bedsupperclub.com%2Fissuu%2Flayout.xml",loadingInfoText: ""};
	var params = {allowfullscreen:"true",menu:"false"};
	var documentId=$(this).anchor.getAttribute("rel")
    swfobject.embedSWF("http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf", "flashcontent", "778", "600", "9.0.0","expressInstall.swf", flashvars, documentId, params);
}

// open the djprofile page
function openDJ(dj_id) {
	document.location = "/acts/bio.php?id="+dj_id;
}

// show subscribe form in shadowbox
function openSubscribe(f) {
	var u = f.action+"?email="+f.elements["email"].value;
	if (Shadowbox) {
		if ($('#followus').attr('openstate')!='closed'){
			$('#followus').animate({top:'-300px'}, 500).attr({openstate:'closed'});
			$('#trigger').removeClass('close');
		}
		Shadowbox.open({
			content:    u,
			player:     "iframe",
			width:      860
		});
		return false;
	}
}
/* http://keith-wood.name/bookmark.html
   Sharing bookmarks for jQuery v1.1.4.
   Written by Keith Wood (kbwood{at}iinet.com.au) March 2008.
   Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and 
   MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses. 
   Please attribute the author if you use it. */
(function($){var q='bookmark';function Bookmark(){this._defaults={url:'',title:'',sites:['delicious','digg','facebook',,'myspace','twitter'],icons:'/css/p/bookmarks_bw.png',iconSize:16,iconCols:16,target:'_blank',compact:true,hint:'share this page via {s}',popup:true,popupText:'share',addFavorite:false,favoriteText:'Favorite',favoriteIcon:0,addEmail:true,emailText:'Email',emailIcon:1,emailSubject:'Bed Supperclub: Interesting page',emailBody:'Hi,\n\nI thought you might find this page interesting:\n\n{t} ({u})',manualBookmark:'Please close this dialog and\npress Ctrl-D to bookmark this page.'};this._sites={'delicious':{display:'del.icio.us',icon:7,url:'http://del.icio.us/post?url={u}&amp;title={t}'},'digg':{display:'Digg',icon:8,url:'http://digg.com/submit?phase=2&amp;url={u}&amp;title={t}'},'facebook':{display:'Facebook',icon:11,url:'http://www.facebook.com/sharer.php?u={u}&amp;t={t}'},'google':{display:'Google',icon:16,url:'http://www.google.com/bookmarks/mark?op=edit&amp;bkmk={u}&amp;title={t}'},'linkedin':{display:'LinkedIn',icon:66,url:'http://www.linkedin.com/shareArticle?mini=true&amp;url={u}&amp;title={t}&amp;ro=false&amp;summary=&amp;source='},'livejournal':{display:'LiveJournal',icon:19,url:'http://www.livejournal.com/update.bml?subject={u}'},'myspace':{display:'MySpace',icon:25,url:'http://www.myspace.com/Modules/PostTo/Pages/?u={u}&amp;t={t}'},'n4g':{display:'N4G',icon:56,url:'http://www.n4g.com/tips.aspx?url={u}&amp;title={t}'},'netlog':{display:'NetLog',icon:101,url:'http://www.netlog.com/go/manage/links/view=save&amp;origin=external&amp;url={u}&amp;title={t}'},'netscape':{display:'Netscape',icon:26,url:'http://www.netscape.com/submit/?U={u}&amp;T={t}'},'netvibes':{display:'Netvibes',icon:102,url:'http://www.netvibes.com/share?url={u}&amp;title={t}'},'newsvine':{display:'Newsvine',icon:28,url:'http://www.newsvine.com/_wine/save?u={u}&amp;h={t}'},'plaxo':{display:'Plaxo Pulse',icon:105,url:'http://www.plaxo.com/pulse/?share_link={u}'},'reddit':{display:'reddit',icon:30,url:'http://reddit.com/submit?url={u}&amp;title={t}'},'slashdot':{display:'Slashdot',icon:33,url:'http://slashdot.org/bookmark.pl?url={u}&amp;title={t}'},'stumbleupon':{display:'StumbleUpon',icon:36,url:'http://www.stumbleupon.com/submit?url={u}&amp;title={t}'},'technorati':{display:'Technorati',icon:38,url:'http://www.technorati.com/faves?add={u}'},'tumblr':{display:'tumblr',icon:119,url:'http://www.tumblr.com/share?v=3&amp;u={u}&amp;t={t}'},'twitter':{display:'Twitter',icon:45,url:'http://twitter.com/home?status=Checking bed supperclub: {u}'},'windows':{display:'Windows Live',icon:40,url:'https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=en-us&amp;url={u}&amp;title={t}'},'wishlist':{display:'Amazon WishList',icon:123,url:'http://www.amazon.com/wishlist/add?u={u}&amp;t={t}'},'yahoobm':{display:'Yahoo Bookmarks',icon:60,url:'http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;u={u}&amp;t={t}'},'yahoobuzz':{display:'Yahoo Buzz',icon:67,url:'http://buzz.yahoo.com/submit?submitUrl={u}&amp;submitHeadline={t}'},'yahoo':{display:'Yahoo MyWeb',icon:41,url:'http://myweb2.search.yahoo.com/myresults/bookmarklet?u={u}&amp;t={t}'},'yoolink':{display:'yoolink',icon:126,url:'http://www.yoolink.fr/post/tag?f=aa&amp;url_value={u}&amp;title={t}'}}}$.extend(Bookmark.prototype,{markerClassName:'hasBookmark',setDefaults:function(a){extendRemove(this._defaults,a||{});return this},addSite:function(a,b,c,d){this._sites[a]={display:b,icon:c,url:d};return this},getSites:function(){return this._sites},_attachBookmark:function(a,b){a=$(a);if(a.hasClass(this.markerClassName)){return}a.addClass(this.markerClassName);this._updateBookmark(a,b)},_changeBookmark:function(a,b){a=$(a);if(!a.hasClass(this.markerClassName)){return}this._updateBookmark(a,b)},_updateBookmark:function(g,h){var i=$.data(g[0],q)||$.extend({},this._defaults);h=extendRemove(i,h||{});$.data(g[0],q,h);var j=h.sites;if(j.length==0){$.each(this._sites,function(a){j.push(a)})}var k=h.hint||'{s}';var l=(h.popup?'<a href="#" class="bookmark_popup_text">'+h.popupText+'</a><div class="bookmark_popup">':'')+'<ul class="bookmark_list'+(h.compact?' bookmark_compact':'')+'">';var m=function(a,b,c,d){var e='<li><a href="'+c+'"'+(d?' onclick="'+d+'"':(h.target?' target="'+h.target+'"':''))+'>';if(b!=null){var f=k.replace(/\{s\}/,a);if(typeof b=='number'){e+='<span title="'+f+'" style="background: '+'transparent url('+h.icons+') no-repeat -'+((b%h.iconCols)*h.iconSize)+'px -'+(Math.floor(b/h.iconCols)*h.iconSize)+'px;'+($.browser.mozilla&&$.browser.version<'1.9'?' padding-left: '+h.iconSize+'px; padding-bottom: '+(Math.max(0,h.iconSize-16))+'px;':'')+'"></span>'}else{e+='<img src="'+b+'" alt="'+f+'" title="'+f+'"'+(($.browser.mozilla&&$.browser.version<'1.9')||($.browser.msie&&$.browser.version<'7.0')?' style="vertical-align: bottom;"':($.browser.msie?' style="vertical-align: middle;"':($.browser.opera||$.browser.safari?' style="vertical-align: baseline;"':'')))+'/>'}e+=(h.compact?'':'&#xa0;')}e+=(h.compact?'':a)+'</a></li>';return e};var n=h.url||window.location.href;var o=h.title||document.title;if(h.addFavorite){l+=m(h.favoriteText,h.favoriteIcon,'#','jQuery.bookmark._addFavourite(\''+n.replace(/'/g,'\\\'')+'\',\''+o.replace(/'/g,'\\\'')+'\')')}if(h.addEmail){l+=m(h.emailText,h.emailIcon,'mailto:?subject='+encodeURIComponent(h.emailSubject)+'&amp;body='+encodeURIComponent(h.emailBody.replace(/{u}/,n).replace(/{t}/,o)))}n=encodeURIComponent(n);o=encodeURIComponent(o);var p=this._sites;$.each(j,function(a,b){var c=p[b];if(c){l+=m(c.display,c.icon,c.url.replace(/{u}/,n).replace(/{t}/,o))}});l+='</ul>'+(h.popup?'</div>':'');g.html(l);if(h.popup){$(g).find('.bookmark_popup_text').click(function(){var a=$(this).parent();var b=a.offset();$('.bookmark_popup',a).css('left',b.left).css('top',b.top+a.outerHeight()).toggle();return false});$(document).click(function(a){$('.bookmark_popup',g).hide()})}},_destroyBookmark:function(a){a=$(a);if(!a.hasClass(this.markerClassName)){return}a.removeClass(this.markerClassName).empty();$.removeData(a[0],q)},_addFavourite:function(a,b){if($.browser.msie){window.external.addFavorite(a,b)}else{alert(this._defaults.manualBookmark)}}});function extendRemove(a,b){$.extend(a,b);for(var c in b){if(b[c]==null){a[c]=null}}return a}$.fn.bookmark=function(a){var b=Array.prototype.slice.call(arguments,1);return this.each(function(){if(typeof a=='string'){$.bookmark['_'+a+'Bookmark'].apply($.bookmark,[this].concat(b))}else{$.bookmark._attachBookmark(this,a||{})}})};$.bookmark=new Bookmark()})(jQuery);