var j = 0;

$(document).ready(function() {
	$('#optinWording').html(
			"Sign up to receieve the Karma newsletter.<br>We will not sell or give out any email addresses."
	);
	$('.shows').hide();
	$('.controls').hide();
	$('#bodyA').offset({top: $(document).height()-$('#bodyA').height(), left:0});
	$('.logo').click(function(){
		document.location.href = "http://www.karmashops.com/index.htm";
	});
	$('.apopup').click(function(){
		var a = $(this).attr('title');
		$('#imageDialog_' + a).dialog({
			width: 720, height: 800,
			modal: true
		});
	});
	$('#blogBtn').click(function(){
		document.location.href="./blog.htm";
	});
	$('#fbBtn').click(function(){
		window.open("http://www.facebook.com/karmashops", "http://www.facebook.com/karmashops");
	});
	$('#tweetBtn').click(function(){
		window.open("http://www.twitter.com/karmashops", "http://www.twitter.com/karmashops");
	});
	$('#fp-image').click(function(){
		$( "#dialog-form" ).dialog({
			height: 400,
			width: 350,
			modal: true, close: function(){
				$('#optinIframe').hide().height(325);
				$('#optinForm').show();
			}});
	});
	$('#ccontact').click(function(){
		$( "#dialog-form" ).dialog({
			height: 400,
			width: 350,
			modal: true, close: function(){
				$('#optinIframe').hide().height(325);
				$('#optinForm').show();
			}});
	});
	$('#ccoptinBtn').click(function(){
		$('#optinForm').hide();
		$('#optinIframe').show();
	});
	 $('.captions').each(function(i){
         $(this).html($(this).attr('caption')).css('width', $(this).prev().width());
     });
});

function lookBookClick(lb){
    $('#grayout').width($(document).width());
    $('#grayout').height($(document).height());
    $('#grayout').fadeTo(1000, .40);
    var tb = Math.abs($('.drilldown').height()/2);
    $('.loadbar').fadeTo(0, 1.0).lrcenter().css('top', 350);
    $.post("./lookbook.htm", {id: lb}, function(html){
            $('#slideShowContainer').html(html);
            var len = $('#slideShowContainer li').length;
            var i = 0;
            var barinc = Math.abs(300/len);
            checkLoad(i, len, barinc);
    }, 'html');
}


function checkLoad(i, len, barinc){
    var images = $('#slideShowContainer li img').toArray();
    
    var count = 0;
    var imgLoaded = function(){
        count ++;
        checkCount();
    }
    function checkCount(){
        if(count >= images.length){
            $('#loader').animate({width: 300}, 200);
            $('#grayout').delay(2000).animate({opacity: 0.0}, 1000, function(){ 
        		$('#grayout').hide(); 
        		$('#loader').width('0px'); 
        		///$('.middle').height(630);
                $('.showLinks').animate({opacity: 'hide'}, 'slow', function(){ 
                	$(this).hide(); 
        			$('.shows').show().animate({opacity: 1.0}, 1000, function(){ 
        					$('.controls').fadeTo(500, 1.0); photoSlideShow(5000); 
        				}); 
                });
        	} );
        }else{
	        var ld = Math.abs($('#loader').width() + barinc);
	        $('#loader').animate({width: ld}, 200);
        }            	
    }

    var img = [];
    $.each(images, function(i){
            img[i] = new Image();
            img[i].data = $(images[i]).attr('src');
            $(img[i]).load(imgLoaded).attr('src', $(images[i]).attr('src'));
            if(i > 0){ 
        		$('#slideShowContainer li:eq('+i+')').fadeTo(0,0.0);
            }
    });
}
function submitContact(){
    $('#grayout').width($(document).width());
    $('#grayout').height($(document).height());
    $('#grayout').fadeTo(1000, .40);
    $.post("./contact.do", $("#contact").serialize(), function(data){ 
		$('#grayout').animate({opacity: 0.0}, 1000, function(){
			$('#grayout').hide();
			$('table').animate({opacity: 0.0}, 1000, function(){
    			$('#element').html("<p class='text'>Thank you for contacting Karma.</p>");    			
			}).hide();
		});
    }, "html")
}

function getFBText(){
	var html = "";
	return html;
}

var blList = 1;
function showBlogUl(w){
	if(w != blList){
		blList = w;
		$('.blogList').each(function(){
			if($(this).hasClass('open')){
				$(this).animate({opacity:0}, 500, function(){ 
					$('#bl_'+w).addClass('open').toggleClass("off").animate({opacity:1.0}, 500);
					$(this).toggleClass("open").toggleClass("off"); 
					return false;
					});
			}
		});
	}
}



