/*------------------------------------------------------------------------
# ThinApp
# ------------------------------------------------------------------------
# Designed by ThinApp
# Websites:  http://getthinapp.com -  Email: info@getthinapp.com
--------------------------------------------------------------------------*/

$(function(){
    var sys_show_popup_login = $(".sys_show_popup_login"),
        sys_popup_common = $("#sys_popup_common");

    /* Homepage Slider
    ---------------------------------------------------------- */
    if ($("#md-slider-1").length) {
        $("#md-slider-1").mdSlider({
            fullwidth: true,
            transitions: "fade",
            width: 1366,
            height: 650,
            responsive: true,
            slideShowDelay: 6000,
            slideShow: true,
            loop: true,
            showLoading: false,
            showArrow: 1,
            showBullet: 1,
            posBullet: 2,
            showThumb: true,
            enableDrag: true
        });
    }

    /* get Twitter
    ---------------------------------------------------------- */

/*
    $('#sys_lst_tweets').tweet({
            modpath: '/twitter/',
            count: 2,
            loading_text: '<p class="rs ta-c fc-white">loading Twitter <br /><img src="images/ajax-loader.gif" alt="loading"/></p>',
            username: 'megadrupal',
            template: '<p class="rs tweet-mind">{text}</p><p class="rs timestamp">{time}</p><i class="icon iTwitter"></i>',
    });
*/

    /* Project Slider
    ---------------------------------------------------------- */
    if ($("#slider1").length > 0) {
        $("#slider1").responsiveSlides({
            auto: true,			// Boolean: Animate automatically, true or false
            pager: true,		// Boolean: Show pager, true or false
            nav: true,			// Boolean: Show navigation, true or false
            speed: 500,			// Integer: Speed of the transition, in milliseconds
            maxwidth: 800,		// Integer: Max-width of the slideshow, in pixels
            //namespace: "centered-btns", 	// String: Change the default namespace used
		  timeout: 4000,          // Integer: Time between slide transitions, in milliseconds
		  random: false,          // Boolean: Randomize the order of the slides, true or false
		  pause: true,           // Boolean: Pause on hover, true or false
		  pauseControls: true,    // Boolean: Pause when hovering controls, true or false
		  prevText: "Previous",   // String: Text for the "previous" button
		  nextText: "Next",       // String: Text for the "next" button
		  navContainer: "after",       // Selector: Where controls should be appended to, default is after the 'ul'
		 // manualControls: "",     // Selector: Declare custom pager navigation
		  manualControls: '#unique-pager',
		  thumb: "img", 			// String: Show a thumbnail in pager. Options: false, img = as image, bg = as background-image
		  thumbSrc : false, 	// String: Define a thumb src as a dataset (data-thumb="src" is called thumbSrc : 'thumb' 		  
		  namespace: "rslides",   // String: Change the default namespace used
		  //before: function(){},   // Function: Before callback
		  //after: function(){}     // Function: After callback			
        });
    }

    /* Tabs
    ---------------------------------------------------------- */
    $(".tabbable").on("click",".nav-tabs > li",function(){
        if($(this).hasClass("disable"))
            return false;
        var getIndex=$(this).index();
        $(this).siblings().removeClass("active").end().addClass("active");
        $(this).parents(".tabbable").find(".tab-content .tab-pane").removeClass("active").eq(getIndex).addClass("active");
        return false;
    });

    $(".tabbable");

    /* Accordion
    ---------------------------------------------------------- */
    $(".accordion").on("click",".accordion-label",function(){
        if($(this).hasClass("active")){
            $(this).removeClass("active").siblings(".accordion-content").slideUp(400,function(){
                $(this).removeClass("active").removeAttr("style");
            });
        }else{
            $(this).parents(".accordion").find(".accordion-label").removeClass("active");
            $(this).addClass("active").siblings(".accordion-content").slideDown(400,function(){
                $(this).addClass("active").removeAttr("style");
            });
            $(this).parent().siblings().find(".accordion-content").slideUp(400,function(){
                $(this).removeClass("active").removeAttr("style");
            });
        }
        return false;
    });

    /* Open popup when click to: Register, Login
    ---------------------------------------------------------- */
    sys_show_popup_login.on("click",function(){
        sys_popup_common.fadeIn();
        $("body").on("keydown.closePopup",function(e){
            var getCode = e.keyCode ? e.keyCode : e.which;
            if(getCode == 27) {
                sys_popup_common.find(".closePopup").trigger("click");
            }
        });
        return false;
    });
    sys_popup_common.on("click.closePopup",".closePopup,.overlay-bl-bg",function(){
        sys_popup_common.fadeOut(function(){
            $("body").off("keydown.closePopup");
        });
    });
    sys_popup_common.on("click",".main-content",function(e){
        e.stopPropagation();
    });

    /* Loadmore button on Category
    ---------------------------------------------------------- */
    $('#showmoreproject').bind('click', function (e) {
        _self = $(this);
        _self.text('Loading...')
        $.ajax({
            url: "ajax/category.php"
        }).done(function(data) {
            $('#list-project-ajax').append(data);
            _self.text('Show more projects')
        });
        return false;
    });

    /* Loadmore button on blog
    ---------------------------------------------------------- */
    $('#showmorepost').bind('click', function (e) {
        _self = $(this);
        _self.text('Loading...')
        $.ajax({
            url: "ajax/blog.php"
        }).done(function(data) {
            $('#list-blog-ajax').append(data);
            _self.text('Show more posts')
        });
        return false;
    });

    /* Loadmore button on search results
    ---------------------------------------------------------- */
    $('#showmoreresults').bind('click', function (e) {
        _self = $(this);
        _self.text('Loading...')
        $.ajax({
            url: "ajax/search-results.php"
        }).done(function(data) {
            $('#list-search-ajax').append(data);
            _self.text('Show more projects')
        });
        return false;
    });

    /* Contact form: Ajax & Validate
    ---------------------------------------------------------- */
    if(jQuery("#contact-form").length > 0){
        // Validate the contact form
        jQuery('#contact-form').validate({

            // Add requirements to each of the fields
            rules: {
                name: {
                    required: true,
                    minlength: 2
                },
                email: {
                    required: true,
                    email: true
                },
                message: {
                    required: true,
                    minlength: 10
                }
            },

            // Specify what error messages to display
            // when the user does something horrid
            messages: {
                name: {
                    required: "Please enter your name.",
                    minlength: jQuery.format("At least {0} characters required.")
                },
                email: {
                    required: "Please enter your email.",
                    email: "Please enter a valid email."
                },
                message: {
                    required: "Please enter a message.",
                    minlength: jQuery.format("At least {0} characters required.")
                }
            },

            // Use Ajax to send everything to processForm.php
            submitHandler: function(form) {
                jQuery("#submit-contact").attr("value", "Sending...");
                jQuery(form).ajaxSubmit({
                    success: function(responseText, statusText, xhr, $form) {
                        jQuery("#response").html(responseText).hide().slideDown("fast");
                        jQuery("#submit-contact").attr("value", "Submit");
                        jQuery(form).find("input[type=text]").val('');
                        jQuery(form).find("input[type=email]").val('');
                        jQuery(form).find("input[type=url]").val('');
                        jQuery(form).find("textarea").val('');
                    }
                });
                return false;
            }
        });
    }

    /* Responsive
    ---------------------------------------------------------- */
    var sys_btn_toggle_search = $("#sys_btn_toggle_search"),
        sys_header_right = $("#sys_header_right");
    sys_btn_toggle_search.on("click",function(){
        sys_header_right.slideToggle(function(){
            if($(this).is(":visible")){
                $(this).addClass("active");
            }else{
                $(this).removeClass("active");
            }
            $(this).removeAttr("style");
        });
        sys_btn_toggle_search.toggleClass("active");
    });

    /* Navigation on mobile (Sidr)
    ---------------------------------------------------------- */
    $('#btn-toogle-menu').sidr({
        side:"left",
        name:"alternate-menu",
        source:"#right-menu"
    });
    if ($("#sys-nav-menu-blog").length > 0) {
        selectnav("sys-nav-menu-blog");
    }

});

