// JavaScript Document

function loadWeather()
{
	new Ajax.Request("/server/LoadWeather.php",
    {
        method: "post"
    }
    );	
}

function showPic()
{
	$('link_pic').className   = 'actve';
	$('link_video').className = '';
	
	$('div_pic').show();
	$('div_video').hide();
}

function showVideo()
{	
	$('link_pic').className   = '';
	$('link_video').className = 'actve';
	
	$('div_pic').hide();
	$('div_video').show();
}

function showWeather()
{
	$('li_weather').className = 'border_left';
	$('li_fixing').className  = '';
	
	$('link_weather').className = 'actve';
	$('link_fixing').className  = '';
	
	$('div_weather').show();
	$('div_fixing').hide();
}

function showFixing()
{
	$('li_weather').className = '';
	$('li_fixing').className  = 'border_right';
	
	$('link_weather').className = '';
	$('link_fixing').className  = 'actve';
	
	$('div_weather').hide();
	$('div_fixing').show();
}




function enlargePic(imgSrc)
{
	var src   = imgSrc;
	var img = $('bigImg');	
	
	var imgPreloader = new Image();
	
	new Effect.Opacity(img, Object.extend(
						{
					   		duration: 0.25, 
							from:1.0, 
							to:0.0,
							afterFinishInternal: function(effect) 
							{
								imgPreloader.src = src;
							}
						})
			);	
	
	// once image is preloaded, appear the image
	imgPreloader.onload = function()
	{
		img.src 	= src;
		new Effect.Appear(img, {duration: 0.25});
	}	
}

function defaultArticleFont()
{
	var div  = $('articleDescription');
	var div2 = $('commentsDiv');
	
	if(div) div.style.fontSize   = "12px";
	if(div2) div2.style.fontSize = "12px";
}

function increaseArticleFont()
{
	var div  = $('articleDescription');
	var div2 = $('commentsDiv');
	
	if(div.style.fontSize == "")
		div.style.fontSize = "12px";
	var size = parseInt(div.style.fontSize.replace("px",""));
	div.style.fontSize = (size + 1) + "px";
	
	if(div2)
	{
		if(div2.style.fontSize == "")
			div2.style.fontSize = "12px";
		div2.style.fontSize = (size + 1) + "px";
	}
	
	
	
}

function decreaseArticleFont()
{
	var div = $('articleDescription');
	var div2 = $('commentsDiv');
	
	if(div.style.fontSize == "")
		div.style.fontSize = "12px";
	var size = parseInt(div.style.fontSize.replace("px",""));
	div.style.fontSize = (size - 1) + "px";
	
	if(div2)
	{
		if(div2.style.fontSize == "")
			div2.style.fontSize = "12px";
		div2.style.fontSize = (size - 1) + "px";
	}
}

function showComments()
{
	$('commentsDiv').show();
	$('articleDiv').hide();
	$('article_lnk').className = "";
	$('comment_lnk').className = "active_in";
}

function showArticleDetails()
{
	$('commentsDiv').hide();
	$('articleDiv').show();
	$('article_lnk').className = "active_in";
	$('comment_lnk').className = "";
}

function openDetailedSearch()
{
	$('det_search').show();
	//$('link_det_search').hide();
	//$('link_srt_search').show();
}

function openShortSearch()
{
	$('det_search').hide();
	//$('link_det_search').show();
	//$('link_srt_search').hide();
}

function openCalendar(inp)
{
	displayCalendar(inp, "dd.mm.yyyy", inp);
}

function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function calculateDate()
{
	var fromDate = new Date();
	var curDate  = $F('from_date').split('.');
	
	var months   = (getCheckedValue($('abonament').plan1));
	if (!months) months   = (getCheckedValue($('abonament').plan2));
	if (!months) months   = (getCheckedValue($('abonament').plan3));

	//var months   = (getCheckedValue($('abonament').plan));
	fromDate.setFullYear(parseInt(curDate[2]), (parseInt(curDate[1]) - 1), curDate[0]);
	
	fromDate.setMonth(fromDate.getMonth() + parseInt(months));
	$('to_date').value = fromDate.getDate() + '.' + (fromDate.getMonth() + 1) + '.' + fromDate.getFullYear();
}



