var dom = 'http://' + document.domain;

function bookmark_us(){
	var title = document.title; 
	var url = window.location.href;

	if (window.sidebar){ // firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all){// ie
		window.external.AddFavorite(url, title);
	}
}

function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=270,height=600,left = 362,top = 100');");
}

function doSearch(t,f){
	if(!f) f=1;
	var q = document.getElementById('q'+f).value;
	q = q.replace(/([^a-zA-Z ]+)/g, '').replace(' ','-');

	if(t=="video"){
		location.href="http://www.cleancafe.com/search/"+q+".html";
	}else{
		location.href="http://www.cleancafe.com/"+t+"/"+q+".html";
	}
}

function relVid(q,t){
	var html = "html";
	if(typeof(t)=="undefined"){
		location.href=dom+"/search/"+q+"."+html;
	}else{
		location.href=dom+"/"+t+"/"+q+"."+html;
	}
}

$(function() {

	$("#videosBtn").bind("click",function() {
		doSearch('video',1);
		return false;
	});
	$("#moviesBtn").bind("click",function() {
		doSearch('movies',1);
		return false;
	});
	$("#picsBtn").bind("click",function() {
		doSearch('pictures',1);
		return false;
	});
	$("#videosBtn").bind("click",function() {
		doSearch('video',2);
		return false;
	});
	$("#moviesBtn").bind("click",function() {
		doSearch('movies',2);
		return false;
	});
	$("#picsBtn").bind("click",function() {
		doSearch('pictures',2);
		return false;
	});


	$(".hoverimg").hover(function() {
		$(this).css({'z-index' : '10'});
		$('#'+$(this).attr('id').replace('h','s')).css('visibility','hidden');
		//$(this).css({'position' : 'absolute'});
		
		//$(this).addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
		$(this).animate({
				marginTop: '-23px', /* The next 4 lines will vertically align this image */ 
				marginLeft: '-60px',
				top: '50%',
				left: '50%',
				width: '180px', /* Set new width */
				height: '135px' /* Set new height */
			}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
			
			switchThumb(this,true);

		} , function() {
		$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
		//$(this).css({'position' : 'relative'});
		$(this).animate({
				marginTop: '0', /* Set alignment back to default */
				marginLeft: '0',
				top: '0',
				left: '0',
				width: '120px', /* Set width back to default */
				height: '90px' /* Set height back to default */
			}, 400, '', function() {
					$('#'+$(this).attr('id').replace('h','s')).css('visibility','visible');
				}
		);
		resetThumb(this,false);
		
	});
});

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		//if(this.className=='shadecols'){
		//	alert(tallest)
			//tallest = (parseInt(tallest.replace('px','')) - 10)+'px';
		//}
		return this.each(function() {
			$(this).height(tallest);//.css("overflow","auto");
		});
	}
})(jQuery);


var ads = [],ct = 0,cur = 0,t, src, id;
var big = false;
function switchThumb(o,biggy) {
	big = biggy;
    t = 0;
    if (o) {
        id = o.id;
        if (typeof(himg) != 'undefined') {
            id = id.replace('h', '');
            src = himg[id];
            t = 3
        }
        if (t != 0) {
            cur++;
            BNexecLoop(o, cur, 1)
        }
    }
}

function BNexecLoop(o, b, a) {
    var n;
    if (o && cur == b) {
        try {
			if(himg[id].length==1) return;
            n = (ct + 1) % src.length;
            if (ads[n] && (ads[n].complete || ads[n].complete == null)) {
                o.src = ads[ct = n].src
            }
            ads[n = (ct + 1) % src.length] = new Image;
            ads[n].onerror = function () {
                resetThumb(o)
            };
			
			if(!big){
				ads[n].src = dom + '/tube/images/' + id + '/' + src[n] + '.jpg';
			}else{
				ads[n].src = dom + '/tube/images/' + id + '/' + src[n] + '_big.jpg';
			}
            if (a == 1) {
                BNexecLoop(o, b);
            } else {
                setTimeout(function () { BNexecLoop(o, b) },500)
            }
        } catch(e) {}
    }
}

function resetThumb(o,biggy) {
	if(himg[id].length==1) return;
	big = biggy;
    try {
		if(!big){
			o.src = dom + '/tube/images/' + id + '/' + himg[id][0] + '.jpg'
		}else{
			o.src = dom + '/tube/images/' + id + '/' + himg[id][0] + '_big.jpg'
		}
    } catch(e) {}
    ct = 0;
    ads = [],prev = '',cur++
}

function share() {
	var title = document.title; 
	var url = window.location.href;
	var object = SHARETHIS.addEntry({
		title: title,
		url: url
	});
	object.attachButton(document.getElementById('anchor'));
	object.popup();
}
$(document).ready(function() {
	$(".infoBtn").hover(
		function(){
			$(this).parent('div').find('.tooltip').hover(
				function(){
					
				},
				function(){
					$(this).parent('div').find('.img').css('border-color','#1b1b1b');
					$(this).fadeOut('fast');
				}
			);			
			$(this).parent('div').find('.tooltip').slideDown('fast');
			$(this).parent('div').find('.img').css('border-color','#545354');
		},
		function(){
			//
		}
	);
});
		
