//SPLIT THUMBS             
$.fn.splitUp=function(splitBy,wrapper){
    var all = $(this).find('>*');
    var fragment = Math.ceil(all.length/splitBy);
    for (var i=0 ; i < fragment; i++)
        all.slice(splitBy * i , splitBy * (i+1)).wrapAll(wrapper);
    return $(this);
}
         
// Menu
jQuery.fn.localmenu = function(){
this.each(function(){
var holder=$('> ul',this), ready=false;
	holder.find('> li > ul:visible').hide();
	holder.find('> li > ul > li > ul').hide();
	holder.find('> li > ul > li > ul').each(function(){
	$(this).css('margin-left',$(this).parent().parent().innerWidth()+'px');
	})


   holder.find('> li > ul > li').hover(function(){
	if (ready) {											
	$(this).attr('in','true');
	$(this).find('> ul:first:not(:visible)').fadeIn();}
   }, 
  function() {
	$(this).attr('in','false');
	var curel=$(this);
	setTimeout(function() {
	    if (curel.attr('in')=='false') {
		    curel.find('> ul:first:visible').fadeOut();
	    }
	},100);
   }
 )

  holder.find('> li').hover(function(){
 	$(this).attr('in','true');
	$(this).find('> ul:first:not(:visible)').slideDown(400,function() {ready=true;});
   }, 
  function() {
	$(this).attr('in','false');
	var curel=$(this);
	setTimeout(function() {
		if (curel.attr('in')=='false') {
			curel.find('> ul:first:visible').slideUp();
			ready = false;
		}
	},100);
   }
 )
})
}

function GetRandom(n){ return Math.floor(Math.random()*n+1)}

function showBottomTextSlide(el) {
   var customText = '';
    var i = 1;
    var size = $(this).find('img').size();
   $(this).find('img').each(function(){
        if($(this).attr('title') !='')
        {   
            if(i != size)
                customText = customText + '<a href="#">' +  $(this).attr('title')+'</a>'+' / ';       
            else
                customText = customText + '<a href="#">' +  $(this).attr('title')+'</a>';       
            i++;    
        }
    });
    $('.img-title-holder').html(customText);
    
    $('.interiorDesignBtm').find('p').html($(this).find('s').html());
    
    $('.topDetail h4 span').text($(this).find('.title-holder').text());
    
    if($('.interiorDesignBtm .quotecuf').length>0)
    {
        var iCount=$('.interiorDesignBtm .quotecuf').length;
        var temp=GetRandom(iCount)-1;
        
//        $('.interiorDesignBtm .quotecuf').css('display','none');
//        $('.interiorDesignBtm .quotecuf').eq(temp).css('display','');
        
        $('.interiorDesignBtm .quotecuf').fadeOut('fast');
        
        $('.interiorDesignBtm .quotecuf').eq(temp).fadeIn(2000);
        
    }
    
    if($('.colction_bottom span').length>0)
    {
        var iCount=$('.colction_bottom span').length;
        var temp=GetRandom(iCount)-1;
        
//        $('.colction_bottom span').css('display','none');
//        $('.colction_bottom span').eq(temp).css('display','');

 $('.colction_bottom span').fadeOut('fast');
        $('.colction_bottom span').eq(temp).fadeIn(2000);
        
        $('.colction_bottom label').html($(this).find('s').html());
    }
    
    if($('.antiques_txt').length>0)
    {
        var iCount=$('.antiques_txt').length;
        var temp=GetRandom(iCount)-1;
        
//        $('.antiques_txt').css('display','none');
//        $('.antiques_txt').eq(temp).css('display','');

$('.antiques_txt').fadeOut('fast');
        $('.antiques_txt').eq(temp).fadeIn(2000);
    }
    
}

function showBottomTextSlideFull(currSlideElement) {
    $('.img-title-holder').html(currSlideElement.title);
}

function onAfter(curr, next, opts) {
    var index = opts.currSlide;
    $('.previous')[index == 0 ? 'hide' : 'show']();
    $('.next_disable')[index == opts.slideCount - 1 ? 'hide' : 'show']();
}