function makeID(l) {
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    for( var i=0; i < l; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}

function initUploads() {
    if (window.File && window.FileList && window.FileReader) {
        $('.fileUpload input').change(function(event){
            var files = event.target.files || event.dataTransfer.files;
            var url   = typeof $(this).parent(".fileUpload").attr("rel") != 'undefined' && $(this).parent(".fileUpload").attr("rel").length ? $(this).parent(".fileUpload").attr("rel") : "/project/upload";

            if (!files.length) {
                return;
            };	

            var xhr = new XMLHttpRequest();
            var file  = files[0];
            var id    = makeID(15)
            var html  = '<div class="progress-bar" id="'+id+'"><div></div><span>0%</span><i></div>';
            var box   = $(event.target).parent().parent();

            box.append(html);

            var fd    = new FormData();

            fd.append("type", $(this).attr('data-type'));
            fd.append("ufile", file);

            // get URL from object type.
            xhr.open('POST', url, true);

            xhr.upload.onprogress = function(e) {
                if (e.lengthComputable) {
                    var pc = parseInt(e.loaded / e.total * 100);
                    $('#' + id + ' div').css('width', pc+'%');

                    if (pc < 100) {
                        $('#' + id + ' span').html(pc+'%');
                    } else {
                        $('#' + id + ' span').html('Uploading finished, optimizing');
                    }
                }
            };			
			
            xhr.onload = function() {
                var resp = JSON.parse(this.response);		

                if (resp.status != 'ok') {
                    $('#' + id + ' div').css('background', 'rgba(205, 87, 87, 0.72)');
                    $('#' + id + ' span').html('Error uploading file, try again');
                } else {				
                    $('#' + id + ' div').css('background', 'rgba(108, 205, 87, 0.72)');
                    $('#' + id + ' span').html(file.name + ' - Done');
                    $('#' + id).append('<input type="hidden" id="input_'+id+'" name="files[]" value="'+resp.id+'">');
                    $('#' + id).append('<i onclick="$(\'#'+ id+'\').remove();">Delete</i>');			
                }
            };

            xhr.send(fd);
        })

        $('.fileUpload').closest('form').submit(function(){
            $(this).find('input[type=file]').remove();
        })
    }
}

$(function(){
    initUploads();
})