var activeColor = '#000000';
var readyColor = '#7b1717';

function pageNew() {
	$('#pageLinks').css('display', 'block');

	if($('#pageMain').length || $('#pageMainRight').length) { 
		pageMain();
	}
	
	if($('#pageServices').length) { 
		pageServices();
	}
	else if($('#pageInfos').length) { 
		pageInfos();
	}
	else if($('#pageShowrooms').length) { 
		pageShowrooms();
	}
	else if($('#pageContacts').length) { 
		pageContacts();
	}
	else if($('#pageDetails').length) { 
		pageDetails();
	}
	
	$('#pageTitleImg').appendTo('#pageTitleBox');
	
	$('#magnifyLink').click(function(){
		magnifyImage();
		return false;
	});
}

function pageMain() {
	var pageBox01Height = $('#pageBox_01').height();
	var pageBox02Height = $('#pageBox_02').height();
	var pageBox03Height = $('#pageBox_03').height();
	var pageBox04Height = $('#pageBox_04').height();
	var pageBoxImgHeight = $('#itemImg').height();
	var pageBoxHeight = pageBox01Height;
	pageBox02Height > pageBoxHeight ? pageBoxHeight = pageBox02Height : pageBoxHeight;
	pageBox03Height > pageBoxHeight ? pageBoxHeight = pageBox03Height : pageBoxHeight;
	pageBox04Height > pageBoxHeight ? pageBoxHeight = pageBox04Height : pageBoxHeight;
	pageBoxImgHeight > pageBoxHeight ? pageBoxHeight = pageBoxImgHeight : pageBoxHeight;
	$('#mainPanel').css('height', pageBoxHeight + 120);
	
	$('#itemImg').css('display', 'none');
	$('#itemName').css('display', 'none');		// docelowo do usuniêcia
	$('#doorPrice').css('display', 'none');
	$('#pageText').css('display', 'none');
	$('#pageBox_01').css('display', 'none');	// docelowo do usuniêcia
	$('#pageBox_02').css('display', 'none');	// docelowo do usuniêcia
	$('#pageBox_03').css('display', 'none');	// docelowo do usuniêcia
	$('#pageBox_04').css('display', 'none');	// docelowo do usuniêcia
	////$('#pageTitleImg').fadeIn(1000);
	$('#itemImg').fadeIn(1000, function(){
		$('#itemName').fadeIn(1000);			// docelowo do usuniêcia
		$('#doorPrice').fadeIn(1000);
		$('#pageText').fadeIn(1000);
		$('#pageBox_01').fadeIn(1000);			// docelowo do usuniêcia
	});
}

function pageDetails() {
	var pageBox01Height = $('#pageDetailsBox_01').height();
	var pageBox02Height = $('#pageDetailsBox_02').height();
	var pageBox03Height = $('#pageDetailsBox_03').height();
	var pageBox04Height = $('#pageDetailsBox_04').height();
	var pageBoxHeight = pageBox01Height;
	pageBox02Height > pageBoxHeight ? pageBoxHeight = pageBox02Height : pageBoxHeight;
	pageBox03Height > pageBoxHeight ? pageBoxHeight = pageBox03Height : pageBoxHeight;
	pageBox04Height > pageBoxHeight ? pageBoxHeight = pageBox04Height : pageBoxHeight;
	$('#mainPanel').css('height', pageBoxHeight + 120);
	
	$('#itemImgSmall').css('display', 'none');
	$('#pageDetailsText').css('display', 'none');
	$('#pageDetailsBox_00').css('display', 'none');
	$('#pageDetailsBox_01').css('display', 'none');
	$('#pageDetailsBox_02').css('display', 'none');
	$('#pageDetailsBox_03').css('display', 'none');
	$('#pageDetailsBox_04').css('display', 'none');
	////$('#pageTitleImg').css('display', 'block');
	$('#itemImgSmall').fadeIn(1000);
	$('#pageDetailsBox_00').fadeIn(1000, function(){
		$('#pageDetailsText').fadeIn(1000);
		$('#pageDetailsBox_01').fadeIn(1000);
	});	
}