function loadShareButton()
{
	new Ajax.Request(file, 
					 { parameters: 
					 	{ 
							func: 'loadShareButton',
							content_type: 'application/javascript',
							evalJS: 'force'
						}, 
						onComplete: function (result) { $('share_button').innerHTML = result.responseText; }
					 }
					);
}

function deleteComment(id)
{
	new Ajax.Request(file, 
					 { parameters: 
					 	{ 
							func: 'deleteComment',
							id: id
						}, 
						onComplete: function (result) { if(result.responseText == "done") $('comment_' + id).remove();}
					 }
					);
}

function makeEditable(id, lang)
{
	new Ajax.InPlaceEditor('c_text_' + id, file + '?el=text' + lang + '&func=editComment&id=' + id, 
						   		{
									formClassName:'formEdit',
									highlightcolor: '#eeeeee',
									hoverClassName: 'overEdit',
									clickToEditText: 'Редактирай',
									rows:10
								}
						   );	
}


function initAjaxIndicator()
{
	Ajax.Responders.register(
	{
  		onCreate: function() 
  		{
    		$('indicator').show();
  		},
  		onComplete: function() 
  		{
    		$('indicator').hide();
  		}
	});
}

function checkCommentCode()
{
	new Ajax.Request(file, 
					 { parameters: 
					 	{ 
							func: 'codeCheck',
							recaptcha_challenge_field: $('addComment').elements['recaptcha_challenge_field'].value,
							recaptcha_response_field: $('addComment').elements['recaptcha_response_field'].value
						}, 
						asynchronous: false,
						onComplete: function (result) 
									{ 
										if(result.responseText == "done") 
										{
											$('addComment').submit();
										} 
										else 
										{
											alert('Не сте въвели Правилен код');
											$('code').focus();	
										}
									}
					 }
					);
}

function validateAddComment()
{
	//if($F('name') == "" || $F('name') == "Име" || $F('name') == "Name")	
	//{
		//alert('You didn`t enter a name');
		//$('name').focus();
		//return false;
	//}
	
	if($F('text') == "")	
	{
		alert('You didn`t enter a comment');
		$('text').focus();
		return false;
	} else {			return true;		}
	
	//checkCommentCode();				
	//return false;
}

function validateSendFriend()
{
	var Exp1=/^[a-zA-Z0-9\._-]+@+[a-zA-Z0-9\._-]+\.+[a-zA-Z]{2,4}$/;
	
	if($F('name2') == "")	
	{
		alert('Не сте въвели `Вашето име`');
		$('name').focus();
		return false;
	}
	
	if($F('from_mail') == "")	
	{
		alert('Не сте въвели `От (e-mail)`');
		$('from_mail').focus();
		return false;
	}
	
	
	if(!(Exp1.exec($F('from_mail'))))
	{
		alert('Моля въведете валиден `От (e-mail)`!');
        $('from_mail').focus();
		return false;
	}
	
	if($F('to_mail') == "")	
	{
		alert('Не сте въвели `До кого (e-mail)`');
		$('to_mail').focus();
		return false;
	}
	
	if(!(Exp1.exec($F('to_mail'))))
	{
		alert('Моля въведете валиден `До кого (e-mail)`!');
        $('to_mail').focus();
		return false;
	}
	
	return true;
}

function validateSubscribe()
{
	
	var Exp1=/^[a-zA-Z0-9\._-]+@+[a-zA-Z0-9\._-]+\.+[a-zA-Z]{2,4}$/;
	
	if($F('name') == "")	
	{
		toolTip($('name'), 4);
		return false;
	}
	
	if($F('mail') == "")	
	{
		toolTip($('mail'), 5);
		return false;
	}
	
	
	if(!(Exp1.exec($F('mail'))))
	{
		toolTip($('mail'), 6);
		return false;
	}
	
	if($F('re_mail') == "")	
	{
		toolTip($('re_mail'), 7);
		return false;
	}
	
	if($F('re_mail') != $F('mail'))	
	{
		toolTip($('re_mail'), 8);
		return false;
	}
	
	
	codeCheck();
	return false;
}

