$(document).ready(function(){
    
    makeHover();
    
    $('.pfclient a.switch').click(function(evt){
        evt.preventDefault();
        var wrap = $(this).parents('.pfwrap');
        if(wrap.hasClass('pfopen')){
            hideOldElems();
            return;
        }
        loadElem(wrap);
    });
    
    checkText($('body'));
    addControls($('body'));
    
    if (SIS_PUID != '') {
  		$('#' + SIS_PUID).intoViewport();
	}

});

function makeHover(){
    $('a.preview:not(processed)').each(function($i){
        $a = $(this);
        $a
          .cycle({
            timeout: 0,
            next: $a,
            speed: 350,
//            easing: 'linear',
            sync: 1,
            prevNextEvent: 'mouseenter',
          }).mouseleave(function(){
            $(this).cycle('prev');
          });
    }).addClass('processed');
}

function loadElem(wrap){
    var bd = wrap.find('.pfbody');
    if(bd.children().length){
        hideOldElems();
        showElem(wrap, bd)
    } else {
    	bd.hide();
        bd.load(
        'index.php?eID=loadpf&puid=' + wrap.attr('id'),
        function(data, status, xhr) {
          afterLoad(wrap, bd);
          addControls(bd);
        });
    }
}

function hideOldElems(){
    $('.pfopen .pfbody').hide();
    $('.pfopen').removeClass('pfopen');
    $('.pffollow').removeClass('pffollow');
}

function afterLoad(wrap, bd){
    makeHover();
    var hasimg = bd.find('.pfimg.current img');
    if(hasimg.length){
    	//showElem(wrap, bd);
		hasimg.load(function (){
			hideOldElems();
			showElem(wrap, bd);
		});
    } else {
    	hideOldElems();
    	showElem(wrap, bd);
    	
    }
}

function showElem(wrap, bd){
    wrap.addClass('pfopen');
	bd.show();
	checkText(bd);
    wrap.parent().nextAll('.tt_content').first().find('.pfwrap').addClass('pffollow');
    wrap.intoViewport();
    bd.find("a[rel^='lightbox']").slimbox({
				overlayOpacity: 0.5,
				captionAnimationDuration: 1,
				resizeDuration: 1
			}
			, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
}

function checkText(elem){
	var t = elem.find('.pftext');
	if(t.css('padding-top') != '0px') return;
	
	var hasimg = elem.find('.pfimg.current img');
	
    if(hasimg.length){
	    var txth = t.height();
	    var bdh  = hasimg.height();
	    if(txth < bdh){
		    t.css('padding-top', (bdh-txth)/2);
	    }
    }	
}

function addControls(elem){
	elem.find('a.pfimgctrl').click(function(evt){
        evt.preventDefault();
        elem.find('a.pfimgctrl').removeClass('current');
        lnk = $(this);
        lnk.addClass('current');
        var id = lnk.attr('id');
        lnk.parent().find('.pfimg').hide();
        $('#pfslim-' + id).show();

    });
    
    elem.find('a.pfprev').click(function(evt){
        evt.preventDefault();
        var plink = $(this);
        var imgs = elem.find('a.pfimgctrl');
        
        var oldindx = 0;
        var newindx = 0;
        var uid = 0;
        
        imgs.each(function(a, b){
        	var lnk = $(b);
        	if(lnk.hasClass('current')){
        		lnk.removeClass('current');
        		oldindx = a;
        		newindx = a - 1;
        		if(newindx < 0) newindx = imgs.length - 1;
        		uid = plink.attr('id').substr(12);
        	}
        });
        $('#pfslim-' + uid + '-' + oldindx).hide();
        $('#pfslim-' + uid + '-' + newindx).show();
        $('#' + uid + '-' + newindx).addClass('current');
    });
    
    elem.find('a.pfnext').click(function(evt){
        evt.preventDefault();
        var plink = $(this);
        var imgs = elem.find('a.pfimgctrl');
        
        var oldindx = 0;
        var newindx = 0;
        var uid = 0;
        
        imgs.each(function(a, b){
        	var lnk = $(b);
        	if(lnk.hasClass('current')){
        		lnk.removeClass('current');
        		oldindx = a;
        		newindx = a + 1;
        		if(newindx > imgs.length - 1) newindx = 0;
        		uid = plink.attr('id').substr(12);
        	}
        });
        $('#pfslim-' + uid + '-' + oldindx).hide();
        $('#pfslim-' + uid + '-' + newindx).show();
        $('#' + uid + '-' + newindx).addClass('current');
    });
    
    elem.find('a.pfmagn').click(function(evt){
        evt.preventDefault();
        var plink = $(this);
        var imgs = elem.find('a.pfimg');
        
        var done = false;

        imgs.each(function(a, b){
        	var lnk = $(b);
        	if(!done && lnk.css('display') != 'none'){
        		lnk.trigger('click');
        		done = true;
        	}
        });
    });
}
