var busy = false;
var busy2 = false;
var busy3 = false;
var strip_phase = 0;
var strip_phase2 = 0;
var cont = '';
var img_count = 0;
var img_count2 = 0;
var cur_img = -1;
var cur_img_cat = -1;
var cur_simg;
var news_id = -1;
var project_id = -1;
var img_dim = new Array();
var img_dim2 = new Array();

var aInfo = false;

var a_busy = false;
var a_strip_phase = 0;
var a_count = 6;

$(document).ready(
	function() {
		$('#menu_'+loc).css('background-position', 'bottom');
		
		$('.menu_button').hover(
			function() {
				if($(this).attr('id').split('_')[1] != loc) {
					$(this).css('background-position', 'bottom');
				}
			},
			function() {
				if($(this).attr('id').split('_')[1] != loc) {
					$(this).css('background-position', 'top');
				}
			});
			
		$('.menu_button').click(
			function() {
				id = $(this).attr('id').split('_')[1];
				
				if(id != loc) {
					switch(id) {
						case 'main':
							redirectTo('index.php');
							break;
						default:
							redirectTo(id + '.php');
							break;
					}
				}
			});
			
		$('a').hover(
			function() {
				$(this).css('color', 'rgb(255, 200, 90)');
				$(this).css('text-decoration', 'underline');
			},
			function() {
				$(this).css('color', 'rgb(225, 180, 60)');
				$(this).css('text-decoration', 'none');
			});
		
		$('#wro2016').hover(function() { $(this).css('text-decoration', 'none'); }, function() {});
			
		$('.a2').hover(
			function() {
				$(this).css('color', 'rgb(150, 70, 30)');
				$(this).css('text-decoration', 'none');
			},
			function() {
				$(this).css('color', 'rgb(200, 120, 0)');
			});
			
		$('.eelink').hover(
			function() {
				$(this).css('color', 'rgb(255, 200, 90)');
				$(this).css('text-decoration', 'underline');
			},
			function() {
				$(this).css('color', 'rgb(225, 180, 60)');
				$(this).css('text-decoration', 'none');
			});
			
		$('.news_icon').hover(
			function() {
				id = $(this).attr('id').split('_')[2];
				$('#news_description').stop(true, false);
				$('#news_description').remove();
				$(this).css('opacity', '0.8');
				$('body').append('<div id="news_description">&nbsp;</div>');
				$('#news_description').html(nd[id]);
				$('#news_description').css('left', ($(this).offset().left + $(this).width() + 5));
				$('#news_description').css('top', ($(this).offset().top));
				$('#news_description').css('opacity', '0');
				$('#news_description').fadeTo(300, 0.8);
			},
			function() {
				$(this).css('opacity', '1');
				$('#news_description').fadeOut(300,
					function() {
						$('#news_description').remove();
					});
			});
			
		$('.project_icon').hover(
			function() {
				id = $(this).attr('id').split('_')[2];
				$('#news_description').stop(true, false);
				$('#news_description').remove();
				$(this).css('opacity', '0.8');
				$('body').append('<div id="news_description">&nbsp;</div>');
				$('#news_description').html(np[id]);
				$('#news_description').css('left', ($(this).offset().left + $(this).width() + 5));
				$('#news_description').css('top', ($(this).offset().top));
				$('#news_description').css('opacity', '0');
				$('#news_description').fadeTo(300, 0.8);
			},
			function() {
				$(this).css('opacity', '1');
				$('#news_description').fadeOut(300,
					function() {
						$('#news_description').remove();
					});
			});
			
		$('.project_iconp').hover(
			function() {
				$(this).css('opacity', '0.8');
			},
			function() {
				$(this).css('opacity', '1');
			});
			
		$('.news_icon').click(
			function() {
				id = $(this).attr('id').split('_')[2];
				redirectTo('view_news.php?id='+id);
			});
			
		$('.project_icon, .project_iconp').click(
			function() {
				id = $(this).attr('id').split('_')[2];
				redirectTo('view_project.php?id='+id);
			});
			
		$('.n_thumb').hover(
			function() {
				$(this).css('opacity', '0.7');
			},
			function() {
				$(this).css('opacity', '1');
			});
			
		$('.n_slide, #news_v_back, #archive_back, .a_slide').hover(
			function() {
				$(this).css('background-position', 'bottom');
			},
			function() {
				$(this).css('background-position', 'top');
			});
			
		$('.n_slide').click(
			function() {
				if(busy2 || busy3) return;
				
				switch($(this).attr('id').substr(8, 2)) {
					case 'l1':
						if(strip_phase > 0) {
							busy2 = true;
							
							$('#thumbnails1').animate({left: '+=480px'}, 240,
								function() {
									strip_phase--;
									
									if(strip_phase == 0) {
										$('#n_slide_l1').css('display', 'none');
									}
									
									$('#n_slide_r1').css('display', 'block');
									
									busy2 = false;
								});
						}
						
						break;
					case 'r1':
						if(img_count > ((strip_phase + 1) * 4)) {
							busy2 = true;
							
							$('#thumbnails1').animate({left: '-=480px'}, 240,
								function() {
									strip_phase++;
									
									if(strip_phase == (Math.ceil(img_count / 4) - 1)) {
										$('#n_slide_r1').css('display', 'none');
									}
									
									$('#n_slide_l1').css('display', 'block');
									
									busy2 = false;
								});
						}
						
						break;
						
					case 'l2':
						if(strip_phase2 > 0) {
							busy2 = true;
							
							$('#thumbnails2').animate({left: '+=480px'}, 240,
								function() {
									strip_phase2--;
									
									if(strip_phase2 == 0) {
										$('#n_slide_l2').css('display', 'none');
									}
									
									$('#n_slide_r2').css('display', 'block');
									
									busy2 = false;
								});
						}
						
						break;
					case 'r2':
						if(img_count2 > ((strip_phase2 + 1) * 4)) {
							busy2 = true;
							
							$('#thumbnails2').animate({left: '-=480px'}, 240,
								function() {
									strip_phase2++;
									
									if(strip_phase2 == (Math.ceil(img_count2 / 4) - 1)) {
										$('#n_slide_r2').css('display', 'none');
									}
									
									$('#n_slide_l2').css('display', 'block');
									
									busy2 = false;
								});
						}
						
						break;
				}
			});
			
		$('#news_v_back').click(
			function() {
				if($(this).attr('class').substr(0, 3) == 'arc') {
					redirectTo('view_archive.php?year=' + $(this).attr('class').substr(3));
				} else {
					redirectTo('news.php');
				}
			});
			
		$('#archive_back').click(
			function() {
				redirectTo('archive.php');
			});
			
		$('.n_thumb1').click(
			function() {
				img_id = $(this).attr('id').substr(1);
				imgLoad(img_id, 1);
			});
			
		$('.n_thumb2').click(
			function() {
				img_id = $(this).attr('id').substr(1);
				imgLoad(img_id, 2);
			});
			
		$('#n_img').click(
			function() {
				imgShow();
			});
			
		$('.artist_span').hover(
			function() {
				$(this).css('color', 'rgb(255, 180, 30)');
			},
			function() {
				$(this).css('color', '#ddd');
			});
		
		$('.artist_span').click(
			function() {
				id = $(this).attr('id').substr(7);
				
				showArtistInfo(id);
			});
				
		$('.archive_news').hover(
			function() {
				//$(this).css('background', 'rgb(30, 30, 30)');
				$(this).css('opacity', 0.5);
				//$(this).css('text-decoration', 'underline');
			},
			function() {
				//$(this).css('background', 'none');
				$(this).css('opacity', 1);
				//$(this).css('text-decoration', 'none');
			});
			
		$('.archive_news').click(
			function() {
				aid = $(this).attr('id').split('_');
				nid = aid[2];
				y = aid[3];
				
				redirectTo('view_news.php?id=' + nid + '&arc=1&year=' + y);
			});
			
		$('.a_thumb').hover(
			function() {
				$(this).css('opacity', 0.8);
				
				id = $(this).attr('id').substr(7);
				
				$('#a_info').html('rok wydania: '+a_rok_wydania[id - 1]+'<br /><span class="text_en">'+a_ilosc_stron[id - 1]+'</span>');
			},
			function() {
				$(this).css('opacity', 1);
				
				$('#a_info').html('');
			});
			
		$('.i_button, .i_text').hover(
			function() {
				$(this).css('background', 'rgb(10, 10, 10)');
			},
			function() {
				$(this).css('background', 'rgb(0, 0, 0)');
			});
			
		$('.a_thumb').click(
			function() {
				id = $(this).attr('id').substr(7);
				
				if(a_link[id - 1]) {
					redirectTo('_artmag/'+id+'.html');
				}
			});
			
		$('.a_slide').click(
			function() {
				if(a_busy) return;
				
				switch($(this).attr('id').substr(8)) {
					case 'l':
						if(a_strip_phase > 0) {
							a_busy = true;
							
							$('#artmag_thumbs').animate({left: '+=570px'}, 285,
								function() {
									a_strip_phase--;
									
									if(a_strip_phase == 0) {
										$('#a_slide_l').css('display', 'none');
									}
									
									$('#a_slide_r').css('display', 'block');
									
									a_busy = false;
								});
						}
						
						break;
					case 'r':
						if(a_count > ((a_strip_phase + 1) * 3)) {
							a_busy = true;
							
							$('#artmag_thumbs').animate({left: '-=570px'}, 285,
								function() {
									a_strip_phase++;
									
									if(a_strip_phase == (Math.ceil(a_count / 4) - 1)) {
										$('#a_slide_r').css('display', 'none');
									}
									
									$('#a_slide_l').css('display', 'block');
									
									a_busy = false;
								});
						}
						
						break;
				}
			});
			
		$('.project_event').hover(
			function() {
				$(this).css('opacity', 0.8);
			},
			function() {
				$(this).css('opacity', 1);
			});
			
		$('.project_event').click(
			function() {
				nid = $(this).attr('id').substr(1);
				
				redirectTo('view_news.php?id=' + nid);
			});
				
		if((loc == 'view_news') || (loc == 'view_project')) {
			if(img_count > 0) {
				imgLoad(1, 1);
			} else if(img_count2 > 0) {
				imgLoad(1, 2);
			}
		}
	});
	
