var galery_window;

function open_galery(url)
{
    galery_window = window.open(url, 'CSTT_Galery', 'location=no, menubar=no, resizable=no, titlebar=no, status=no, scrollbars=no, height=630, width=620, dependent=yes');
    return false;
}
function post_search( f )
{
    window.location.href = 'http://cstt.next-inform.hu/search/'+encodeURIComponent(dge(f).value).replace(/%20/g, '+');
}
function comments_preview( d )
{
    var txt = dge(d).value;
    var cont = 'comments_previewarea';
    var callback = 'fillCont';
    if(!ajaxObjs[aOid]) {
        ajaxObjs[aOid] = new Ajax('admin.php?mod=comments&tpl=comments_ajax-commentPreview',false);
    }
    ajaxObjs[aOid].request(cont,callback,'txt='+encodeURIComponent(txt),aOid++);
    dge('comments_previewarea').style.display = "";
}
function comments_hidepreview()
{
    dge('comments_previewarea').style.display = "none";
    dge('comments_previewarea').innerHTML = "";
}
function validate_newcarform()
{
    if(	dge('mezo0').value != "" &&
	dge('mezo1').value != "" &&
	dge('mezo2').value != "" &&
	dge('mezo3').value != "" &&
	dge('mezo4').value != "" )
    {
	return true;
    }
    else
    {
	alert('Minden mezőt kötelező kitölteni.');
	return false;
    }
}
function addToBasket( item )
{
    $("#sidebox_basket").load("index.php?mod=webshop&tpl=webshop-basketAdd&id="+item);
}
function delFromBasket( item )
{
    $("#sidebox_basket").load("index.php?mod=webshop&tpl=webshop-basketDel&id="+item);
}
function clearBasket()
{
    $("#sidebox_basket").load("index.php?mod=webshop&tpl=webshop-basketClear");
}
function reloadBasketArea()
{
    $("#sidebox_basket").load("index.php?mod=webshop&tpl=sidebox_basket");
}
function checkBasketRows()
{
    if( $("#finalbasket tr:last :input").eq(0).val() != "" &&
	$("#finalbasket tr:last :input").eq(1).val() != "" ) {
	$("#finalbasket").append(
	    '<tr><td>'+(1+$("#finalbasket tr").length)+'.</td><td><input type="text" onkeyup="checkBasketRows()" name="termekkod[]" class="ajanlat_termekkod" /></td><td><input type="text" onkeyup="checkBasketRows()" name="megnevezes[]" class="ajanlat_megnevezes" /></td><td><input type="text" onkeyup="checkBasketRows()" name="darab[]" class="ajanlat_darab" /></td></tr>'
	);
	$("#finalbasket tr:last").show("fast");
    }
    else {
	var prev = $("#finalbasket tr").length - 1;
	if( $("#finalbasket tr:nth-child("+prev+") :input").eq(0).val() == "" &&
	    $("#finalbasket tr:nth-child("+prev+") :input").eq(1).val() == "" &&

	    $("#finalbasket tr:last :input").eq(0).val() == "" &&
	    $("#finalbasket tr:last :input").eq(1).val() == "" ) {
	    $("#finalbasket tr:last").remove();
	}
    }
}
function typothchk(o)
{
    if(o.value=='0') {
	$("#typothrow").show();
    }
    else {
	$("#typothrow").hide();
    }
}

function thumbToName( t )
{
    return t.replace('.','_big.');
}


// preload headers
tmpimg = new Image();
for( i=1; i<8; i++) {
    tmpimg.src = site_base + 'img/hatter'+i+'.jpg';
}