function validateAbonament()
{
	
	m=0;m2=0;m3=0;
			
	if (document.forms['abonament'].plan1.length)
	{
		for(k=0;k<document.forms['abonament'].plan1.length;k++)
		{
			if (document.forms['abonament'].plan1[k].checked==true) 
			{
				 m=1;
			}
		}
	}
	else 
	{
		if (document.forms['abonament'].plan1.checked==true) 
			{
				 m=1;
			}
	}
	
	
	if (document.forms['abonament'].plan2.length)
	{
		for(k=0;k<document.forms['abonament'].plan2.length;k++)
		{
			if (document.forms['abonament'].plan2[k].checked==true) 
			{
				 m2=1;
			}
		}
	}
	else 
	{
		if (document.forms['abonament'].plan2.checked==true) 
			{
				 m2=1;
			}
	}
	
	if (document.forms['abonament'].plan3.length)
	{
		for(k=0;k<document.forms['abonament'].plan3.length;k++)
		{
			if (document.forms['abonament'].plan3[k].checked==true) 
			{
				 m3=1;
			}
		}
	}
	else 
	{
		if (document.forms['abonament'].plan3.checked==true) 
			{
				 m3=1;
			}
	}
	
	
	if ( (m==0) && (m2==0) && (m3==0) )
	{
		 alert('Не сте избрали `Тарифи`');
		 return false;	
	}
	
	
	/*
	m=0
			
	if (document.forms['abonament'].plan.length)
	{
		for(k=0;k<document.forms['abonament'].plan.length;k++)
		{
			if (document.forms['abonament'].plan[k].checked==true) 
			{
				 m=1;
			}
		}
	}
	else 
	{
		if (document.forms['abonament'].plan.checked==true) 
			{
				 m=1;
			}
	}
	
	if (m==0) 
	{
		 alert('Не сте избрали `Тарифи`');
		 return false;	
	}
	*/
	if($F('name') == "")	
	{
		alert('Не сте въвели `Име`');
		$('name').focus();
		return false;
	}
	
	if($F('last_name') == "")	
	{
		alert('Не сте въвели `Фамилия`');
		$('last_name').focus();
		return false;
	}
	
	if($F('country') == "")	
	{
		alert('Не сте въвели `Държава`');
		$('country').focus();
		return false;
	}
	
	if($F('city') == "")	
	{
		alert('Не сте въвели `Град`');
		$('city').focus();
		return false;
	}
	
	if($F('address') == "")	
	{
		alert('Не сте въвели `Адрес`');
		$('address').focus();
		return false;
	}
	
	if($F('post_code') == "")	
	{
		alert('Не сте въвели `Пощенски код`');
		$('post_code').focus();
		return false;
	}
	
	if($F('phone') == "")	
	{
		alert('Не сте въвели `Телефонен номер`');
		$('phone').focus();
		return false;
	}
	
	
	if($F('mail') == "")	
	{
		alert('Не сте въвели `E-mail`');
		$('mail').focus();
		return false;
	}
	
	var Exp1=/^[a-zA-Z0-9\._-]+@+[a-zA-Z0-9\._-]+\.+[a-zA-Z]{2,4}$/;
	if(!(Exp1.exec($F('mail'))))
	{
		alert('Моля въведете валиден `E-mail`!');
        $('mail').focus();
		return false;
	}
	
	if($('present').checked)	
	{
		if($F('name3') == "")	
		{
			alert('Не сте въвели `Име`');
			$('name3').focus();
			return false;
		}
	
		if($F('last_name2') == "")	
		{
			alert('Не сте въвели `Фамилия`');
			$('last_name2').focus();
			return false;
		}
		if($F('address2') == "")	
		{
			alert('Не сте въвели `Адрес`');
			$('address2').focus();
			return false;
		}
	
		if($F('post_code2') == "")	
		{
			alert('Не сте въвели `Пощенски код`');
			$('post_code2').focus();
			return false;
		}
	
		if($F('phone2') == "")	
		{
			alert('Не сте въвели `Телефонен номер`');
			$('phone2').focus();
			return false;
		}
	
		if($F('mail2') == "")	
		{
			alert('Не сте въвели `E-mail`');
			$('mail2').focus();
			return false;
		}
	
		if(!(Exp1.exec($F('mail2'))))
		{
			alert('Моля въведете валиден `E-mail`!');
        	$('mail2').focus();
			return false;
		}
	}
	
	return true;
}

