function CheckIfImageExist(img_url) {
    
    var img = new Image();

    // wrap our new image in jQuery, then:
    $(img)
    // once the image has loaded, execute this code
    .load(function () {
        good = true;
    })
    // if there was an error loading the image, react accordingly
    .error(function () {
        good = false;
        })
    // *finally*, set the src attribute of the new image to our image
    .attr('src', img_url);

   return good;
}

function testImage(URL) {
    var tester=new Image();
    tester.onLoad=isGood;
    tester.onError=isBad;
    tester.src=URL;
}

function EmbedVideo(video_url,video_img, video_add, postroll) {
    var baseurl=window.location.hostname;
   
    if(video_url == null) {
        video_url=$("div#video_gallery div#video_wrap img").attr("rel");
    }
   
    if(video_img == null) {
        video_img=$("div#video_gallery div#video_wrap img").attr("src");
    }
    
    if(video_img == undefined) {
        video_img= 'http://' + baseurl + '/assets/delo_v3/img/logo.png';
    }
    
    //alert('Video url:'+video_url);
    //alert('Video img:'+video_img);
    
    var wrap= $("div#video_gallery div#video_wrap");
    //izprazni container
    wrap.empty();
     
    if (video_url != null) {
                                                                                                                                                    
        var patt1="youtube\.com";
                                                                                                                                                                                                
        //imamo you tube video
        if (video_url.match(patt1) != null){
               
            // Old video url parsing.
            //var clean_video_url = $.jYoutubeVideoUrl(video_url);
            //var embed = '<object width="629px" height="378px"><param name="movie" value="'+ clean_video_url +'?fs=1&amp;hl=sl_SI&amp;rel=0&amp;color1=#eeeff0&amp;color2=#eeeff0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+ clean_video_url +'?fs=1&amp;hl=sl_SI&amp;rel=0&amp;color1=#eeeff0&amp;color2=#eeeff0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="629px" height="378px" ></embed></object>';

            // New video URL parsing.
            // The URL has to have the following form:
            // http://....com?v=<video id>
            var vid;
            var results;
            results = video_url.match("[\\?&]v=([^&#]*)");
            vid = ( results === null ) ? video_url : results[1];

            // iPad compliant, HTML5 capable youtube embed code
            var embed = '<iframe class="youtube-player" type="text/html" width="629px" height="378px" src="http://www.youtube.com/embed/' + vid
                        + '?theme=light&color=red" frameborder="0"></iframe>';
            
            //pljuni embed codo v div
            wrap.html(embed);
            return;
        }
        var patt2="assets/media/video";
        if (video_url.match(patt2) != null){
            
             var img = new Image();
            // wrap our new image in jQuery, then:
            $(img)
            // once the image has loaded, execute this code
            .load(function () {
                
                //alert(video_img);
                
                flowplayer("video_wrap", "/flowplayer/commercial/3.2.7/flowplayer.commercial-3.2.7.swf",
                //{config: 
                {
                    key: '#@9c60ec6088c4462e21b',
                    clip:video_url+"?" + Math.floor(Math.random()*1100),
                    autoPlay: false, 
                    autoBuffering: false,
                    bufferLength: 3,
                    play: {
                        replayLabel: '\u0160e enkrat'
                    },
                    logo: {
                        url:  '/assets/studio.delo.si/images/logo_small.png',
                        top: 10, 
                        right: 10, 
                        fullscreenOnly: false,
                        linkUrl: '/',
                        opacity:  1,
                        zIndex:   1
                    },
                    canvas: {
                        backgroundGradient: "none",
                        backgroundColor: "#d0d0d0"  
                    },
                    playlist: [
                        // video image
                        {
                            url: video_img, 
                            scaling: 'fit'
                        },
                        // video clip
                        {
                            url: video_url+"?" + Math.floor(Math.random()*1100), 
                            autoPlay: false,
                            bufferLength: 3
                        }
                    ]
                }
                //}
                ); //end of function call
            })
            // if there was an error loading the video image, react accordingly
            .error(function () {
                
                //alert('no video image');
                //flashembed("video_wrap", "/flowplayer/gpl/3.2.7/flowplayer-3.2.7.swf", {config: {
                //flashembed("video_wrap", "/flowplayer/commercial/3.2.7/flowplayer.commercial-3.2.7.swf", {config: {
                //flashembed("video_wrap", 
                //          {src: "/flowplayer/commercial/3.2.7/flowplayer.commercial-3.2.7.swf",cachebusting: true}, 
                flowplayer("video_wrap", "/flowplayer/commercial/3.2.7/flowplayer.commercial-3.2.7.swf",
                //{config: 
                {
                    key: '#@9c60ec6088c4462e21b',
                    play: {
                        replayLabel: '\u0160e enkrat'
                    },
                    logo: {
                        url:  '/assets/studio.delo.si/images/logo_small.png',
                        top: 10, 
                        right: 10, 
                        fullscreenOnly: false,
                        linkUrl: '/',
                        opacity:  1,
                        zIndex:   1
                    },
                    canvas: {
                        backgroundGradient: "none",
                        backgroundColor: "#d0d0d0"  
                    },
                    playlist: [
                        // video clip
                        {
                            url: video_url+"?" + Math.floor(Math.random()*1100), 
                            autoPlay: false,
                            autoBuffering: false,
                            bufferLength: 3
                        }
                    ]
                }
                //}
                ); //end of function call
            })
            // *finally*, set the src attribute of the new image to our image
            .attr('src', video_img);
        } 
    }
}