// page init
$(function(){
     var footer = $('#footer');
    var footer_inside = $('#footer-inside'); 
    var site_map = $('.site-map-holder');  
    //:nth-child(3) 
    var newsletter_sel = $('ul.alignleft li:nth-child(2)').html();  
    var sitemap_sel = $('ul.alignleft li:nth-child(3)').html();  

    $('ul.alignleft li:eq(2) a').bind('click',function(){   
        if(footer.css('overflow')=='hidden')  
            {   
            $(this).text('CLOSE SITE MAP');
            footer.css("overflow",'visible');
            footer_inside.animate({
                marginTop: "-=200px"
            });

            site_map.animate({
                height:'200px'
            });                        
            //$('ul.alignleft li:eq(1)').hide();
        }
        else 
            {
            footer_inside.animate({
                marginTop: "0px"
            });

            site_map.animate({
                height:'0px'
            },function(){
                footer.css("overflow",'hidden');      
            });   
            $(this).text('SITE MAP');   
            //$('ul.alignleft li:eq(1)').show(); 
        }   
    });  
    
    $('ul.alignleft li:eq(1)').click(function(){
    $('ul.alignleft li:eq(2) a').click();
    }) 

//    $('ul.alignleft li:eq(1) a').bind('click',function(){  
//        if(footer.css('overflow')=='hidden')  
//            {   
//            $(this).text('CLOSE NEWSLETTER');
//            footer.css("overflow",'visible');
//            footer_inside.animate({
//                marginTop: "-=200px"
//            });

//            site_map.animate({
//                height:'200px'
//            });                        
//            $('ul.alignleft li:eq(2)').hide();
//        }
//        else 
//            {
//            footer_inside.animate({
//                marginTop: "0px"
//            });

//            site_map.animate({
//                height:'0px'
//            },function(){
//                footer.css("overflow",'hidden');      
//            });   
//            $(this).text('NEWSLETTER');   
//            $('ul.alignleft li:eq(2)').show(); 
//        }   
//    });   


    // SLIDE PRODUCTS  
    if($(".cs_slider").size()>0)
    {    
        $(".cs_slider").cycle({ 
            fx:     'fade', 
            timeout: 0,
            next:   '.cs_leftBtn', 
            prev:   '.cs_rightBtn'  
        });
    }

    // SLIDE BLOG PAGES
    if($("#slide_blog").size()>0)
    {
        $("#slide_blog").cycle({
            fx:     'fade', 
            timeout: 0,
            next:   '.next_disable', 
            prev:   '.previous',
            after:   onAfter       
        });       
    }
               
    if($("#designSlider").size()>0)
    {
        $("#designSlider").nivoSlider({
            effect:"fade",
            slices:15,
            animSpeed:800,
            pauseTime:5000,
            startSlide:0,
            directionNav:false,
            directionNavHide:false, 
            controlNav:true,
            controlNavThumbs:false, 
            keyboardNav:true, 
            pauseOnHover:true, 
            manualAdvance:false,
            captionOpacity:1
        });
    }
    var i=0;
    $(".productSlide").each(function(){
        var size = $(this).find('img').size();
        if(size > 1)
        {
            var nav1 = "nav_"+ i;
            var nav2 = "#nav_"+i;

            $(this).after('<div class="nivo-controlNav" id="'+ nav1 +'">').cycle({
                fx:'fade',
                timeout:0,
                pager: nav2.toString(),
                pagerAnchorBuilder: function(idx, slide) { 
                    return '<a class="nivo-control"></a>'; 
                } 
            });  
            i++;         
        }
    });
    /*    $(".productSlide").nivoSlider({
    effect:"fade",
    slices:15,
    animSpeed:800,
    pauseTime:3000,
    directionNav:false,
    directionNavHide:false,
    controlNav:true, 
    controlNavThumbs:false, 
    keyboardNav:true,
    pauseOnHover:true,
    manualAdvance:false,
    captionOpacity:0
    });
    */
    if($("#antiquesSlider").size()>0)
    {
        $("#antiquesSlider").nivoSlider({
            effect:"fade",
            slices:15,
            animSpeed:800,
            pauseTime:5000,
            startSlide:0,
            directionNav:false,
            directionNavHide:false, 
            controlNav:true,
            controlNavThumbs:false, 
            keyboardNav:true, 
            pauseOnHover:true, 
            manualAdvance:false,
            captionOpacity:0
        });    
    }
    //slide thumbs
    //.splitUp(6,'<li class=splitUp><ul>')
  /*  $('.panel').after('<ul class="filmstrip">').cycle({ 
        fx:     'fade', 
        speed:  1000, 
        timeout: 10000, 
        pager:  '.filmstrip', 
        activePagerClass: 'activeThumb',
        pagerAnchorBuilder: function(idx, slide) {         
            return '<li><a href="#"><img src="' + slide.src + '" width="100" /></a></li>'; 
        } 
    });
    */
    /**
    * We use the initCallback callback
    * to assign functionality to the controls
    */
    

    // Ride the carousel...
   /*
        jQuery(".filmstrip").jcarousel({
            scroll: 8,
            initCallback: mycarousel_initCallback,
            // This tells jCarousel NOT to autobuild prev/next buttons
            buttonNextHTML: null,
            buttonPrevHTML: null
        });
        */
 /*   $('ul.filmstrip').splitUp(8,'<ul>').cycle({
        fx:'scrollHorz',
        next:   '.next-act',
        timeout: 0, 
        prev:   '.prev-act'
    })

    */
        // PAGINATION PRODUCTS
    
    if($('.productsPage').size()>0)
    {    
    
    $('.productsPage').after('<div id="products_navi" class="wp-pagenavi"><div class="clear">').splitUp(8,'<div>').cycle({ 
            fx:     'fade', 
            timeout: 0, 
            pager:  '#products_navi', 
            pagerAnchorBuilder: function(idx, slide) {
                  var pag = idx + 1;  
                return '<a href="#" class="page">'+pag +'</a>'; 
            }
    });
    
    }
 })


