// This function executes once all the HTML is loaded.
// It fixes PNGs for Internet Explorer and applies a
// style to the first p element in divs with the class
// sm-content
function initiate() {
	var browser = navigator.appVersion.split('MSIE')
	var width;
	var height;
	if ((parseFloat(browser[1]) >= 5.5) && (document.body.filters)) {
		iePng();
		var isIe = true;
	}
	else {
		var isIe = false;
	}
/*	var contentDivs = document.getElementsByClassName('sm-content'); 
	for (var i = 0; i < contentDivs.length; i++) { 
		if (contentDivs[i].className.indexOf('sm-content') != -1) {
			var paragraphs = contentDivs[i].getElementsByTagName('p');
			if (paragraphs.length > 0) {
				if (paragraphs[0].className.indexOf('no-js') == -1) {
					paragraphs[0].className = paragraphs[0].className + ' sm-first-paragraph';
				}
			}
		}
	}*/
}

function boobs() {
	var answer = confirm("Are you over 18?")
	if (answer){
		window.location = "http://www.speedmonkey.net/view-gallery";
	}
	else{
		return -1;
	}
}

// Gets the style on an object including those set
// from CSS style sheets.
function getStyle(element) {
	var computedStyle;
	if (typeof document.defaultView != 'undefined' && typeof document.defaultView.getComputedStyle != 'undefined') {
		computedStyle = document.defaultView.getComputedStyle($(element), '');
	}
	else if (typeof $(element).currentStyle != 'undefined') {
		computedStyle = $(element).currentStyle;
	}
	else {
		computedStyle = $(element).style;
	}
	return computedStyle;
}

function loadFmk(id) {
    $('sm-fmk-' + id).innerHTNL = 'Loading...';
    new Ajax.Updater('sm-fmk-' + id, '/get-list-fmk.sm?id=' + id, {asynchronous:true});
}
// This function fixes PNGs for IE.
function iePng() {
	jQuery('img[@src$=.png]').ifixpng();
	/*
	// Fix HX's with background images.
	var allElements = document.all;
	for( var i=0; i < allElements.length; i++ ){
		var element = allElements[i];
		var bg	= element.currentStyle.backgroundImage;
		if (bg && bg.substring(bg.length-5, bg.length-2).toUpperCase() == "PNG") {
			if (bg.indexOf('/menu/') == -1) {
				//alert(element.className);
				var src = bg.substring(5,bg.length-2);
				element.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='crop')";
				element.style.backgroundImage = "url(/css/blank.gif)";
			}
		}
	}
	for(var i=0; i<document.images.length; i++) {
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
			img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.src + "', sizingMethod='scale');" ;
			width = img.width;
			height = img.height;
			img.src = '/css/blank.gif';
			img.height = height;
			img.width = width;
		}
	}
	*/
}

// Select an item in a dropdown list
function selectDropdown(value, dropdownId) {
	dropdown = document.getElementById(dropdownId);
	for (var i = 0; i < dropdown.options.length; i++) {
		if (dropdown.options[i].value == value) {
			dropdown.options[i].selected = true;
		}
	}
}

// Rate an item
function rateItem(itemTitle, rating) {
    var rateUrl = '/rate-item.sm?rating=' + rating + '&title=' + itemTitle;   
    new Ajax.Request(rateUrl, {asynchronous:true, evalScripts:true});
    var ratingInPx = rating * 18;
    var currentRating = $('rating-current-rating').style.width.substring(0, 2);
    var newRating = (parseInt(currentRating, 10) + parseInt(ratingInPx, 10)) / 2;
    $('rating-current-rating').style.width = newRating + 'px';
    var currentVotes = $('rating-votes');
    currentVotes.innerHTML = parseInt(currentVotes.innerHTML, 10) + 1;
}

function rateImage(imageId, rating) {
    var rateUrl = '/rate-image.sm?rating=' + rating + '&image=' + imageId;
    new Ajax.Request(rateUrl, {asynchronous:true, evalScripts:true});
    var ratingInPx = rating * 18;
    var currentRating = $('rating-current-rating').style.width.substring(0, 2);
    var newRating = (parseInt(currentRating, 10) + parseInt(ratingInPx, 10)) / 2;
    $('rating-current-rating').style.width = newRating + 'px';
    var currentVotes = $('rating-votes');
    currentVotes.innerHTML = parseInt(currentVotes.innerHTML, 10) + 1;
}

function ajaxError(t) {
	alert('An error occured: ' + t.status + ' ' + t.statusText);
}

