/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

// IMAGE OVERLAY
jQuery(document).ready(function() {

    //  IMAGE PRELOADER   
    jQuery('.preload').preloadImages({
        showSpeed: 2000,   // length of fade-in animation, 500 is default
        easing: 'easeOutQuad'   // optional easing, if you don't have any easing scripts - delete this option
    });
    jQuery('.meta-like').click(function(){
        var currentLike=jQuery(this);
        if(jQuery(this).attr('href')!='#')
            jQuery.post(jQuery(this).attr('href'), function(response) {
                currentLike.parent().html(response);
            });

        return false;
    });
    //PRETTYPHOTO
    jQuery("a[rel^='prettyPhoto']").prettyPhoto({deeplinking:false});

    //OVERLABEL
    jQuery("label.overlabel").overlabel();

    //SCROLLABLE
    jQuery(".scrollable").scrollable({
        circular: true,
        mousewheel: true
    }).navigator().autoscroll({
        interval: 5000
    });

    // IMAGE OVERLAY
    imgIconOverlay(jQuery);

    // MASONRY
    
    //YOUTUBE VMODE FIX
    jQuery("iframe").each(function(){
        var ifr_source = jQuery(this).attr('src');
        var pos = ifr_source.indexOf("youtube.com");
        if(pos > -1) {
            var wmode = "?autohide=1&wmode=opaque";
            jQuery(this).attr('src',ifr_source+wmode);
        }
    });

    // ENTRY IMAGE SLIDE
    jQuery('.entry-image-slide').each(function(index){
        jQuery(this).after('<ul class="jcycle-pager nav-item'+index+'">').cycle({
            pager:  '.jcycle-pager',
            fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
            easing: 'jswing',
            pause: true,
            autostop: false,
            pager:  '.nav-item'+index,
            pagerAnchorBuilder: function(idx, slide) {
                return '<li><a href="#">*</a></li>';
            }
        });
    });
	
    if(typeof(optionss)!="undefined"){
        //jCycle Slider Function
        if(optionss['slidername']=='jCycle'){
            $jcyclepager=jQuery('<div class="jcycle-pager" id="jcycle-pager">');
            jQuery('.jcycle').after($jcyclepager);
            jQuery('.jcycle').cycle({
                random: optionss['j_ramdom'],
                pager:$jcyclepager,
                //                allowPagerClickBubble:optionss['j_allowPagerClickBubble'],
                autostop: optionss['j_autostop'],
                autostopCount: optionss['j_autostopCount'],
                backwards:optionss['j_backwards'],
                //                continuous:optionss['j_continuous'],
                //                delay:optionss['j_delay'],
                //                easeIn:optionss['j_easeIn'],
                //                easeOut:optionss['j_easeOut'],
                //                easeInOutExpo:optionss['j_easeInOutExpo'],
                //                fx:optionss['j_fx'],
                //                manualTrump:optionss['j_manualTrump'],
                                pause:optionss['j_pause'],
                                pauseOnPagerHover:optionss['j_pauseOnPagerHover'],
                //                requeueTimeout:optionss['j_requeueTimeout'],
                slideExpr:optionss['j_slideExpr'],
                slideResize:optionss['j_slideResize'],
                speed:optionss['j_speed'],
                speedIn:optionss['j_speedIn'],
                speedOut:optionss['j_speedOut'],
                startingSlide:optionss['j_startingSlide'],
                timeout:optionss['j_timeout']
            });
            
        }
        // ROUND ABOUT SLIDER FUNCTION
        if(optionss['slidername']=='Roundabout'){
            jQuery('ul#roundabout').roundabout(
            {
                easing: optionss['r_easing'],
                duration: optionss['r_duration'],
                startingChild: optionss['r_startingChild'],
                //                shape: optionss['r_shape'],
                animationspeed: optionss['r_animationspeed']
            });
            // ROUND ABOUT SLIDER AUTOPLAY FUNTION
            var interval;

            jQuery('ul#roundabout').hover(
                function() {
                    // oh no, it's the cops!
                    clearInterval(interval);
                },
                function() {
                    // false alarm: PARTY!
                    interval = startAutoPlay();
                });

            function startAutoPlay() {
                return setInterval(function() {
                    jQuery('ul#roundabout').roundabout_animateToNextChild(500, optionss['easing']);
                }, 5000);
            }
            
            // let's get this party started
            interval = startAutoPlay();
        }
        if(optionss['slidername']=='Kwicks'){
            jQuery('#kwicks').kwicks({
                max: optionss['k_max'],
                min: optionss['k_min'],
                isVertical: optionss['k_isVertical'],
                spacing: optionss['k_spacing'],
                delay: optionss['k_delay'],
                sticky: optionss['k_sticky'],
                defaultKwick: optionss['k_defaultKwick'],
                event: optionss['k_event'],
                duration: optionss['k_duration'],
                easing: optionss['k_easing'],
                autoplay: true
            });
        }
        if(optionss['slidername']=='Nivo' || optionss['slidername']=='NivoFullwidth'){
            jQuery('#nivo-slider').nivoSlider({
                effect:optionss['n_effect'],
                slices:optionss['n_slices'],
                animSpeed:optionss['n_animSpeed'],
                pauseTime:optionss['n_pauseTime'],
                startSlide: optionss['n_startSlide'],
                directionNav:optionss['n_directionNav'],
                directionNavHide:optionss['n_directionNavHide'],
                controlNav:optionss['n_controlNav'],
                controlNavThumbs:false,
                keyboardNav:optionss['n_keyboardNav'],
                pauseOnHover:optionss['n_pauseOnHover'],
                manualAdvance:optionss['n_manualAdvance'],
                captionOpacity:optionss['n_captionOpacity']
            });
        }
    }
    jQuery("#contactform").validate();
    jQuery("#commentform").validate();
	
	jQuery('.anchorLink').click(function() {
		jQuery('body,html').animate({scrollTop:0},800);
	});
	if (jQuery.browser.msie) {
        var zIndexNumber = 1000;
        jQuery('#navigation-container div,.slider-container div').each(function() {
            jQuery(this).css('zIndex', zIndexNumber);
            zIndexNumber -= 10;
        });
    }
})

