$(function(){
	init_productmaster();
});

function init_productmaster()
{
	switch_cat();
	move_to_sidecolumn();
	activate_faq();
	initLightbox();
}

function switch_cat()
{
	var $_GET 	= getQueryParams(document.location.search);
	var ref		= '';
	if($_GET['MP'] && $_GET['MP'] != undefined)
	{
		ref 	= $_GET['MP'].substring($_GET['MP'].indexOf('-')+1);
		if(ref == instal_prod || ref == adv_prod || ref == end_prod)
		{
			$('#pm-body-support').hide();
			$('#pm-sidecol-support').hide();	
	
			correct_brochure(ref);
			appendSwitch(ref, 'prod', $_GET);
		}
		else if(ref == instal_sup || ref == adv_sup || ref == end_sup)
		{
			$('#pm-body-prod').hide();
			$('#pm-sidecol-prod').hide();

			show_prod_image();
			correct_doc_links();
			appendSwitch(ref, 'sup', $_GET);
		}
	}
}

function appendSwitch(ref, func, $_GET)
{
	var url 		= [];
	var img 		= 'typo3conf/ext/lwtpl_mark_v1/tpl/resources/info_button.jpg';
	var container 	= $('#pm-sidecol-support');

	if(func == "prod")
	{
		if(ref == instal_prod)
		{
			$_GET['MP'] = prod + '-' + instal_sup;
		}
		else if(ref == adv_prod)
		{
			$_GET['MP'] = prod + '-' + adv_sup;
		}
		else
		{
			$_GET['MP'] = prod + '-' + end_sup;
		}
		img 		= 'typo3conf/ext/lwtpl_mark_v1/tpl/resources/downloads_button.jpg';
		container 	= $('#pm-sidecol-prod');	
	}
	else
	{
		if(ref == instal_sup)
		{
			$_GET['MP'] = prod + '-' + instal_prod;
		}
		else if(ref == adv_sup)
		{
			$_GET['MP'] = prod + '-' + adv_prod;
		}
		else
		{
			$_GET['MP'] = prod + '-' + end_prod;
		}
	}

	for (var i in $_GET)
	{
		 url.push(i + '=' + $_GET[i])
	}

	url = 'index.php?' + url.join('&');
	$(container).append(
				$('<div></div>')
					.attr({'style' : 'background:url('+img+') no-repeat;width:273px;height:57px;cursor:pointer;margin:auto;margin-top:10px;'})
					.click(function(){document.location.href = url })
			
		);
}

function getProdPage(pid)
{
	document.location.href = 'index.php?id=' + pid + document.location.search.substring(document.location.search.indexOf('&'));
}

function move_to_sidecolumn()
{
	$('#body-right-col-inner-b2').append($('#pm-sidecol-prod'));
	$('#body-right-col-inner-b2').append($('#pm-sidecol-support'));
}

function show_prod_image()
{
	$('#pm-body-support').prepend($('#pm-body-prod-prod-img'));
}

function correct_doc_links()
{
	var files		= '';
	var filenames	= '';
	var filename	= '';
	var file_ext	= '';
	var curr 		= '';

	if($('.pm-body-support-doc-container-double').length)
	{
		$('.doc-container').each(function(index) {
				files		= $(this).children(':last-child').children(':first-child').children(':first-child').html().split(',')
				filenames	= $(this).children(':last-child').children(':first-child').children(':last-child').html().split('|')

				curr 		= $(this).children(':last-child')
				curr.html('');

				$.each(files, function(key, value){
					file_ext	= value.substring(value.indexOf('.') + 1);
					file_name 	= filenames[key];	

					curr.append(
						$('<a></a>').attr({'class' : 'pm-doc-link', 'href' : 'uploads/tx_templavoila/' + value}).append(
							$('<div></div>')
							.attr({'class' : 'pm-doc-link-container pm-doc-type-unknown pm-doc-type-' + file_ext})
							.html((file_name != '') ? file_name : value)
						)
					)
				});
/*
				$(this).attr({'class' : 'pm-doc-link', 'href' : 'uploads/tx_templavoila/' + value})
				$(this).parent().prepend(
						$('<div></div>')
						.attr({'class' : 'pm-doc-link-container pm-doc-type-' + file_ext})
						.append($(this).parent().children('#pm-body-support-techbooks-filesize'))
						.prepend($(this))
					);
					*/
		});
	}
}

