$('document').ready(function(){


	$('input[name="pgNumber"]').focus(function(){
		$(this).css({
			'background': '#eee'
		});
	}).blur(function(){
		$(this).css({
			'background': '#fff'
		});
	}).keypress( function(e){
		if(e.which == 13) {
			$('.field').remove();
			document.location.href='#pg:'+$(this).val();
			placePreviewInMain();
		}
	});

	$('#pageIcon').hover(
		function(){
			$(this).css({ 'cursor':'pointer'});
		},
		function(){
			$(this).css({ 'cursor':'default'});
		}
	).click(function(){
			$('.field').remove();
			document.location.href='#pg:'+$('input[name="pgNumber"]').val();
			placePreviewInMain();
	});


	$('input[name="productCode"]').focus(function(){
		$(this).css({
			'background': '#eee'
		});
	}).blur(function(){
		$(this).css({
			'background': '#fff'
		});

	}).keypress( function(e){
		if(e.which == 13) {
			$('.field').remove();
			searchForCode($(this).val());
		}
	});

	$('#searchIcon').hover(
		function(){
			$(this).css({ 'cursor':'pointer'});
		},
		function(){
			$(this).css({ 'cursor':'default'});
		}
	).click(function(){
			$('.field').remove();
			searchForCode($('input[name="productCode"]').val());
	});



	$('#chapters').load('folder/menu.html', function(){
		$('#chapters').accordion({
			autoHeight: false,
			change: function(event, ui){
				$(ui.oldContent).animate({
					marginBottom: '0px'
				},
				{
					duration: 400
				});

				$(ui.newContent).animate( {
					marginBottom: '7px'
				}, {
					duration: 800
				});


				
				if( $(ui.oldHeader).attr("colorcode") != null){
					var colorC = divideColor( $(ui.oldHeader).attr("colorcode") );
				}
				else{
					var colorC = '';
				}
				


				$(ui.oldHeader).removeClass('ui-state-default ui-state-focus')
				.css({
					'font-weight':'normal',
					'background-color': colorC
				});
			},
			changestart: function(event, ui){
				$(ui.oldHeader).css({
					'font-weight':'normal'
				});


				$(ui.newHeader).css({
					'font-weight':'bold'
				});
			},
			collapsible: true,
			active: 'none'
		});

		
		$('#chapters h3').each( function(){
		var iinn = $(this).html();
		$(this).html(unescape(iinn));
		
		var color = divideColor($(this).attr('colorcode'));
		
		
		$(this).removeClass('ui-state-default');
		$(this).css({
		 'border-bottom': '3px solid '+color,
		 'background-color': color
		});
		$(this).next().css({
		 'border-bottom': '3px solid '+color
		});
	});


	$('#chapters').find('div').find('a').each( function(){
		var ins = $(this).html();
		$(this).html(unescape(ins));

		var color = $($(this).parent()).css('border-bottom-color')

		$(this).mouseover(function(){
			$(this).css({
				'background': color
			});
		}).mouseout(function(){
			$(this).css({
				'background': 'none'
			});
		});


		$(this).click(function(){
			var tmp = $(this).attr("href");
				tmp = tmp.split('#');
				tmp = tmp[1];
				

			var pgNumber = tmp.split(':');
			placePreviewInMain(pgNumber[1]);
		});
	});






	


//end of ready
	});


placeArrows();
placePreviewInMain();


$('body').supersleight();


activateHelp();

placePdfLinks();
});



//unused for now
function divideColor(color){

		color = color.replace(/^rgb\(/g, '');
		color = color.replace(/\)/g, '');
		var cTable = color.split(',');
		var newColor = new Array;
		$(cTable).each(function(){
			newColor.push(parseInt(255-(255-parseInt(this))/2));
		});

		color = 'rgb('+newColor[0]+','+newColor[1]+','+newColor[2]+')';
		return color;
}




function activateHelp(){
	embedHelp();
	$('#help-activator').click(
		function(){
			$('#help-dialog').dialog('open');
			return false;
		}).hover(
		function(){
			$(this).find('div').css({
				'cursor':'pointer'
			}).addClass('ui-state-hover ui-corner-all');
		},
		function(){
			$(this).find('div').css({
				'cursor':'default'
			}).removeClass('ui-state-hover ui-corner-all');
		}
	);
}


function embedHelp(){
	$('#help-activator').html(
		'<div style="font-size: 13px; margin-top: 10px; font-weight: bold; width: 198px; padding-top: 5px; padding-bottom: 5px;"><img src="images/help_icon.png" style="width: 16px; height: 16px; padding-left: 5px; padding-right: 5px;" alt="" align="left" />'+translated.need_help+'</div>'
	);

	$('<div id="help-dialog" style="display:none" title="'+translated.help+'">\n\
		<h4>'+translated.cat_nav+'</h4>\n\
			<p>'+translated.browse_next_prev+'</p>\n\
			<p>'+translated.menu_left+'</p>\n\
			<p><div style="float:left; width: 300px;"><img src="images/help_miniature.jpg" alt="" /></div><div style="float: left; width: 250px; margin-left: 5px; margin-top: 60px;">'+translated.zoom+'<br />'+translated.unzoom+'</div></p>\n\
			<p style="clear:both; padding-top: 10px;"><div style="float: left;"><img src="images/help_code.jpg" alt="" /></div><div style="float: right; width: 250px;">'+translated.goto_shop+'</div></p><hr />\n\
			<p style="clear:both; padding-top: 10px;"><img src="images/help_page.jpg" alt="" align="left" style="padding-top: 10px;" />'+translated.quick_goto_page+'</p>\n\
			<p style="clear:both; padding-top: 10px;"><img src="images/help_search.jpg" alt="" align="left" style="padding-top: 10px;" />'+translated.search+'</p>\n\
			<p>'+translated.search_x+'</p>\n\
			<p>'+translated.search_go+'</p>\n\
			<p><img src="images/pdf.png" style="width: 32px; height: 32px;margin-right: 5px;" alt="" align="left" />'+translated.pdf+'</p>\n\
		</div>'

	).appendTo('body').dialog({
			autoOpen: false,
			show: 'blind',
			hide: 'blind',
			zIndex: 2000,
			resizable: false,
			width: 600,
			height: 600,
			position: [310, 20]
	});


}