function showArtistInfo(aid) {
	if(!aInfo) {
		aInfo = true;
			
		$('body').append('<div id="img_cover"><div id="n_loading">&nbsp;</div></div>');
		
		if($.browser.msie) {
			$('#img_cover').width(document.documentElement.clientWidth);
			$('#img_cover').height(document.documentElement.clientHeight);
		} else {
			$('#img_cover').width($(document).width());
			$('#img_cover').height($(document).height());
		}
		
		scrTop = $(document).scrollTop();
		scrLeft = $(document).scrollLeft();
		winW = $(window).width();
		winH = $(window).height();
		
		lleft = ((winW - 16) / 2) + scrLeft;
		ltop = ((winH - 16) / 2) + scrTop;
		
		$('#n_loading').css('display', 'block');
		$('#n_loading').css('left', lleft + 'px');
		$('#n_loading').css('top', ltop + 'px');
		
		$('#img_cover').css('opacity', 0);
		$('#img_cover').css('display', 'block');
		
		$('#img_cover').fadeTo(300, 0.7,
			function() {
				
				$.get('artist_info.php', { id: aid },
					function(data) {
						$('#n_loading').fadeOut(300, 
							function() {
								$('#artist_'+aid).css('color', 'rgb(255, 200, 60)');
								
								$('body').append('<div id="artist_info">'+data+'</div>');
								$('#artist_info').css('opacity', 0);
								$('#artist_info').css('display', 'block');
								
								lleft = ((winW - 410) / 2) + scrLeft;
								if(lleft < 0) lleft = 0;
								
								ltop = ((winH - $('#artist_info').height()) / 2) + scrTop;
								if(ltop < 0) ltop = 0;
								
								$('#artist_info').css('left', lleft+'px');
								$('#artist_info').css('top', ltop+'px');
								
								$('#artist_info').fadeTo(300, 0.7,
									function() {
										$('a').hover(
											function() {
												$(this).css('color', 'rgb(255, 200, 90)');
												$(this).css('text-decoration', 'underline');
											},
											function() {
												$(this).css('color', 'rgb(225, 180, 60)');
												$(this).css('text-decoration', 'none');
											});
			
										$('#img_cover').click(
											function() {
												hideArtistInfo(aid);
											});
					
										aInfo = false;
									});
							});
					}, 'text');
			});
	}
}