$(document).ready(function(){
    
    //READ MORE BIO
//    $('.contributors_content > .read_more').click(function(){
//        var content = $(this).parent().children('.content_expand');
//        $(this).hide();
//        $(this).prev().hide();
//        content.css({opacity:0}).animate({
//            opacity: 1,
//            height:'toggle'
//        }, 400, function() {
//            //       $(this).parent().children('.read_more').hide();
//        });
//    }); 
//    $('.contributors_content > .content_expand > .close').click(function(){
//        $(this).parent().parent().children('.summary').show()
//        $(this).parent().animate({
//            opacity: 0,
//            height:'toggle'
//        },400,function(){
//            $(this).parent().children('.read_more').show();  
//        });    
//    })
    
    
$(".current_page_item").localmenu();
    // TOP NAV STYLE
   // $("#header .topNav ul li:last-child").css({margin:'0'});    
   // $("#header .topNav ul ul li").hide(); 
    //$("#header .topNav ul ul").parents("li:eq(0)").toggle(function(){
      //  $("#header .topNav ul ul li").addClass("active");
        //$("#header .topNav ul ul li").slideDown(800);
    //}, function () {
     //   $("#header .topNav ul ul li").removeClass("active");
       // $("#header .topNav ul ul li").slideUp(200);
    //});
    //$("#header .topNav ul ul ul").parents("li:eq(0)").addClass("hasKids");
    //$("#header .topNav ul ul li").slideDown("slow");
    //$("#header .topNav ul ul ul li:first-child").addClass("first");
    //if ($.browser.msie && $.browser.version > 6 ) $("#header .topNav ul ul li").css("lineHeight", "120%"); 
    //Product page nr images to slide
    var nr_slides =  $('.productSlide > a').size();
    
    $(".zoomImg").click(function(){
        var nr_img = $(this).parent().children('a').size();
        if(nr_img > 1)
        {
            $(this).colorbox({inline:true, href:".cs_article .zoomPopup",onComplete:function(){
                    $('.thumbs > li > a').click(function(){  
                        var big_img = $(this).children('img').attr('src');
                        $('.zoomPopup > img').attr('src',big_img);
                    })
            }});                    
        }
        else if(nr_img == 1)
        {
            $(this).colorbox();               
        }
    });


    $(".contactItem").colorbox({inline:true, href:"#contactPopup"});

    //CUSTOM SLIDESHOW
    if($('#slider').size()>0)
    {
    $('#slider').after('<div id="nav">').cycle({
        fx:     'fade',
        speed:   2000,
        timeout: 2000,
        pager:  '#nav',
        next:   '#next',
        prev:   '#prev',
        after:   showBottomTextSlide
    });
    }
    
    if($('#slider_full').size()>0)
    {
    $('#slider_full').after('<div id="nav">').cycle({
        fx:     'fade',
        speed:   2000,
        timeout: 10000,
        pager:  '#nav',
        next:   '#next',
        prev:   '#prev',
        after:   showBottomTextSlideFull
    });
    }

    var next = $('#next');
    var prev = $('#prev');
    if (document.getElementById('slider') != null){
    var myslider =  $('#slider');
    }
    else{
    var myslider =  $('#slider_full');
    }
    var mouseinslider=false;

    next.hide();
    prev.hide();   

        myslider.mousemove(function(e){
			if (mouseinslider)	{					
            var sag = e.pageY - $(this).offset().top;         
            var offsetleft = e.pageX - this.offsetLeft;
            var offsettop = e.pageY - this.offsetTop;    

            if (offsetleft < $(this).width() / 2) {   
                if ($('#next:visible').length || !$('#prev:visible').length) {
				next.hide();
				prev.show();
                }
                prev.css('top',sag);
            }
            else
                {
                if ($('#prev:visible').length||!$('#next:visible').length) {
				prev.hide();
				next.show();
                }
                next.css('top',sag);
            }
			}
        });     
        next.css({
            cursor: 'pointer'   
        })
        prev.css({
            cursor: 'pointer'   
        })
    myslider.hover(function(){
		mouseinslider=true;					
    },function(){
		mouseinslider=false;
		prev.hide();
		next.hide();
    });

    prev.hover(function(){
		prev.show();
    },function(){
		prev.hide();
    });

    next.hover(function(){
		next.show();
    },function(){
		next.hide();
    });
    
    // ZOOM ICON
    $(".productSlide").hover(function(){
        $(".imageDescription").append("<div class='zoomIcon'></div>");                              
    }, function () {
        $(".zoomIcon").remove();
    });

});

function openFullScreen(url){
       window.open(url,"fullScreenWindow","fullscreen=yes, scrollbars=yes");
       }