function offerMenu() {
	$('.offerStartCell > a > img').mouseenter(function(){
		var imgName = $(this).attr('src'); 
		var imgNameActive = imgName.replace('.', '_out.');
		$(this).attr('src', imgNameActive);
		$(this).mouseleave(function(){
			$(this).attr('src', imgName);
		});
	});
}

function pageServices() {
	if($('#servicesDataBox').length) {
		$('#mainPanel').css('height', $('#servicesDataBox').height() + 100);
	}
	else {
		$('#mainPanel').css('height', $('#pageServices').height() + 40);
	}
	
	$('#itemImgSmall').css('display', 'none');
	$('#pageDetailsText').css('display', 'none');
	$('#pageServices').css('display', 'none');
	/////$('#pageTitleImg').css('display', 'block');
	$('#itemImgSmall').fadeIn(1000);
	$('#pageServices').fadeIn(1000, function(){
		$('#pageDetailsText').fadeIn(1000);
	});	
}

function pageInfos() {
	var pageBox01Height = $('#pageBox_01').height();
	var pageBox02Height = $('#pageBox_02').height();
	var pageBox03Height = $('#pageBox_03').height();
	var pageBox04Height = $('#pageBox_04').height();
	var pageBoxImgHeight = $('#pageImg').height();
	var pageBoxHeight = pageBox01Height;
	pageBox02Height > pageBoxHeight ? pageBoxHeight = pageBox02Height : pageBoxHeight;
	pageBox03Height > pageBoxHeight ? pageBoxHeight = pageBox03Height : pageBoxHeight;
	pageBox04Height > pageBoxHeight ? pageBoxHeight = pageBox04Height : pageBoxHeight;
	pageBoxImgHeight > pageBoxHeight ? pageBoxHeight = pageBoxImgHeight : pageBoxHeight;
	$('#mainPanel').css('height', pageBoxHeight + 40);
	
	$('#pageImg').css('display', 'none');
	$('#pageBox_01').css('display', 'none');
	$('#pageBox_02').css('display', 'none');
	$('#pageBox_03').css('display', 'none');
	$('#pageBox_04').css('display', 'none');
	/////$('#pageTitleImg').css('display', 'block');
	$('#pageImg').fadeIn(1000, function(){
		$('#pageBox_01').fadeIn(1000);
	});	
}

function pageShowrooms() {
	var pageBox01Height = $('#showroomMain').height();
	var pageBox02Height = $('#showroomDrive').height();
	var pageBox03Height = $('#showroomMap').height();
	var pageBoxHeight = pageBox01Height;
	pageBox02Height > pageBoxHeight ? pageBoxHeight = pageBox02Height : pageBoxHeight;
	pageBox03Height > pageBoxHeight ? pageBoxHeight = pageBox03Height : pageBoxHeight;
	$('#mainPanel').css('height', pageBoxHeight + 40);
	
	$('#showroomGallery').css('display', 'none');
	$('#showroomText').css('display', 'none');
	$('#showroomDrive').css('display', 'none');
	$('#showroomMap').css('display', 'none');
	/////$('#pageTitleImg').css('display', 'block');
	$('#showroomGallery').fadeIn(1000, function(){
		$('#showroomText').fadeIn(1000);
	});	
}

function pageContacts() {
	var pageBox01Height = $('#showroomMain').height();
	var pageBox02Height = $('#showroomDrive').height();
	var pageBoxHeight = pageBox01Height;
	pageBox02Height > pageBoxHeight ? pageBoxHeight = pageBox02Height : pageBoxHeight;
	$('#mainPanel').css('height', pageBoxHeight + 40);
	
	$('#contactText').css('display', 'none');
	$('#showroomDrive').css('display', 'none');
	/////$('#pageTitleImg').css('display', 'block');
	$('#showroomText').fadeIn(1000);
}

