$(document).ready(function(){
	var _hold = $('div.visual');
	var _imgW = _hold.find('img').eq(0).outerWidth();
	var _box = $('div.fade');
	var _boxW = _box.outerWidth();
	_box.css('left', (_imgW-_boxW));
	
	var searchF = $('#searchField').val();
	
	$('#searchField').focus(function() {
	
		if($(this).val() == 'Search content') {
			$(this).val('');
		}
		$(this).css('color', 'rgb(0,0,0)');
		
	});
	
	if($('#content > h1:first').html() == 'News') {
		$('.newsletter').show();
	}
	
	$('.subnav:not(:has(> ul))').hide();
	
	var cases = $('#cases').html();
	$('#cases').detach();
	
	if($('.navbar')) {
		var navheight = $('.navbar > .active').height();
		navheight = Math.ceil(navheight / 2);
		navheight = navheight - 2;
		navheight = navheight - 9;
		$('.navarrow').css('margin-top', navheight);
	}
	
	$('#customers').after('<ul class="sitemap_a">' + cases + '</ul>');
	
	$('#searchField').blur(function() {
	
		if($(this).val() == '') {
			$(this).val('Search content');
		}
		$(this).css('color', 'rgb(161,161,161)');
	});

	$('.slide > img').css("display", "none");
	$('.slide2 > div').css('display', 'none');

	$('.slide > img').first().toggleClass('slideCur');
	$('.slide > img').last().addClass('slideLast');
	
	$('.slide2 > div').first().toggleClass('slideCur');
	$('.slide2 > div').last().addClass('slideLast');
	$('.slideCur').next().toggleClass('slideNext');
	


	
	function slide(once){
		
		if($('.slideNext').hasClass('slideLast')) {
			$('.slide2 > div').first().toggleClass('slideNext');
			$('.slide > img').first().toggleClass('slideNext');
			$('.slideCur').toggleClass('slideCur');
			$('.slideLast').removeClass('slideNext');
			$('.slideLast').toggleClass('slideCur');
		} else {
			$('.slideCur').toggleClass('slideCur');
			$('.slideNext').toggleClass('slideCur');
			$('.slideCur').toggleClass('slideNext');
			$('.slideCur').next().toggleClass('slideNext');
		}
			$('.slideNext').css("display", "block");
			
		if(once != true) {
			$('.slideNext').animate({
				'opacity' : '1.0'
			}, 1200, function() {
	
			});
			
			$('.slideCur').animate({
				'opacity' : '0.0'
			}, 1200, function() {
				$('.slideCur').css("display", "none");
			});
		} else {
			$('.slideNext').animate({
				'opacity' : '1.0'
			}, 300, function() {
	
			});
			
			$('.slideCur').animate({
				'opacity' : '0.0'
			}, 300, function() {
				$('.slideCur').css("display", "none");
			});		
		}
		
		if(once != true) {
			t=setTimeout(function() {slide()}, 6000);
		}
	}
		
	slide();
	
	function playstop() {
		
		var slidebtn = $('.stopslide').html();

		if(slidebtn == 'stop') {
			clearTimeout(t);
			$('.stopslide').html('play');
		} else {
			t=setTimeout(slide, 6000);
			$('.stopslide').html('stop');
		}
	}
	
	$('.stopslide').click(function() {
		playstop();
	});
	
	$('.nextslide').click(function() {
		clearTimeout(t);
		$('.stopslide').html('play');
		slide(true);
	});
	
	$('.jumpto').change(function() {
		var jumptourl = $('.jumpto > option:selected').val();
		window.location=jumptourl;
	});
	
	$('.tabset > li').click(function() {
		$('.tabset > li').removeClass('active');
		$(this).addClass('active');
		
		if($('.tableft').hasClass('active')) {
			$('.tableft').removeClass('active');
			$('.tabright').addClass('active');
		} else {
			$('.tabright').removeClass('active');
			$('.tableft').addClass('active');
		}
	});
	
	if($('.caseload')) {
		$('.caseload').load('EN-cases');
	}

	var caseid = 0;
	var lastcaseid = 0;
	var maxcaseid = 999;
	var mincaseid = 0;

	$('.casenext').click(function() {

		caseid = $('.caseload > blockquote').attr('title');

		$.ajax({
			type: 'GET',
			url: 'EN-cases',
			data : 'id=' + caseid,
			success: function(data) {
				if(data != '') {
					$('.caseload').html(data);
					
					$('.caseprev').animate({
						'opacity' : '1'
					}, 300);
					
				} else {
					caseid = 0;
					$.ajax({
						type: 'GET',
						url: 'EN-cases',
						data : 'id=' + caseid,
						success: function(data) {
							if(data != '') {
								$('.caseload').html(data);
								
								$('.caseprev').animate({
									'opacity' : '1'
								}, 300);
								
							} else {
								$('.caseload').html('Error while loading case..');
							}
							lastcaseid = $('.caseload > blockquote').attr('title');
							caseid = lastcaseid;
						}
					});
				}
				lastcaseid = $('.caseload > blockquote').attr('title');
				caseid = lastcaseid;
			}
		});
	});
	
	$('.caseprev').click(function() {
		$.ajax({
			type: 'GET',
			url: 'EN-cases',
			data : 'mid=' + caseid + '&cm_sort_dir=desc',
			success: function(data) {
				if(data != '') {
					$('.caseload').html(data);
					$('.casenext').animate({
						'opacity' : '1'
					}, 300);
				} else {
					caseid = 999;
					$.ajax({
						type: 'GET',
						url: 'EN-cases',
						data : 'mid=' + caseid + '&cm_sort_dir=desc',
						success: function(data) {
							if(data != '') {
								$('.caseload').html(data);
								$('.casenext').animate({
									'opacity' : '1'
								}, 300);
							} else {
								$('.caseload').html('Error while loading case..');
							}
							lastcaseid = $('.caseload > blockquote').attr('title');
							caseid = lastcaseid;
						}
					});
				}
				lastcaseid = $('.caseload > blockquote').attr('title');
				caseid = lastcaseid;
			}
		});
	});
	
	$('.send').click(function() {
		$('.contactRequest').hide();
		$('.sendPage').show();
		$('.lightbox').fadeIn('fast');
	});
	
	$('.print').click(function() {
		window.print();
	});
	
	$('.contactReq').click(function() {
		$('.sendPage').hide();
		$('.contactRequest').show();
		$('.lightbox').fadeIn('fast');
	});

	$('.closeSendPage').click(function() {
		$('.lightbox').fadeOut(function() {
			$('.sendPage').hide();
		});
	});

	$('.closecontactRequest').click(function() {
		$('.lightbox').fadeOut(function() {
			$('.contactRequest').hide();
		});
	});
	
	$('.smallimage').click(function() {
	
		var imgdesc = $(this).attr('alt');
		var bigimgurl = $(this).attr('title');
		var bigdlurl = $(this).parent().next('div').children('a').attr('href');
		var folder = $('#foldername').attr('title');
		var imglinktext = $(this).parent().next('div').children('a').html();
		$('.bigimage').html('<img class="bigpic" src="/files/eniram/' + folder + '/' + bigimgurl + '" /><div class="bigimageclose"></div><table class="imgdesc"><tr><td class="imgdesctext">' + imgdesc + '</td><td class="imglink"><a href="' + bigdlurl + '" target="_blank">' + imglinktext + '</a></td></tr></table>');

		$('.lightbox').fadeIn('fast');
		$('.lightbox').html('<div class="preload">Loading.. please wait..</div>');
		
		var img = $('.bigpic');
		var imageh = $('.bigimage');

		$(img).load(function() {
		$(img).hide();
		setTimeout( function() {
			var imgheight = $(img).attr('height');
			var imgwidth = $(img).attr('width');
	
			if(imgheight == '') {
				imgheight = $(img).height();
			}
			if(imgwidth == '') {
				imgwidth = $(img).width();
			}
			var winheight = $(window).height();
			var winwidth = $(window).width();
			var maxheight = winheight-100;
			var maxwidth = winwidth-100;
			
			imgheight = Math.ceil(imgheight);
			imgwidth = Math.ceil(imgwidth);
			maxheight = Math.ceil(maxheight);
			maxwidth = Math.ceil(maxwidth);
			
			if((imgheight < winheight) || (imgwidth < winwidth)) {
				var newwidth = imgwidth;
				var newheight = imgheight;
			} else {
			if(imgheight > imgwidth) {
				var ratio = (imgwidth / imgheight);
				var newheight = maxheight;
				var newwidth = ratio * maxheight;
				$(img).height(maxheight);
				$(img).width(ratio * maxheight);
			} else {
				var ratio = (imgheight / imgwidth);
				var newheight = ratio * maxheight;
				var newwidth = maxheight;
				$(img).height(ratio * maxheight);
				$(img).width(maxheight);
			}
			
			if(newheight > winheight) {
				var ratio = (imgwidth / imgheight);
				var newheight = winheight-100;
				var newwidth = ratio * newheight;
				$(img).height(newheight);
				$(img).width(ratio * newheight);
				$(img).css('height', newheight);
				$(img).css('width', ratio * newwidth);
			}
	
			if(newwidth > winwidth) {
				var ratio = (imgheight / imgwidth);
				var newheight = ratio * winwidth;
				var newwidth = winwidth-100;
				newheight = Math.ceil(newheight);
				newwidth = Math.ceil(newwidth);
				$(img).height(ratio * newwidth);
				$(img).width(newwidth);
				$(img).css('height', ratio * newheight);
				$(img).css('width', newwidth);
			}}
			
			newheight = Math.ceil(newheight);
			newwidth = Math.ceil(newwidth);
			
			var margintop;
			margintop = winheight - newheight;
			margintop = margintop / 2;
			
			var marginleft;
			marginleft = winwidth - newwidth;
			marginleft = marginleft / 2;
	
			margintop = Math.ceil(margintop);
			marginleft = Math.ceil(marginleft);
			
			$('.imgdesc').css('width', newwidth);
			$('.imgdesctext').css('width', newwidth-100);
			//alert($('.imgdesc').css('width') + ' ' + newwidth);
			//alert('newheight : ' + newheight + ', newwidth : ' + newwidth + ',winwidth : ' + winwidth + ', winheight : ' + winheight + ', margintop : ' + margintop + ', marginleft : ' + marginleft + ', imgheight : ' + imgheight + ', imgwidth : ' + imgwidth);
			
			$(imageh).css('top', margintop-20);
			$(imageh).css('left', marginleft);
			
			$(img).fadeIn('fast');
			$('.lightbox').html('');
				
		}, 1000);
		
			$(imageh).fadeIn();

		});
	});
	
	$('.bigimageclose').live('mousedown', function() {
		$('.lightbox').fadeOut(function() {
			$('.lightbox').fadeOut('fast');
		});
		$('.bigimage').hide();
		$('.bigimage').css('top', '-100px');
	});
});