function mailCheck()
{
	if($F('mail') != '')
	{
	  $('mail').disabled = true;
	  new Ajax.Request(file, 
					 { parameters: 
					 	{ 
							func: 'mailCheck',
							mail: $F('mail')
						}, 
						onComplete: function (result) { if(result.responseText != "done") {$('mail').value = ''; toolTip($('mail'), 10);} $('mail').disabled = false;}
					 }
					);
	}
}

function codeCheck()
{
	new Ajax.Request(file, 
					 { parameters: 
					 	{ 
							func: 'codeCheck',
							recaptcha_challenge_field: $('subscribeForm').elements['recaptcha_challenge_field'].value,
							recaptcha_response_field: $('subscribeForm').elements['recaptcha_response_field'].value
						}, 
						onComplete: function (result) 
						{ 
							//$('code').disabled = false;
							if(result.responseText == "done") 
							{
								$('subscribeForm').submit();
							} 
							else 
							{
								toolTip($('codePointer'), 11);
							}
						}
					 }
					);
}





function hideMessages()
{
	var msgA = document.getElementsByClassName('okmsg');
	for (var i=0; i<msgA.length; i++){
		var div = msgA[i];
		
		new Effect.DropOut(div);	
	}
	
	var msgA = document.getElementsByClassName('errormsg');
	for (var i=0; i<msgA.length; i++){
		var div = msgA[i];
		
		new Effect.DropOut(div);
	}
}

function showHideSendFriend()
{
	if($('sendFriend').style.display == 'none')
	{
		showSendFriend();
	}
	else
	{
		hideSendFriend();
	}	
}

function subscribeFriend(chk)
{
	if(chk.checked)
	{
		$('subscribe_friend').show();
	}
	else
	{
		$('subscribe_friend').hide();
	}		
}

function paymentForm(chk)
{
	if(chk.checked)
	{
		$('payment_form').show();
	}
	else
	{
		$('payment_form').hide();
	}		
}

function tarifni(type)
{
	if(type == 2)
	{
		$('tTable1').hide();
		$('tTable2').show();
		$('tTable3').hide();
		
		
	}
	else if(type == 3)
	{
		$('tTable1').hide();
		$('tTable2').hide();
		$('tTable3').show();
		
	}
	else
	{
		$('tTable1').show();
		$('tTable2').hide();
		$('tTable3').hide();
		
		
	}
	
	
}	

function subscribePaymentMethod(sel)
{
	if(sel.value == 3)
	{
		$('agentsTbl').show();
	}
	else
	{
		$('agentsTbl').hide();
	}
	if(sel.value == 1)
	{
		$('banksTbl').show();
	}
	else
	{
		$('banksTbl').hide();
	}	
	
	if(sel.value == 2)
	{
		$('ckechsTbl').show();
	}
	else
	{
		$('ckechsTbl').hide();
	}	
}

function showSendFriend()
{
	new Effect.Grow('sendFriend');
}

function hideSendFriend()
{
	new Effect.Shrink('sendFriend');
}

function loadAlt(imgAltContainer)
{
	var galTitle    = "";
	var imgTitle    = "";
	var titleToShow = "";
	
	if($(imgAltContainer)) imgTitle = $(imgAltContainer).innerHTML;
	if($('gallery_title')) galTitle = $('gallery_title').innerHTML;
	
	if(imgTitle != "") 		titleToShow = imgTitle;
	else if(galTitle != "") titleToShow = galTitle;
	
	if(titleToShow != "" && $('big_image_title')) $('big_image_title').innerHTML = titleToShow;
}