$(document).ready(function () {
    $("#navigation").treeview({
	animated: "fast",
	collapsed: true,
	persist: "location",
	unique: false
    });

    $(".errors").bind("click",function(e){
	$(this).hide("slow");
    });

    $(".messages").bind("click",function(e){
	$(this).hide("slow");
    });
/*
    $('#telepules').autocomplete({
	url: "/autocomplete/telepules"
    });

    $('#intezmeny').autocomplete({
	url: "/autocomplete/intezmeny",
	cacheLength: 0,
	extraParams: {
	    telepules: function(){return $('#telepules').val()},
	    tipus: function(){return $('#tipus').val()}
	}
    });

    $('#intezmeny').autocomplete("result",function(event,result,formatted){
	$.post("/autocomplete/address",{'name':formatted},function(data){
	    $('#cim').val(data);
	},"text");
    });

    $('#tipus').bind('change',function(){
	$('#telepules').val('');
	$('#intezmeny').val('');
	$('#cim').val('');
    });

    $('#product_dialog').attr("title","Gyártó weboldala");
    $('#product_dialog').dialog({
	modal: true,
	autoOpen: false,
	width: 800,
	height: 600,
	hide: "fast",
	position: "center",
	overlay: {
	    opacity: 0.5,
	    filter: 'alpha(opacity=50)',
	    background: "black"
	},
	buttons: {
	    "Vissza a FitoSystem kft. oldalára": function() {
		$(this).html('');
		$(this).dialog("close");
	    }
	}
    });

    $('.product_link').bind('click',function(){
	$('#product_dialog').html('<iframe id="product_home" src="'+$(this).attr('href')+'"></iframe>');
	$('#product_dialog').dialog("open");
	if( $(this).attr("akuma:productid") ) {
	    addToBasket( $(this).attr("akuma:productid") );
	}
	return false;
    });

    $('.jq-thumbzone img').each(function(i){
	$(this)
	    .after('<div class="jq-imagezone"><img src="' + thumbToName( $(this).attr('src') ) + '" alt="" title="Kattintson a képre, vagy mozgassa le róla az egérmutatót a kép eltüntetéséhez."/></div>')
	    .parent()
	    .hover(
		function(){ $(this).find('div:hidden').fadeIn(300); },
		function(){ $(this).find('div:visible').fadeOut(300); }
	    )
	    .find("div.jq-imagezone")
	    .click(function(){
		$(this).fadeOut(300);
	    });
    });
*/

    $('#product_ajax').bind('Basic Details Downloads Properities Related Price',function(e){
	$(this).load('index.php?mod=products&tpl=products_AJAX-' + e.type + '&id='+$("#id").val() );
    });
    $('#product_ajax').trigger('Basic');
    $('.product_menu a').eq(0).addClass('active');

    $('.product_menu a').click(function(){
	$('#product_ajax').trigger($(this).attr('akuma:trigger'));
	$('.product_menu a').removeClass('active');
	$(this).addClass('active');
	return false;
    });

    $('.product_thumb img').live('click',function(e){
	$('.product_bigthumb img').attr('src',site_base + 'index.php?mod=products&tpl=products_RAWImage&id=' + $(this).attr('akuma:imgid') + '&size=normal');
    });

    $('.product_thumb').load('index.php?mod=products&tpl=products_AJAX-Thumbs&id='+$("#id").val(),function(){
	$('.product_thumb img').eq(0).click();
    });

    $(".searchform").bind('submit',function(e){
	window.location.href = site_base + 'search/' + encodeURIComponent( $('input[name=search]').val() ).replace(/%20/g, '+');
	return false;
    });

    $("#ajax_comment").load('index.php?mod=blog&tpl=blog_CommentPanel');

    $("#sndComment").live('click',function(e){
	$("#ajax_comment").load('index.php?mod=blog&tpl=blog_AddComment' +
	    '&blog_entry_id=' + encodeURIComponent( $('#blog_entry_id').val() ) +
	    '&content=' + encodeURIComponent($('#content').val())
	);
    });
    
    $(".searchform input[name=search]").focus(function(){
	if( $(this).val() == 'keresés' ) {
	    $(this).val('');
	}
    });

    $(".searchform input[name=search]").blur(function(){
	if( $(this).val() == '' ) {
	    $(this).val('keresés');
	}
    });
    $(".searchform input[name=search]").blur();

    $(".loginform input[name=user]").focus(function(){
	if( $(this).val() == 'felhasználónév' ) {
	    $(this).val('');
	}
    });

    $(".loginform input[name=user]").blur(function(){
	if( $(this).val() == '' ) {
	    $(this).val('felhasználónév');
	}
    });
    $(".loginform input[name=user]").blur();

    if(!$.browser.msie) {
	$(".loginform input[name=pass]").each(function(){
	    this.type = 'text';
	});
    }
    $(".loginform input[name=pass]").focus(function(){
	if( $(this).val() == 'jelszó' ) {
	    $(this).val('');
	    if(!$.browser.msie) {
		this.type = 'password';
	    }
	}
    });
    $(".loginform input[name=pass]").blur(function(){
	if( $(this).val() == '' ) {
	    $(this).val('jelszó');
	    if(!$.browser.msie) {
		this.type = 'text';
	    }
	}
    });
    $(".loginform input[name=pass]").blur();

    // fade
    $(".page_menu").prepend('<div id="head_img"></div>');
    $("#head_img").append('<div id="head_img_swapper"></div>');
    
    var totalHead = 7;
    var currentHead = 1;
    $("#head_img_swapper").bind('fadeNext',function(){
	$(this).hide("fast",function(){
	    if( ++currentHead == 8 ) currentHead = 1;
	    this.style.backgroundImage = 'url(' + site_base + 'img/hatter' + currentHead + '.jpg)';
	    $(this).fadeIn(2000,function(){
		$("#head_img").get(0).style.backgroundImage = 'url(' + site_base + 'img/hatter' + currentHead + '.jpg)';
	    });
	});
    });
    setInterval(function(){
	$("#head_img_swapper").trigger('fadeNext');
    },10000);
});
