﻿$(document).ready(function(){
	// automatically focus elements with a class of "autofocus"
	$('.logout').click(function(){
	    if ( confirm("ログアウトしても良いですか?") )
        { document.location.href = "/logout"; }
	    else
        { return false; }
	});
	
	$('.loginHd').click(function(){
		// First toggle the other field
		$('.loginHd + div').toggle('slow');
		// This toggles the next div after the title bar from hidden to displayed
		// It assumes two classes called '.loginHd' - one hidden, one displayed
		var thisImg = $("img", this);
		var hiddenImgVal = "-hide"
		var imgSuffix = ".jpg"
		var thisId = $(this).attr('id')
		var isHideMeImg = true
		
		// Get the 'other' image
		if (thisId == 'existingUser'){
			var otherImg = $('img','#newUser')			
		} else {
			var otherImg = $('img','#existingUser')			
		}
		
//		Swap each image to its alternate state
		thisImg.attr("src", getSwapImageState(thisImg));
		otherImg.attr("src", getSwapImageState(otherImg));
		
		function getSwapImageState(theImg) {
//			alert($('#existingUser'))
			if (theImg.attr("src").indexOf(hiddenImgVal+imgSuffix) == -1) {
				return theImg.attr("src").match(/[^\.]+/) + hiddenImgVal + imgSuffix;
			} else {
				return theImg.attr("src").replace(hiddenImgVal, "");
			}			
		}
	});
	
	$('.sendtofriend .close').click(function(){
		$('.sendtofriend').hide();
	});
	
	$('.starRating').mouseover(function(){
		var iImg = $("img", this).attr("id").replace(/(\D)/gi, "");
		for (i=1; i<=iImg; i++)
		{
			document.getElementById("imgStar" + i).src = "/images/rate-star-on.gif";
		}
	})
	
	$('.starRating').mouseout(function(){
		for (i=1; i<6; i++)
		{
			document.getElementById("imgStar" + i).src = "/images/rate-star.gif";
		}
	})
	
	// Pre-Load Images
	MM_preloadImages('/images/btn-home-on.jpg', '/images/btn-upload-on.jpg', '/images/btn-browse-gallery-on.jpg', '/images/btn-my-gallery-on.jpg', '/images/btn-gifts-on.jpg', '/images/btn-tips-on.jpg', '/images/btn-upload-img-over.jpg');
	
	// Setup Logo Animation
	$('#logoPetrait ul').innerfade({ speed: 1000, timeout: 5000, type: 'sequence', containerheight: '106px' });
});

sharewithfriend = function(grcid) {
	$('.sendtofriend input[type=text]').val('http://www.petrait.jp/browsephoto?grcID=' + grcid).parent().toggle();
	$('.sendtofriend input[type=text]').select();
}

ratePet = function(igrcID, iRating){
	$.getJSON("/_ajax/getspecies.cfm",{grcID: igrcID, Rating: iRating}, function(j)
	{
		$("div#rateMe").hide();
		$("div#rateMeResponse").show();
	})
	return;
}

validateHTMLNavigation = function(oFrm,topValue){
	if (validateform(oFrm, "page")) {
		if (isNaN(oFrm.page.value)) {
			alert(oFrm.page.alt);
			oFrm.page.value="1";
			oFrm.page.focus();
			return false;
		} else { 
			if (oFrm.page.value > topValue ){
				alert('The pages you requested does not exist.\nPlease enter a number between 1 and ' + topValue)
				oFrm.page.value="1";
				oFrm.page.focus();
				return false;
			} else {
				return true;
			}
		}
		
	}
	else
		return false;
}

