"use strict";
var $container        =   jQuery('.tzBlogContainer'),
    Desktop           =   variables_blog.desktop,
    tabletportrait    =   variables_blog.tabletportrait,
    mobilelandscape   =   variables_blog.mobilelandscape,
    mobileportrait    =   variables_blog.mobileportrait,
    //$filter_status    =   variables_portfolio.filter_status,
    //$paging           =   variables_portfolio.paging,
    resizeTimer = null;

/*
 * Method reize image
 * @variables class
 */
function interiart_resize_image(obj){
    var widthStage;
    var heightStage ;
    var widthImage;
    var heightImage;
    obj.each(function (i,el){

        heightStage = jQuery(this).height();

        widthStage = jQuery (this).width();

        var img_url = jQuery(this).find('img').attr('src');

        var image = new Image();
        image.src = img_url;

        widthImage = image.naturalWidth;
        heightImage = image.naturalHeight;

        var tzimg	=	new resizeImage(widthImage, heightImage, widthStage, heightStage);
        jQuery(this).find('img').css ({ top: tzimg.top, left: tzimg.left, width: tzimg.width, height: tzimg.height });


    });
}

/*
 * Method portfolio column
 * @variables Desktop
 * @variables tabletportrait
 * @variables mobilelandscape
 * @variables mobileportrait
 */
function tz_init(Desktop, tabletportrait, mobilelandscape, mobileportrait){
    var contentWidth    = jQuery('.tzBlogContainer').width();
    var numberItem      = 2;
    var newColWidth     = 0;
    var featureColWidth = 0;
    var widthWindwow = jQuery(window).width();
    if (widthWindwow >= 992) {
        numberItem = Desktop;
        jQuery('.tzBlogContainer').addClass('tzBlogDesktop-'+Desktop+'-Column');
    } else if (  widthWindwow >= 768) {
        numberItem = tabletportrait ;
        jQuery('.tzBlogContainer').addClass('tzBlogTabletPortrait-'+Desktop+'-Column');
    } else if (  widthWindwow >= 480) {
        numberItem = mobilelandscape ;
        jQuery('.tzBlogContainer').addClass('tzBlogMobileLandscape-'+Desktop+'-Column');
    } else if (widthWindwow < 480) {
        numberItem = mobileportrait ;
        jQuery('.tzBlogContainer').addClass('tzBlogMobile-'+Desktop+'-Column');
    }
    newColWidth = Math.floor(contentWidth / numberItem);
    featureColWidth = newColWidth * 2;
    jQuery('.blogColumn-item').width(newColWidth);
    jQuery('.tz_feature_item').width(featureColWidth);
    var $container  = jQuery('.tzBlogContainer') ;
    $container.imagesLoaded(function(){
        $container.isotope({
            masonry:{
                columnWidth: newColWidth
            }
        });

    });
    interiart_resize_image(jQuery('.tzBlogQuote_bg'));

    // HEIGHT VIMEO + HTML5
    jQuery('.tzBlogVideo').each(function(){
        var $width_video = jQuery(this).width();
        jQuery(this).css('height',(($width_video*9)/16)+'px');
    });

    jQuery('.tzBlog_videoHtml5').each(function(){
        var $width_html5 = jQuery(this).width();
        jQuery(this).css('height',(($width_html5*9)/16)+'px');
    });
}

/*
 * Method reize
 */
jQuery(window).bind('load resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout("tz_init(Desktop, tabletportrait, mobilelandscape, mobileportrait)", 100);
});

/*
 * Method filter portfolio
 */