function correct_brochure(ref)
{
	var file_container  = '';
	var filename		= '';
	var file_ext		= '';

	if(ref == end_prod)
	{
		$('#pm-sidecol-prod-brochure-default').hide();
		file_container	= $('#pm-sidecol-prod-brochure-enduser');
	}
	else
	{
		$('#pm-sidecol-prod-brochure-enduser').hide();
		file_container 	= $('#pm-sidecol-prod-brochure-default');
	}
	if($(file_container).length)
	{
		value 		= $(file_container).html();
		filename	= value.substring(value.lastIndexOf('/') + 1,value.indexOf('.'));
		file_ext	= value.substring(value.indexOf('.') + 1);

		$(file_container).html('');

		$(file_container).append(
				$('<div></div>')
				.attr({'class' : 'pm-doc-link-container pm-doc-type-' + file_ext})
				.append(
					$('<a></a>')
					.attr({'class' : 'pm-doc-link', 'href' : value})
					.append($('#pm-sidecol-prod-brochure-name'))	
					)
				.append(
					$('<div></div>')
					.attr({'class' : 'pm-doc-file-info', 'id' : 'pm-file' + filename}) 
					)
				);
	}
	if($('#pm-sidecol-prod-brochure-link').length)
	{
		var url 	= $('#pm-sidecol-prod-brochure-link a').attr('href')
		var $_GET 	= getQueryParams(document.location.search);
		var ref 	= $_GET['MP'].substring($_GET['MP'].indexOf('-')+1);

		if(ref == instal_prod)
		{
			 $('#pm-sidecol-prod-brochure-link a').attr({'href' : 'index.php?id=' + instal_broch + '&L=' + $_GET['L']})
		}
		else if(ref == adv_prod)
		{
			$('#pm-sidecol-prod-brochure-link a').attr({'href' : 'index.php?id=' + adv_broch + '&L=' + $_GET['L']})
		}
		else
		{
			$('#pm-sidecol-prod-brochure-link a').attr({'href' : 'index.php?id=' + end_broch + '&L=' + $_GET['L']})
		}
	}
}

function activate_faq()
{
	var uid 		= '';

	if ($('.tx-irfaq-pi1').length){
		$('.tx-irfaq-pi1').append($('<div></div>').attr({'id' : 'pm-faq-answer'}));	
		$('#tx-irfaq-questions-custom').append($('#pm-body-support-faqdatabase-linktxt'));
		$('.tx-irfaq-answer').hide();
			
		$('.li-irfaq').each(function(index) {
				$(this).click(function(){
					uid			= '#' + $(this).attr('id').substring($(this).attr('id').indexOf('li_')+3);

					$('#pm-faq-answer').css({'opacity' : 0});
					$('#pm-faq-answer').html($(uid).html());
					$('#pm-faq-answer').stop().animate({opacity: 1}, 500);
				});
		});
	}
}

function getQueryParams(qs) {
	var url = window.location.toString();
	url.match(/\?(.+)$/);
	var params = RegExp.$1;
	var params = params.split("&");
	var queryStringList = {};

	for(var i=0;i<params.length;i++)
	{
		var tmp = params[i].split("=");
		queryStringList[tmp[0]] = unescape(tmp[1]);
	}
	return queryStringList;
}

function initLightbox()
{
	$('#pm-body-prod-mood-img img').each(function(index){
		$(this).attr({'rel' : '#img'+index})
	});

	$('#pm-body-prod-mood-img-big img').each(function(index){
		$(this).wrap($('<div></div>').attr({'id' : 'img'+index, 'class' : 'img-overlay'}));
	});

	$("img[rel]").overlay({
			mask: {
				color: '#000',
				loadSpeed: 200,
				opacity: 0.3
			}
	});
}


