/**
* Site Component
*
* @author Szijártó Tamás ( szicsu )
**/
if ( typeof Site != 'object'){
    var Site = {};
}
    

Site.Component = new function(){

}

Site.Component.MultiFile = function(){

    $.require('plugins/jquery.MultiFile.js');
    $.require('plugins/jquery.filestyle.js');

    var multiFileConf = {
        accept:'gif|jpg',
        max:3,
        STRING:{
            remove:'Törlés',
            selected:'Kiválasztva: $file',
            denied:'Érvénytelen kiterjesztés: $ext!',
            duplicate:'Duplikált kép :\n$file!'
        },
        callBack:function( newObj ){
            newObj.filestyle( fileStyleConf );
        }
    };

    var fileStyleConf = {
        image: "/image/buttons/hu/btn_talloz.gif",
        imageheight : 18,
        imagewidth : 61,
        width : 61
    };

    $("input:file")
        .filter('.multi')
            .MultiFile( multiFileConf )
            .filestyle( fileStyleConf );


}

Site.Component.Tooltip =  new function(){
    var self = this;

    this.init = function( param ){
        $.require('plugins/jquery.tooltip.js');
            if( typeof param == 'string' ){
            temp = param;
            param = {};
            param.selector = temp;
            delete temp;
        }

        if( !param.selector ){
            alert('No selector!');
            return false;
        }

        param.template = param.template || 'blue';
        param.bodySrc = param.bodySrc || 'title';
        param.bodyStyle = param.bodyStyle || '';
        param.bodyHtml = param.bodyHtml || false;
        param.tooltipConf = param.tooltipConf || {};


        if(  typeof param.tooltipConf.bodyHandler != 'function' ){
            param.tooltipConf.bodyHandler = function(){
                return self.getContent( $(this), param );
            }
        }


        return $( param.selector ).tooltip( param.tooltipConf );
    }


    this.getTemplate = function( data, template ){

        switch( template ){

            case 'blue':
            default:
                 arr = new Array(
                    '<div class="guideBubbles" style="{bodyStyle}">',
                        '<div class="relativeDiv">',
                            '<div class="contentDiv">',
                                '<table cellpadding="0" cellspacing="0" border="0" >',
                                    '<tr class="blurTr" >',
                                        '<td class="blurTd_topLeft"><img src="/image/bubbles/guideBubble_topLeft.png" /></td>',
                                        '<td class="fullTd_top">&nbsp;</td>',
                                        '<td class="blurTd_topRight"><img src="/image/bubbles/guideBubble_topRight.png" /></td>',
                                    '</tr>',
                                    '<tr>',
										'<td class="contentTd_left">&nbsp;</td>',
                                        '<td class="contentTd"><p>{html}</p></td>',
                                        '<td class="contentTd_right">&nbsp;</td>',
                                    '</tr>',
                                    '<tr class="blurTr" >',
                                        '<td class="blurTd_bottomLeft"><img src="/image/bubbles/guideBubble_bottomLeft.png" /></td>',
                                        '<td class="fullTd_bottom">&nbsp;</td>',
                                        '<td class="blurTd_bottomRight"><img src="/image/bubbles/guideBubble_bottomRight.png" /></td>',
                                    '</tr>',
                                '</table>',
                            '</div>',
                            '<div class="directionDiv"></div>',
                        '</div>',
                    '</div>'
                );
            break;
        }

        return  $.template( arr.join('') ).apply( data ) ;
    }

    this.getContent = function( obj, param ){

        data = {};

        if( param.bodyHtml ){
            data.html = param.bodyHtml;

        }
        else{
            data.html = obj.attr( param.bodySrc );

            //TODO ezen van mit még csiszolni :)
            if( data.html.substring(0,1) == '#' ){
                data.html = $(data.html).html();
            }
        }

        if( param.bodyStyle ){
            data.bodyStyle = param.bodyStyle;
        }

        return self.getTemplate( data, param.template );
    }

}

Site.Component.OverLay = new function(){

    var overLayId = 'PU_overlay';

    this.init = function(){

        if( $.browser.msie == true && parseInt( $.browser.version ) < 7 ){
            $('select').css({visibility: 'hidden'});
        }
        $('body').append('<div id="'+ overLayId +'">&nbsp;</div>');

    }

    this.remove = function(){

        if( $.browser.msie == true && parseInt( $.browser.version ) < 7 ){
            $('select').css({visibility: 'visible'});
        }
        $('#'+overLayId).remove();
    }
}

