// jayan.js

// Menu Pral
var oMenuPral = {
	 sImgSrcPtrn: ''
	,sImgSrcHvrSuffix: ''
	,reAIdPtrn: /nothing/
	,sSelectedId: ''
	,hideSub: function() {
		$(this).find('ul').stop(true, true);
		$(this).find('ul').fadeOut('Slow');
	}
	,showSub: function() {
		$(this).find('ul').stop(true, true);
		$(this).find('ul').fadeIn('fast');
	}
	,swapImg: function() {
		$(this).find('img').attr('src', oMenuPral.getSrc($(this).find('img').attr('id'), true));
	}
	,restoreImg: function() {
		$(this).find('img').attr('src', oMenuPral.getSrc($(this).find('img').attr('id'), false));
	}
	,getSrc: function(sLiId, bHvr) {
		var sId = sLiId.replace(oMenuPral.reAIdPtrn, '$1');
		var sSrc = oMenuPral.sImgSrcPtrn.replace('<%id%>', sId).replace('<%hvr%>', bHvr ? oMenuPral.sImgSrcHvrSuffix : '');
		return sSrc;
	}
	,init: function() {
		$('#ulMenuPral > li').each( function() {
			$(this).hover( oMenuPral.showSub, oMenuPral.hideSub );
			$(this).click( oMenuPral.hideSub );
			if($(this).attr('id') != oMenuPral.sSelectedId) {
				$(this).find('> a').hover( oMenuPral.swapImg, oMenuPral.restoreImg );
				jQuery("<img>").attr('src', oMenuPral.getSrc($(this).find('img').attr('id'), true));
			}
		} );
		$('#ulMenuPral li li').each( function() {
			$(this).hover( function() { $(this).addClass('Hover'); }, function() { $(this).removeClass('Hover'); } );
			$(this).click( function(eventObj) { if(!$(eventObj.target).attr('href')) document.location.href = $(this).find('a').attr('href'); } );
			$(this).addClass('Pointer');
		} );
	}
}

// Menu sec, 2nd and 3rd levels
oMenuSec = {
	 init: function() {
		$('#ulMenuSec > li > ul > li').each( function() {
			if(!$(this).hasClass('Selected')) {
				$(this).find('> ul').hide();
			}
			$(this).click( function(eventObj) {
				if($(eventObj.target).attr("href") == "javascript:void(0)") {
					$(this).find('> ul').slideToggle('fast');
					$(this).find('> a').blur();
				}
			} );
		} );
	}
}

// HoverImageSource
oHoverImage = {
	 sHoverSuffix: ''
	,getSrc: function(sSrc, bHvr) {
		var sHvrSuf = oHoverImage.sHoverSuffix;
		var re = new RegExp('('+sHvrSuf+')?\.(gif|jpg|png)$');
		sSrc = sSrc.replace(re, bHvr ? sHvrSuf+'.$2' : '.$2');
		return sSrc;
	}
	,preload: function(sSrc) {
		jQuery('<img>').attr('src', oHoverImage.getSrc(sSrc, true));
	}
}

// HoverSwapImage class
oAHoverSwapImage = {
	 init: function() {
		$('.HoverSwapImage').each(
			function() {
				$(this).hover(
					function(eventObj) {
						$(this).find('img').attr('src', oHoverImage.getSrc($(this).find('img').attr('src'), eventObj.type == 'mouseenter'));
					}
				);
				oHoverImage.preload($(this).find('img').attr('src'));
				jQuery('<img>').attr('src', oHoverImage.getSrc($(this).find('img').attr('src'), true));
			}
		);
	}
}

// Home Headline
oHomeHeadline = {
	 init: function() {
		$('body.Home div.Headline').each(
			function() {
				$(this).addClass('Pointer');
				$(this).hover(
					function() {
						$(this).addClass('Hover');
					}
					,function() {
						$(this).removeClass('Hover');
					}
				);
				$(this).click(
					function(eventObj) {
						if(!$(eventObj.target).attr('href')) document.location.href = $(this).find('.MoreInfo a').attr('href');
					}
				);
				$(this).find('.MoreInfo').hide();
			}
		);
	 }
}

// Round Image Borders
oRoundBorders = {
	 init: function() {
		$('span.RoundBorders').each( function() {
			$(this).append('<span class="RoundBorder LeftTop"></span>');
			$(this).append('<span class="RoundBorder RightTop"></span>');
			$(this).append('<span class="RoundBorder LeftBottom"></span>');
			$(this).append('<span class="RoundBorder RightBottom"></span>');
			var jqImage = $(this).find('img');
			jqImage.load( function() {
				var jqImage = $(this);
				var jqImagePos = $(this).position();
				var jqSpanParent = $(this).parents('span.RoundBorders');
				var nRadius = jqSpanParent.find('span.RoundBorder').width();
				jqSpanParent.find('span.RoundBorder.LeftTop').css('left', jqImagePos.left);
				jqSpanParent.find('span.RoundBorder.LeftTop').css('top', jqImagePos.top);
				jqSpanParent.find('span.RoundBorder.RightTop').css('left', jqImagePos.left + jqImage.width() - nRadius);
				jqSpanParent.find('span.RoundBorder.RightTop').css('top', jqImagePos.top);
				jqSpanParent.find('span.RoundBorder.LeftBottom').css('left', jqImagePos.left);
				jqSpanParent.find('span.RoundBorder.LeftBottom').css('top', jqImagePos.top + jqImage.height() - nRadius);
				jqSpanParent.find('span.RoundBorder.RightBottom').css('left', jqImagePos.left + jqImage.width() - nRadius);
				jqSpanParent.find('span.RoundBorder.RightBottom').css('top', jqImagePos.top + jqImage.height() - nRadius);
			} );
			jqImage.trigger('load');
		} );
	}
}

// Init when body loaded
$(document).ready( function() {
	oMenuPral.init();
	oAHoverSwapImage.init();
	oHomeHeadline.init();
	oMenuSec.init();
	oRoundBorders.init();
	$("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});
	$("div.PrettyPhotoInline").css("display", "none");
} );

