$(function() {
//    $('li.shop-item-thumb').remove();
    /*
     $('body').videoBG({
     position:"fixed",
     zIndex:-1,
     mp4:'/wp/wp-content/uploads/assets/christmas_snow.mp4',
     ogv:'/wp/wp-content/uploads/assets/christmas_snow.ogv',
     webm:'/wp/wp-content/uploads/assets/christmas_snow.webm',
     poster:'/wp/wp-content/uploads/assets/christmas_snow.jpg',
     opacity:1
     });
     

    $(".test0").click(function(){
        setTimeout("document.getElementById('test0').play()",4000)
        alert("here");
          })
       */   

$('.fancybox').fancybox({
				'titlePosition'		: 'inside',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
                 'onComplete'       : function(obj){
					                      
                                div_href=$(this).attr('href');
								vidpos= div_href.lastIndexOf("_" );
								vidpos=vidpos+1
								vid_id=div_href.substr(vidpos);
                                document.getElementById(vid_id).play();
                                 },
                  'onClosed'      : function(){
							    div_href=$(this).attr('href');
								vidpos= div_href.lastIndexOf("_" );
								vidpos=vidpos+1
								vid_id=div_href.substr(vidpos);
                                document.getElementById(vid_id).pause();
                                 
                                 }
			});
          
    $('.line').first().delay(2000).fadeIn();
    $('.home-link').delay(2000).fadeIn();
    $('.line,.main-navigation').delay(3000).fadeIn();
    $('.line,.mainnav2').delay(3000).fadeIn();
    $("#latest-container img").each(function () {
        if ($(this).attr('title').length) {
            var pic = new Image();
            pic.src = $(this).attr('title');
            $(this).attr('src', pic.src).hide().delay(5000).fadeIn();
            $(this).attr('title', '');
            $('.lead-content').hide().delay(5000).fadeIn();
        }
    });
    $('a.prettyphotobox').prettyPhoto({deeplinking:false, social_tools:false});
    $('a.prettyvideobox').prettyPhoto({deeplinking:false, social_tools:false,
        default_width: 800,
        default_height: 500
    });


    $('.posts-by-categories > li, .additional-container,.shop-item-preview-single,.shop-items-descriptions li, li.portfolio-item-preview, li.portfolio-video-preview, #latest-works-thumbs li,#latest-works-previews li').hide();
//    $('#latest-works li').first().click();
    $('.category-link').click(function() {
        $('.posts-by-categories > li').fadeOut();
        var work_to_show = $(this).attr('rel');
        $('.posts-by-categories > li[rel*="' + work_to_show + '"]').delay(500).fadeIn();
    });
    $('.list-header').click(function() {
        var included_items = $('.portfolio-categories li.active').attr('rel');
        if ($(this).hasClass('active')) {
            $('.list-header, .work-title').removeClass('active');
            $(this).parent().find('li[rel*="' + included_items + '"]').not('.list-header').fadeOut().removeClass('current');
        }
        else {
            $('.current').fadeOut();
            $('.list-header, .work-title').removeClass('active');
            $(this).parent().find('li[rel*="' + included_items + '"]').not('.list-header').fadeIn().first().click();
            $(this).parent().find('li[rel*="' + included_items + '"] img').each(function () {
                if ($(this).attr('title').length) {
                    $(this).attr('src', $(this).attr('title'));
                    $(this).attr('title', '');
                }

            });

            $(this).parent().find('li[rel*="' + included_items + '"]').not('.list-header').addClass('current');
            $(this).addClass('active');
        }


    });
    $('.shop-item-preview-multi a').click(function() {
        $('.shop-items-thumbs:visible').fadeOut();
        var work_to_show = $(this).attr('rel');
        $('.shop-items-descriptions li.shop-item-preview-single[rel*="' + work_to_show + '"]').first().delay(500).fadeIn();
        $('.shop-items-descriptions li.shop-description[rel*="' + work_to_show + '"]').delay(500).fadeIn();
    });
    $('.portfolio-item-thumb').click(function() {
        if ($(this).hasClass('active')) {
            return false;
        }
        var video_id = $('video:visible').first().attr('id');
        if (document.getElementById(video_id)) {
            document.getElementById(video_id).pause();
        }
        $('.portfolio-item-thumb').removeClass('active');
        $(this).addClass('active');

        var work_to_show = $(this).attr('rel');
        if ($('.portfolio-items li.portfolio-item-preview:visible').length || $('.portfolio-items li.portfolio-video-preview:visible').length) {
            $('.portfolio-items li.portfolio-item-preview:visible, .portfolio-items li.portfolio-video-preview:visible').fadeOut(function() {
                $('.portfolio-items li.portfolio-item-preview[rel*="' + work_to_show + '"]').fadeIn();
                $('.portfolio-items li.portfolio-video-preview[rel*="' + work_to_show + '"]').fadeIn(function() {
                    var video_to_play = $('.portfolio-items li.portfolio-video-preview[rel*="' + work_to_show + '"]:visible a').attr('id');
                });
            });
        }
        else {
            $('.portfolio-items li.portfolio-item-preview[rel*="' + work_to_show + '"]').fadeIn();
            $('.portfolio-items li.portfolio-video-preview[rel*="' + work_to_show + '"]').fadeIn(function() {
                var video_to_play = $('.portfolio-items li.portfolio-video-preview[rel*="' + work_to_show + '"]:visible a').attr('id');
            });

        }

    });
    $('.shop-item-thumb').click(function() {
        if ($(this).hasClass('active')) {
            return false;
        }
        $('.shop-item-thumb').removeClass('active');
        $(this).addClass('active');

        var work_to_show = $(this).attr('rel');
        if ($('.shop-items li.shop-item-preview-single:visible').length > 0) {
            $('.shop-items li.shop-item-preview-single:visible').fadeOut(function() {
                $('.shop-items li.shop-item-preview-single[rel*="' + work_to_show + '"]').fadeIn();
            });
        }
        else {
            $('.shop-items li.shop-item-preview-single[rel*="' + work_to_show + '"]').fadeIn();
        }

    });
    $('#containers').children('li').first().show();
    $('#navbar ul > li').first().addClass('active');
    $('.main-navigation li').click(function() {
        if ($(this).hasClass('active')) {
            return false;
        }
        var video_id = $('video:visible').first().attr('id');
        if (document.getElementById(video_id)) {
            document.getElementById(video_id).pause();
        }
        $('#latest-works-thumbs li, .additional-container').fadeOut();
        $('li.latest-work, li.shop-item-thumb,li.portfolio-item-thumb,#navbar ul li').removeClass('active');
        $(this).addClass('active');
        var containerID = "#" + $(this).attr('rel') + "-container";
        var additional_containers = "." + $(this).attr('rel') + ".additional-container";
        $(additional_containers).fadeIn();
        if ($(containerID).filter(':visible').length) {
            return false;
        }
        $('#containers').children('li:visible').fadeOut(function() {
            $('.information-items > li,.portfolio-items > li,.shop-items > li, .portfolio-item-preview, .subnavbar2 ul li, .latest-work-preview, .brand-title').hide();
            $('.subnavbar2 ul').parent().find('div.line').hide();
            $('.subnavbar1 ul li,.subnavbar2 ul li').removeClass('active');

            $(containerID).fadeIn(function() {
                $('.subnavbar1:visible ul li').first().click();
                var category_id = $('.subnavbar1 ul li.active').attr('rel');
                $('.brand-title, .list-header[rel*=' + category_id + '], .portfolio-titles > li').fadeIn();
                $('.subnavbar2 ul').parent().find('div.line').fadeIn();

            });
        });
        return false;
    });
    $('.portfolio-categories li').live('click', function() {
        if ($(this).hasClass('active')) {
            return false;
        }
        var video_id = $('video:visible').first().attr('id');
        if (document.getElementById(video_id)) {
            document.getElementById(video_id).pause();
        }
        $('.portfolio-categories li').removeClass('active');
        $(this).addClass('active');
        var id = $(this).attr('rel');
        $('.subnavbar2 ul').parent().find('div.line').fadeOut();
        $('.portfolio-item-preview, .portfolio-items > li').fadeOut();
        if ($('.portfolio-titles li.list-header:visible,.portfolio-titles li ul li:visible').length) {
            $('.portfolio-titles li.list-header:visible,.portfolio-titles li ul li:visible').fadeOut(function() {
                $('.portfolio-titles li ul li').hide();
                $('.portfolio-titles li ul li').removeClass('active');
                $('.portfolio-titles li ul li.list-header[rel*="' + id + '"]').fadeIn(function() {
                    $('.subnavbar2 ul').parent().find('div.line').show();
                });
            });

        }
        else {
            $('.portfolio-titles li ul li').hide();
            $('.portfolio-titles li ul li').removeClass('active');
            $('.portfolio-titles li ul li.list-header[rel*="' + id + '"]').fadeIn(function() {
                $('.subnavbar2 ul').parent().find('div.line').show();
            });

        }
    });
    $('.information-categories li').live('click', function() {
        if ($(this).hasClass('active')) {
            return false;
        }
        var video_id = $('video:visible').first().attr('id');
        if (document.getElementById(video_id)) {
            document.getElementById(video_id).pause();
        }
        $('.information-items').first().fadeIn();
        $('.information-categories li').removeClass('active');
        $(this).addClass('active');
        var id = $(this).attr('rel');
        if ($('.information-items > li:visible, .information-titles > li:visible').length) {
            $('.information-items > li:visible, .information-titles > li:visible').fadeOut(function() {
                $('.information-titles li').removeClass('active');
                $('.information-titles li[rel*="' + id + '"]').fadeIn(function() {
                    $('.information-titles li[rel*="' + id + '"]').first().addClass('active');
                    var work_to_show = $('.information-titles li:visible').first().attr('rel');
                    $('.information-items > li[rel*="' + work_to_show + '"]').fadeIn();

                });

            });
        }
        else {
            $('.information-titles li').removeClass('active');
            $('.information-titles li[rel*="' + id + '"]').fadeIn(function() {
                $('.information-titles li[rel*="' + id + '"]').first().addClass('active');
                var work_to_show = $('.information-titles li:visible').first().attr('rel');
                $('.information-items > li[rel*="' + work_to_show + '"]').fadeIn();

            });
        }
    });
    $('.shop-categories li').not('.view-cart-link').click(function() {
        $('.shop-items img').not('.excluded').each(function () {
            if ($(this).attr('title').length) {
                $(this).attr('src', $(this).attr('title'));
                $(this).attr('title', '');
            }

        });
        var video_id = $('video:visible').first().attr('id');
        if (document.getElementById(video_id)) {
            document.getElementById(video_id).pause();
        }
        $('.shop-item-thumb, .shop-categories li').removeClass('active');
        $(this).addClass('active');
        if ($('.shop-items > li:visible').length) {
            $('.shop-items > li:visible').fadeOut(function() {
                $('.shop-items-descriptions li').hide();
                var work_to_show = $('.shop-categories li.active').attr('rel');
                $('.shop-items > li[rel*="' + work_to_show + '"]').fadeIn(function() {
                    $('.shop-items-thumbs').show();
                    if ($('.shop-items > li[rel*="' + work_to_show + '"]').length < 2) {
                        $('.shop-items-descriptions > li[rel*="' + work_to_show + '"]').not('.shop-item-preview-single').fadeIn();
                        $('.shop-items-descriptions > li.shop-item-thumb[rel*="' + work_to_show + '"]').first().addClass('active');
                        $('.shop-items-descriptions > li.shop-item-preview-single[rel*="' + work_to_show + '"]').first().fadeIn();
                        $('.shop-items-thumbs, .shop-items-thumbs > li[rel*="' + work_to_show + '"]').hide();
                    }
                    else {
                        $('.shop-items-thumbs > li[rel*="' + work_to_show + '"]').fadeIn();
                        $('.shop-items > li[rel*="' + work_to_show + '"] > ul').fadeIn();
                    }

                });
            });
        }
        else {
            $('.shop-items-descriptions li').hide();
            var work_to_show = $('.shop-categories li.active').attr('rel');
            $('.shop-items > li[rel*="' + work_to_show + '"]').fadeIn(function() {
                $('.shop-items-thumbs').show();
                if ($('.shop-items > li[rel*="' + work_to_show + '"]').length < 2) {
                    $('.shop-items-descriptions > li[rel*="' + work_to_show + '"]').not('.shop-item-preview-single').fadeIn();
                    $('.shop-items-descriptions > li.shop-item-thumb[rel*="' + work_to_show + '"]').first().addClass('active');
                    $('.shop-items-descriptions > li.shop-item-preview-single[rel*="' + work_to_show + '"]').first().fadeIn();
                    $('.shop-items-thumbs, .shop-items-thumbs > li[rel*="' + work_to_show + '"]').hide();
                }
                else {
                    $('.shop-items-thumbs > li[rel*="' + work_to_show + '"]').fadeIn();
                    $('.shop-items > li[rel*="' + work_to_show + '"] > ul').fadeIn();
                }

            });
        }
    });
    $('.portfolio-titles li ul li').live('click', function() {
        if ($(this).hasClass('active') || $(this).hasClass('list-header')) {
            return false;
        }

        $('li.portfolio-item-thumb,.portfolio-titles li').removeClass('active');
        $(this).addClass('active');
        $(this).parent().find('.list-header').addClass('active');
        var id = $(this).attr('rel');
        if ($('.portfolio-items > li:visible').length) {
            var video_id = $('video:visible').first().attr('id');
            if (document.getElementById(video_id)) {
                document.getElementById(video_id).pause();
            }
            $('.portfolio-items > li:visible').fadeOut(function() {
                $('.portfolio-item-preview, .portfolio-video-preview').hide();
                $('.portfolio-items > li[rel*="' + id + '"]').fadeIn(function() {
                    var video_id = $('.portfolio-items > li[rel*="' + id + '"] video').first().attr('id');
                    if (document.getElementById(video_id)) {
                        //$('.portfolio-items > li[rel*="' + id + '"] video').is(':visible')
                        document.getElementById(video_id).play();
                    }
                    $('.portfolio-items > li[rel*="' + id + '"] img').each(function () {
                        if ($(this).attr('title').length) {
                            $(this).attr('src', $(this).attr('title'));
                            $(this).attr('title', '');
                        }
                    });
                    $('li.portfolio-item-thumb:visible').first().click();
                })
            });
        }
        else {
            $('.portfolio-items > li[rel*="' + id + '"]').fadeIn(function() {
                $('.portfolio-items > li[rel*="' + id + '"] img').each(function () {
                    if ($(this).attr('title').length) {
                        $(this).attr('src', $(this).attr('title'));
                        $(this).attr('title', '');
                    }

                });

                $('li.portfolio-item-thumb:visible').first().click();
            })
        }
    });
    $('.information-titles li').live('click', function() {
        if ($(this).hasClass('active')) {
            return false;
        }
        var video_id = $('video:visible').first().attr('id');
        if (document.getElementById(video_id)) {
            document.getElementById(video_id).pause();
        }
        $('.information-titles li').removeClass('active');
        $(this).addClass('active');
        var id = $(this).attr('rel');
        $('.information-items > li:visible').fadeOut(function() {
            $('.information-items > li[rel*="' + id + '"]').fadeIn();
        });
    })
});