Site.Component.Select = new function(){

    this.empty = function( selectObj ){
		selectObj=selectObj[0].innerHTML = '';		
    }

    this.append = function( selectObj, data, value ){

		sObj=selectObj[0];
		for( var i = 0; i< data.length; i++ ){
			optObj = document.createElement('option'); 
			optObj.value = 	data[i]['id'];
			optObj.text  = 	data[i]['value'];
			try{ 
				sObj.add( optObj ,null);
			}
			catch(ex){
				sObj.add( optObj );
			}
		}
		delete optObj;
		delete sObj;
		
		if( value ){
            selectObj.val( value );
        }
    }

    this.newOptions = function( selectObj, data, value  ){

        this.empty( selectObj );
        this.append( selectObj, data, value );
    }
}

Site.Component.PopUp = new function(){
    var self = this;
    var windowId = 'PU_widow';
    var closeId = 'PU_close';
    var win = false;

    function removeWindow(){
        $('#'+windowId).remove();
        win = false;
        Site.Component.OverLay.remove();
    }

    function initWindow( html, param ){
        $('<div id="'+windowId+'"></div>')
            .appendTo('body')
            //.css({ width: param.width, height: param.height, marginTop: parseInt( param.height / 2 ) * -1, marginLeft:  parseInt( param.width / 2 ) * -1 })
            .css({ width: param.width, height: param.height, marginTop: parseInt( param.height  / 2 ) * -1 + parseInt( $(window).scrollTop() ) , marginLeft:  parseInt( param.width / 2 ) * -1 })
            .html( html )
            .find('#'+closeId).bind('click', self.close);

        $(document).bind('keydown.popup', function( e ){
            (e.keyCode) ? keyCode=e.keyCode : keyCode=e.which;
            if(keyCode == 27){
                self.close();
            }
        });
    }


    this.open = function( param , callBack){

        removeWindow();
        Site.Component.OverLay.remove();


        param = param || {};

        /** default beállítások **/
        param.template= param.template || 'index';
        param.title = param.title || '';
        param.close = param.close || '';
        param.width = param.width || 452;
        param.height = param.height || 400;
        param.overlay = param.overlay || true;

        if( param.overlay ){
            Site.Component.OverLay.init();
        }

        html = getTemplate( param );
        initWindow( html , param );

        if( typeof( callBack ) == 'function'){
            callBack( $('#' + windowId ) );
        }
    }


    this.close = function(){
        $(document).unbind('keydown.popup');
        removeWindow();
        Site.Component.OverLay.remove();
    }



    function getTemplate( param ){

        switch( param.template ){

            case 'kepek':
                html = new Array(
                    '<div class="popupKepek" style="{style}">',
                        '<span class="popupKepek_close">',
                                '<a href="#" id="'+closeId+'" title="{close}"><img src="images/popup_close_bg.gif" alt="{close}" /></a>',
                        '</span>',
                        '<img class="kepekImg" src="{kep}" alt="" />',
                    '</div>'
                );
            break;

            case 'uploading':
                html = new Array(
                        '<br /><br /><br /><br /><h1 style="font-size: 22px; color: #FFFFFF">adatok feltöltése folyamatban</h1>'
                );
            break;


            case 'oldaltovabbkuldes':

             html = new Array(
                    '<div class="guideBubbles">',
                        '<div class="relativeDiv">',
                            '<div class="contentDiv">',
                                '<table cellpadding="0" cellspacing="0" border="0" >',
                                    '<tr class="blurTr" >',
                                        '<td class="blurTd_topLeft"><img src="/image/bubbles/guideBubble_topLeft.png" /></td>',
                                        '<td class="fullTd_top">&nbsp;</td>',
                                        '<td class="blurTd_topRight"><img src="/image/bubbles/guideBubble_topRight.png" /></td>',
                                    '</tr>',
                                    '<tr>',
										'<td class="contentTd_left">&nbsp;</td>',
                                        '<td class="contentTd"><p>{text}</p></td>',
                                        '<td class="contentTd_right">&nbsp;</td>',
                                    '</tr>',
                                    '<tr class="blurTr" >',
                                        '<td class="blurTd_bottomLeft"><img src="/image/bubbles/guideBubble_bottomLeft.png" /></td>',
                                        '<td class="fullTd_bottom">&nbsp;</td>',
                                        '<td class="blurTd_bottomRight"><img src="/image/bubbles/guideBubble_bottomRight.png" /></td>',
                                    '</tr>',
                                '</table>',
                            '</div>',
                            '<div class="directionDiv"></div>',
                        '</div>',
                    '</div>'
                );
                break;

            case 'dialog':
                 html = new Array(
                    '<table cellpadding="0" cellspacing="0" style="width: {width}px;height: {height}px;">',
                        '<tr><td valign="middle">',
                            '<div class="guideBubbles" style="width: {width}px;">',
                                '<table cellpadding="0" cellspacing="0" border="0" style="width: {width}px;">',
                                    '<tr class="blurTr" >',
                                        '<td class="blurTd_topLeft"><img src="/image/bubbles/guideBubble_topLeft.png" /></td>',
                                        '<td class="fullTd_top">&nbsp;</td>',
                                        '<td class="blurTd_topRight"><img src="/image/bubbles/guideBubble_topRight.png" /></td>',
                                    '</tr>',
                                    '<tr>',
										'<td class="contentTd_left">&nbsp;</td>',
                                        '<td class="contentTd" align="center">{text}</td>',
                                        '<td class="contentTd_right">&nbsp;</td>',
                                    '</tr>',
                                    '<tr class="blurTr" >',
                                        '<td class="blurTd_bottomLeft"><img src="/image/bubbles/guideBubble_bottomLeft.png" /></td>',
                                        '<td class="fullTd_bottom">&nbsp;</td>',
                                        '<td class="blurTd_bottomRight"><img src="/image/bubbles/guideBubble_bottomRight.png" /></td>',
                                    '</tr>',
                                '</table>',
                            '</div>',
                        '</td></tr>',
                    '</table>'
                );
            break;


            default:
                html = new Array(
                    '<div class="popup" >',
                        '<div class="popup_inner">',
                            '<span class="popup_title">{title}</span>',
                            '<span class="popup_date">{date}</span>',
                            '<span class="popup_close">',
                                '<a href="#" id="'+closeId+'" title="{close}"><img src="images/popup_close_bg.gif" alt="{close}" /></a>',
                            '</span>',
                            '<div class="clr">&nbsp;</div>',
                            '<div class="popup_text scroll-pane">{text}</div>',
                            '<div class="clr">&nbsp;</div>',
                        '</div>',
                    '</div>'
                    );
            break;
        }

        return $.template( html.join('') ).apply( param );
    }

}