function hideArtistInfo(aid) {
	if(!aInfo) {
		aInfo = true;
		
		$('#img_cover').animate({opacity: 0}, {queue: false, duration: 300});
		$('#artist_info').fadeOut(300,
			function() {
				$('#artist_info').remove();
				$('#img_cover').remove();
				
				$('#artist_'+aid).css('color', '#ddd');
				
				aInfo = false;
			});
	}
}
	
function imgLoad(id, cat) {
	if(!busy && !busy3) {
		if(((id != cur_img) || (cat != cur_img_cat)) && (id >= 1) && (((cat == 1) && (id <= img_count)) || ((cat == 2) && (id <= img_count2)))) {
			busy = true;
			
			$('#n_img').fadeOut(300,
				function() {
					$('#news_images').append('<div id="n_loading">&nbsp;</div>');
					$('#n_loading').css('opacity', '0');
					$('#n_loading').css('display', 'inline');
					$('#n_loading').fadeTo(300, 1,
						function() {
							if(cat == 1) {
								imgW = img_dim[(id - 1) * 2];
								imgH = img_dim[(id - 1) * 2 + 1];
							} else if(cat == 2) {
								imgW = img_dim2[(id - 1) * 2];
								imgH = img_dim2[(id - 1) * 2 + 1];
							}
							
							if((imgW > 590) || (imgH > 400)) {
								if((imgW / imgH) > (59/40)) {
									newW = 590;
									newH = 590 * (imgH / imgW);
								} else {
									newW = 400 * (imgW / imgH);
									newH = 400;
								}
							} else {
								newW = imgW;
								newH = imgH;
							}
		
							var preload = new Image(imgW, imgH);
							
							preload.onload = 
								function() {
									$('#n_loading').fadeOut(300, 
										function() {
											if(cont == 'n') {
												$('#n_img').attr('src', '_news/' + news_id + '/' + id + ((cat == 2) ? '_2' : '') + '.jpg?' + timestamp);
											} else if(cont == 'p') {
												$('#n_img').attr('src', '_projects/' + project_id + '/' + id + '.jpg?' + timestamp);
											}
											
											$('#n_img').width(newW);
											$('#n_img').height(newH);
											
											$('#n_img').fadeIn(300,
												function() {
													cur_img = id;
													cur_img_cat = cat;
													busy = false;
												});
												
											$(this).remove();
										});
								};
      			
      						if(cont == 'n') {
								preload.src = '_news/' + news_id + '/' + id + ((cat == 2) ? '_2' : '') + '.jpg?' + timestamp;
							} else if(cont == 'p') {
								preload.src = '_projects/' + project_id + '/' + id + '.jpg?' + timestamp;
							}
						});
				});
		}
	}
}

