$(document).ready(function() {
    blankTarget();

    $('.slideshow').each(function(index) {
        $(this).cycle({
			fx:     'fade',
			timeout: 0,
			speed:   600,
			sync:    false,
			prev:    '#prev',
			next:    '#next',
			pager:   '#nav',
			pagerAnchorBuilder: function(i) {
				if (index == 0) {
                    return '<a href="#">'+(i+1)+'</a>';
				}
                else {
                    return '#nav a:eq('+i+')';
                }
        	},
			onPagerEvent: function(n){
				if(n == 0){
					$('#prev').addClass('hidden');
				} else {
					$('#prev').removeClass('hidden');
				}
			},
			onPrevNextEvent: function(i, n){
				if(n == 0){
					$('#prev').addClass('hidden');
				} else {
					$('#prev').removeClass('hidden');
				}				
			}
		});
    });
	
	$('#prev').addClass('hidden');

    $("a.fancybox").fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true,
        'hideOnContentClick': false,
        'titlePosition'	: 'over',
        'autoDimensions' : true,
		'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
		    if(typeof title == 'undefined') title = '';
		    return '<span id="fancybox-title-over">(' +  (currentIndex + 1) + ' / ' + currentArray.length + ')&nbsp;&nbsp;&nbsp;' + title + '</span>';
		}
    });

    $("a.fancybox_notitle").fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true,
        'hideOnContentClick': false,
        'autoDimensions' : true,
		'titleShow': false
    });


    $("a.iframe").fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true,
        'overlayOpacity': 0.7,
        'overlayColor': '#a4a4a4',
        'hideOnContentClick': false,
        'width': 525,
        'height': 330+19
    });

    cart();
});