Site.Component.AjaxPager = new function(){
    var self= this;

    this.getUlObj = function(){
        return $('#ajaxPager');
    }


    this.init = function(){

        if( self.initFirstItem() != true ) {
            return;
        }
        this.getUlObj().find('li > a')
            .bind('click', Site.Component.AjaxPager.num )
            .eq(0).parents('li').addClass('currentItem');


    }

    this.initFirstItem = function(){

        lisObj = this.getUlObj().find('li');
        if( lisObj.size() < 2  ){
            delete lisObj
            return false;
        }

        newAObj = lisObj.eq(1).find('a').clone();
        newHref = newAObj.attr('href').split('/');
        newHref[ ( newHref.length -1 ) ] = '0';
        newAObj
            .text('1')
            .attr('href', newHref.join('/') );


        lisObj.eq(0)
            .find('u').remove().end()
            .prepend( newAObj );


        delete lisObj;
        delete newAObj;
        delete newHref;
        return true;
    }

    this.num = function(){
        self.setItem( $(this) );
        return false;
    }

    this.setItem = function( aObj ){

        $.ajax({
            url: aObj.attr('href'),
            success: function( data ){
                $('#boxCommens').html( data );
            }
        });
        $('li.currentItem', this.getUlObj() ).removeClass('currentItem');
        aObj.parents('li').addClass('currentItem');

        return false;
    }

}