// execute your scripts when the DOM is ready. this is mostly a good habit
$(function() {
    
    //
    if ( $("div#video_gallery").length ) {
        //check if thumscroller is loaded
        var n = $("div#video_gallery div.d_thumbscroller").length;
        if (n==0) {
            EmbedVideo(null,null, null, false);
        }
    }
    
    //for each img element that has rel with youtube in it get the thumbnail
    $("div#video_gallery .items div.thumbnail_overlay").each(function() {
        var video_url=$(this).attr("rel");
                                                                                                                                                
        if (video_url != null) {
            var patt1="youtube\.com";
                                                                                                                                        
            if (video_url.match(patt1) != null){
                                                                                                                                            
                var thumbnail = $.jYoutube(video_url, 'small');
                if (thumbnail != null) {
                    $("div#video_gallery .items div.thumbnail img[rel='"+video_url+"']").attr("src", ""+thumbnail+"");
                    $(this).attr('src',thumbnail );
                }
            }
        }
    });                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
    $("div#video_gallery .scrollable").scrollable({
        activeClass:"active",
        circular:true,
        clonedClass:"cloned",
        disabledClass:"disabled",
        easing:"swing",
        initialIndex:0,
        item:null,
        items:".items",
        keyboard:true,
        mousewheel:false,
        prev:".prev",
        next:".next",
        speed:400,
        touch:true,
        vertical:false,
        wheelSpeed:0
    });
    
    //vidoe gallery thumbnail klik
    $("div#video_gallery .items div.thumbnail_overlay").click(function() {
                                                                                                                                                                                                                                                                                                                                                    
        // see if same thumb has already being clicked
        if ($(this).hasClass("active")) {
            return;
        }
                                                                                                                                                                                                
        var wrap = $("div#video_gallery div#video_wrap");
        //empty the video container
        wrap.empty();
                                   
        //get video url from rel                           
        var video_url=$(this).attr("rel");
        var video_img=$(this).attr("src");
                                                                                                                                                
        if (video_url != null) {
                                                                                                                                                    
            var patt1="youtube\.com";
                                                                                                                                                                                                
            //check if we have youtube video
            if (video_url.match(patt1) != null){
                                                                                                                                                
                // Old video url parsing.
                // var clean_video_url = $.jYoutubeVideoUrl(video_url);
                // var embed = '<object width="629px" height="378px"><param name="movie" value="'+ clean_video_url +'?fs=1&amp;hl=sl_SI&amp;rel=0&amp;color1=#eeeff0&amp;color2=#eeeff0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+ clean_video_url +'?fs=1&amp;hl=sl_SI&amp;rel=0&amp;color1=#eeeff0&amp;color2=#eeeff0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="629px" height="378px" ></embed></object>';
                
                // New video URL parsing.
                // The URL has to have the following form:
                // http://....com?v=<video id>
                var vid;
                var results;
                results = video_url.match("[\\?&]v=([^&#]*)");
                vid = ( results === null ) ? video_url : results[1];

                // iPad compliant, HTML5 capable youtube embed code
                var embed = '<iframe class="youtube-player" type="text/html" width="629px" height="378px" src="http://www.youtube.com/embed/' + vid
                            + '?theme=light&color=red" frameborder="0"></iframe>';
                                                                                                                                                                                                    
                //pljuni embed codo v div
                wrap.html(embed);
                                                                                                                                                                                
                // activate item
                $("div#video_gallery .items div.thumbnail_overlay").removeClass("active");
                $(this).addClass("active");
                                                                                                                                                
                return;
            }
            
            //check if we have studio delo video
            var patt2="assets/media/video";
            if (video_url.match(patt2) != null){
                //studio delo video                                                                                                                                                                                                
                
                EmbedVideo(video_url,video_img, null, false);
                                                                                                                                                                        
                // activate item
                $("div#video_gallery .items div.thumbnail_overlay").removeClass("active");
                $(this).addClass("active");
                                                                                                                                        
                return;                                                                                                                           
            }
        }     
                                                                                                                                                                                   
    // when page loads simulate a "click" on the first image
    });
    $("div#video_gallery .items div.thumbnail_overlay").filter(":first").click();  
});                                                                                                                                                                     
                                                                                                                                                                             