function imgShow() {
	if(!busy && !busy2 && !busy3) {
		busy3 = true;
		
		$('body').append('<div id="img_cover">&nbsp;</div><img id="img_big" src="" alt="" />');
		
		if($.browser.msie) {
			$('#img_cover').width(document.documentElement.clientWidth);
			$('#img_cover').height(document.documentElement.clientHeight);
		} else {
			$('#img_cover').width($(document).width());
			$('#img_cover').height($(document).height());
		}
		$('#img_cover').css('opacity', 0);
		$('#img_cover').css('display', 'block');
		
		$('#img_big').attr('src', $('#n_img').attr('src'));
		$('#img_big').width($('#n_img').width());
		$('#img_big').height($('#n_img').height());
		$('#img_big').css('left', $('#n_img').offset().left + 'px');
		$('#img_big').css('top', $('#n_img').offset().top + 'px');
		$('#img_big').css('opacity', 0);
		$('#img_big').css('display', 'block');
		
		scrTop = $(document).scrollTop();
		scrLeft = $(document).scrollLeft();
		winW = $(window).width();
		winH = $(window).height();
		
		if(cur_img_cat == 1) {
			imgW = img_dim[(cur_img - 1) * 2];
			imgH = img_dim[(cur_img - 1) * 2 + 1];
		} else if(cur_img_cat == 2) {
			imgW = img_dim2[(cur_img - 1) * 2];
			imgH = img_dim2[(cur_img - 1) * 2 + 1];
		}
		
		if((imgW > (winW - 20)) || (imgH > (winH - 20))) {
			if((imgW / imgH) > (winW / winH)) {
				newW = winW - 20;
				newH = Math.floor((winW - 20) * (imgH / imgW));
				newL = 10 + scrLeft;
				newT = ((winH - newH) / 2) + scrTop;
			} else {
				newW = Math.floor((winH - 20) * (imgW / imgH));
				newH = winH - 20;
				newL = ((winW - newW) / 2) + scrLeft;
				newT = 10 + scrTop;
			}
		} else {
			newW = imgW;
			newH = imgH;
			newL = ((winW - newW) / 2) + scrLeft;
			newT = ((winH - newH) / 2) + scrTop;
		}
		
		$('#img_cover').animate({opacity: 0.8}, {queue: false, duration: 300});
		$('#img_big').animate({width: (newW+'px'), height: (newH+'px'), left: (newL+'px'), top: (newT+'px'), opacity: '1'}, 300,
			function() {
				if(((cur_img_cat == 1) && (img_count > 1)) || ((cur_img_cat == 2) && (img_count2 > 1))) {
					cur_simg = cur_img;
					$('body').append('<div class="btn_nav" id="btn_prev">&nbsp;</div><div class="btn_nav" id="btn_next">&nbsp;</div>');
					$('.btn_nav').css('top', (newT + 64) + 'px');
					$('.btn_nav').css('opacity', 0.7);
					$('#btn_prev').css('left', (newL + 'px'));
					$('#btn_next').css('left', (newL + newW - 64) + 'px');
				
					$('#img_cover').hover(
						function() {
							$('.btn_nav').css('display', 'none');
						},
						function() {});
					
					$('#img_big').mousemove(
						function(e) {
							if(busy || busy2 || busy3) return;
							
							var imgX = e.pageX - $(this).offset().left;
							
							if(imgX <= Math.max(($(this).width() / 4), 64)) {
								if($('#btn_prev').css('display') == 'none') {
									$('#btn_prev').css('display', 'block');
									
									if($('#btn_next').css('display') == 'block') {
										$('#btn_next').css('display', 'none');
									}
								}
							} else if(imgX >= Math.min(($(this).width() * 3 / 4), ($(this).width() - 64))) {
								if($('#btn_next').css('display') == 'none') {
									$('#btn_next').css('display', 'block');
									
									if($('#btn_prev').css('display') == 'block') {
										$('#btn_prev').css('display', 'none');
									}
								}
							} else {
								if($('#btn_prev').css('display') == 'block') {
									$('#btn_prev').css('display', 'none');
								}
								
								if($('#btn_next').css('display') == 'block') {
									$('#btn_next').css('display', 'none');
								}
							}
						});
					
					$('#img_big').mouseup(
						function(e) {
							if(busy || busy2 || busy3) return;
							
							var imgX = e.pageX - $(this).offset().left;
							
							if(imgX <= Math.max(($(this).width() / 4), 64)) {
								imgPrev();
							} else if(imgX >= Math.min(($(this).width() * 3 / 4), ($(this).width() - 64))) {
								imgNext();
							} else {
								imgHide();
							}
						});
						
					$('.btn_nav').click(
						function() {
							if(busy || busy2 || busy3) return;
							
							switch($(this).attr('id').substr(4)) {
								case 'prev':
									imgPrev();
									break;
								case 'next':
									imgNext();
									break;
							}
						});
				} else {
					$('#img_big').click(
						function() {
							if(busy || busy2 || busy3) return;
							
							imgHide();
						});
				}
				
				$('#img_cover').click(
					function() {
						imgHide();
					});
					
				busy3 = false;
			});
	}
}

