﻿
ua = navigator.userAgent.toLowerCase(); 
isIE = ((ua.indexOf("msie") != -1) );
isIE6 = ((ua.indexOf("msie 6.0") != -1) );
isOPERA = ((ua.indexOf("opera") != -1) );
isFF = ((ua.indexOf("firefox") != -1) );
isSafari = ((ua.indexOf("safari") != -1) );

$(document).ready(function(){

if(isIE6)
{
DD_belatedPNG.fix('img,#top h1 a,.contactheader .title_questions,.contactheader .title_phone,.contactheader .skype, #top,#slider,#thead, .ui-slider .ui-slider-handle, .dataform .button.big, #slider .item a.btn_order, .centerimg1 a.box_css, .centerimg1 a.box_js, #form_bid .pricebox, .langselect, .langselect h1, .langselect .content .title, .langselect .content ul a, #thead .title_phone'); 
}

$('#turntarget').fold(
  {
  directory : '/assets/tools/turn',
  side: 'right',
  startingWidth: 90,
  startingHeight: 90
  });
  
//$('#slider').nivoSlider();
  


});
//-----------------------------------------------
function debug(txt)
{
try {
	console.debug(txt);
	}
catch(e){}
}
//-----------------------------------------------
function indexofarray(array, key)
{
for(idx in array)
	{
	if(array[idx]==key)	return idx;
	}
return -1;	
}
//-----------------------------------------------
function Faqobj(formid, options)
{
var mainform=$('#'+formid);
var mainobj=this;	

var defaults = 
{

};	

opts = $.extend({}, defaults, options);

$(mainform).find('.fhead').click( function(event)
	{
	event.preventDefault();

	//$(mainform).find('.text').slideUp(500);
	
	$(mainform).find('.text').animate(
			{
			opacity : 'hide',
			height : 'hide'	
			}, 500);
	
	$(mainform).find('.rght').html(opts.txt_open);
	$(mainform).find('.rght').removeClass('up');
	
	var itemobj=$(this).parents('.item');
	var textobj=$(itemobj).find('.text');
	
	if( $(textobj).is(':hidden') )
		{	
		//$(textobj).slideDown(500);
		
		$(textobj).animate(
			{
			opacity : 'show',
			height : 'show'	
			}, 500);
		
		$(itemobj).find('.rght').html(opts.txt_close);
		$(itemobj).find('.rght').addClass('up');
		}
	
	});

//END;
}
//-----------------------------------------------
function slidercomponent(selector, options)
{
var mainelem=$(selector);
var mainobj=this;

var sliderarea=$(selector).find('.sliderarea');
var value_elem=$(mainelem).find(".value");


//debug(value_elem);

var defaults={
		value: 1,
		min: 1, 
		max: 20,
		step: 1,
		change: function(event, ui) { mainobj.onchange(event, ui); },
		afterchange: function(value) {  }
	}
	


var opts = $.extend({}, defaults, options);

$(sliderarea).slider( opts );


$(mainelem).find('.minus').click( function(event)
	{
	event.preventDefault();	
	
	var value=$(sliderarea).slider('value');
	
	$(sliderarea).slider('value', value-1 );
	});
	
$(mainelem).find('.plus').click( function(event)
	{
	event.preventDefault();	
	
	var value=$(sliderarea).slider('value');
	
	$(sliderarea).slider('value', value+1 );
	});	
	
	
this.onchange=function(event, ui)
	{
	$(value_elem).html( ui.value );	
	
	$(mainelem).find('input[name='+options.sc_hiddenname+']').val( ui.value );
	
	opts.afterchange( ui.value );
	}
	
//END;
}
//-----------------------------------------------
function objform_bid(formid, options)
{
var defaults = 
	{
	lang : 'hu',
	upload_url	: '',
	flash_url	: 'tools/swfupload/swfupload.swf',
	flash9_url  : 'tools/swfupload/swfupload_fp9.swf',
	button_image_url : 'images/select_file_button.png',
	imgdir : 'images',
	session_id : '',	
	afterupload : function() {},
	prices : {}
	};	

opts = $.extend({}, defaults, options);


	
var mainform=$('#'+formid);
var mainobj=this;	
var priceobj=opts.prices;

if(opts.lang=='hu')
	{
	var months= {1:'január', 2:'február', 3:'március', 4:'április', 5:'május', 6:'június', 7:'július', 8:'augusztus', 9:'szeptember', 10:'október', 11:'november', 12:'december'};
	var txt_day='nap';
	}
else
	{
	var months= {1:'January', 2:'February', 3:'March', 4:'April', 5:'May', 6:'June', 7:'July', 8:'August', 9:'September', 10:'October', 11:'November', 12:'December'};
	var txt_day='day';
	}

var action=$(mainform).attr('action');

var slider1=new slidercomponent('#dayslider', { sc_hiddenname : 'days', value : 5,  afterchange : calcprice });
var slider2=new slidercomponent('#pageslider', { sc_hiddenname : 'pages', afterchange : calcprice });


var queueobj=$(mainform).find('.queue');

var settings={
	upload_url : opts.upload_url, 
	flash_url : opts.flash_url,
	flash9_url  : opts.flash9_url,
	
	//post_params : { 'session' : opts.session_id }, 
	 	
	file_queue_limit : 10,
	file_size_limit : "35 MB",
	
	debug : false, 
	
	button_placeholder_id : "file_upload_placeholder",
	button_image_url : opts.button_image_url, 
	button_width : 152, 
	button_height : 34, 
	button_text_left_padding : 35, 
	button_text_top_padding : 5,
	//button_text : "<span class=\"btn\">Válasszon fájlt...</span>",
	button_text_style : ".btn { font-size: 13px; font-family: Helvetica; color: #999999;  }", 
	button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT,
	
	
	file_queue_error_handler		: file_queue_error,
	file_dialog_complete_handler	: file_dialog_complete,
	upload_start_handler : upload_start, 
	upload_progress_handler : upload_progress,
	upload_complete_handler : upload_complete,
	upload_success_handler : upload_success
}

$(mainform).find('.selectit :checkbox[value=fixwidth]').click(function(){$(mainform).find('.selectit :checkbox[value=fluid]').removeAttr('checked') });
$(mainform).find('.selectit :checkbox[value=fluid]').click(function(){$(mainform).find('.selectit :checkbox[value=fixwidth]').removeAttr('checked') });

$(mainform).find('.selectit :checkbox').click(calcprice);
$(mainform).find('select[name=doctype]').change(calcprice);


calcprice();

function calcprice()
{
if(!opts.prices.currency) return;
	
var days=$(mainform).find('input[name=days]').val();
var pages=$(mainform).find('input[name=pages]').val();
var doctype=$(mainform).find('select[name=doctype]').val();

var sumprice=priceobj['page_price_frst']+(pages-1)*priceobj['page_price_other'];

if( priceobj['pricemod_days'][days] )
	sumprice=Math.round(sumprice*priceobj['pricemod_days'][days]);

if( doctype==2 ) sumprice+=priceobj['strict'];	
	
$(mainform).find('.selectit input:checked').each(function()
	{
	var value=$(this).val();
			
	if( priceobj['selecit'][value] )
		{
		if( indexofarray(priceobj['selecit_pp'], value)>=0 )
			sumprice+=priceobj['selecit'][value]*pages;
			
		else
			sumprice+=priceobj['selecit'][value];
		}

	});	
	
var pricestr='';

if(priceobj['currency']=='Ft') pricestr=sumprice+' Ft';
else pricestr=priceobj['currency']+sumprice;

$(mainform).find('.pricebox .price .value').html(pricestr);
$(mainform).find('.projectfooter .total .value').html(pricestr);

var msecsInADay = 86400000;
var today=new Date();
var enddate=new Date(today.getTime() + days*msecsInADay);;

var end_year=enddate.getFullYear();
var end_month=enddate.getMonth()+1;
var end_day=enddate.getDate();

var datestr=months[end_month]+' '+end_day+'. ('+days+' '+txt_day+')'

$(mainform).find('.pricebox .date .value').html(datestr);
$(mainform).find('.projectfooter .date .value').html(datestr);

//debug(enddate);
}


swfu = new SWFUpload(settings); 

var msghtml='';

$(mainform).find('a.button.big').click( function(event)
	{
	event.preventDefault();
	
	$(mainform).find('.field').removeClass('error'); 
	
	var stats=swfu.getStats();
	
	mainobj.loader(true);
	
	$.ajax({
		type: 'POST',
		url: action,
		data: $(mainform).serialize()+'&files_queued='+stats.files_queued ,
		dataType: 'json',
		success: function(result)
			{
			if(result.success==0)
				{
				for (idx in result.errors)
					{
					var value=result.errors[idx];
					
					//$(mainform).find('#cnt_'+idx+' .errormsg').html(value);	
																
					$(mainform).find('#cnt_'+idx).addClass('error');
					//$('#cnt_'+idx).addClass('error');
					}
				}
			if( result.success==1 )
				{
				$(mainform).find('input[name=inq_id]').val(result.data.inq_id);
										
				var stats=swfu.getStats();

				if (stats.files_queued!=0)
					{
					msghtml=result.data.msghtml;
					mainobj.Startupload();	
					}
				else
					{
					mainobj.msgsuccess(result.data.msghtml);
					mainobj.loader(false);
					}
					
				}
			else mainobj.loader(false);			
			}
		});
	
	});

//---------------------------------------------------------
this.loader=function(on)
{
if( on )
	{
	$(mainform).find('a.button.big').css('display', 'none');
	$(mainform).find('.info').css('display', 'none');
	$(mainform).find('.loader').css('display', 'block');
	}
else
	{
	$(mainform).find('a.button.big').css('display', 'inline-block');
	$(mainform).find('.info').css('display', 'inline');
	$(mainform).find('.loader').css('display', 'none');	
	}
}
//---------------------------------------------------------
this.RefreshQueue=function()
{
var stats=swfu.getStats();
var filedata;

$(queueobj).html('');

var allfilesnmr=0;
for(idx in stats) allfilesnmr+=stats[idx];

for(var i=0;i<allfilesnmr;i++)
	{
	filedata=swfu.getFile(i);
	
	if(filedata.filestatus!=SWFUpload.FILE_STATUS.QUEUED) continue; 

	var sizestring=mainobj.CalcFilesize(filedata.size);

	$(queueobj).append('<div class="fileinfoitem" id="fileinfoitem_'+filedata.id+'"><img src="'+opts.imgdir+'/del.png" alt="" class="del" /><input type="text" name="filename" value="'+filedata.name+' ('+sizestring+')" readonly="readonly" /></div>');
	
	}
	
$(queueobj).find('img.del').click( function(event) { mainobj.ClickRemove(event, this) } );	

if(isIE6) DD_belatedPNG.fix('img.del');


}
//---------------------------------------------------------
this.CalcFilesize=function(size)
{
if(size < 1024*1024 ) return Math.round(size/1024)+'kb';
return (size/(1024*1024) ).toFixed(2)+'Mb';
}
//---------------------------------------------------------
this.ClickRemove=function(event, sender)
{
event.preventDefault(); 

var fileid= $(sender).parents('.fileinfoitem').attr('id').replace('fileinfoitem_', '') ;

swfu.cancelUpload(fileid);

mainobj.RefreshQueue();
}
//---------------------------------------------------------
this.Startupload=function(event, sender)
{
$(mainform).find('.progress').css('display', 'inline');

swfu.startUpload();
}
//---------------------------------------------------------
this.msgsuccess=function(txt)
{
$(mainform).html(txt);	
}




//Uploader Events

//---------------------------------------------------------
function file_dialog_complete(selected_num, files_num, total_num)
{
mainobj.RefreshQueue();	
}
//---------------------------------------------------------
function file_queue_error(file, errorCode, message)
{
try {
		// Handle this error separately because we don't want to create a FileProgress element for it.
		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
			alert("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
			return;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			alert("The file you selected is too big. "+file.name);
			this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			alert("The file you selected is empty.  Please select another file.");
			this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			alert("The file you choose is not an allowed file type.");
			this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		default:
			alert("An error occurred in the upload. Try again later.");
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		}
	} catch (e) {	}

}
//---------------------------------------------------------
function upload_start(file)
{
swfu.addPostParam( 'inquiry_id', $(mainform).find('input[name=inq_id]').val() );
}
//---------------------------------------------------------
function upload_progress(file, bytesLoaded, bytesTotal) 
{
try {
	var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);

	$(mainform).find('.progress').html(percent+'%');

	} catch (e) { }
}
//---------------------------------------------------------
function upload_complete(file)
{
try{

var stats=swfu.getStats();

if( stats.files_queued==0 ) 
	{
	mainobj.RefreshQueue();		
	};
	}
catch(e) {}

swfu.startUpload();
}
//---------------------------------------------------------
function upload_success(file, result, received_response)
{	
var stats=swfu.getStats();

if (stats.files_queued!=0) return;

if( !result )
	{
	mainobj.loader(false);
	mainobj.msgsuccess(msghtml);	
	return;
	}		
	
/*if (typeof (JSON) !== 'undefined' && 	typeof (JSON.parse) === 'function') result=JSON.parse(result);
else*/ result= eval('('+ result + ')' );


if( result.success==1 )	
	{		
	$(mainform).find('.progress').css('display', 'none');
		
	mainobj.msgsuccess(result.data.msghtml);
		
	mainobj.loader(false);
		
	opts.afterupload(result);
	}
}

