/* Mobile redirects */
/*
if (/mobile|blackberry|j2me|windows ce/i.test(navigator.userAgent)) {
  window.location = "http://mobile.naughty.com";
}
*/

/* Movies namespace */
Drupal.movies = {}

/* Tabs */
Drupal.movies.enableTabs = function () {
  var tabs = $('<ul class="primary"></ul>')
  var ids = ['#comments', '#block-blockcache-12']
	for (var i in ids) {
		var tab = $(ids[i])
		if (tab.length == 1) {
			tab.addClass('block-tab')
			var id = tab.attr('id')
			var title = $('h2:first', tab).text()
			$('<li><a href="#' + id + '"' + '>' + title + '</a></li>').appendTo(tabs)
		}
	}
	if ($('li', tabs).length > 1) {
		tabs.insertAfter('.node-video-full')
		tabs.idTabs()
	}
}

/* Carousel */
Drupal.movies.enableSlides = function () {
	var block = $('#block-movies-random_favorites .content, #block-blockcache-13 .content')
	var nodes = $('.node', block).fadeOut(0).css({position: 'absolute'})
	nodes.active = 0;
	nodes.eq(nodes.active).fadeIn(300)
	setInterval(function() {
		nodes.eq(nodes.active).fadeOut(1000)
		nodes.active = (nodes.active + 1) % nodes.length;
		nodes.eq(nodes.active).fadeIn(1000);
	}, 7500);
}

/* Previews */
Drupal.movies.enablePreview = function() {
	var thumbnails = $('.node .thumbnail img').each(function() {
		var range = $(this).attr('class').match(/thumbs-(\d+)/)
		if (range) {
			this.thumbcount = range[1]
			$(this)
				.mouseover(function() {
					image = this
					if(!this.thumbs) {
						this.thumbs = []
						this.original = this.src
						setTimeout(function() {
							var cacheImg = new Image()
							var path = image.original.substr(0, image.original.lastIndexOf('/')+1)
							for(i = 0; i < image.thumbcount; ++i) {
								cacheImg.src = path + i + '.jpg'
								image.thumbs.push(cacheImg.src)
							}
							image.thumbcount = null
						},0);
					}
					this.thumbno = 0
					this.timer = setInterval(function () {
						if(image.thumbs.length > 0) {
							var cacheImg = new Image()
							cacheImg.src = image.thumbs[image.thumbno]
							if(cacheImg.width > 0) {
								image.src = cacheImg.src 
							}
							image.thumbno = (image.thumbno + 1) % image.thumbs.length 
						}
					},750);
				})
				.mouseout(function() {
					clearInterval(this.timer)
					this.src = this.original
				})
		}
	})
}
Drupal.movies.fastSearch = function() {
  $('form#search-theme-form').submit(function() {
  	window.location='/movies/search/node/' + escape($('#edit-search_theme_form_keys', this).val()).replace(/%20/g, '+')
  	return false
  });
  $('form#search-form').submit(function() {
  	window.location='/movies/search/node/' + escape($('#edit-keys', this).val()).replace(/%20/g, '+')
  	return false
  });
  $('form#sphinx-search-form').submit(function() {
  	window.location='/movies/search/node/' + escape($('#edit-searchstring', this).val()).replace(/%20/g, '+')
  	return false
  });
}

Drupal.movies.socialSites = {
	"Reddit": [
		"http://reddit.com/r/nsfw/", 
		"http://reddit.com/r/nsfw", 
		"http://www.reddit.com/r/nsfw/", 
		"http://www.reddit.com/r/nsfw", 
		"http://reddit.com/r/pornography/", 
		"http://reddit.com/r/pornography", 
		"http://www.reddit.com/r/pornography/", 
		"http://www.reddit.com/r/pornography", 
		"http://reddit.com/r/Boobies/", 
		"http://reddit.com/r/sex/", 
		"http://reddit.com/r/ass/", 
		"http://reddit.com/r/Sexy/"]
}
 
Drupal.movies.socialButtons = function() {
	//var v = SocialHistory(Drupal.movies.socialSites)
	// if ( v.doesVisit("Reddit") ) {
	if (false) {
		htmlcode = '<a href="http://www.reddit.com/submit" onclick="window.location=\'http://www.reddit.com/submit?url=\'+encodeURIComponent(window.location); return false"><img src="http://www.reddit.com/static/spreddit14.gif" alt="submit to reddit" border="0" /></a>'
	} else {
		htmlcode = '<script type="text/javascript">var backlinxxx_pid=610</script>' + 
			'<script type="text/javascript" src="http://cdn.backlinxxx.com/backlinxxx/js/widget.js?pid=610"></script>' + 
			'<a href="http://www.backlinxxx.com/share.php" onclick="backlinxxx_url=location.href; backlinxxx_title=document.title; return backlinxxx_share(this);" target="_blank"><img src="http://cdn.backlinxxx.com/backlinxxx/images/hb6.gif?pid=610" border="0" alt="backlinxxx Social Bookmark Button"/></a>'
	}
	$('div.socialButtons').html(htmlcode);
}

// Global Killswitch
if (Drupal.jsEnabled) {
	$(Drupal.movies.enableTabs)
	$(Drupal.movies.enableSlides)
//	$(Drupal.movies.enablePreview)
	$(Drupal.movies.fastSearch)
//	$(Drupal.movies.socialButtons)
}

