/*
THEME         : edirne
TEMPLATE      : mekan
ADMIN TEMPLATE: 
*/
var THEME = 'edirne';
var TEMPLATE = 'mekan';
var _lang = {"close":"Kapat", "ok":"Tamam", "cancel":"İptal"};var getPage;
var theme = {
    initialize: function(e){
        // AJAX Supports
        AJAX = ("onhashchange" in window) && AJAX ? true : false;
        // NAV LINKS CLICK EVENT
        if(AJAX){
            $('nav a').click(function(e){
                e.preventDefault();
                $('nav a.selected').removeClass('selected');
                $(this).addClass('selected');
                // set hash location to start xhttp request
                location.href = '#!'+$(this).attr('href');
            }).css({
                'opacity':0,
                'margin-top':0,
                'z-index':100
            });
            $('nav a').each(function(ID){
                $(this).delay(200).animate({
                    'opacity':1
                }, {
                    duration: 200*(ID+1)
                });
            });
        }
        this.menu();
        // LANGUAGES
        $('#languages a').click(function(){
            $.post('/', {
                'LANG':$(this).data('val')
            }, function(){
                location.href = href ? href : location.href;
            });
        });
        // LOGO - NAV
        $('#logo img').load(function(){
            $('nav').css({
                'left':$(this).width()
            });
        });
        // RESIZE + MEDIA QUERY FIXED
        var normal = function(){
            // LOGO - NAV
            $('nav').css({
                'left':$('#logo img').width()
            });
        };
        var tablet = function(){
            // LOGO - NAV
            $('nav').css({
                'left':$('#logo img').width()
            });
            $('#content').width($('#page').width()-$('#aside').width()-10);
        };
        var mobile = function(){
        };
        var layout = function(){
            var w = $(document).width();
            if(w <= 610){
                mobile()
            } else if(w > 610 && w < 960){
                tablet();
            } else {
                normal();
            }
        };
        $(window).resize(function(){
            layout();
        });
        layout();
    },
    // PAGE MENU LINKS CLICK EVENT
    menu: function(){
        if(AJAX)
            $('menu a').click(function(e){
                e.preventDefault();
                $('menu a.selected').removeClass('selected');
                $(this).addClass('selected');
                // set hash location to start xhttp request
                location.href = '#!'+$(this).attr('href');
            }).hide();
        $('menu a').fadeIn();
    }
};
document.createElement('header');
document.createElement('hgroup');
document.createElement('nav');
document.createElement('menu');
document.createElement('section');
document.createElement('article');
document.createElement('aside');
document.createElement('footer');var pop = function(options) {
    $('#result').remove();
    var settings = {
        'position': 'fixed',
        'zIndex'  : 9000,
        'width'   : 400,
        'height'  : 150,
        'content' : ''
    };
    if(options) $.extend(settings, options);
    var r = $('<div>',{
        'id':'result',
        'css':{
            'position': settings.position,
            'z-index': settings.zIndex,
            'width': settings.width,
            'min-height': settings.height
        }
    }).appendTo(document.body).hide()
    .html('<div class="pop rounded">'
        +'<div class="container">'
        + settings.content
        +'<div class="clear"></div>'
        +'</div></div>') .fadeIn('fast');
    r.css({
        'top':(($(window).height() - r.outerHeight(1))*50/$(window).height()) + '%',
        'left':(($(window).width() - r.outerWidth(1))*50/$(window).width()) + '%'
    });
    $(document).bind('keydown',function(e){
        if(e.keyCode == 27){ // esc
            $('#result').fadeOut('fast');
            $(document).unbind();
        }
    });
    return 0;
};
// FORMAT
(function( $ ){
    var s;
    // Methods
    var methods = {
        init: function(e){},
        start: function(e){},
        complete: function(r){},
        error: function(r){
            alert(r.error);
            return false;
        },
        captcha: function(){
            $('img.captcha')
            .attr('src',$('img.captcha').attr('src')+'?'+Math.random());
        },
        result: function(r,form){
            r.code = r.code || 800;
            if(r.code == 200) settings.complete(r);
            $(form).find('.submit').removeAttr('disabled');
            if(r.fields) for (var i in r.fields)
                if(form)
                    $(form).find('[name="'+r.fields[i]+'"]').focus().addClass('marked');
                else
                    $('.'+r.fields[i]).focus().addClass('marked');
            +(r.debug?'<p class="debug">'+ r.debug +'</p><div class="clear"></div>':'')
            +'<div class="close" onclick="$(\'#result\').html(\'\');">'+_lang.close+'</div>';
            pop({
                'content':'<h1>'+ r.status +'</h1><p>'+ r.message +'</p>'
            });
            $('#result').addClass(r.code>=400 ? (r.code>=600 ? 'error' : 'alert') : 'success');
            //console.log(r);
            if(r.redirect) location.href = r.redirect;
            return 0;
        }
    };
    $.fn.format = function(options) {
        // Settings
        settings = {
            'init'        : methods.init,
            'start'       : methods.start,
            'complete'    : methods.complete,
            'type'        : 'json',
            'result'      : '',
            'nosupport'   : 'No support for the File API in this web browser',
            'noimage'     : 'Unsupported file type!',
            'uploaded'    : 'Uploaded',
            'maxsize'     : '10240', //Kb
            'post'        : 'upload.php'
        };
        if(options == 'result')
            return methods.result.apply(this, Array.prototype.slice.call( arguments, 1 ));
        else if(options) $.extend(settings, options);
        this.each(function(){
            settings.init($(this));
            $(this).bind('submit', function(e) {
                var form = $(this);
                e.preventDefault();
                $('#result').empty();
                $(this).find('.submit').attr('disabled','disabled');
                $(this).find('.loader').show();
                $(this).find('.marked').removeClass('marked');
                $(this).find('input, textarea').focus();
                settings.start($(this));
                $.ajax({
                    url: $(this).attr('action'),
                    type: 'post',
                    data: $(this).serialize() + '&js=1',
                    dataType: settings.type,
                    success: function(r){
                        form.find('input, textarea').blur();
                        if(r) methods.result(r,form);
                        methods.captcha();
                        form.find('.submit').removeAttr('disabled');
                        form.find('.loader').hide();
                    }
                });
            });
        });
        $('a.changecaptcha').click(function(){
            methods.captcha();
        });
        $('.vlabel')
        .each(function(){
            if($(this).data('value') && !$(this).val()){
                $(this).val($(this).data('value'));
                $(this).data('label',$(this).val());
            } else if(!$(this).data('value') && $(this).val()) {
                $(this).data('label',$(this).val());
            }
        })
        .focus(function(){
            if($(this).data('label') == $(this).val()){
                $(this).val('');
                $(this).removeClass('vlabel')
            }
        })
        .blur(function(){
            if($(this).val() == '') {
                $(this).val($(this).data('label'));
                $(this).addClass('vlabel')
            }
        });
    }
})( jQuery );
/*
var tool = {
    countries: function(url,item,selected) {
        $.ajax({
            url:url,
            type:'post',
            dataType:'json',
            success:function(r){
                if(r.countries){
                    $.each(r.countries, function(j, i){
                        $('<option>',{
                            'selected':(selected==i.code)
                        }).val(i.code).html(i.name).appendTo('#'+item);
                    });
                }
            }
        });
    },
    regions: function (url,item,selected) {
        $('#'+item).empty();
        $('<option>').val('').html('loading ...').appendTo(item);
        $.ajax({
            url:url,
            type:'post',
            dataType:'json',
            success:function(r){
                if(r.regions){
                    $('#'+item).empty();
                    $.each(r.regions, function(j, i){
                        $('<option>',{
                            'selected':(selected==i.code)
                        }).val(i.code).html(i.name).appendTo('#'+item);
                    });
                }
            }
        });
    }
};
*/
(function( $ ){
    var s;
    // Methods
    var m = {
        init: function(e){},
        start: function(e){},
        complete: function(r){},
        error: function(r){
            alert(r.error);
            return false;
        },
        traverse: function(files,area){
            if (typeof files !== "undefined") {
                for (var i=0, l=files.length; i<l; i++) {
                    m.upload(files[i], area);
                }
            } else {
                area.html(nosupport);
            }
        },
        upload: function(file, area){
            //area.empty();
            var progress = $('<div>',{
                'class':'progress'
            });
            area.append(progress);
			
            // File type control
            if (typeof FileReader === "undefined" || !(/image/i).test(file.type)) {
                //area.html(file.type,s.noimage);
                alert('only image files: jpeg, png, gif');
                return false;
            }

            // File size control
            if (file.size > (s.maxsize * 1024)) {
                //area.html(file.type,s.maxsize);
                alert('max upload size: ' + s.maxsize + 'Kb');
                return false;
            }
			
            // Uploading - for Firefox, Google Chrome and Safari
            var xhr = new XMLHttpRequest();
            // Update progress bar
            xhr.upload.addEventListener("progress", function (e) {
                if (e.lengthComputable) {
                    var loaded = Math.ceil((e.loaded / e.total) * 100) + "%";
                    progress.css({
                        'height':loaded
                    }).html(loaded);
                }
            }, false);
			
            // File uploaded
            xhr.addEventListener("load", function (e) {
                var r = jQuery.parseJSON(e.target.response);
                //console.log(area,r);
                s.complete(r);
                area.find('img').remove();
                //area.data('value',r.name);
                area.append($('<img>',{
                    'src': r.image + '?' + Math.random()
                }));
                progress.addClass('uploaded');
                progress.html(s.uploaded).fadeOut('slow');
            }, false);
			
            xhr.open("post", s.post, true);
            
            // Set appropriate headers
            xhr.setRequestHeader("content-type", "multipart/form-data");
            xhr.setRequestHeader("x-file-name", file.fileName);
            xhr.setRequestHeader("x-file-size", file.fileSize);
            xhr.setRequestHeader("x-file-type", file.type);

            // Set request headers
            for (var i in area.data())
                if (typeof area.data(i) !== "object")
                    xhr.setRequestHeader('x-param-'+i, area.data(i));

            xhr.send(file);
        }
    };
    $.fn.droparea = function(o) {
        // Settings
        s = {
            'init': m.init,
            'start': m.start,
            'complete': m.complete,
            'instructions': 'drop an image file here',
            'over'        : 'drop file here!',
            'nosupport'   : 'No support for the File API in this web browser',
            'noimage'     : 'Unsupported file type!',
            'uploaded'    : 'Uploaded',
            'maxsize'     : '10240', //Kb
            'post'        : 'upload.php'
        };
        this.each(function(){
            if(o) $.extend(s, o);
            var instructions = $('<div>')
            .css({
                'width':$(this).width()-4,
                'height':$(this).height()-4
            })
            .appendTo($(this));
            s.init($(this));
            if(!$(this).data('value'))
                instructions.addClass('instructions').html(s.instructions);

            $(this)
            .bind({
                dragleave: function (e) {
                    e.preventDefault();
                    if($(this).data('value'))
                        instructions.removeClass().empty();
                    else
                        instructions.removeClass('over').html(s.instructions);
                },
                dragenter: function (e) {
                    e.preventDefault();
                    instructions.addClass('instructions over').html(s.over);
                },
                dragover: function (e) {
                    e.preventDefault();
                }
            });
            this.addEventListener("drop", function (e) {
                e.preventDefault();
                s.start($(this));
                m.traverse(e.dataTransfer.files, $(this));
                instructions.removeClass().empty();
            },false);
        });
    };
})( jQuery );


$(document).ready(function() {
    $('.form').format();
//$('<div>').format('result','1','2','3');
});
