var emptyQuestionBox = 'Type your question here...';

var Prefix;

$(function(){

	$('#ask-question-details').focus(
		function() {
			if($(this).val() == emptyQuestionBox) {
				$(this).val('');
			}
		}
	).blur(
		function() {
			if($(this).val() == '') {
				$(this).val(emptyQuestionBox);
			}
		}
	).val(emptyQuestionBox);


	// click on side box titles
	$(".side-results .grey-title, .side-results .blue-title").click(function(){
		var boxTitle = $(this).parent();
		var boxContent = $(".box-content", boxTitle.parent());

		if (boxContent.css('display') == 'none') {
			boxContent.show();
			$(".box-title-hide", boxTitle).removeClass("box-title-hide").addClass("box-title-show");
		} else {
			boxContent.hide();
			$(".box-title-show", boxTitle).removeClass("box-title-show").addClass("box-title-hide");
		}
	});


	if($('img.thumbnailPreview').length) {

		$('img.thumbnailPreview').imgPreview({
			containerID: 'imgPreviewWithStyles',
			// When container is shown:
			onShow: function(link){
				// Animate link:
				$(link).stop().animate({opacity:0.4});
				// Reset image:

				$('img', this).stop().css({opacity:0});
			},
			// When image has loaded:
			onLoad: function(link){
				$(this).animate({opacity:1}, 300);
				previewPosition(container, mouseX, mouseY);
			},
			// When container hides:
			onHide: function(link){
				// Animate link:
				$(link).stop().animate({opacity:1});
			},

			srcAttr: 'src'
		});
	}

	if (typeof screenshotterIp == 'undefined') {
		screenshotterIp = '38.101.43.11'
	}

	if(typeof filePrefix == 'undefined' || filePrefix == 1) {
		Prefix = 'http://' + screenshotterIp + '/screenshot2.php?';
	} else {
		Prefix = 'http://' + screenshotterIp + '/screenshot.php?';
	}

	Prefix += ((document.location.toString().search(/Reload=true/i) != -1)? 'Reload=true&': '') + 'Url=';

	$('a.thumbnailPreviewLink').find('img').attr('src', ((typeof LoaderPath != 'undefined')? LoaderPath: '/images/loader80stopped.gif'));
	LoadScreenshots(true);


	//Bookmark

	if(window.opera) {
		if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
			$("a.jqbookmark").attr("rel","sidebar");
		}
	}

	$("a.jqbookmark").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		var url = this.href;
		var title = this.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			alert('Unfortunately, this browser does not support the requested action,'
			+ ' please bookmark this page manually.');
		}

	});

});

var ScreenshotImages = new Array();

function LoadScreenshots(Indexing) {
	var Index = 0
	$('a.thumbnailPreviewLink').each(
		function () {
			Index ++;

			url = (typeof filePrefix == 'undefined' || filePrefix == 1) ? encodeURIComponent($(this).attr('rel').toLowerCase()) : $(this).attr('rel');
			img = $(this).find('img');

			//console.log("loading")
			img.attr('Index', Index).attr('Loaded', false);
			img.attr('PreviewSrc', url),
			ScreenshotImages[Index] = new Image();
			ScreenshotImages[Index].Index = Index;
			ScreenshotImages[Index].src = Prefix + url + '&Dimensions=' + ((typeof Dimensions != 'undefined')? Dimensions: '80x60') + '&Rand=' + Math.random();
			ScreenshotImages[Index].PreviewSrc = url;


			ScreenshotImages[Index].onload = function () {
				//if (document.location.toString().search('debugview') != -1) {
				//	alert('thumbnail is loaded')
				//}

				$("img.thumbnailPreview[@Index='" + this.Index + "']").attr('Loaded', true).addClass('Loaded').attr('src', this.src).attr('PreviewSrc', this.PreviewSrc);
				$("img.thumbnailPreview2[@Index='" + this.Index + "']").attr('Loaded', true).addClass('Loaded').attr('src', this.src).attr('PreviewSrc', this.PreviewSrc); // for affsites.php
			}
		}
	);

	setTimeout("LoadScreenshots2(false)", 5000)
}


function LoadScreenshots2(Indexing) {
	$('img.thumbnailPreview[@Index][@PreviewSrc][@Loaded=false]').each(
		function () {
			//console.log("loading")
			var Index = $(this).attr('Index');

			delete ScreenshotImages[Index];
			ScreenshotImages[Index] = new Image();
			ScreenshotImages[Index].Index = Index;
			ScreenshotImages[Index].src = Prefix + $(this).attr('PreviewSrc') + '&Dimensions=' + ((typeof Dimensions != 'undefined')? Dimensions: '80x60') + '&Rand=' + Math.random();
			ScreenshotImages[Index].onload = function () {
				$("img.thumbnailPreview[@Index='" + this.Index + "']").attr('Loaded', true).addClass('Loaded').attr('src', this.src).attr('PreviewSrc', this.PreviewSrc);
			}
		}
	)

	setTimeout("LoadScreenshots2(false)", 5000)
}


function changeLocation(e, link)
{
	if( (!$.browser.msie && e.button == 0) || ($.browser.msie && e.button == 1) ) {
		location.href = link;
	}
}
