(function(){
	var autoStep = true;

	var nextTimer = 5000;

    //var site_url = 'http://www.aldata-solution.com/';

	var init = function() {

		$.getScript('/javascript/jquery/ui.js', function() {

			$("#latest-news .accordion").show();

			$(".accordion").accordion({

				header : '.article-heading',
				
				autoHeight : false,
				
				change : function(event, ui) {

					ui.newHeader.toggleClass('selected'); // jQuery object, activated header

					ui.oldHeader.toggleClass('selected'); // jQuery object, previous header

				}
			
			});

			$("div.tabbed").show();
			
			$("div.tabbed").each(function() {

				var height = 0;
			
				$("div.tab-content > div.panel-bd", this).each(function(e) {
												
					var panelHeight = $(this).height();
					
					if (panelHeight > height)
					
						height = panelHeight;
					
				});
				
				height = height + 10;
				
				var api = $("ul.tabs", this).tabs($("div.tab-content > div.panel-bd", this), {
																			api : true,
																			tabs : 'span'
																		});
			
				$('.tab-content', this).height(height);
			
				$("ul.tabs li span", this).click(function(e){
												
					e.preventDefault();
												
					$(this).parent().siblings().removeClass('selected');
	
					$(this).parent().addClass('selected');
	
				}); 
	
				$("ul.tabs li span.current", this).each(function(e){
												
					$(this).parent().addClass('selected');
	
				}); 
				
			});

			// console.log(api.getCurrentPane());
		
		}); // 
		
		$.getScript('/javascript/jquery/tools.js', function() {
			
			$("#scrollbox").each(function(){
			
				var api = $("#scrollbox").scrollable({ 
					vertical:true,  
					items: '#scrollbox .links',
					size: 3, 
					api: true,
					nextPage: '#scroll-down', 
					prevPage: '#scroll-up',
					onSeek : function() {
						if (api.getIndex() == api.getSize() - 0) $('#scroll-down').addClass('disabled');
						else if (api.getIndex() == 0) $('#scroll-up').addClass('disabled');
						else if (api.getIndex() > 0) $('#scroll-up').removeClass('disabled');
						else if (api.getIndex() < api.getSize() - 0) $('#scroll-down').removeClass('disabled');
					}
				});
	
				if (api.getIndex() == api.getSize() - 0) $('#scroll-down').addClass('disabled');
				else if (api.getIndex() == 0) $('#scroll-up').addClass('disabled');
				else if (api.getIndex() > 0) $('#scroll-up').removeClass('disabled');
				else if (api.getIndex() < api.getSize() - 0) $('#scroll-down').removeClass('disabled');

			});

		});

		$('input').each(setDefault);


        $('#main-local #site_id').change(function(){

			$(this).parents('form').get()[0].submit();

		});

		/* temporary script */
		
		$('#solutions-diagram').each(function(){

			var flashvars = {lang : base_url + "/" + site_url + "/xml/solutions"};
			
			 //console.log(base_url + "/" + site_url + "/xml/solutions");
	
			var params = { allowscriptaccess : "always", wmode : "transparent" };
            
			
			var swf = swfobject.embedSWF(base_uri + "/apps/solutions.swf", "solutions-diagram-flash", "598", "550", "9.0.0", null, flashvars, params);

            
            
		});
	
		$('#video-player').each(function(){
																		 
			var video = $('#video-url').val();
						
			if (video) {
			
				var flashvars = {video : video};
				
				var params = { allowscriptaccess : "always", wmode : 'transparent'};

				var swf = swfobject.embedSWF(base_uri + "/apps/vidplayer.swf", "video-player-output", "350", "294", "9.0.0", null, flashvars, params);

			}

		});

		$('#carousel').each(initCarousel);
		
		$('.gallery-carousel').each(initGalleryCarousel);

		initBookmarks();

	} // end init

	var initCarousel = function() {
		
		$('#carousel-controls li .control-panel', this).click(function(e) {
		
			autoStep = false;
			
			var panel = $(this).parent();
	
			selectPanel(panel);
			
		});

		$('#carousel-controls li.selected .control-panel', this).each(function() {
		
			var panel = $(this).parent();
	
			selectPanel(panel);
			
		});

	};
	
	var initGalleryCarousel = function() {
		
		var id = $(this).attr('id').split('-')[1];
		
		// var flashvars = { config_url : base_url + "/apps/xml/id/" + id };

		var flashvars = { config_url : "/carousel/gallery/id/" + id };

		var params = { allowscriptaccess : "always", wmode : 'transparent' };
		
		var swf = swfobject.embedSWF(base_url + "/apps/carousel.swf", "carousel-" + id + "-output", "302", "126", "9.0.0", null, flashvars, params);
		
	}

	var nextPanel = function() {
		
			var next = $('#carousel-controls li.selected').next('li');
		
			if (next.length != 0) {
			
				selectPanel(next);
	
			} else {
				
				selectPanel($('#carousel-controls li:first'));
	
			}

		

	}

	window.nextPanel = nextPanel;

	var selectPanel = function(panel) {
		
		var id = $(panel).attr('id');
				
		var type = $(panel).attr('class').split(' ')[0];
		
		var content = $('#' + id + '_content');
		
		$(content).siblings().removeClass('selected');

		$(content).addClass('selected');
		
		$(panel).siblings().removeClass('selected');
		
		$(panel).addClass('selected');
				
		if (type == 'video') {
		
			stopVideo();
			
			stopFlash();

			startVideo(content);
			
		} // end if
		
		if (type == 'flash') {
						
			stopVideo();
			
			stopFlash();

			startFlash(content);
			
		}
		
		if (type == 'image') {
			
			// console.debug('step: ' + autoStep);

			stopVideo();

			stopFlash();

			if (autoStep)
			
				setTimeout(nextPanel, nextTimer);					
			
		}
		
	} // end selectPanel

	var videoContent = null;

	var flashContent = null;

	var startVideo = function(content) {
		
		videoContent = content;
		
		loadVideo();
		
	}
	
	var stopVideo = function() {
		
		$('#carousel-controls .video').each(function(){
			
			var id = $(this).attr('id');

			var content = $('#' + id + '_content');

			var started = $(content).data('started');
			
			if (started == 1) {
	
				var swf = $('object', content).get(0);
			
				// console.log(swf);
			
				swf.stopVideo();

			}

		});

	}

	var loadVideo = function() {
	
		var loaded = videoContent.find('.loaded').val();

		if (loaded == 0) {
		
			setTimeout(loadVideo, 500);
			
		} else if (loaded == -1) {
			
			$('.video-output', videoContent).css('visibility', 'visible');
			
			setTimeout(nextPanel, nextTimer);					
			
		} else {
			
			$(videoContent).data('started', 1);
				
			var swf = $('.flvplayer', videoContent).get(0);
			
			try {
				
				swf.playVideo();

			} catch(e) {
				
				setTimeout(loadVideo, 500);
				
				return false;
				
			}

			videoContent = null;
			
		}
		
	}

	var startFlash = function(content) {
	
		
		flashContent = content;
				
		loadFlash();
		
	}
	

	var stopFlash = function() {
		
		$('#carousel-controls .flash').each(function(){
			
			var id = $(this).attr('id');

			var content = $('#' + id + '_content');

			var started = $(content).data('started');
			
			// console.log(started);
			
			if (started == 1) {

				var swf = $('object', content).get(0);
				
				swf.stopMovie();

			} // end if

		});

	}
	
	var loadFlash = function() {
		
		//if (flashContent == null) {

			//setTimeout(loadFlash, 500);

		//} else {

			var loaded = flashContent.find('.loaded').val();
								
			if (loaded == 0) {
			
				setTimeout(loadFlash, 500);
				
			} else if (loaded == -1) {
				
				$('.flash-output', flashContent).css('visibility', 'visible');
				
				setTimeout(nextPanel, nextTimer);					
				
			} else {
			
				var swf = $('object', flashContent).get(0);
				
				// alert(swf);
				
				// console.log(swf);
				
				var started = $(flashContent).data('started');
								
				// console.log(started);
				
				if (started == 1) {
	
					try {
						
						swf.playMovie();
		
					} catch(e) {
						
						setTimeout(loadFlash, 500);
						
						return false;
						
					}
					
				} else {
	
					try {
												
						swf.startMovie();
		
						$(flashContent).data('started', 1);
						
					} catch(e) {
						
						// console.log(e);
						
						setTimeout(loadFlash, 500);
						
						return false;
						
					}
					
				} // end if
	
				flashContent = null;
				
			}
			
		//}
		
	}

	var removeDefault = function() { 
				
		$(this).val(''); 
		
		$(this).removeClass('default');
		
		$(this).unbind('focus', removeDefault);

		$(this).unbind('blur', setDefault);
	
		$(this).bind('blur', setDefault);

	} // end removeDefault

	var setDefault = function() {
		
		//var debug = $(this).attr('type') == "hidden";
		
		//if (debug) {
		
			//return false;

		//}

		if ($('label[for="' + $(this).attr('id') + '"]').css('display') == 'none' && $(this).val() == "") {
		
			var name = $(this).attr('name');
			
			var text = $('label[for="' + $(this).attr('id') + '"]').text();

			if ($(this).attr('type') == "password") {

				var html = '<input id="' + name + '" type="text" value="' + text + '" name="' + name + '" />';

				$(this).replaceWith(html);

				$('#' + name).addClass('default');

				$('#' + name).bind('focus', function(){
				
					var html = '<input id="' + name + '" type="password" value="" name="' + name + '" />';

					$(this).replaceWith(html);
					
					$('#' + name).get()[0].focus();
					
					$('#' + name).removeClass('default');
				
					$('#' + name).bind('blur', setDefault);
			
				});
				
			} else {
				
				$(this).val(text);	
				
				$(this).addClass('default');

				$(this).bind('focus', removeDefault);

			} // end if

			$(this).parents('form').bind('submit', submitDefault);

			//console.log($(this).parents('form').length);

		} // end if
		
		if ($(this).hasClass('default-value')) {
			
			var val = $(this).val();
			
			var text = $(this).data('default');
			
			if (!text || val == "") {
			
				if (!text)
					
					text = val;
	
				$(this).val(text);
			
				$(this).data('default', text);

				$(this).bind('focus', removeDefault);
				
				$(this).addClass('default');
			
				$(this).parents('form').bind('submit', submitDefault);

				//console.log($(this).parents('form').length);

			}
			
		}
		
	} // end setDefault

	var submitDefault = function(ev)
	{
		
		$('input.default', this).val('');
		
		//console.log('test');
		
		return true;
		
	}

	var ucfirst = function(str) {
		
		// http://kevin.vanzonneveld.net
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   bugfixed by: Onno Marsman
		// +   improved by: Brett Zamir (http://brett-zamir.me)
		// *     example 1: ucfirst('kevin van zonneveld');
		// *     returns 1: 'Kevin van zonneveld'
 
		str += '';
		
		var f = str.charAt(0).toUpperCase();

		return f + str.substr(1);

	}

	var initBookmarks = function() {
		
		$('#bookmarks').show();
		
		$('#bookmarks').live('click', showBookmarks);
		
		var bookmark_url = $('#bookmark-url').val();
		
		$.post(base_uri + '/panels/bookmark', { 'url' : bookmark_url }, function(data) {

			$('#main-block').append(data);
																																									
		});
	
		
	}
	


	var showBookmarks = function() {
		


//	var loc = $('#main-ft').position();
		
		var loc = $('#bookmarks').position();
		
		var top = loc.top - $('#bookmarks-box').height() - 45;
		
		$('#bookmarks-box').css('top', top);

		$('#bookmarks-box').css('left', loc.left + 0);
		
		$('#bookmarks-box').fadeIn(800, function(){
																						 
			$(document).bind('click', hideBookmarks);
			
		});

	} // end showBookmarks

	var hideBookmarks = function() {

		$('#bookmarks-box').fadeOut(800);
														 
		$(document).unbind('click', hideBookmarks);
		
	};

	$(document).ready(init);

})();