Event.observe(window, 'load', function (){setTimeout("hideMessages()", 4000);}, false);



/////////////////// NEW /////////////////////////////////////

function loadIndexArticles(active, container, type)
{
	new Ajax.Updater(container, file, 
					 { parameters: 
					 	{ 
							func: 'aGetIndexArticlesHtml',
							active: active,
							type: type,
							container: container
						}
					 }
					);
}

function loadIndexVideoNews(active)
{
	new Ajax.Updater('video_news', file, 
					 { parameters: 
					 	{ 
							func: 'aGetIndexVideoNewsHtml',
							active: active
						}
					 }
					);	
}

function rSetStyle(x, cls)
{
	x.className = cls;
}

function noteField(el)
{
	el = $(el);
	var txt = el.value;
	new Event.observe(el, 'focus', function() 
	{
		if(el.value == txt)
		{
			el.value = "";
		}
	});
	
	new Event.observe(el, 'blur', function() 
	{
  		if(el.value == "")
		{
			el.value = txt;
		}	
	});
}

function selectAll(el)
{
	new Event.observe(el, 'click', function() 
	{
		$(el).focus();
		$(el).select();
	});
	
}

function showUrl(url)
{
	//var u = url + '&mode=articles&' + $('category_id').serialize();
	var u = url + '?mode=' + $F('mode') + '&lng=' + $F('language') + '&img=' + $F('img') + '&' + $('category_id').serialize();
	$('url').value = u;
}

function showHideCats()
{
	if($F('mode') != 'articles')
	{
		$('category_id').hide();	
		$('redText').hide();	
	}
	else
	{
		$('category_id').show();
		$('redText').show();
	}
}

var current_leading = 1;
var num_leading		= 3;

function slide(div, px)
{
	var tbl = $(div);
	
	
	if(tbl.hiddenWidth == null)
	{
		tbl.hiddenWidth = (tbl.getWidth() - (tbl.parentNode).getWidth());
		//alert(tbl.hiddenWidth);
	}
	
	if(tbl.posProperty == null)
	{
		tbl.posProperty = tbl.hiddenWidth;
		//alert(tbl.posProperty);
	}
	
	
	if(tbl.posProperty>=0 && tbl.posProperty<=tbl.hiddenWidth)
	{
		var toMove = null;
		var newPos = null;
		
		if((tbl.posProperty + px) < 0)
		{
			toMove = -tbl.posProperty;
			newPos = 0;
		}
		else if((tbl.posProperty + px) > tbl.hiddenWidth)
		{
			toMove = tbl.hiddenWidth - tbl.posProperty;
			newPos = tbl.hiddenWidth;	
		}
		else
		{
			toMove = px;
			newPos = tbl.posProperty + px;		
		}
		
		new Effect.Move (div, 
					 { 
					 	x     : toMove, 
						y     : 0, 
						mode  : 'relative',
						queue : {position:'front', scope:'one', limit:1},
						afterFinish : function () { tbl.posProperty = newPos; }
					 }
					 );
	}
}

function setNumberLeading(num)
{
	num_leading = num;
}

function setLeadingLink(dir)
{
	var increased = current_leading + dir;
	if(increased<=num_leading && increased>0)
	{
		current_leading = increased;
		setLinkClass();
	}
}

function setLinkClass()
{
	var links = $('leadingPaging').getElementsByTagName("a");
	if(links[current_leading-1])
	{
		for(var i=0; i<links.length; i++)
		{
			links[i].className = "";
		}
		links[current_leading-1].className = "active_p";
	}	
}

function linkLeading(num)
{
	var px          = (current_leading - num)*470;
	current_leading = num;
	
	slide('leadingContainer', px);
	setLinkClass();
}

////////////////////////////////////////////////////////////