Site.Component.ToolsMenu = function(){

    $('#sendtoprinter').bind ( 'click' , function() {
        window.print();
    });

    $('.PrintAndEmail_Email').bind('click',function(){

        html = new Array(
            '<form action="#" methor="post"><div class="ajaxHirdetes email_kuldes">',
            '<input type="hidden" name="id" id="postUrl" value="{url}" />',

            '<label class="fp_label" id="label_postFeladoNev" for="postFeladoNev">Feladó neve:</label><br />',
            '<input type="text" name="email" id="postFeladoNev" class="fp_input" value="" /><br />',

            '<label class="fp_label" id="label_postFeladoEmail" for="postFeladoEmail">Feladó e-mail címe:</label><br />',
            '<input type="text" name="email" id="postFeladoEmail" class="fp_input" value="" /><br />',

            '<label class="fp_label" id="label_postCimzettNev" for="postCimzettNev">Címzett neve:</label><br />',
            '<input type="text" name="email" id="postCimzettNev" class="fp_input" value="" /><br />',

            '<label class="fp_label" id="label_postCimzettEmail" for="postCimzettEmail">Címzett e-mail címe:</label><br />',
            '<input type="text" name="email" id="postCimzettEmail" class="fp_input" value="" /><br />',

            '<label class="fp_label" id="label_postText" for="postText">Üzenet:</label><br />',
            '<textarea name="text" class="fp_input" id="postText"></textarea><br /></div>'
        );
        html = $.template( html.join('') ).apply({ url: window.location.href });

        html+="<div class='dialogButtons'><input type='button' id='btn_mehet' class='submit button' /><input type='button' id='btn_megse' class='cancel button' /><div class='clr'>&nbsp;</div></div></form>";


        Site.Component.PopUp.open({
            height:390,
            width:223,
            template:'oldaltovabbkuldes',
            text:html
        },function( win ){
            $('#btn_mehet', win).bind('click',function(){

                postData = {};
                postData.url = $('#postUrl', win).val();
                postData.felado = $('#postFeladoNev', win ).val();
                postData.feladoemail = $('#postFeladoEmail', win ).val();
                postData.cimzett = $('#postCimzettNev', win ).val();
                postData.cimzettemail = $('#postCimzettEmail', win ).val();
                postData.text = $('#postText', win ).val();

                $.ajax({
                    url:'/ajaxtovabbkuld/index',
                    data: postData,
                    success: function( data ){
                        
						if( $.trim( data ) == 'ok' ){
							Site.alert( 'Levlét sikeresen kiküldtük!' );
						}
						else{
							Site.error('Kiküldés sikertene!<br />Kérjük próálja meg később');
						}
						setTimeout("Site.Component.PopUp.close();",3000);
                    }
                });
            });

            $('#btn_megse').bind('click' , Site.Component.PopUp.close );

        });

        return false;
    });
}

Site.Component.TextareCounter = function( param ){


    $( param.textarea ).bind ( 'keydown' , function(e) {
        (e.keyCode) ? keyCode=e.keyCode : keyCode=e.which;
        //alert ( keyCode );

        if( ( e.ctrlKey == true || e.altKey == true || keyCode == 45) ){
            return false;
        }

        if ( keyCode==8 || keyCode==9 ) {
            return true;
        }
        if ( $(this).val().length >= param.max ) {
            return false;
        }
        return true;

    })
    .bind ( 'keyup' , function() {
        $(param.counter).text( param.max - $(this).val().length ) ;
    })
    .bind('blur' , function() { $(this).val( $(this).val().substring(0, param.max) ) } )

    .trigger('keyup');

}



Site.Component.Galery = function(){


    $('head').append('<link href="/css/plugins/prettyPhoto.css" type="text/css" rel="stylesheet" />');
    $.require('plugins/jquery.prettyPhoto.js');

    $("a[rel^='prettyPhoto']").prettyPhoto({
        animationSpeed: 'normal', /* fast/slow/normal */
        padding: 40, /* padding for each side of the picture */
        opacity: 0.75, /* Value betwee 0 and 1 */
        showTitle: true, /* true/false */
        allowresize: true /* true/false */
    });

}


Site.Component.PicRemove = new function(){
    var self = this;

    this.confirm = function(){
        aObj = $(this);
        html = new Array(
            '<div class="ajaxPicRemove">',
                '<h2>Biztos törölni szeretnéd?</h2>',
            '</div>',
            '<div class="ajaxUserSaveButtons">',
                '<input type="button" id="btn_mehet" class="submit button" />',
                '<input type="button" id="btn_megse" class="cancel button" />',
                '<div class="clr">&nbsp;</div>',
            '</div>'
        );

        html = $.template( html.join('') ).apply();

        Site.Component.PopUp.open({
            height:200,
            width:130,
            template:'oldaltovabbkuldes',
            text:html
        },function( win ){
            $('#btn_mehet', win).bind('click',function(){
                self.runDelete( aObj );
                Site.Component.PopUp.close();
            });

            $('#btn_megse').bind('click' , Site.Component.PopUp.close );
        });
        return false;
    }

    this.runDelete = function( aObj ){

        imgData = aObj.attr('rel').split('|');

        $.ajax({
            url:'/ajaxremovepic/removepic',
            data:{
                adid: imgData[0],
                pic: imgData[1]
            },
            async: true,
            dataType: "json",
            success: function( data ){

                if( data && data.success == true ){
                    aObj.parent().remove();
                }
                else{
                    Site.error( 'Sikertlen törlés!' );
                }
            },
            error: function(){
                Site.error( 'Sikertlen törlés!' );
            }
        });
    }
}

Site.Component.CheckMail = function( email ){
	ok = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(  email );
	return ok;
}
