var getStyleProperty = (function(){
 
  function getStyleProperty(propName, element) {
    element = element || document.documentElement;
    var style = element.style,
        prefixed;
 
    // test standard property first
    if (typeof style[propName] == 'string') return propName;
 
    // capitalize
    propName = propName.charAt(0).toUpperCase() + propName.slice(1);
 
    }
 
  return getStyleProperty;
})();

var this_domain = 'kensingtonband.com';


$(document).ready(function() {
		setBorderImages();
		preparelinks(this_domain);
		shadowboxWrapImages();
		Shadowbox.init({animSequence:'sync'});	
		setSocialOpacity();
		
		//tourdatesBehavior()


		
});

function setBorderImages() {
	var boxes = $('#twitter, #recent, #music, #video, #blog_top, #blog_bottom, #shows, #contact');
	
	/* If border-image works out-of-the-box (Opera 10.5 for example) */
	if (typeof getStyleProperty('borderImage') == 'string') {
		/* ...set standard CSS3 */
		boxes.css('border-image','url("/img/shade_bottom_right.png") 1 6 7 2');
	} else {
		/* ... or else, run the script for IE, Firefox, Safari and Chrome */
		boxes.borderImage('url("/img/shade_bottom_right.png") 1 6 7 2');
	}

	/* Either way, set the right border width */
	boxes.css('border-width','1px 6px 7px 2px');	
}

function setSocialOpacity() {
	
	function toDefault() {
		$('#hookup li').css('opacity','0.6');
		$('#myspace').css('opacity','0.5');
	}
	
	toDefault();
	
	$('#hookup li').hover(
		function() {
			$(this).css('opacity','0.9');
		},
		toDefault
	);		
	
	$('#hookup').show();
}

function tourdatesBehavior() {
	$(".more_info a").click(function () {
	 $(".tourdates").hide("slide", { direction: "left" }, 100);
	 $(".location_td").hide();
	 $(".tourdates").show("slow");
	});
}
function preparelinks(this_domain) {
  var lnks = document.getElementsByTagName("a");
  for (var i=0; i<lnks.length; i++) {
if ((lnks[i].className.indexOf("newwindow") != -1) || (lnks[i].href.indexOf('mailto:') == -1 && lnks[i].href.indexOf(this_domain) == -1 && lnks[i].getAttribute('rel') != 'shadowbox'))  {
      lnks[i].onclick = function() {
        popUp(this.getAttribute("href"),this.className);
        return false;
      }
    }
  }
}
	
function popUp(href,className) {
   var className = (className == null) ? Array() : className;
    carr = className.split(' ');
    if(carr.length > 2) {
    		carr[1] = parseInt(carr[1]) + 40;
    		carr[2] = parseInt(carr[2]) + 40;
    		window.open(href,'','width='+carr[1]+',height='+carr[2]+'');
    } else {
    		window.open(href);
    }
}


function shadowboxWrapImages() {
	
	$('#blog img').each(function() {
		var src = $(this).attr('src');
		var alt = $(this).attr('alt');
		var directory = src.match(/^.*[\/\\]/);
		var basename = src.replace(directory, '');
		var full_size_filename = basename.replace('t','');
		var href = directory+full_size_filename;
		$(this).wrap('<a class="thumb" href="'+href+'" rel="shadowbox" title="'+alt+'"></a>');
	});
}

var twitter_load_opts = { 
  id: 'kensingtonband', 
  count: 20,
  page: 1,
  enableLinks: true, 
  ignoreReplies: false, 
  clearContents: true,
  template: '%text%<br><a class="twitter_permalink" href="http://twitter.com/%user_screen_name%/statuses/%id%/">(%time%)</a>',
  callback: renderTwitter
}; 

/* bool var to be set when all click events are set up */
var twitter_events_initiated = false;

/* last direction in which was skipped through the pagination */
var twitter_last_pagination_direction = 'backwards';


function renderTwitter() {
	addTwitPics();
	renderTwitterBox();
	
	/* open links in new window */
	$('#twitter_content a').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	/* don't duplicate click events */
	if (!twitter_events_initiated) twitter_events_initiated = true;
}

function addTwitPics() {
	$('#twitter_content li').each(function(){
		var this_li = $(this); // because injectImage needs to access the current li element
		function injectImage(match,url) {
			/* shrink the tweet text */
			this_li.css({
				'font-size': '11px',
				'width': '130px',
				'top': '-29px'
			});
			
			/* return the img element for replacement */
			return '<img src="http://twitpic.com/show/mini/' + url + '"></a>';
		}
		
		/* swap link text for img elemnt, and chop off " - " hyphen thing */
		$(this).html($(this).html().replace(/http:\/\/twitpic\.com\/([a-z0-9]*)<\/a> - /g,injectImage));
	});
}

function renderTwitterBox() {
	
	if (twitter_last_pagination_direction == 'backwards') {
		var first_or_last = 'first';
		
	} else {
		var first_or_last = 'last';
		
	}
	
	current = $('#twitter_content li:' + first_or_last);
	
	current
		.css('display','block');
	
		
	
		
	if (!twitter_events_initiated) {	
	 
		/* up down nav */
		$('#twitter_content').after('<ul id="twitternav"><li id="tweetup" title="Newer tweet"><a href="#">up</a></li><li id="tweetdown"><a href="#" title="Older tweet">down</a></li></ul>');
		$('#tweetup').css('display','none');
		
		if (current.next().length == 0) $('#tweetdown').css('display','none');
		
		/* up down behavior */	
		$('#tweetdown a').click(function(){
			if (current.next().length == 0) {
				/* Reload with older set */
				$(this).unbind('click.skipbackpage'); /* If already set, unbind first */
				twitter_load_opts.page++;
				getTwitters('twitter_content', twitter_load_opts);
				twitter_last_pagination_direction = 'backwards';
			} else {
				current.css('display','none');
				current = current.next();
				current.css('display','block');
				if ($('#tweetup').css('display') == 'none') $('#tweetup').css('display','block');
				
				if (current.next().length == 0) { /* note: this is the next-next one! */
					$('#tweetdown a').bind('click.skipbackpage',function() {
						twitter_load_opts.page++;
						getTwitters('twitter_content', twitter_load_opts);
						twitter_last_pagination_direction = 'backwards';
						$(this).unbind('click.skipbackpage');
					});
				}
			}
			return false;
			
		});
		
		$('#tweetup a').click(function(){
			if (current.prev().length == 0 && twitter_load_opts.page > 1) {
					/* Reload with newer set */
					$(this).unbind('click.skipforwardpage'); /* If already set, unbind first */
					twitter_load_opts.page--;
					getTwitters('twitter_content', twitter_load_opts);
					twitter_last_pagination_direction = 'forwards';
				
			} else {
				current.css('display','none');
				current = current.prev();
				current.css('display','block');
			
				if (current.prev().length == 0) { /* note: this is the pre-previous one! */
					if (twitter_load_opts.page > 1) {
						$('#tweetup a').bind('click.skipforwardpage',function() {
							twitter_load_opts.page--;
							getTwitters('twitter_content', twitter_load_opts);
							twitter_last_pagination_direction = 'forwards';
							$(this).unbind('click.skipforwardpage');
						});
					} else {
						$('#tweetup').css('display','none');
					}
				}
			} 
			
			
			return false;
			
		});
	}
}

getTwitters('twitter_content', twitter_load_opts);
	