function showroomGallery() {
	for(i=4; i>0; i--) {
		$('#showroomImgMain').attr('src', $('#showroomImgThumb_'+i).attr('src').replace('_thumb', ''));
	}
	
	$('#showroomImgThumb_1').addClass('showroomThumbActive').fadeTo(0, 0.2);

	$('.showroomThumbReady').click(function(){
		var activeImg = $(this).attr('src').replace('_thumb', '');
		$('.showroomThumbActive').removeClass('showroomThumbActive').addClass('showroomThumbReady').fadeTo(500, 1);
		$(this).removeClass('showroomThumbReady').addClass('showroomThumbActive').fadeTo(500, 0.2);
		$('#showroomImgMain').fadeTo(250, 0.2, function(){
			$(this).attr('src',activeImg).fadeTo(250, 1);
		});
	});
}

function clickAnchorLinks(type) {
	var aLink = new Array(5);
	var aBox = new Array(5);

	if(type == 'info') {
		aLink[1] = '#pageLink01';
		aLink[2] = '#pageLink02';
		aLink[3] = '#pageLink03';
		aLink[4] = '#pageLink04';
		
		aBox[1] = '#pageBox_01';
		aBox[2] = '#pageBox_02';
		aBox[3] = '#pageBox_03';
		aBox[4] = '#pageBox_04';
	}
	if(type == 'showroom') {
		aLink[1] = '#pageLink01';
		aLink[2] = '#pageLink02';
		aLink[3] = '#pageLink03';
		aLink[4] = '#pageLink04';
		
		aBox[1] = '#showroomMain';
		aBox[2] = '#showroomDrive';
		aBox[3] = '#showroomMap';
		aBox[4] = '#empty';
	}
	else {
		aLink[1] = '#pageDetailsLink01';
		aLink[2] = '#pageDetailsLink02';
		aLink[3] = '#pageDetailsLink03';
		aLink[4] = '#pageDetailsLink04';
		
		aBox[1] = '#pageDetailsBox_01';
		aBox[2] = '#pageDetailsBox_02';
		aBox[3] = '#pageDetailsBox_03';
		aBox[4] = '#pageDetailsBox_04';
	}
	
	$(aLink[1]).click(function(){
		var isBox02Visible = $(aBox[2]).css('display');
		var isBox03Visible = $(aBox[3]).css('display');
		var isBox04Visible = $(aBox[4]).css('display');
		activeLink(aLink[1]);
		if(isBox02Visible == 'block') {
			$(aBox[2]).fadeOut(1000, function(){
				$(aBox[1]).fadeIn(1000);
			});
			readyLink(aLink[2]);
		}
		if(isBox03Visible == 'block') {
			$(aBox[3]).fadeOut(1000, function(){
				$(aBox[1]).fadeIn(1000);
			});
			readyLink(aLink[3]);
		}
		if(isBox04Visible == 'block') {
			$(aBox[4]).fadeOut(1000, function(){
				$(aBox[1]).fadeIn(1000);
			});
			readyLink(aLink[4]);
		}
	});
	
	$(aLink[2]).click(function(){
		var isBox01Visible = $(aBox[1]).css('display');
		var isBox03Visible = $(aBox[3]).css('display');
		var isBox04Visible = $(aBox[4]).css('display');
		activeLink(aLink[2]);
		if(isBox01Visible == 'block') {
			$(aBox[1]).fadeOut(1000, function(){
				$(aBox[2]).fadeIn(1000);
			});
			readyLink(aLink[1]);
		}
		if(isBox03Visible == 'block') {
			$(aBox[3]).fadeOut(1000, function(){
				$(aBox[2]).fadeIn(1000);
			});
			readyLink(aLink[3]);
		}
		if(isBox04Visible == 'block') {
			$(aBox[4]).fadeOut(1000, function(){
				$(aBox[2]).fadeIn(1000);
			});
			readyLink(aLink[4]);
		}
	});
	
	$(aLink[3]).click(function(){
		var isBox01Visible = $(aBox[1]).css('display');
		var isBox02Visible = $(aBox[2]).css('display');
		var isBox04Visible = $(aBox[4]).css('display');
		activeLink(aLink[3]);
		if(isBox01Visible == 'block') {
			$(aBox[1]).fadeOut(1000, function(){
				$(aBox[3]).fadeIn(1000);
			});
			readyLink(aLink[1]);
		}
		if(isBox02Visible == 'block') {
			$(aBox[2]).fadeOut(1000, function(){
				$(aBox[3]).fadeIn(1000);
			});
			readyLink(aLink[2]);
		}
		if(isBox04Visible == 'block') {
			$(aBox[4]).fadeOut(1000, function(){
				$(aBox[3]).fadeIn(1000);
			});
			readyLink(aLink[4]);
		}
	});
	
	$(aLink[4]).click(function(){
		var isBox01Visible = $(aBox[1]).css('display');
		var isBox02Visible = $(aBox[2]).css('display');
		var isBox03Visible = $(aBox[3]).css('display');
		activeLink(aLink[4]);
		if(isBox01Visible == 'block') {
			$(aBox[1]).fadeOut(1000, function(){
				$(aBox[4]).fadeIn(1000);
			});
			readyLink(aLink[1]);
		}
		if(isBox02Visible == 'block') {
			$(aBox[2]).fadeOut(1000, function(){
				$(aBox[4]).fadeIn(1000);
			});
			readyLink(aLink[2]);
		}
		if(isBox03Visible == 'block') {
			$(aBox[3]).fadeOut(1000, function(){
				$(aBox[4]).fadeIn(1000);
			});
			readyLink(aLink[3]);
		}
	});
}