// Vote on poll
function voteOnPoll(pollUrl, optionId) {
	var voteUrl = "/vote-on-poll.sm?title=" + pollUrl + "&option=" + optionId;
	new Ajax.Request(voteUrl, {onSuccess:function(t) {
		new Ajax.Updater('sm-poll', '/get-poll.sm');
	}, onFailure:ajaxError});
	return false;
}


			function voteOnFmk(fmk, fmkId, optionNo) {
				$('fmk-option-' + optionNo + '-f').style.color = '#576568';
				$('fmk-option-' + optionNo + '-m').style.color = '#576568';
				$('fmk-option-' + optionNo + '-k').style.color = '#576568';
				$('fmk-option-' + optionNo + '-' + fmk).style.color = 'white';
				document.getElementById('fmk-' + fmk + '-' + fmkId).value = optionNo;
			}
			function saveFmk(fmkId) {
                $('button-vote-fmk').value = 'Saving...';
                $('button-vote-fmk').disabled = 1;
                new Ajax.Request('/save-fmk.sm', {method:'post', postBody:'f=' + $F('fmk-f-' + fmkId) + '&m=' + $F('fmk-m-' + fmkId) + '&k=' + $F('fmk-k-' + fmkId) + '&id=' + fmkId, onSuccess:function(t) {
                    var responseArray = t.responseText.split('|');
                    if (responseArray[0] == '0') {
                        alert(responseArray[1]);
                        $('button-vote-fmk').value = 'Save';
                        $('button-vote-fmk').disabled = 0;
                    }
                    else {
                        new Ajax.Updater('sm-fmk', '/get-fmk.sm', {asynchronous:true});
                    }
                }, onFailure:ajaxError});
            }
            function saveFmkList(fmkId) {
                $('button-vote-fmk-' + fmkId).value = 'Saving...';
                $('button-vote-fmk-' + fmkId).disabled = 1;
                var pb = 'f=' + $F('fmk-f-' + fmkId) + '&m=' + $F('fmk-m-' + fmkId) + '&k=' + $F('fmk-k-' + fmkId) + '&id=' + fmkId;
                alert(pb);
                new Ajax.Request('/save-fmk.sm', {method:'post', postBody:pb, onSuccess:function(t) {
                    var responseArray = t.responseText.split('|');
                    if (responseArray[0] == '0') {
                        alert(responseArray[1]);
                        $('button-vote-fmk-' + fmkId).value = 'Save';
                        $('button-vote-fmk-' + fmkId).disabled = 0;
                    }
                    else {
                        new Ajax.Updater('sm-fmk-' + fmkId, '/get-list-fmk.sm?id=' + fmkId, {asynchronous:true});
                    }
                }, onFailure:ajaxError});
            }
			
// Add a comment
function addComment() {
	
	var commentUrl = window.location + '/add-comment';
	if (commentUrl.indexOf('#') != -1) {
		commentUrl = commentUrl.substring(0, commentUrl.indexOf('#')) + '/add-comment';
	}
	//alert(commentUrl);
	var commentName = $F('sm-comment-name');
	var commentEmail = $F('sm-comment-email');
	var commentComment = $F('sm-comment-comment');
	//var commentCaptcha = $F('sm-comment-captcha');
	$('button-add-comment').disabled = 1;
	$('button-add-comment').value = 'Please wait...';
	new Ajax.Request(commentUrl, {method:'post', postBody:'name=' + commentName + '&email=' + commentEmail + '&comment=' + commentComment, onSuccess:function(t) {
		var responseArray = t.responseText.split('|');
		if (responseArray[0] == '0') {
			alert(responseArray[4]);
			$('button-add-comment').value = 'Add Comment';
			$('button-add-comment').disabled = 0;
		}
		else {
			createComment(responseArray[1], responseArray[2], commentName, responseArray[3], commentComment);
		}
	}, onFailure:ajaxError});
}

// Create a comment
function createComment(commentId, commentDate, commentName, commentEmail, commentComment) {
	var element=$E({
		tag:'div',
		className:'sm-item-comment',
		id:'sm-item-comment-' + commentId,
		children: [{
			tag:'table',
			width:'290',
			children: {
				tag: 'tbody',
				children:{
					tag:'tr',
					children:[{
						tag:'td',
						className:'sm-item-comment-cell',
						width:'66',
						children:{
							tag:'div',
							className:'sm-item-comment-avatar',
							children:{
								tag:'img',
								height:'64',
								width:'64',
								src:'http://www.gravatar.com/avatar.php?gravatar_id=' + commentEmail + '&size=64'
							}
						}
					},{
						tag:'td',
						className:'sm-item-comment-cell',
						align:'left',
						width:'224',
						children:[{
							tag:'div',
							className:'sm-item-comment-user',
							children:[commentName]
							},{
							tag:'div',
							className:'sm-item-comment-date',
							children:[commentDate]
							},{
							tag:'div',
							className:'sm-item-comment-comment',
							children:[commentComment]
						}]
					}]
				}
			}
		}]
	});
	if ($('sm-comment-no-comments')) {
		$('sm-comment-no-comments').style.display = 'none';
	}
	$('sm-comments').appendChild(element);
	$('sm-add-comment').style.display = 'none';
	var currentComments = $('comment-count');
	currentComments.innerHTML = parseInt(currentComments.innerHTML, 10) + 1;
}

function mmmCandy() {
	//new Ajax.PeriodicalUpdater('sm-eyecandy', '/get-candy.sm', {asynchronous:true, frequency:15});
}

  function stringReverse (str) {
    var s = typeof this.valueOf() == 'string' ? this : str;
    var r = '';
    for (var i = s.length - 1; i >= 0; i--)
      r += s.charAt(i);
    return r;
  }
  String.prototype.reverse = stringReverse;
  
function get_basename(filename) {
    return filename.reverse().substr(0, filename.reverse().indexOf('\\')).reverse();   
}
function imageUploadCaption(id) {
	
    var basename = $('image-' + id).value;
   // alert(basename);
    jQuery('#image-' + id).after(' &nbsp;main: <input type="radio" name="main" value="'+basename+'">');
    $('image-' + id + '-alt').name = 'image_alts[' + basename + ']'; 
    $('image-' + id + '-title').name = 'image_titles[' + basename + ']'; 
    return true;
}

    //var filename = $('image-' + id).value;
    //var filenameReversed = filename.reverse();
    ///var positionOfSlash = filenameReversed.indexOf('\\');
    //var basenameReversed = filenameReversed.substr(0, positionOfSlash);
//return basenameReversed.reverse();



sfHover = function() {
	
	/*
    var sfEls = document.getElementById("sm-topnav").getElementsByTagName("li");
    for (var i=0; i<sfEls.length; i++) {
        sfEls[i].onmouseover=function() {
            this.className+=" sfhover";
        }
        sfEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
        }
    }
    */
}
if (window.attachEvent) window.attachEvent("onload", sfHover);