var spln = {
	URL: '',
	msie: false,
	
	initialize: function(object){
		this.URL = '';
		
		for(var key in object){
			this[key] = object[key];
		}
		
		if($.browser.msie)
			if($.browser.version == '6.0')
				this.msie = true;

		this.prepActions();
	},
	prepActions: function(){
		/* ----------------------------------------- */
		if( $('#slideshow').length > 0 )
			window.setTimeout( "spln.slideShow()", 500 );
		
		/*-----------------------------------*/
		if( $('.img-wrapper').length > 0 )
			window.setTimeout("spln.slide_show()",6000);
			
		/* ----------------------------------------- */
		if( $('.inside-menu').length > 0 )
			this.insideMenuHover();
			
		/* ----------------------------------------- */
		if( $('.photoslide').length > 0 )
			this.photoSlide();
			
		/* ----------------------------------------- */
		if( $('.fittings').length > 0 )
			this.productActions();
		
		/* ----------------------------------------- */
		if( $('.project-lists').length > 0 )
			this.projectActions();
		
		/* ----------------------------------------- */
		if( $('.download-list').length > 0 )
			this.downloadActions();
	},
	slide_show: function(){
		var container = $('.img-wrapper');
		if( container.hasClass('noSlide') )
			return;
		
		container.find('div:first').fadeOut(500, function(){
			var temp = $(this);
			$(this).remove();
			container.append(temp);
			container.find('div:first').fadeIn(500, function(){
				window.setTimeout("spln.slide_show()",5000);
			});
		});
	},
	slideShow: function(){
		var container = $('#slideshow');
		
		if( $.browser.msie && ( $.browser.version == '6.0' || $.browser.version == '7.0' ) )
			container.find('a').live('click', function(){ window.location = $(this).attr('href'); } );
		
		container.find('span:first').stop().animate({
			marginTop: -70
		},
			1000, 
			'',
			function(){
				container.find('span:first').css({ 'margin-top': 0 });
				
				var temp = container.find('span:first span:first');
				container.find('span:first span:first').remove();
				container.find('span:first').append(temp);
				
				window.setTimeout( "spln.slideShow()", 500 );
		});
	},
	insideMenuHover: function(){
		$('.inside-menu .im-img img').css({ 'opacity': 0.5 });
		
		if( $.browser.msie && ( $.browser.version == '6.0' || $.browser.version == '7.0' ) )
			$('.inside-menu li a').live('click', function(){ window.location = $(this).attr('href'); });
		
		$('.inside-menu li').hover(
			function(){
				//$(this).find('a').addClass('hover');
				$(this).find('.im-img img').stop().animate({ 'opacity': 1 },300);
				$(this).find('.im-button').stop().animate({ 'opacity': 0.75 },300);
				//$(this).find('.im-button span').stop().animate({'color':'#565656'}, 300);
			},
			function(){
				//$(this).find('a').removeClass('hover');
				$(this).find('img').stop().animate({ 'opacity': 0.5 },300);
				$(this).find('.im-button').stop().animate({ 'opacity': 1 },300);
				//$(this).find('.im-button span').stop().animate({'color':'#fff'}, 300);
			}
		);
	},
	photoSlide: function(){
		var container = $('.photoslide');
		
		container.find('a.ic').click(function(e){
			var cur_num = container.attr('id');
			var act = ( $(this).attr('id') == 'left' )? -1:1;
			var next_num = ( act < 0 )? (parseInt(cur_num) - 1):(parseInt(cur_num) + 1);
			
			next_num = ( next_num < 1 )? photoslide.length:next_num;
			next_num = ( next_num > photoslide.length  )? 1:next_num;
			
			container.find('a.pic').each(function(){ $(this).removeClass('active'); });
			container.find('a.pic[rel='+ next_num +']').addClass('active');
			container.attr('id', next_num);
			
			container.find('img').attr('src', photoslide[(next_num - 1)]);
		});
		
		container.find('a.pic').click(function(e){
			e.preventDefault();
			
			container.find('a.pic').each(function(){ $(this).removeClass('active'); });
			container.find('img').attr('src', photoslide[($(this).attr('rel') - 1)]);
			container.attr('id', $(this).attr('rel'));
			
			$(this).addClass('active');
		});
	},
	productActions: function(){
		if( spln.msie ){
			$('.flists a').hover(
				function(){
					$(this).addClass('hover');	
				},
				function(){
					$(this).removeClass('hover');	
				}
			);	 
		}
		
		$('.flists a').live('click', function(e){
			e.preventDefault();
			
			if( $(this).hasClass('active') )
				return;
											  
			$('.flists a').each(function(){ $(this).removeClass('active'); });
			$(this).addClass('active');
			
			var newDetail = $('<div></div>').append('<div class="dtl" style="padding-bottom:10px;line-height:1.6em;text-align:left;"></div>').append('<div class="img"></div>').append('<div class="title"></div>');
				newDetail.find('.dtl').append('<span>- Longitudinal Section</span><span style="margin-left:30px;">- Legend : ' + $('input#detail_legend').val() + '</span>');
				newDetail.find('.img').append('<img alt="" src="' + $(this).find('img').attr('src') + '" width="450" />');
				newDetail.find('.title').append('<span>' + $(this).find('span.title').html() + '</span>');
			
			//$('.detail').html('').append(newDetail);
			var oriHeight = $('.detail').height();
			$('.detail').height( oriHeight ).find('div:first').fadeOut(500, function(){
				$('.detail').height('auto');
				$(this).html(newDetail.html());
				$(this).fadeIn(500);
			});
		});
	},
	projectActions: function(){
		if( $.browser.msie && $.browser.version == '8.0' )
		{
			$('.pl-main').find('td').live('click', function(){
				
				var parent = $(this).parent('tr');
				if( parent.hasClass('active') )
					return;
				
				$('.pl-main').find('tr').each(function(){
					$(this).removeClass('active');
				});
				parent.addClass('active');
				
				var id = parent.attr('id').replace('_', '');
				var me = parent;
				$('.content-projects .top').addClass('loading');
				$('.content-projects .top .detail-wrapper').hide();
				
				$.get(
					spln.URL + 'projects/getDetail/' + id,
					{},
					function( data ){
						var image = new Array();
						var text = '';
						
						text = me.find('td.detail_text').html();
						if( data.length > 0 ){
							for( var i = 0; i < data.length; i++ )
								image.push(data[i]['pic']);
						}
						
						$('.detail-wrapper .text-wrapper .text').html('<p>' + text + '</p>');
						
						$('.detail-wrapper .img-wrapper').html('');
						var display = '';
						for( var j = 1; j <= image.length; j++ ){
							var img = $('<div></div>');
							img.attr('id', 'img_' + j );
							img.attr('class', 'img');
							if( j == 1 )
								img.addClass('active');
							else
								img.css({ 'display': 'none' });
							
							img.html('<img src="' + image[j-1].replace('images/', spln.URL.replace('without-flash/spln/','') + 'media/images/spln/') + '" alt="" />');
							$('.detail-wrapper .img-wrapper').append(img);
						}
						
						$('.detail-wrapper .al-right .alr-right').html('');
						var active = 'active'; var last = ''; var separator = '';
						var links = '';
						for( var j = 1; j <= image.length; j++ ){
							if( j == image.length )
								last = 'last';
							
							if( active != '' && last != '' )
								separator = ' ';
								
							links += '<a href="javascript:;" id="ilink_'+ j +'" class="'+ active + separator + last +'" />';
							
							active = '';
						}
						$('.detail-wrapper .al-right .alr-right').html(links);
						  
						$('.detail-wrapper .al-title').html( me.find('td.project').html() );
						
						$('.content-projects .top').removeClass('loading');
						$('.content-projects .top .detail-wrapper').show();
					},
					"json"
				);
			});	
		}else{
			$('.pl-main').find('tr').live('click', function(){
				if( $(this).hasClass('active') )
					return;
				
				$('.pl-main').find('tr').each(function(){
					$(this).removeClass('active');
				});
				$(this).addClass('active');
				
				var id = $(this).attr('id').replace('_', '');
				var me = $(this);
				$('.content-projects .top').addClass('loading');
				$('.content-projects .top .detail-wrapper').hide();
				
				$.get(
					spln.URL + 'projects/getDetail/' + id,
					{},
					function( data ){
						var image = new Array();
						var text = '';
						
						text = me.find('td.detail_text').html();
						if( data.length > 0 ){
							for( var i = 0; i < data.length; i++ )
								image.push(data[i]['pic']);
						}
						
						$('.detail-wrapper .text-wrapper .text').css({ 'marginTop': '0px' }).html('<div><p>' + text + '</p></div>');
						
						$('.detail-wrapper .img-wrapper').html('');
						var display = '';
						for( var j = 1; j <= image.length; j++ ){
							var img = $('<div></div>');
							img.attr('id', 'img_' + j );
							img.attr('class', 'img');
							if( j == 1 )
								img.addClass('active');
							else
								img.css({ 'display': 'none' });
							
							img.html('<img src="' + image[j-1].replace('images/', spln.URL.replace('without-flash/spln/','') + 'media/images/spln/') + '" alt="" />');
							$('.detail-wrapper .img-wrapper').append(img);
						}
						
						$('.detail-wrapper .al-right .alr-right').html('');
						var active = 'active'; var last = ''; var separator = '';
						var links = '';
						for( var j = 1; j <= image.length; j++ ){
							if( j == image.length )
								last = 'last';
							
							if( active != '' && last != '' )
								separator = ' ';
								
							links += '<a href="javascript:;" id="ilink_'+ j +'" class="'+ active + separator + last +'" />';
							
							active = '';
						}
						$('.detail-wrapper .al-right .alr-right').html(links);
						  
						$('.detail-wrapper .al-title').html( me.find('td.project').html() );
						
						$('.content-projects .top').removeClass('loading');
						$('.content-projects .top .detail-wrapper').show();
					},
					"json"
				);
			});	
		}
		
		$('.detail-wrapper .alr-right a').live('click', function(e){
			e.preventDefault();
			
			if( $(this).hasClass('active') )
				return;
			
			var id = $(this).attr('id').replace('ilink_','');
			var me = $(this);
			$('.detail-wrapper .img-wrapper').find('.active').fadeOut(500, function(){
				$(this).removeClass('active');
				$('.detail-wrapper .alr-right a').each(function(){ $(this).removeClass('active'); });
				me.addClass('active');
				
				$('.detail-wrapper .img-wrapper').find('#img_' + id ).fadeIn(500, function(){
					$(this).addClass('active');
				});
			});			
		});
		
		var offsetTop = 0; var offsetBottom= 0;
		$('.detail-wrapper .arrow-wrapper a.arrow-down').live('click', function(e){
			e.preventDefault();
			
			/*var speed = 250;
			var textWrapper = $('.detail-wrapper .text');
			var height = textWrapper.height();
			var marginTop = parseInt(textWrapper.css('marginTop').replace('px',''));
			
			if( textWrapper.height() <= 250 )
				return;
			
			if( textWrapper.css('marginTop') == '-' + (height - 250) + 'px' )
				return;
			
			
			offsetTop = Math.abs(marginTop);
			speed = ( ( ( height - offsetTop ) - 250 ) >= 250 )? 250:( height - offsetTop ) - 250;
			
			textWrapper.stop().animate({ 'marginTop': '-=' + speed + 'px' });*/
		});
		
		
		$('.detail-wrapper .arrow-wrapper a.arrow-up').live('click', function(e){
			e.preventDefault();
			
			/*var speed = 250;
			var textWrapper = $('.detail-wrapper .text');
			var height = textWrapper.height();
			var marginTop = parseInt(textWrapper.css('marginTop').replace('px',''));
			
			if( textWrapper.height() <= 250 )
				return;
			
			if( textWrapper.css('marginTop') == '0px' )
				return;
			
			offsetTop = Math.abs(marginTop);
			speed = ( offsetTop >= 250 )? 250:offsetTop;
			
			textWrapper.stop().animate({ 'marginTop': '+=' + speed + 'px' });*/
		});
	},
	downloadActions: function(){
		if( spln.msie ){
			$('.download-list a').hover(
				function(){
					$(this).addClass('hover');	
				},
				function(){
					$(this).removeClass('hover');	
				}
			);	 
		}
	}
}

