$(document).ready(function()
{	
	// Check url for localhost or live server
	var baseUrl = window.location.href.split('/');
	if(baseUrl[0] + '//' + baseUrl[2] == 'http://localhost')
	{
		var baseUrl = '/' + baseUrl[3] + '/public';
		var rootJquery = baseUrl + '/jquery/';
		var rootFancybox = baseUrl + '/jquery/fancybox/';
		var rootUploadify = baseUrl + '/jquery/uploadify/';
		var rootTinymce = baseUrl + '/jquery/tinyMCE_jquery/';
		var rootCss = baseUrl + '/css/admin/';
		var rootSwf = baseUrl + 'swf/';
	}
	else
	{
		var baseUrl = '';
		var rootJquery = '/public/jquery/';
		var rootFancybox = '/public/jquery/fancybox/';
		var rootUploadify = '/public/jquery/uploadify/';
		var rootTinymce = '/public/jquery/tinyMCE_jquery/';
		var rootCss = '/public/css/admin/';
		var rootSwf = 'public/swf';
	}

	if($('table').attr('class') != 'form_table' && $('table').attr('id') != 'news_table' && $('table').attr('id') != 'news_overview_table' && $('table').attr('id') != 'news_preview_table' && $('table').attr('id') != 'picture_table')
	{
		$("tr:even").css("background-color", "#f9eafe");
		$("tr:odd").css("background-color", "#FFFFFF");
	}
	var baseUrlMain = window.location.href.split('/');
	if(baseUrlMain[3] == 'detovenaarvanoz')
	{
		$("tr:even").css("background-color", "#fffbe5");
		$("tr:odd").css("background-color", "#FFFFFF");
	}
	
	$('.video_gallery').Loader({
		url: [
			rootFancybox + 'jquery.fancybox-1.3.2.pack.js',
			rootFancybox + 'jquery.easing-1.3.pack.js',
			rootFancybox + 'jquery.mousewheel-3.0.4.pack.js',
			rootFancybox + 'jquery.fancybox-1.3.2.css'
		],
		debug: [true],
		cache: [true],
		success: function(target)
		{			
			$('.video_gallery').click(function()
			{
				var hrefVideo;
				if($(this).attr('id') == 'youtube')
				{
					hrefVideo = this.href.replace(new RegExp("watch\\?v=", "i"), 'v/');
				}
				if($(this).attr('id') == 'vimeo')
				{
					hrefVideo = this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1');
				}
				$.fancybox({
					'autoDimensions'	: true,
					'autoScale'			: true,
					'width'				: 720,
					'height'			: 480,
					'padding'			: 10,
					'overlayOpacity'	: '0.85',
					'overlayColor'		: '#000000',
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'title'      		: '<div class="fancybox_video_title">' + this.title + '</div>',
					'titlePosition' 	: 'inside',
					'href'				: hrefVideo,
					'cyclic'			: true,
					'type'				: 'swf',
					'centerOnScroll'	: true,
					'hideOnContentClick': false,
					'swf'				: {
											'wmode'				: 'transparent',
											'allowfullscreen'	: 'true'
											}
				});
				$.fancybox.center
				return false;
			});
		}
	});
	
	$('.textEditorSimple').Loader({
		url: [rootTinymce + 'jquery.tinymce.js'],
		debug: [true],
		cache: [true],
		success: function(target)
		{
			$('.textEditorSimple').tinymce({
				script_url 								: rootJquery + "tinyMCE_jquery/tiny_mce_gzip.php",
				relative_urls 							: false,
				theme									: "advanced",
				skin 									: "cirkuit",
				editor_selector 						: "textEditorSimple",
				theme_advanced_resizing 				: false,
				theme_advanced_resize_horizontal 		: false,
				advimage_update_dimensions_onchange 	: true,
				theme_advanced_buttons1 				: "bold,italic,underline,strikethrough,link,unlink,emotions,charmap",
				theme_advanced_buttons2 				: "",
				theme_advanced_buttons3 				: "",
				plugins 								: 'inlinepopups,emotions',
				force_br_newlines 						: true,
				force_p_newlines 						: false,
				forced_root_block 						: '',
				theme_advanced_toolbar_location			: "top",
				theme_advanced_toolbar_align 			: "right",
				theme_advanced_statusbar_location 		: "bottom",
				theme_advanced_resizing 				: true,
				entity_encoding 						: "raw"
			});
		}
	});
	
	$('.guestbook_add_message_link').Loader({
		url: [
			rootFancybox + 'jquery.fancybox-1.3.2.pack.js',
			rootFancybox + 'jquery.easing-1.3.pack.js',
			rootFancybox + 'jquery.mousewheel-3.0.4.pack.js',
			rootFancybox + 'jquery.fancybox-1.3.2.css'
		],
		debug: [true],
		cache: [true],
		success: function(target){			
			$(this).fancybox({
				'autoDimensions'	: true,
				'autoScale'			: true,
				'overlayOpacity'	: '0.85',
				'overlayColor'		: '#000000',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titleShow'			: false,
				'cyclic'			: true,
				'showCloseButton'	: false,
				'href'				: $(this).attr('href'),
				'type'				: 'iframe',
				'height'			: 500,
				'width'				: 600,
				'centerOnScroll'	: true,
				'onClosed': function() {
					parent.location.reload(true);
				}
			});
		}
	});
	
	// Picturegallery in fancybox
	$('a[rel=picture_gallery]').Loader({
		url: [
			rootFancybox + 'jquery.fancybox-1.3.2.pack.js',
			rootFancybox + 'jquery.easing-1.3.pack.js',
			rootFancybox + 'jquery.mousewheel-3.0.4.pack.js',
			rootFancybox + 'jquery.fancybox-1.3.2.css'
		],
		debug: [true],
		cache: [true],
		success: function(target){
			$(this).fancybox({
				'autoDimensions'	: true,
				'autoScale'			: true,
				'overlayOpacity'	: '0.85',
				'overlayColor'		: '#000000',
				'transitionIn'		: 'fade',
				'transitionOut'		: 'fade',
				'titlePosition' 	: 'over',
				'cyclic'			: true,
				'showCloseButton'	: true,
				'titlePosition' 	: 'over',
				'centerOnScroll'	: true,
				'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over"><table class="nobordertable" border="1" width="100%"><tr><td class="fancyboxBorderandPadding">' +  (currentIndex + 1) + ' / ' + currentArray.length + '</td><td class="fancyboxPadding">' + title + '</td></tr></table></span>';
				},
				'type'				: 'image',
				'href'				: $(this).attr('src')
			});
		}
	});
	
	// All images in fancybox
	$('img').each(function(){
		wrapped = $(this).parent();
		if(wrapped[0].nodeName != 'A')
		{
			var a = $('<a/>').attr('href', $(this).attr('src')).attr('rel', 'openimagefancybox').attr('title', $(this).attr('title'));
			$(this).wrap(a);
		}
    })
	
	$("a[rel=openimagefancybox]").Loader({
		url: [
			rootFancybox + 'jquery.fancybox-1.3.2.pack.js',
			rootFancybox + 'jquery.easing-1.3.pack.js',
			rootFancybox + 'jquery.mousewheel-3.0.4.pack.js',
			rootFancybox + 'jquery.fancybox-1.3.2.css'
		],
		debug: [true],
		cache: [true],
		success: function(target){
			$(this).fancybox({
				'autoDimensions'	: true,
				'autoScale'			: true,
				'overlayOpacity'	: '0.85',
				'overlayColor'		: '#000000',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'cyclic'			: true,
				'showCloseButton'	: true,
				'titlePosition' 	: 'over',
				'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
					return '<span id="fancybox-title-over"><table border="0"><tr><td class="fancyboxBorderandPadding">' +  (currentIndex + 1) + ' / ' + currentArray.length + '</td><td class="fancyboxPadding">' + title + '</td></tr></table></span>';
				},
				'type'				: 'image',
				'href'				: $(this).attr('src'),
			});
		}
	});
	
	$('.news_print_link').click(function()
	{
		window.print();
		return false;
	});
	
	$('.textEditorSimple').Loader({
		url: [rootTinymce + 'jquery.tinymce.js'],
		debug: [true],
		cache: [true],
		success: function(target)
		{

		}
	});
	
	$('#jquery_jplayer_1').Loader({
		url: [rootJquery + 'jquery.jplayer.min.js'],
		debug: [true],
		cache: [true],
		success: function(target)
		{
			var playlistArray = []; 
			$('#jp_playlist_1 li').each(function(){
				playlistArray.push({
					name: $('a', this).attr('id'),
					mp3: $('a', this).attr('href')
				});
			});
			
			$(".jp-temp-data").remove(); 
			
			// AUDIO PLAYER STUFF //
			
			var Playlist = function(instance, playlist, options) {
				var self = this;

				this.instance = instance; // String: To associate specific HTML with this playlist
				this.playlist = playlist; // Array of Objects: The playlist
				this.options = options; // Object: The jPlayer constructor options for this playlist

				this.current = 0;

				this.cssId = {
					jPlayer: "jquery_jplayer_",
					interface: "jp_interface_",
					playlist: "jp_playlist_"
				};
				this.cssSelector = {};

				$.each(this.cssId, function(entity, id) {
					self.cssSelector[entity] = "#" + id + self.instance;
				});

				if(!this.options.cssSelectorAncestor) {
					this.options.cssSelectorAncestor = this.cssSelector.interface;
				}

				$(this.cssSelector.jPlayer).jPlayer(this.options);

				$(this.cssSelector.interface + " .jp-previous").click(function() {
					self.playlistPrev();
					$(this).blur();
					return false;
				});

				$(this.cssSelector.interface + " .jp-next").click(function() {
					self.playlistNext();
					$(this).blur();
					return false;
				});
			};

			Playlist.prototype = {
				displayPlaylist: function() {
					var self = this;
					$(this.cssSelector.playlist + " ul").empty();
					for (i=0; i < this.playlist.length; i++) {
						var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last'>" : "<li>";
						listItem += "<a href='#' id='" + this.cssId.playlist + this.instance + "_item_" + i +"' tabindex='1'>"+ this.playlist[i].name +"</a>";

						// Create links to free media
						if(this.playlist[i].free) {
							var first = true;
							listItem += "<div class='jp-free-media'>(";
							$.each(this.playlist[i], function(property,value) {
								if($.jPlayer.prototype.format[property]) { // Check property is a media format.
									if(first) {
										first = false;
									} else {
										listItem += " | ";
									}
									listItem += "<a id='" + self.cssId.playlist + self.instance + "_item_" + i + "_" + property + "' href='" + value + "' tabindex='1'>" + property + "</a>";
								}
							});
							listItem += ")</span>";
						}

						listItem += "</li>";

						// Associate playlist items with their media
						$(this.cssSelector.playlist + " ul").append(listItem);
						$(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function() {
							var index = $(this).data("index");
							if(self.current !== index) {
								self.playlistChange(index);
							} else {
								$(self.cssSelector.jPlayer).jPlayer("play");
							}
							$(this).blur();
							return false;
						});

						// Disable free media links to force access via right click
						if(this.playlist[i].free) {
							$.each(this.playlist[i], function(property,value) {
								if($.jPlayer.prototype.format[property]) { // Check property is a media format.
									$(self.cssSelector.playlist + "_item_" + i + "_" + property).data("index", i).click(function() {
										var index = $(this).data("index");
										$(self.cssSelector.playlist + "_item_" + index).click();
										$(this).blur();
										return false;
									});
								}
							});
						}
					}
				},
				playlistInit: function(autoplay) {
					if(autoplay) {
						this.playlistChange(this.current);
					} else {
						this.playlistConfig(this.current);
					}
				},
				playlistConfig: function(index) {
					$(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
					$(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
					this.current = index;
					$(this.cssSelector.jPlayer).jPlayer("setMedia", this.playlist[this.current]);
				},
				playlistChange: function(index) {
					this.playlistConfig(index);
					$(this.cssSelector.jPlayer).jPlayer("play");
				},
				playlistNext: function() {
					var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : 0;
					this.playlistChange(index);
				},
				playlistPrev: function() {
					var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
					this.playlistChange(index);
				}
			};

			var audioPlaylist = new Playlist("1",
					playlistArray, 
				{
				ready: function() {
					audioPlaylist.displayPlaylist();
					audioPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
				},
				ended: function() {
					audioPlaylist.playlistNext();
				},
				play: function() {
					$(this).jPlayer("pauseOthers");
				},
				swfPath: rootSwf,
				supplied: "mp3"
			});
		}
	});
});