function imgReload() {
	$('#img_big').fadeOut(300,
		function() {
			scrTop = $(document).scrollTop();
			scrLeft = $(document).scrollLeft();
			winW = $(window).width();
			winH = $(window).height();
			
			if(cur_img_cat == 1) {
				imgW = img_dim[(cur_simg - 1) * 2];
				imgH = img_dim[(cur_simg - 1) * 2 + 1];
			} else if(cur_img_cat == 2) {
				imgW = img_dim2[(cur_simg - 1) * 2];
				imgH = img_dim2[(cur_simg - 1) * 2 + 1];
			}
			
			if((imgW > (winW - 20)) || (imgH > (winH - 20))) {
				if((imgW / imgH) > (winW / winH)) {
					newW = winW - 20;
					newH = Math.floor((winW - 20) * (imgH / imgW));
					newL = 10 + scrLeft;
					newT = ((winH - newH) / 2) + scrTop;
				} else {
					newW = Math.floor((winH - 20) * (imgW / imgH));
					newH = winH - 20;
					newL = ((winW - newW) / 2) + scrLeft;
					newT = 10 + scrTop;
				}
			} else {
				newW = imgW;
				newH = imgH;
				newL = ((winW - newW) / 2) + scrLeft;
				newT = ((winH - newH) / 2) + scrTop;
			}
			
			$('.btn_nav').css('top', (newT + 64) + 'px');
			$('#btn_prev').css('left', (newL + 'px'));
			$('#btn_next').css('left', (newL + newW - 64) + 'px');
			
			$('body').append('<div id="n_loading">&nbsp;</div>');
			$('#n_loading').css('left', ((winW - 16) / 2 + scrLeft) + 'px');
			$('#n_loading').css('top', ((winH - 16) / 2 + scrTop) + 'px');
			$('#n_loading').css('opacity', '0');
			$('#n_loading').css('display', 'block');
			$('#n_loading').fadeTo(300, 0.7,
				function() {
					var preload = new Image(imgW, imgH);
					
					preload.onload = 
						function() {
							$('#n_loading').fadeOut(300, 
								function() {
									if(cont == 'n') {
										$('#img_big').attr('src', '_news/' + news_id + '/' + cur_simg + ((cur_img_cat == 2) ? '_2' : '') + '.jpg?' + timestamp);
									} else if(cont == 'p') {
										$('#img_big').attr('src', '_projects/' + project_id + '/' + cur_simg + '.jpg?' + timestamp);
									}
									
									$('#img_big').width(newW);
									$('#img_big').height(newH);
									$('#img_big').css('left', newL + 'px');
									$('#img_big').css('top', newT + 'px');
									
									$('#img_big').fadeIn(300,
										function() {
											busy3 = false;
										});
										
									$(this).remove();
								});
						};
		
					if(cont == 'n') {
						preload.src = '_news/' + news_id + '/' + cur_simg + ((cur_img_cat == 2) ? '_2' : '') + '.jpg?' + timestamp;
					} else if(cont == 'p') {
						preload.src = '_projects/' + project_id + '/' + cur_simg + '.jpg?' + timestamp;
					}
				});
		});
}