//END;
}
//-----------------------------------------------
function objform_contact(formid, options)
{
var mainform=$('#'+formid);
var mainobj=this;	
var action=$(mainform).attr('action');

$(mainform).find('a.button.big').click( function(event)
	{
	event.preventDefault();
	
	$(mainform).find('.field').removeClass('error'); 
	
	mainobj.loader(true);
	
	$.ajax({
		type: 'POST',
		url: action,
		data: $(mainform).serialize(),
		dataType: 'json',
		success: function(result)
			{
			mainobj.loader(false);
				
			if(result.success==0)
				{
				for (idx in result.errors)
					{
					var value=result.errors[idx];
					
					//$(mainform).find('#cnt_'+idx+' .errormsg').html(value);												
					$(mainform).find('#cnt_'+idx).addClass('error');
					}
				}
			if( result.success==1 )
				{
				mainobj.msgsuccess(result.data.msghtml);
				}				
			}
		});
	
	});

//---------------------------------------------------------
this.loader=function(on)
{
if( on )
	{
	$(mainform).find('a.button.big').css('display', 'none');
	$(mainform).find('.loader').css('display', 'block');
	}
else
	{
	$(mainform).find('a.button.big').css('display', 'block');
	$(mainform).find('.loader').css('display', 'none');	
	}
}
//---------------------------------------------------------
this.msgsuccess=function(txt)
{
$('.innerbox_w_bottom').html(txt);	
}

//END;
}
//---------------------------------------------------------
function initslider(id)
{
var mainelement=$('#'+id);
var itemcnt=$(mainelement).find('.item').length;
var timermsec=10000;
var timer;

var current=Math.round(Math.random());

$(mainelement).find('.item').css('display', 'none');
$(mainelement).find('.item').eq(current).css('display', 'block');


timer=setTimeout( function()
  	{
  	opennext(1);		
  	}, timermsec );


function opennext(step)
	{
	clearTimeout(timer);
	
	timer=setTimeout( function()
  	{
  	opennext(1);		
  	}, timermsec );
		
	var newpos=current+=step;
	
	if(newpos<0) newpos=itemcnt-1;
	if(newpos>=itemcnt) newpos=0;
			
	openbyidx(newpos);
	}

function openbyidx(idx)
	{
	current=idx;	
		
	$(mainelement).find('.item:not(:eq('+idx+'))').animate(
		{
		opacity : 'hide'
		}, 1000);
		
	$(mainelement).find('.item').eq(idx).animate(
		{
		opacity : 'show'
		}, 1000);
		
	}
//END;
}
//---------------------------------------------------------
function initlanglayer(id, options)
{
var mainelement=$('#'+id);

var defaults = 
	{
	current : 'none', 
	opened : false
	};	

opts = $.extend({}, defaults, options);

$(mainelement).find('a.'+opts.current).click( function(event)
	{
	event.preventDefault();
	hide();
	});


if(opts.opened) show();

function show()
{
$('#overlay').show();
$(mainelement).show();	
}

function hide()
{
$('#overlay').hide();
$(mainelement).hide();	
}


//END;	
}