function imgIconOverlay($) {
    // This will select the items which should include the image overlays
    jQuery("div.hover-content").each(function(){
        var	ctnr = jQuery(this).find('a.item-preview');
        var cntrDiv=jQuery(this);
        // insert the overlay image
        ctnr.each(function(){
            if (jQuery(this).children('img')) {
                if (jQuery(this).hasClass('iconPlay')) {
                    jQuery(this).append(jQuery('<div class="imgOverlay"><div class="symbolPlay"></div></div>'));
                } else if  (jQuery(this).hasClass('iconDoc')) {
                    jQuery(this).append(jQuery('<div class="imgOverlay"><div class="symbolDoc"></div></div>'));
                } else if (ctnr.hasClass('iconZoom')){
                    jQuery(this).append(jQuery('<div class="imgOverlay"><div class="symbolZoom"></div></div></div>'));
                }
            }
        })


        var overImg = ctnr.children('.imgOverlay');
        // var hoverInfo = ctnr.parent.next();

        if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 6) {
        // IE sucks at fading PNG's with gradients so just use show hide

        } else {
            // make sure it's not visible to start
            overImg.css('opacity',0);

            cntrDiv.hoverIntent(
                function(){
                    overImg.animate({
                        'opacity':'1'
                    },200,'swing');
                /*jQuery(this).children(':last-child').animate({
                        'opacity':'1'
                    },200,'swing');*/
                //                    jQuery(this).children('.jcycle-pager').animate({
                //                        'opacity':'0'
                //                    },500,'swing');

                },		// mouseover
                function(){
                    overImg.animate({
                        'opacity':'0'
                    },200,'swing');
                /* jQuery(this).children(':last-child').animate({
                        'opacity':'0'
                    },200,'swing');*/
                //                    jQuery(this).children('.jcycle-pager').animate({
                //                        'opacity':'1'
                //                    },500,'swing');

                }		// mouseout
                );
        }
    });
}

// JQUERY OVERLABLE
jQuery.fn.overlabel = function() {
    this.each(function(index) {
        var label = jQuery(this);
        var field;
        var id = this.htmlFor || label.attr('for');
        if (id && (field = document.getElementById(id))) {
            var control = jQuery(field);
            label.addClass("overlabel-apply");
            if (field.value !== '') {
                label.css("display", "none");
            }
            control.focus(function () {
                label.css("display", "none");
            }).blur(function () {
                if (this.value === '') {
                    label.css("display", "block");
                }
            });
            label.click(function() {
                var label = jQuery(this);
                var field;
                var id = this.htmlFor || label.attr('for');
                if (id && (field = document.getElementById(id))) {
                    field.focus();
                }
            });
        }
    });
};