//function loadPortfolio(){
//    if ( $filter_status == 'show' ) {
//        var $optionSets = jQuery('.tzFilter'),
//            $optionLinks = $optionSets.find('button');
//        $optionLinks.on('click',function(event){
//            event.preventDefault();
//            var $this = jQuery(this);
//            // don't proceed if already selected
//            if ( $this.hasClass('selected') ) {
//                return false;
//            }
//            var $optionSet = $this.parents('.tzFilter');
//            $optionSet.find('.selected').removeClass('selected');
//            $this.addClass('selected');
//
//            // make option object dynamically, i.e. { filter: '.my-filter-class' }
//            var options = {},
//                key = $optionSet.attr('data-option-key'),
//                value = $this.attr('data-option-value');
//
//            // parse 'false' as false boolean
//            value = value === 'false' ? false : value;
//            options[ key ] = value;
//
//            if ( key === 'layoutMode' && typeof changeLayoutMode === 'function' ) {
//                // changes in layout modes need extra logic
//                changeLayoutMode( $this, options );
//            } else {
//                // otherwise, apply new options
//                $container.isotope( options );
//            }
//            return false;
//        });
//        tz_init(Desktop, tabletportrait, mobilelandscape, mobileportrait);
//    }
//}
//jQuery(document).ready(function(){
//    $container.imagesLoaded( function(){
//        tz_init(Desktop, tabletportrait, mobilelandscape, mobileportrait);
//    });
//    //create_tags();
//    loadPortfolio();
//});

//if ( $paging != 'pagenavi' ){
//
//    jQuery(function(){
//        $container.infinitescroll({
//                navSelector  : '#loadajax a',    // selector for the paged navigation
//                nextSelector : '#loadajax a:first',  // selector for the NEXT link (to page 2)
//                itemSelector : '.portfolio-item',     // selector for all items you'll retrieve
//                errorCallback: function(){
//                    jQuery('#tz_append a').addClass('tzNomore');
//
//                },
//                loading: {
//                    msgText:'',
//                    finishedMsg: '',
//                    img: variables_portfolio.image,
//                    selector: '#tz_append'
//                }
//            },
//            // call Isotope as a callback
//            function( newElements ) {
//                var $newElems =   jQuery( newElements ).css({ opacity: 0 });
//                // ensure that images load before adding to masonry layout
//                $newElems.imagesLoaded(function(){
//                    // show elems now they're ready
//                    $newElems.animate({ opacity: 1 });
//                    // trigger scroll again
//                    $container.isotope( 'appended', $newElems);
//                    if (String(jQuery.browser.safari) && String(document.readyState) !== "complete") {
//                        tz_init(Desktop, tabletportrait, mobilelandscape, mobileportrait);
//                    } else {
//                        tz_init(Desktop, tabletportrait, mobilelandscape, mobileportrait);
//                    }
//                    //if there still more item
//                    if($newElems.length){
//                        //move item-more to the end
//                        jQuery('div#tz_append').find('a:first').show();
//                    }
//                });
//
//                create_tags();
//            }
//        );
//
//        if ( $paging == 'ajaxbutton' ){
//            jQuery(window).unbind('.infscr');
//
//            jQuery('div#tz_append a').on('click',function(){
//                jQuery('div#tz_append').find('a:first').hide();
//                $container.infinitescroll('retrieve');
//            });
//        }
//
//    });
//}

jQuery(document).ready(function(){
    "use strict";

    // JQUERY VIDEO HTML5
    jQuery('.tzblog_autoplay').on('click',function(){
        jQuery(this).hide();
        jQuery(this).parent().parent().find('.bg-video').hide();
        jQuery(this).parent().parent().find('.tzblog_pauses').show();
        if (jQuery(this).parent().parent().find('.videoID')[0].paused)
            jQuery(this).parent().parent().find('.videoID')[0].play();

    }) ;
    jQuery('.tzblog_pauses').on('click',function(){
        jQuery(this).hide();
        jQuery(this).parent().parent().find('.bg-video').show();
        jQuery(this).parent().parent().find('.tzblog_autoplay').show();
        if (jQuery(this).parent().parent().find('.videoID')[0].play)
            jQuery(this).parent().parent().find('.videoID')[0].pause();

    });

}) ;
jQuery(window).load(function(){
    // SLIDESHOW FLEXSLIDER
    jQuery('.tzBlogSlider').flexslider({
        animation: "slide",
        controlNav: true,
        animationLoop: true,
        directionNav: false,
        prevText: "Previous",
        nextText: "Next",
        smoothHeight: true
    });
});