function blankTarget()
{
    if(!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for(var i=0; i<anchors.length; i++)
    {
        var anchor = anchors[i];
        anchor.tabindex = i;
        if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
        {
            anchor.target = "_blank";
        }
    }
}

var newWin = null;

function winpopup(url, windowname, width, height, scroll)
{
	if(newWin != null && !newWin.closed)
    {
        newWin.close();
    }
	newWin = window.open(url,windowname,"location=0,menubar=0,toolbar=0,hotkeys=no,scrollbars="+ scroll +",resizable=no,personalbar=0,status=0,left="+ (((screen.availWidth)/2)-(width/2)) +",top="+ (((screen.availHeight)/2)-(height/2)) +",width="+ width +",height="+ height);
	//newWin.focus();
}

function setFontSize(size, item) {
    $('.font_size1').removeClass('actual');
    $('.font_size2').removeClass('actual');
    $('.font_size3').removeClass('actual');

    $(item).addClass('actual');

    $('p.lead').css('font-size', size+'px');
    $('div.ck').css('font-size', size+'px');
}

function ajax(url, data, async, type, target, readyfunction)
{
    if(typeof async == 'undefined') {
        async = true;
    }

    if(typeof type == 'undefined') {
        type = 'html';
    }

    $.ajax({
		type: "POST",
		url: url,
		data: data,
        dataType: type,
        async: async,
		complete: function(xhr) {
		    if(typeof target == 'undefined' && xhr.responseText != '') {
                //alert(xhr.responseText);
            }
            else if(typeof target != 'undefined') {
                $( target ).html( xhr.responseText );
                if(typeof readyfunction != 'undefined') {
                    eval(readyfunction);
                }
            }
		}
	});
}

function reload(file, val, target_id, def, func)
{
    var fk = $(val).attr('id');
    var selected = $(val).val();

    var d = '';
    d += '&selected=' + selected;
    d += '&def=' + def;

    if(typeof func == 'undefined') {
        func = '';
    }

    ajax(file, d, true, 'html', target_id, "$( target ).val("+ def +");" + func);
}

function kepzes_helyszin(portal_id, kepzes_id)
{
    var d = '';
    d += '&portal_id=' + portal_id;
    d += '&kepzes_id=' + kepzes_id;

    ajax('_kepzes_helyszin.php', d, true, 'html', '#kepzes_helyszin', 'cart()');
}

function kosarba(id)
{
    var data = 'id=' + id;

    $.ajax({
        url: "_kosarba.php",
        type: "POST",
        data: data,
        cache: false,
        async: false,
        success: function (response) {
            $("#kosar_tartalom").load("_kosar_tartalom.php");
            $("#darab_" + id).val('');
        }
    });
}

function cart()
{
    $(".cart").fancybox({
        'autoScale' : true,
    	'type' : 'ajax',
        'titleShow' : false,
        'href' : '_kosar.php',
        'overlayOpacity' : 0.75,
		'overlayColor' : '#000',
        'hideOnContentClick': false,
    });
	
    $("a.iframe_erdeklodes").fancybox({
        'hideOnContentClick': false,
        'titleShow': false,
        'width': 580,
        'height': 580,
        'type': 'iframe'
    });
}

function changeAll(form, input)
{
    var chkBox = null;

    for (i=0; i < document.getElementById(form).elements.length; i++) {
        if (document.getElementById(form).elements[i].name.search(input) != -1) {
            var id = "checkall_" + form + "_" + input;
            document.getElementById(form).elements[i].checked = document.getElementById(id).checked;
        }
    }
}

function reloadCaptcha(id)
{
	document.getElementById('captcha_'+id).src = 'include/kcaptcha/?'+Math.random();
}



function reloadPortalOktato(val, target_id, target_id2)
{
    var selected = $(val).val();

    var d = '';
    d += '&selected=' + selected;
	
	if($("#portal_id").val() == '0' && $("#oktato_id").val() != '0'){
    	valasztott_oktato = $("#oktato_id").val();
    	ajax('_reloadkepzes_portal.php?oktato_id='+valasztott_oktato, d, true, 'html', target_id);
	}

	if($("#portal_id").val() != '0' && $("#oktato_id").val() == '0'){
    	valasztott_portal = $("#portal_id").val();
    	ajax('_reloadkepzes_oktato.php?portal_id='+valasztott_portal, d, true, 'html', target_id2);
	}
	
	if($("#oktato_id").val() == '0' && $("#portal_id").val() == '0'){
		ajax('_reloadkepzes_oktato.php', d, true, 'html', target_id2);
		ajax('_reloadkepzes_portal.php', d, true, 'html', target_id);
	}
	
	if(selected == '0' && $("#oktato_id").val() == '0' && $("#portal_id").val() == '0'){
		ajax('_reloadkepzes_reset.php', d, true, 'html', '#kepzes_id');
	}

}

function reloadKepzesOktato(val, target_id, target_id2)
{
    var selected = $(val).val();

    var d = '';
    d += '&selected=' + selected;
	
	if($("#oktato_id").val() == '0' && $("#kepzes_id").val() != '0'){
		valasztott_kepzes = $("#kepzes_id").val();
    	ajax('_reloadportal_oktato.php?kepzes_id='+valasztott_kepzes, d, true, 'html', target_id);
	}	

	if($("#oktato_id").val() != '0' && $("#kepzes_id").val() == '0'){
		valasztott_oktato = $("#oktato_id").val();
    	ajax('_reloadportal_kepzes.php?oktato_id='+valasztott_oktato, d, true, 'html', target_id2);
	}
	
	
	/*if($("#oktato_id").val() == '0'){
    	ajax('_reloadportal_oktato.php', d, true, 'html', target_id);
	} else {
		valasztott_oktato = $("#oktato_id").val();
		ajax('_reloadportal_kepzes.php?oktato_id='+valasztott_oktato, d, true, 'html', target_id2);	
	}
	*/
	
	if($("#kepzes_id").val() == '0' && $("#oktato_id").val() == '0'){
		ajax('_reloadportal_oktato.php', d, true, 'html', target_id);
		ajax('_reloadportal_kepzes.php', d, true, 'html', target_id2);
	}
	
	if(selected == '0'){
		ajax('_reloadportal_reset.php', d, true, 'html', '#portal_id');
	}
}

function reloadKepzesPortal(val, target_id, target_id2)
{
    var selected = $(val).val();

    var d = '';
    d += '&selected=' + selected;
	
	if($("#kepzes_id").val() == '0' && $("#portal_id").val() != '0'){
		valasztott_portal = $("#portal_id").val();
    	ajax('_reloadoktato_kepzes.php?portal_id='+valasztott_portal, d, true, 'html', target_id2);
	}
	
	if($("#kepzes_id").val() != '0' && $("#portal_id").val() == '0'){
		valasztott_kepzes = $("#kepzes_id").val();
    	ajax('_reloadoktato_portal.php?kepzes_id='+valasztott_kepzes, d, true, 'html', target_id);
	}	
	
	
	/*if($("#portal_id").val() == '0'){
    	ajax('_reloadoktato_portal.php', d, true, 'html', target_id);
	} else {
		valasztott_portal = $("#portal_id").val();
		ajax('_reloadoktato_kepzes.php?portal_id='+valasztott_portal, d, true, 'html', target_id2);
	}
	*/
	
	if($("#kepzes_id").val() == '0' && $("#portal_id").val() == '0'){
		ajax('_reloadoktato_kepzes.php', d, true, 'html', target_id2);
		ajax('_reloadoktato_portal.php', d, true, 'html', target_id);
	}
}