function activeLink(linkId) {
	$(linkId).removeClass('pageLinkReady').addClass('pageLinkActive');
}

function readyLink(linkId) {
	$(linkId).removeClass('pageLinkActive').addClass('pageLinkReady');
}

function magnifyImage() {
	var plateWidth = document.body.clientWidth;
	var plateHeight = document.body.clientHeight;
	var plate = document.createElement('div');
	$(plate).css('position', 'absolute').css('left', '0px').css('top', '0px');
	$(plate).css('width', plateWidth).css('height', plateHeight);
	$(plate).css('display', 'none');
	$(plate).attr('id', 'plate');
	$(plate).appendTo('body');
	$(plate).css('opacity','0').show().fadeTo(10, 0.7, function(){
		var imgPath = $('#magnifyLink > img').attr('src');
		var aImgName = imgPath.split('/');
		var sImgName = aImgName[aImgName.length-1].replace('jpg', 'png');
		var doorPresentationImg = document.createElement('img');
		$(doorPresentationImg).appendTo('body');
		$(doorPresentationImg).attr('id', 'magnifyImg');
		$(doorPresentationImg).attr('src', '../../images/stories/oferta/magnify/' + sImgName);
		$(doorPresentationImg).error(function(){
			sImgName = sImgName.replace('png', 'jpg');
			$(doorPresentationImg).attr('src', '../images/stories/oferta/magnify/' + sImgName);
			$(doorPresentationImg).error(function(){
                    $(doorPresentationImg).attr('src', 'http://www.klimek-klus.pl/images/stories/oferta/magnify/' + sImgName);
               });
		});
		var imgWidth = $('#magnifyImg').width();
		imgWidth == 0 ? imgWidth = 300 : imgWidth;
		var imgLeft = (plateWidth - imgWidth) / 2;
		$(doorPresentationImg).css('position', 'absolute').css('left', imgLeft).css('top', '20px');
		$(doorPresentationImg).css('display', 'none');
		$(doorPresentationImg).fadeIn(200);
		$(plate).click(function(){
			$(doorPresentationImg).fadeOut(200, function(){
				$(doorPresentationImg).remove();
				$(plate).fadeOut(10, function(){
					$(plate).remove();
				});
			});
		});
		$(doorPresentationImg).click(function(){
			$(doorPresentationImg).fadeOut(200, function(){
				$(doorPresentationImg).remove();
				$(plate).fadeOut(10, function(){
					$(plate).remove();
				});
			});
		});
	});
}
