$(document).ready(function() {

	loadTabFunctions();
	loadSlideShowFunctions();
	loadMediaFunctions();
	
	$('.slideshow').cycle({
		fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeoutFn: calculateTimeout 
	});

});

function loadTabFunctions()
{	
	$("ul.css-tabs").tabsNoUiConflict("div.css-panes > div", {
		onClick: function(e, tabIndex)
		{
			var name = "#tabindex_" + tabIndex;

			$("ul.css-tabs > li").removeClass('current');
			$(name).addClass('current');
		}
	});
}

function loadSlideShowFunctions()
{	
	if ($("div[id^='slideshow_']").length > 0)
	{
		galleries = $("div[id^='thumbs_']");
		for($i = 0; $i < galleries.length; $i++)
		{
			id = galleries[$i].id;
			id = id.replace(/thumbs_/g, "");
			
			var gallery = $('#thumbs_' + id).galleriffic({
				delay:                     3000, // in milliseconds
				numThumbs:                 18, // The number of thumbnails to show page
				preloadAhead:              40, // Set to -1 to preload all images
				enableTopPager:            false,
				enableBottomPager:         true,
				maxPagesToShow:            7,  // The maximum number of pages to display in either the top or bottom pager
				imageContainerSel:         '#slideshow_' + id, // The CSS selector for the element within which the main slideshow image should be rendered
				controlsContainerSel:      '#controls_' + id, // The CSS selector for the element within which the slideshow controls should be rendered
				captionContainerSel:       '#caption_' + id, // The CSS selector for the element within which the captions should be rendered
				loadingContainerSel:       '#loading_' + id, // The CSS selector for the element within which should be shown when an image is loading
				renderSSControls:          true, // Specifies whether the slideshow's Play and Pause links should be rendered
				renderNavControls:         true, // Specifies whether the slideshow's Next and Previous links should be rendered
				playLinkText:              'Play',
				pauseLinkText:             'Pause',
				prevLinkText:              '&lsaquo; Previous Photo',
				nextLinkText:              'Next Photo &rsaquo;',
				nextPageLinkText:          'Next &rsaquo;',
				prevPageLinkText:          '&lsaquo; Prev',
				enableHistory:             false, // Specifies whether the url's hash and the browser's history cache should update when the current slideshow image changes
				enableKeyboardNavigation:  true, // Specifies whether keyboard navigation is enabled
				autoStart:                 false, // Specifies whether the slideshow should be playing or paused when the page first loads
				syncTransitions:           false, // Specifies whether the out and in transitions occur simultaneously or distinctly
				defaultTransitionDuration: 1000, // If using the default transitions, specifies the duration of the transitions
				onSlideChange:             undefined, // accepts a delegate like such: function(prevIndex, nextIndex) { ... }
				onTransitionOut:           undefined, // accepts a delegate like such: function(slide, caption, isSync, callback) { ... }
				onTransitionIn:            undefined, // accepts a delegate like such: function(slide, caption, isSync) { ... }
				onPageTransitionOut:       undefined, // accepts a delegate like such: function(callback) { ... }
				onPageTransitionIn:        undefined, // accepts a delegate like such: function() { ... }
				onImageAdded:              undefined, // accepts a delegate like such: function(imageData, $li) { ... }
				onImageRemoved:            undefined  // accepts a delegate like such: function(imageData, $li) { ... }
			});
		}
	}
}

function loadMediaFunctions()
{
	$('a.media').media();
}

function calculateTimeout(currElement, nextElement, opts, isForward) { 
    // here we set even number slides to have a 2 second timeout; 
    // by returning false for odd number slides we let those slides 
    // inherit the default timeout value (4 sec)
    return parseInt(currElement.getAttribute('waitTime'));
} 


function swapOnMouseOverImage(id, language)
{
	var img = urlPrefix + "/content/images/sitemap/img_over_" + language + "_" + id + ".gif";

	var name = "#img_" + id;
	$(name).attr('src', img);
}

function swapOnMouseOutImage(id, language)
{
	var img = urlPrefix + "/content/images/sitemap/img_" + language + "_" + id + ".gif";

	var name = "#img_" + id;
	$(name).attr('src', img);
}

function swapOnMouseOutImageSubsite(id)
{
	var img = urlPrefix + "/content/images/subsites/img_" + id + ".gif";

	var name = "#img_" + id;
	$(name).attr('src', img);
}

function swapOnMouseOverImageSubsite(id)
{
	var img = urlPrefix + "/content/images/subsites/img_over_" + id + ".gif";

	var name = "#img_" + id;
	$(name).attr('src', img);
}

function redirect(url)
{
	window.location = url;
}

function toggleElement(id)
{
	var name = "#" + id;
	if ($(name).css('display') == 'block')
	{
		$(name).css('display', 'none');
	}
	else
	{
		$(name).css('display', 'block');
	}
}

function showElement(id)
{
	var name = "#" + id;
	$(name).css('display', 'block');
}

function hideElement(id)
{
	var name = "#" + id;

	setTimeout(function()
	{
		$(name).css('display', 'none');
	}, 3000);

}

function changeSearchField()
{
	var search = $("#txtSearch");
	search.val("");

	//if (search.val().match(/\.\.\.$/))
	//{
	//	search.val("");
	//}
}

function resetSearchField()
{
	var search = $("#txtSearch").val();

	if (!search.length)
	{
		$("#txtSearch").val("enter keyword...");
	}
}

function refreshCaptcha()
{
	ajax_call = $.ajax({
	   type: "GET",
	   url: urlPrefix + "/prg/forms.pl",
	   data: "ajax=1&id_form=" + $("#id_form").val() + "&id_sitemap=" + $("#id_sitemap").val(),
	   success: function(data){eval(data);}
	 });
}

function setCaptcha(md5sum)
{
	$(document).ready(function()
	{
		$("#md5sum").val(md5sum);
		$("#captcha").attr('src', urlPrefix + "/content/images/captcha/" + md5sum + ".png");
	});
}