function imgPrev() {
	if(!busy && !busy2 && !busy3) {
		busy3 = true;
		
		$('.btn_nav').css('display', 'none');
		
		cur_simg--;
		
		if(cur_simg <= 0) {
			if(cur_img_cat == 1) {
				cur_simg = img_count;
			} else {
				cur_simg = img_count2;
			}
		}
		
		imgReload();
	}
}

function imgNext() {
	if(!busy && !busy2 && !busy3) {
		busy3 = true;
		
		$('.btn_nav').css('display', 'none');
		
		cur_simg++;
		
		if(cur_img_cat == 1) {
			if(cur_simg > img_count) {
				cur_simg = 1;
			}
		} else {
			if(cur_simg > img_count2) {
				cur_simg = 1;
			}
		}
		
		imgReload();
	}
}

function imgHide() {
	if(!busy && !busy2 && !busy3) {
		busy3 = true;
		
		$('.btn_nav').remove();
//		$('#img_cover').animate({opacity: 0}, {queue: false, duration: 300});
		
		curW = $('#n_img').width();
		curH = $('#n_img').height();
		curL = $('#n_img').offset().left;
		curT = $('#n_img').offset().top;
		
// 		$('#img_big').animate({width: (curW+'px'), height: (curH+'px'), left: (curL+'px'), top: (curT+'px'), opacity: '0'}, 300,
// 			function() {
// 				$('#img_cover, #img_big').remove();
// 					
// 				busy3 = false;
// 			});
			
		$('#img_big').fadeOut(300,
			function() {
				$('#img_cover').fadeOut(300,
					function() {
						$('#img_cover, #img_big').remove();
					});
					
				busy3 = false;
			});
	}
}
	
function redirectTo(url) {
	location.href = url;
}