var ad_search = "off"
var TimerDelay = null;
var showPopupDelay = null;
var hidePopupDelay = null;
var delay = 200;
var image_pop = true;

userAgent = navigator.userAgent;

function stripHTML(arguments){
	arguments = arguments.replace(new RegExp("(<([^>]+)>)", "g"), "");
	return arguments;
}

var spin = $("<div class='spin'></div>").hide().appendTo("#body");

$(document).ready(function(){
    var spin = $("<div class='spin'></div>").hide().appendTo("body");
    
    if ($("div.discbox").length != 0 || $("div.discbox_bible").length != 0 || $("div.discbox_passage").length != 0){
	    var discOpenClose = $("<div class=\"disc_open_close\"><img src=\"images/cross.gif\" border=\"0\" title=\"" + close_disc + "\"></div>").click(function() {
	    	if ($('div.basic').css("display") == 'none') {
			    $('div.basic').slideDown('fast');
			    $('a.head').slideDown('fast');
			    $(this).children().eq(0).attr("src", "images/cross.gif");
			    $(this).children().eq(0).attr("title", close_disc);		
			    $('.min').children().eq(0).attr("src", "images/minus.gif");
			}	
			else {	
				$('div.basic').slideUp('fast');
				$(this).children().eq(0).attr("src", "images/disc_arrow.gif");
				$(this).children().eq(0).attr("title", open_disc);
				$('.min').children().eq(0).attr("src", "images/advanced.gif");
			}
	    });
    }
    
    $(function() {
        if ($("div.discbox").length != 0){
	        var v = $("#vstatsbox");
	        if (v.length == 0) {
	            v = $("<div id=\"vstatsbox\"><img src=\"" + "ajax/" + "../images/spin.gif\" style=\"padding:90px\"></div>").appendTo($("#statsbox"));
	            v.load("ajax/" + "discovery.php?q=" + escape(q) + "&mode=" + escape(mode), function(){
		            $('div.head').next().slideDown('fast');
			        $('div.head_disc').next().css('display', 'none').end().click(function() {
			            $(this).next().toggle('fast');	            
			        });
			        
			        $('div.head').next().each(function() {
			            if ($(this).outerHeight() > 200) {
			                $(this).css('height',200).css('overflow','auto');
			            }
			        });
			        
			        $('div.head').click(function() {
			            if ($(this).next().css("display") == 'none') {
			            	$(this).children('.disc_title').children().eq(0).attr("src", "images/minus.gif");
							$(this).children('.disc_title').children().eq(0).attr("title", hide_content);
							$(this).children('.disc_cut').css({display:'none'});
							$(this).children('.disc_dots').css({display:'none'});
						}
			            else {
			            	$(this).children('.disc_title').children().eq(0).attr("src", "images/advanced.gif");
							$(this).children('.disc_title').children().eq(0).attr("title", show_content);			            	
							$(this).children('.disc_cut').css({display:'block'});
							$(this).children('.disc_dots').css({display:'block'});
			            }
			            $(this).next().toggle('fast');
			        });
			        
			        var basicLeft = $("#statsbox").offset().left;
			        
			       $('div.head').each(function() {
			       		var leftB = $(this).children('.disc_title').eq(0).children('b').offset().left + $(this).children('.disc_title').eq(0).children('b').width();
			       		var leftCut = leftB - basicLeft;
			       		var discCut = $("<div class=\"disc_cut\"></div>").css({left:leftCut, width:180-leftCut, height:15, display:'none'});
			       		var discDots = $("<div class=\"disc_dots\">...</div>").css({left:185, width:15, display:'none'});
			       		var textCut = stripHTML($(this).next().html());
			       		$(this).append(discDots).append(discCut.html(textCut.substring(0,100)));
			       });
		        });
		    }
		    var discoBible = $("div.head_disco").append(discOpenClose);
			var widthDisc = discoBible.width();
			var widthPlus = discOpenClose.width();
			discOpenClose.css({left:widthDisc-widthPlus, top:0});
		    return false;
	    }
    });

    $(function() {
        if ($("div.discbox_bible").length != 0){
	        var v = $("#vstatsbox");
	        if (v.length == 0) {
	            v = $("<div id=\"vstatsbox\"><img src=\"" + "ajax/" + "../images/spin.gif\" style=\"padding:90px\"></div>").appendTo($("#statsbox"));
	            v.load("ajax/" + "discovery_bible.php?book=" + escape(book) + "&chapter=" + escape(chapter) + "&tab=" + escape(tab), function(){
		            $('a.head').next().slideDown('fast');
			        $('div.head_disc').next().css('display', 'none').end().click(function() {
			            $(this).next().toggle('fast');	            
			        });
			        $('a.head').click(function() {
			        	if ($(this).next().css("display") == 'none') {
			            	$(this).children().eq(0).attr("src", "images/minus.gif");
			            	$(this).children().eq(0).attr("title", hide_content);
			            }
			            else {
			            	$(this).children().eq(0).attr("src", "images/advanced.gif");
			            	$(this).children().eq(0).attr("title", show_content);
			            }

			            $(this).next().toggle('fast');	            
			        });
			        $('a.head').next().each(function() {
				        if ($(this).outerHeight() > 200) {
				        	$(this).css('height',200).css('overflow','auto');
						}
			        });
		    	});
		    }
		    
		    var discoBible = $("div.head_disco").append(discOpenClose);
			var widthDisc = discoBible.width();
			var widthPlus = discOpenClose.width();
			//console.log(discoBible.width());
			discOpenClose.css({left:widthDisc-widthPlus, top:0});
			
		    return false;
    	}
    	
    	if ($("div.discbox_passage").length != 0){
	        var v = $("#vstatsbox");
	        if (v.length == 0) {
	            v = $("<div id=\"vstatsbox\"><img src=\"" + "ajax/" + "../images/spin.gif\" style=\"padding:90px\"></div>").appendTo($("#statsbox"));
	            v.load("ajax/" + "discovery_passage.php?book=" + escape(book) + "&chapter=" + escape(chapter) + "&passage=" + escape(passage), function(){
	            	$('a.head').next().slideDown('fast');
			        $('div.head_disc').next().css('display', 'none').end().click(function() {
			            $(this).next().toggle('fast');	            
			        });
			        $('a.head').click(function() {
			        	if ($(this).next().css("display") == 'none') {
			            	$(this).children().eq(0).attr("src", "images/minus.gif");
			            	$(this).children().eq(0).attr("title", hide_content);
			            }
			            else {
			            	$(this).children().eq(0).attr("src", "images/advanced.gif");
			            	$(this).children().eq(0).attr("title", show_content);
			            }
			            $(this).next().toggle('fast');	            
			        });
			        $('a.head').next().each(function() {
				        if ($(this).outerHeight() > 200) {
				        	$(this).css('height',200).css('overflow','auto');
						}
			        });
	            });
	        }
	        
			
		    var discoBible = $("div.head_disco").append(discOpenClose);
			var widthDisc = discoBible.width();
			var widthPlus = discOpenClose.width();
			discOpenClose.css({left:widthDisc-widthPlus, top:0});
	        return false;
	    }
    });
    
    $("p.versions").click(function() {
        this.mode = this.id.substring(0,4);
        this.verse  = this.id.substring(5);
        id = this.verse
        var r = $("#r" + this.verse);
        var d = $("#d" + this.verse);
        var v = $("#v" + this.verse);
        if (v.length == 0) {
            r = $("<tr  id=\"r" + this.verse + "\"><td></td><td id=\"d" + this.verse + "\" colspan = 3></td></tr>").appendTo($("#t" + this.verse));
            v = $("<div class=\"slidedown\" id=\"v" + this.verse + "\"><img src=\"" + "ajax/" + "../images/spin.gif\" style=\"padding:100px\"></div>").appendTo($("#d" + this.verse));
            $("p#solr_"+id).html("<img src=\"" + "ajax/" + "../images/spin.gif\" id=\"image_wait\" width=10 height=10>");
            v.load("ajax/" + "version.php?id=" + this.verse + "&q=" + escape(q), function() {
                v.slideDown("slow", function() {
                    $("p#solr_"+id).html("<img src=\"" + "ajax/" + "../images/minus.gif\">");
                });
            });
        } else {
            v.slideUp("slow", function() {
                $("p#solr_"+id).html("<img src=\"" + "ajax/" + "../images/advanced.gif\">");
            });
            r.remove();
        }
        return false;
    });

    $('#list1a').accordion({
        header:'a.head',
        autoheight: false		    
    });
    $('.target').slideUp('fast');
    $('.trigger').click(function() {
        if ($('.target').css('display') == 'none')
            $('.target').slideDown('slow');
        else
            $('.target').slideUp('slow');
    });
    
    $("a.ad_search").click(function(){
        if (ad_search=="off"){
            $("#left,#main").animate({"top": "300px"}, "fast");
            ad_search="on";
            $('#ad_search_box').slideDown('fast');
            }
        else {
            $('#ad_search_box').slideUp('fast');
            $("#left,#main").animate({"top": "70px"}, "fast");  
            ad_search="off";
            }
    });
    
    $("a.adv_help").click(function(){
        if ($('div.ad_desc').css('display') == 'none'){
            $('div.ad_desc').animate({"opacity": "show"}, "fast");
            $(this).html("Hide Help");
            return false;
        }  
        else {
            $('div.ad_desc').animate({"opacity": "hide"}, "fast");
            $(this).html("Show Help");  
            return false;
        }
    });
    
    $('a.head').click(function() {
        if ($(this).next().css('display') == 'none')
            $(this).next().slideDown('fast');
        else
            $(this).next().slideUp('fast');
    });
    
    $('a.min').click(function() {
        if ($(this).children().eq(0).attr("src") == "images/minus.gif") {
        	$('.head').slideDown('fast');
        	$('.head').next().slideUp('fast');
        	$('.head').children().attr("src", "images/advanced.gif");
            $(this).children().attr("src", "images/advanced.gif");
	        $(this).children().eq(0).attr("title", show_all_content);
	        
			//disc untuk bible agak beda karena ada teaser content
			$('div.head').children('.disc_title').children().attr("src", "images/advanced.gif");
			$('div.head').children('.disc_title').children().attr("title", show_content);			            	
			$('div.head').children('.disc_cut').css({display:'block'});
			$('div.head').children('.disc_dots').css({display:'block'});
        } else {
        	$('div.basic').slideDown('fast');
	        $('.head').slideDown('fast');
	        $('.head').next().slideDown('fast');
	        $('.head').children().attr("src", "images/minus.gif");
        	$(this).children().eq(0).attr("src", "images/minus.gif");
			$(this).children().eq(0).attr("title", hide_all_content);
	        
	        //disc untuk bible agak beda karena ada teaser content
	        $('div.head').children('.disc_title').children().attr("src", "images/minus.gif");
			$('div.head').children('.disc_title').children().attr("title", hide_content);
			$('div.head').children('.disc_cut').css({display:'none'});
			$('div.head').children('.disc_dots').css({display:'none'});
        }
        $(".disc_open_close").children().eq(0).attr("src", "images/cross.gif");
    });
    
    $('a.plus').click(function() {
        $('div.basic').slideDown('fast');
        $('a.head').slideDown('fast');
    });
    
    $('a.minA').click(function() {
        $('a.head').slideDown('fast');
        $('a.head').next().slideUp('fast');
    });
    
    $('a.plusA').click(function() {
        $('div.basic').slideDown('fast');
        $('a.head').slideDown('fast');
        $('a.head').next().slideDown('fast');
    });
    
    
    $('div.head_stat').click(function() {
        if ($(this).next().css('display') == 'none'){
            $(this).next().toggle('fast');
        } else {
            $(this).next().slideUp('fast');
        }
    });
    
    $("#sortable").tablesorter({sortList:[[1,0],[2,0]], headers: { 3: { sorter: false}}});
	$("#sortable_2").tablesorter({sortList:[[0,0],[1,0]], headers: { 2: { sorter: false}}});
	$('a.jt').cluetip({cluetipClass: 'rounded', positionBy: 'mouse', width: '400', dropShadow: false, sticky: true, ajaxCache: true, arrows: true, closeText: '<b>X</b>'});
    $('a.citytext').cluetip({cluetipClass: 'rounded', positionBy: 'mouse', width: '400', dropShadow: false, sticky: true, ajaxCache: true, arrows: true, closeText: '<b>X</b>'});

    $("#navigation").treeview({
	    persist: "location",
	    collapsed: true,
	    unique: false
    });
    
    $("a.lex_strong").click(function() {
        if ($("#image_wait").length == 0) {
            this.word = this.id.substring(7);
            v = $("<div class=\"lex_strong_in\" id=\"strong_" + this.word + "\"></div>").appendTo($(".lex_strong_div"));
            $(".lex_strong").html("<img src=\"" + "ajax/" + "../images/spin.gif\" id=\"image_wait\" width=10 height=10>");
            v.load("ajax/" + "lexicon_strong.php?word=" + this.word, function () {
                $(".lex_strong").remove();
            });
        }
    });
    
    $("a.show_pericope").click(function() {
        var el = this.id;
        var pass = el.replace(new RegExp("__", "g"), ":");
        var pass = pass.replace(new RegExp("_", "g"), " ");
        var v;
        if ($("#" + el).children().is('#image_adv_' + el)) {
            if ($("#verses_" + el).length == 0) {
                v = $("<div class=\"slideDown\" style=\"padding: 5px 20px\" id=\"verses_" + el + "\"></div>").appendTo($("#per_" + el));
                $("#" + el).html("<img src=\"" + "images/spin.gif\" id=\"image_wait_" + el +"\" width=10 height=10>");
                v.load("ajax/" + "pericope.php?passage=" + escape(pass), function() {
                    v.slideDown("slow", function() {
                        $("#" + el).html("<img src=\"" + "images/minus.gif\" id=\"image_coll_" + el + "\" title=\"Hide pericope\">");
                    });
                });
            } else {
                $("#verses_" + el).slideDown("slow", function () {
                    $("#" + el).html("<img src=\"" + "images/minus.gif\" id=\"image_coll_" + el + "\" title=\"Hide pericope\">");
                }); 
            }
        }
        if ($("#" + el).children().is('#image_coll_' + el)) {
            $("#verses_" + el).slideUp("slow", function () {
                $("#" + el).html("<img src=\"" + "images/advanced.gif\" id=\"image_adv_" + el + "\" title=\"Show pericope\">");
            });
        }
    });
    
    $("a.hide_warning").click(function() {
        $(".warning").slideUp();
        return false;
    });
    
    $("a.hide_warning_id").click(function(event) {
        var hideWarning = findParent($(event.target), "a.hide_warning_id");
        var word = hideWarning.attr("id").substr(2);
        $("#dw" + word).slideUp();
        return false;
    });
    
    $(".uni_note_trigger").hover(function(event) {
        TimerDelay = setTimeout(function() {
            TimerDelay = null;
            if ($(".uni_note").length == 0) {
                uniNote = $("<div class=\"uni_note\"></div>").css({top: '215px', left: '12px'}).html(uni_note_1+uni_note_2+uni_note_3+uni_note_4+uni_note_5+uni_note_6+uni_note_7+uni_note_8+uni_note_9);
                $("body").append(uniNote);
            }
            else {
                //$(".uni_note").css({top: event.clientY + 5, left: event.clientX + 5});
                $(".uni_note").fadeIn();
            }
        }, delay);
    }, function(event) {
        $(".uni_note").fadeOut();
        if (TimerDelay) {
            clearTimeout(TimerDelay);
            TimerDelay = null;
        }
    });
    
    function findParent(element, parent) {
        if (!element.is(parent))
            element = element.parents(parent);
        return element;
    }
    
    function regexVerse(verse, pos) {
        if (pos != 0) {
            pattern = "<n id=\"(" + pos + ")\" />";
            verse = verse.replace(new RegExp(pattern, "g"), "<span class=\"hl\"><sup>$1</sup></span>");
        }
        verse = verse.replace(new RegExp("<sc>(.+?)</sc>", "g"), "<span class=\"sc\">$1</span>");
        verse = verse.replace(new RegExp("</?[p].*?>", "g"), " ");
        verse = verse.replace(new RegExp("<n id=\"(\\d+)\" />", "g"), "<sup>$1</sup>");
        return verse;
    }
    
    function isElementsDisplayed(element, except_id) {
    	var a=0;
    	if (except_id) {
	    	element.each(function(){
	    		if ($(this).css("display") == "block" && ($(this).attr("id") != except_id))
	    			a+=1;
	    	});
	    }
	    else {
	    	element.each(function(){
	    		if ($(this).css("display") == "block")
	    			a+=1;
	    	});
	    }
	    	
		if (a==0)
			return false
		else
			return true
    }
    
    function closeParent(element){
    	var parent_me = element.attr("parent");
        var parent_me_class = $("." + parent_me + "");
        var parent_me_id = $("#" + parent_me + "");
        if (!isElementsDisplayed(parent_me_class, element.attr("id")) && !parent_me_id.is(".verse_sticky") && !parent_me_id.is(".note_sticky") && !parent_me_id.is(".strong_sticky") && !parent_me_id.is(".verse_on") && !parent_me_id.is(".note_on") && !parent_me_id.is(".strong_on")) {//cek apa ada saudaranya yang masih displayed, kalo tidak, matikan parent {
    		var hidePopupDelay1 = setTimeout(function() {
                parent_me_id.attr("_timer", "");
                var id = parent_me_id.attr("id").substr(1);
                parent_me_id.fadeOut("slow", function(){
                	if (parent_me_id.attr("parent")){
                		closeParent(parent_me_id);
                	}
                });
            }, delay);
            parent_me_id.attr("_timer", hidePopupDelay1);	
    	}
	}
    
    function otherPopupsToBack(z_index) {
		$(".verse_popup").css({zIndex:z_index});
		$(".note_popup").css({zIndex:z_index});
		$(".strong_popup").css({zIndex:z_index});
	}
        
    $(document).mouseover(function(event) {
        var noteTrigger = findParent($(event.target), "a.note_trigger");
        if (noteTrigger.length) {            
            var id = $(event.target).attr("id").substr(1);
            var notePopupWrap = $("div#np" + id);
            
            if (notePopupWrap.attr("_timer")) {
                clearTimeout(notePopupWrap.attr("_timer"));
                notePopupWrap.attr("_timer", "");
            }
            
            showPopupDelay = setTimeout(function() {
                showPopupDelay = null;
                
                var notePopupTitle;
                var notePopupWrapIn;
                var notePopup;
                
                function noteResizing() {
                    if (notePopupWrapIn.height() > notePopup.outerHeight({margin: true})) {
                        notePopupWrapIn.css({height: notePopup.outerHeight({margin: true}) + 1});
                        notePopupWrap.css({height: notePopup.outerHeight({margin: true}) + notePopupTitle.height()});
                    }
                }
                
                function notePositioning() {
                    if (/MSIE (\d+\.\d+);/.test(userAgent))
                        xf = 15;
                    else
                        xf = 0;
                    if (($(window).height()-event.clientY) < notePopupWrap.height())
                        notePopupWrap.css({top: event.pageY-event.clientY+$(window).height()-notePopupWrap.height()-5-xf});
                    else
                        notePopupWrap.css({top: event.pageY+5-xf});
                    if (($(window).width()-event.clientX) < notePopupWrap.width())
                        notePopupWrap.css({left: event.pageX-event.clientX+$(window).width()-notePopupWrap.width()-5-xf});
                    else
                        notePopupWrap.css({left: event.pageX+5-xf});
                }
                
                var notePopupWrap = $("div#np" + id);
                
                if (notePopupWrap.css("display") == "block")
                    return;
                if (!notePopupWrap.length) {
                    notePopupTitle = $("<div class=\"note_popup_title\"></div>");
                    var noteButtonSticky = $("<div class=\"note_button_sticky\" title=\"sticky\"></div>").addClass("note_button_sticky_off");
                    var noteButtonClose = $("<div class=\"note_button_close\" title=\"close\"></div>");
                    notePopup = $("<div class=\"note_content\"></div>");
                    notePopupWrapIn = $("<div class=\"note_popup_in\"></div>").append(notePopup);
                    notePopupWrap = $("<div class=\"note_popup\" id=\"np" + id + "\"></div>").append(notePopupTitle).append(notePopupWrapIn).append(noteButtonSticky).append(noteButtonClose).draggable({handle:'div.note_popup_title', cursor: 'move'}).resizable({autohide: true, knobHandles: true}).resize(function() {
                        notePopupWrapIn.css({height: notePopupWrap.height() - notePopupTitle.height(), width: notePopupWrap.width()});
                        notePopupTitle.css({width: notePopupWrap.width()});
                    });
                    
                    $("body").append(notePopupWrap.css({visibility: 'hidden'}));
                    var versePos = id.split("_");
                    var noteVerse = versePos[0];
                    var notePos = versePos[1];
                    spin.css({top: event.pageY - 16, left: event.pageX}).show();
                    $.getJSON("ajax/notes.php", {id: noteVerse, pos: notePos}, function(data) {
                        spin.hide();
                        var notes = "";
                        $.each(data, function(i, item) {
                        	var note = "";
                            note += "<p><b>" + item['type'] + "</b> " + item['note'] + "</p>";
                            notes += note;
                        });
                        notePopup.html(notes);
                        notePopupTitle.html("<b>" + data[0]['ref'] + "<sup>" + notePos + "</sup>" + "</b>");
                        noteResizing();
                        notePositioning();
                        notePopupWrap.css({visibility: 'visible', zIndex: 95}).fadeIn("slow");
                    });
                }
                else {    
                    notePositioning();
                    notePopupWrap.css({zIndex: 95}).fadeIn("slow");
                }
            }, delay);
        }
               
        var notePopupWrap = findParent($(event.target), "div.note_popup");
        if (notePopupWrap.length) {
            if (notePopupWrap.attr("_timer")) {
                clearTimeout(notePopupWrap.attr("_timer"));
                notePopupWrap.attr("_timer", "");
            }
        }
        
        var verseTrigger = findParent($(event.target), "a.verse_trigger");
        if (verseTrigger.length) {
            var id = $(event.target).attr("id");
            var vsf = $(event.target).attr("vsf");
            //if ($(event.target).attr("ver"))
            //	var ver = $(event.target).attr("ver");
            //else
            //	var ver = '';            
            var context = $(event.target).attr("context");
            var vid = id;
            var vid_pos = 0;
            var direction;
            if ($(event.target).attr("_dir"))
            	direction = $(event.target).attr("_dir");
            else
            	direction = "down";
            
            var versePopupWrap = $("div#vp" + vid.replace(new RegExp(",", "g"), "_"));
            
            if (versePopupWrap.attr("_timer")) {
                clearTimeout(versePopupWrap.attr("_timer"));
                versePopupWrap.attr("_timer", "");
            }
            
            showPopupDelay = setTimeout(function() {
                showPopupDelay = null;
                
                var versePopupTitle;
                var versePopupWrapIn;
                var versePopup;
                var versePopupWrap = $("div#vp" + vid.replace(new RegExp(",", "g"), "_"));
                
                function verseResizing() {
                    if (versePopupWrapIn.height() > (versePopup.outerHeight({margin: true}))) {
                        versePopupWrapIn.css({height: versePopup.outerHeight({margin: true}) + 1});
                        versePopupContext.css({top: versePopup.outerHeight({margin: true}) + versePopupTitle.height()});
                        versePopupWrap.css({height: versePopup.outerHeight({margin: true}) + versePopupTitle.height() + versePopupContext.height()});
                    }
                }
                
                function versePositioning() {
                	if (/MSIE (\d+\.\d+);/.test(userAgent))
                        xf = 15;
                    else
                        xf = 0;
                	if (direction == 'down') {
	                    if (($(window).height()-event.clientY) < versePopupWrap.height())
	                        versePopupWrap.css({top: event.pageY-event.clientY+$(window).height()-versePopupWrap.height()-5-xf});
	                    else
	                        versePopupWrap.css({top: event.pageY+5-xf});
	                        
	                    if (($(window).width()-event.clientX) < versePopupWrap.width())
	                        versePopupWrap.css({left: event.pageX-event.clientX+$(window).width()-versePopupWrap.width()-5-xf});
	                    else
	                        versePopupWrap.css({left: event.pageX+5-xf});
	                } else if (direction == 'up') {
						if (event.clientY - versePopupWrap.height()-20 < 0) {
	                        versePopupWrap.css({top:event.pageY-xf});
	                    }
	                    else
	                        versePopupWrap.css({top: event.pageY-versePopupWrap.height()-20-xf});
	                        
	                    if (($(window).width()-event.clientX) < versePopupWrap.width())
	                        versePopupWrap.css({left: event.pageX-event.clientX+$(window).width()-versePopupWrap.width()-5-xf});
	                    else
	                        versePopupWrap.css({left: event.pageX+5-xf});
	            	}
                }
                
                if (versePopupWrap.css("display") == "block"){
                    versePositioning();
                    return;
                }
                if (!versePopupWrap.length) {
                    //var fSize = $.cookie("verse_popup_fontsize");
                    //if (!fSize) {
                    //    fSize = 11;
                    //}
                    //var lHeight = $.cookie("verse_popup_lineHeight");
                    //if (!lHeight) {
                    //    lHeight = 1.5;
                    //}
                    versePopupTitle = $("<div class=\"verse_popup_title\"></div>");
                    var verseButtonSticky = $("<div class=\"verse_button_sticky\" title=\"sticky\"></div>").addClass("verse_button_sticky_off");
                    var verseButtonClose = $("<div class=\"verse_button_close\" title=\"close\"></div>");
                    var verseBig = $("<div class=\"verse_button_big\"></div>");
					var verseSmall = $("<div class=\"verse_button_small\"></div>");
					versePopup = $("<div class=\"verse_content\"></div>");
                    versePopupContext = $("<div class=\"verse_popup_context\"></div>");
                    versePopupWrapIn = $("<div class=\"verse_popup_in\"></div>").append(versePopup);
                    //versePopupWrapIn = $("<div class=\"verse_popup_in\"></div>").css({fontSize: fSize, lineHeight: lHeight}).append(versePopup);
                    var parent_verse = '';
                    if (verseTrigger.parent().parent().parent().parent().is(".note_popup") || verseTrigger.parent().parent().parent().parent().is(".strong_popup")) {
                    	parent_verse = "" + verseTrigger.parent().parent().parent().parent().attr("id") + " ";
                    }
                    
                    versePopupWrap = $("<div class=\"" + parent_verse + "verse_popup\" id=\"vp" + vid.replace(new RegExp(",", "g"), "_") + "\" parent=\"" + parent_verse + "\"></div>").append(versePopupTitle).append(versePopupWrapIn).append(verseButtonSticky).append(verseButtonClose).append(verseBig).append(verseSmall).draggable({handle:'div.verse_popup_title', cursor: 'move'}).resizable({autohide: true, knobHandles: true}).resize(function() {
                        versePopupWrapIn.css({height: versePopupWrap.height() - versePopupTitle.height() - versePopupContext.height(), width: versePopupWrap.width()});
                        versePopupTitle.css({width: versePopupWrap.width()});
                        versePopupContext.css({top: versePopupTitle.height() + versePopupWrapIn.height(), width: versePopupWrap.width()});
                    });
                         
                    if (context) {
                        versePopupWrap.append(versePopupContext);
                    }
                                   
                    $("body").append(versePopupWrap.css({visibility: 'hidden'}));
                    versePopupTitle.css({width: versePopupWrap.width()});
                    spin.css({top: event.pageY - 16, left: event.pageX}).show();
                    $.getJSON("ajax/verses.php", {id: vid}, function(data) {
                        spin.hide();
                        var verses = "";
                        var a = 0;
                        var book_start = 0;
                        var chapter_start = 0;
                        var verse_start = 0;
                        var book_end = 0;
                        var chapter_end = 0;
                        var verse_end = 0;
                        $.each(data['content'], function(i, item) {
                            verse = "<b>" + item.chapter + ":" + item.verse + "</b> " + item.text + " ";
                            verses += regexVerse(verse, vid_pos);
                        });
                        if (context) {
                            versePopupWrap.css({height: "170px"});
                            context_split = context.split("__");
                            versePopupContext.html("<div style=\"position:relative; top:8px;\">" + "Is this verse reference correct?" + " <form id='context_form' action='ajax/context.php' method='post'><input type='hidden' name='suggestion' value='true'><input type='hidden' name='table' value=" + context_split[0] + "><input type='hidden' name='id' value=" + context_split[1] + "><input type='hidden' name='vref' value=" + escape(context_split[2]) + "><input type='hidden' name='url' value=" + context_split[3] + "><input type='submit' value='" + "yes" + "' height='15px' style='font-size:100%'/></form>" + " <form id='correct_form' ><input type='hidden' name='suggestion' value='false'><input type='hidden' name='table' value=" + context_split[0] + "><input type='hidden' name='id' value=" + context_split[1] + "><input type='hidden' name='vref' value=" + escape(context_split[2]) + "><input type='hidden' name='url' value=" + context_split[3] + "><input type='submit' value='" + "no" + "' height='15px' style='font-size:100%'/></form>" + "</div>");
                        }
                        versePopup.html("<p>" + verses + "</p>");
                        if (vsf)
                            versePopupTitle.html("<div style=\"position:relative; float:left; top:2px;\"><b>" + data['ref'] + "</b></div> <div style=\"position:relative; top:2px;\">&nbsp;<a href=\"" + "information.php" + "\"><img src=\"images/information.gif\" border=0 width=16px height=16px></a>");
                        else
                            versePopupTitle.html("<b>" + data['ref'] + "</b>");
                        verseResizing();
                        versePositioning();
                        otherPopupsToBack(90);
                        versePopupWrap.css({visibility: 'visible', zIndex: 95}).fadeIn("slow");
                    });
                }
                else {
                    versePositioning();
                    otherPopupsToBack(90);
                    versePopupWrap.css({zIndex: 95}).fadeIn("slow");
                }
            }, delay);
        }
        
        var versePopupWrap = findParent($(event.target), "div.verse_popup");
        if (versePopupWrap.length) {
            if (versePopupWrap.attr("_timer")) {
                clearTimeout(versePopupWrap.attr("_timer"));
                versePopupWrap.attr("_timer", "");
            }
        }
        
        var strongTrigger = findParent($(event.target), "a.strong_trigger");
        if (strongTrigger.length) {
            var sid = $(event.target).attr("id");
            var stype = $(event.target).attr("type");
            var strongPopupWrap = $("div#sp" + vid);
            
            if (strongPopupWrap.attr("_timer")) {
                clearTimeout(strongPopupWrap.attr("_timer"));
                strongPopupWrap.attr("_timer", "");
            }
            
            showPopupDelay = setTimeout(function() {
                showPopupDelay = null;
                
                var strongPopupTitle;
                var strongPopupWrapIn;
                var strongPopup;
                var strongPopupWrap = $("div#sp" + sid);
                
                function strongResizing() {
                    if (strongPopupWrapIn.height() > (strongPopup.outerHeight({margin: true}))) {
                        strongPopupWrapIn.css({height: strongPopup.outerHeight({margin: true}) + 1});
                        strongPopupContext.css({top: strongPopup.outerHeight({margin: true}) + strongPopupTitle.height()});
                        strongPopupWrap.css({height: strongPopup.outerHeight({margin: true}) + strongPopupTitle.height()});
                    }
                }
                
                function strongPositioning() {
                    if (/MSIE (\d+\.\d+);/.test(userAgent))
                        xf = 15;
                    else
                        xf = 0;
                    if (($(window).height()-event.clientY) < strongPopupWrap.height())
                        strongPopupWrap.css({top: event.pageY-event.clientY+$(window).height()-strongPopupWrap.height()-5-xf});
                    else
                        strongPopupWrap.css({top: event.pageY+5-xf});
                    if (($(window).width()-event.clientX) < strongPopupWrap.width())
                        strongPopupWrap.css({left: event.pageX-event.clientX+$(window).width()-strongPopupWrap.width()-5-xf});
                    else
                        strongPopupWrap.css({left: event.pageX+5-xf});
                }
                
                if (strongPopupWrap.css("display") == "block"){
                    strongPositioning();
                    return;
                }
                if (!strongPopupWrap.length) {
                    //var fSize = $.cookie("strong_popup_fontsize");
                    //if (!fSize) {
                    //    fSize = 11;
                    //}
                    //var lHeight = $.cookie("strong_popup_lineHeight");
                    //if (!lHeight) {
                    //    lHeight = 1.5;
                    //}
                    strongPopupTitle = $("<div class=\"strong_popup_title\"></div>");
                    var strongButtonSticky = $("<div class=\"strong_button_sticky\" title=\"sticky\"></div>").addClass("strong_button_sticky_off");
                    var strongButtonClose = $("<div class=\"strong_button_close\" title=\"close\"></div>");
                    var strongBig = $("<div class=\"strong_button_big\"></div>");
					var strongSmall = $("<div class=\"strong_button_small\"></div>");
					strongPopup = $("<div class=\"strong_content\"></div>");
                    strongPopupContext = $("<div class=\"strong_popup_context\"></div>");
                    strongPopupWrapIn = $("<div class=\"strong_popup_in\"></div>").append(strongPopup);
                    //strongPopupWrapIn = $("<div class=\"strong_popup_in\"></div>").css({fontSize: fSize, lineHeight: lHeight}).append(strongPopup);

                    var parent_strong = '';
                    if (strongTrigger.parent().parent().parent().parent().is(".strong_popup")) {
                    	parent_strong = "" + strongTrigger.parent().parent().parent().parent().attr("id") + " ";
                    }

                    strongPopupWrap = $("<div class=\"" + parent_strong + "strong_popup\" id=\"sp" + sid + "\" parent=\"" + parent_strong + "\"></div>").append(strongPopupTitle).append(strongPopupWrapIn).append(strongButtonSticky).append(strongButtonClose).append(strongBig).append(strongSmall).draggable({handle:'div.strong_popup_title', cursor: 'move'}).resizable({autohide: true, knobHandles: true}).resize(function() {
                        strongPopupWrapIn.css({height: strongPopupWrap.height() - strongPopupTitle.height() - strongPopupContext.height(), width: strongPopupWrap.width()});
                        strongPopupTitle.css({width: strongPopupWrap.width()});
                        strongPopupContext.css({top: strongPopupTitle.height() + strongPopupWrapIn.height(), width: strongPopupWrap.width()});
                    });
                                   
                    $("body").append(strongPopupWrap.css({visibility: 'hidden'}));
                    strongPopupTitle.css({width: strongPopupWrap.width()});
                    spin.css({top: event.pageY - 16, left: event.pageX}).show();
                    
                    $.getJSON("ajax/strong.php", {id: sid, type: stype}, function(data) {
                        spin.hide();
                        def = data['definition'];
                        in_bible = data['in_bible'];
                        word_id = data['word_id'];
                        if (def)
                            //strongPopup.append("<p>" + def + "</p><img class=\"en_str\" id=\"s"+ sid + "\" src=\"images/show_english.png\">");
                            strongPopup.append("<p>" + def + "</p>");
                        if (def && in_bible)
                            strongPopup.append("<hr>");
                        if (in_bible)
                            strongPopup.append("<p>In NET (" + data['count'] + ") : " + in_bible + "</p>");
                        
                        if (word_id != "NULL" && word_id) {
                            //if ((stype == "twot" || stype == "tdnt") && version_language != "en") {
                            //    strongPopupTitle.html("<b>" + data['word_id'] + " <" + data['id'] + "></b> <i>(" + in_eng + ")</i>");
                            //}
                            if ((stype == "twot" || stype == "tdnt")) {
                                strongPopupTitle.html("<b>" + data['word_id'] + " <" + data['id'] + "></b>");
                            }
                            else {                            
                                strongPopupTitle.html("<b>" + data['word_id'] + " <<a href=\"strong.php?id=" + data['id'] + "\">" + data['id'] + "</a>></i></b>");
                            }
                        } 
                        else
                            strongPopupTitle.html("<b><" + data['id'] + "></b>");
                        strongResizing();
                        strongPositioning();
                        otherPopupsToBack(90);
                        strongPopupWrap.css({visibility: 'visible', zIndex: 95}).fadeIn("slow");
                    });                    
                }
                else {
                    strongPositioning();
                    otherPopupsToBack(90);
                    strongPopupWrap.css({zIndex: 95}).fadeIn("slow");
                }
            }, delay);
        }        
       
        var strongPopupWrap = findParent($(event.target), "div.strong_popup");
        if (strongPopupWrap.length) {
        	strongPopupWrap.addClass("strong_on");
        	if (strongPopupWrap.attr("_timer")) {
            	clearTimeout(strongPopupWrap.attr("_timer"));
                strongPopupWrap.attr("_timer", "");
            }
        }        
        
		var imageTrigger = findParent($(event.target), "img.image_art");
        if (imageTrigger.length) {
        	var imagePopupWrap = $("div#image_zoom");
        	if (imagePopupWrap.attr("_timer")) {
                clearTimeout(imagePopupWrap.attr("_timer"));
                imagePopupWrap.attr("_timer", "");
            }
            var left = imageTrigger.offset().left;
			var top = imageTrigger.offset().top;
			var image_width = imageTrigger.width();
			var image_height = imageTrigger.height();
			
			//console.log("left: " + left + ", top: " + ", );
			
			var left_after = left - (image_width/2);
			var top_after = top - (image_height);
			var width_after = image_width + (image_width);
			var after_height = image_height + (image_height);
			
            showPopupDelay = setTimeout(function() {
                showPopupDelay = null;
            
            	var imagePopupWrap = $("div#image_zoom");
            	if (!imagePopupWrap.length) {
            		var imagePopupWrap = $("<div id=\"image_zoom\"></div>").css({position:"absolute", display:"none"});
            		imagePopupWrap.appendTo("body");
            	} else {
            		var imagePopupWrap = $("#image_zoom");
            	}
            	imagePopupWrap.css({left:left, top:top, width:image_width, height:image_height, cursor:"pointer", display:"block"});
				if (typeof(imageTrigger.parent().attr("href")) != 'undefined')
					var href_image = imageTrigger.parent().attr("href");
				else
					var href_image = "";
				var new_image = $("<a href=\"" + href_image + "\"><img src=\"" + imageTrigger.attr("src") + "\" width=" + width_after + " height=" + after_height + "></a>");
				imagePopupWrap.html(new_image).css({visibility: 'visible', zIndex: 95, top:top_after, height:after_height, left:left_after, width:width_after}).fadeIn("fast");
            }, delay/4);
        }
        
        var imagePopupWrap = findParent($(event.target), "div#image_zoom");
        if (imagePopupWrap.length) {
        	imagePopupWrap.addClass("verse_on");
        	if (imagePopupWrap.attr("_timer")) {
            	clearTimeout(imagePopupWrap.attr("_timer"));
                imagePopupWrap.attr("_timer", "");
            }
        }
        
        var smallImageTrigger = findParent($(event.target), "img.image_art_dict");
        if (smallImageTrigger.length) {
        	var smallImageWrap = $("div#preview");
        	if (smallImageWrap.attr("_timer")) {
                clearTimeout(smallImageWrap.attr("_timer"));
                smallImageWrap.attr("_timer", "");
            }
            showPopupDelay = setTimeout(function() {
                showPopupDelay = null;
                
                var smallImageWrap = $("div#preview");
                this.t = smallImageTrigger.parent().attr("title");
                this.add = smallImageTrigger.parent().attr("pathout");
                if (!this.add)
                	this.add = smallImageTrigger.parent().attr("href");
				this.title = "";	
				this.path = smallImageTrigger.parent().attr("path");
				var c = (this.t != "") ? "<br/>" + this.t : "";
				if (!smallImageWrap.length)
					$("body").append("<div id='preview'><p align='center'><img src='"+ this.path +"' alt='Image preview' /><a style='color:white' href='" + this.add + "' target='_blank'>"+ c +"</a></p></div>");								 
				else
					smallImageWrap.html("<p align='center'><img src='"+ this.path +"' alt='Image preview' /><a style='color:white' href='" + this.add + "' target='_blank'>"+ c +"</a></p>");
				var prev_top = smallImageTrigger.parent().offset().top - $("div#preview").height() - 30;
				var prev_left = smallImageTrigger.parent().offset().left;
				$("#preview")
					.css("top",prev_top + "px")
					.css("left",prev_left + "px")
					.fadeIn("slow");
				if ($("#preview").width() > 200) {
					$("#preview").width("200px");
				}
            });
        }
        
        var smallImageTrigger2 = findParent($(event.target), "img.image_art_disc");
        if (smallImageTrigger2.length) {
        	var smallImageWrap = $("div#preview");
        	if (smallImageWrap.attr("_timer")) {
                clearTimeout(smallImageWrap.attr("_timer"));
                smallImageWrap.attr("_timer", "");
            }
            showPopupDelay = setTimeout(function() {
                showPopupDelay = null;
                
                var smallImageWrap = $("div#preview");
                this.t = smallImageTrigger2.parent().attr("title");
                this.add = smallImageTrigger2.parent().attr("pathout");
				this.title = "";
				this.path = smallImageTrigger2.parent().attr("path");
				var parentTop = smallImageTrigger2.parent().parent().offset().top;
				var c = (this.t != "") ? "<br/>" + this.t : "";
				if (!smallImageWrap.length)
					$("body").append("<div id='preview'><p align='center'><img src='"+ this.path +"' alt='Image preview' />"+ c +"</p></div>");								 
				else
					smallImageWrap.html("<p align='center'><img src='"+ this.path +"' alt='Image preview' />"+ c +"</p>");
				$("#preview")
					.css("top",(parentTop - $("div#preview").height() - 30) + "px")
					.css("left",($("#statsbox").offset().left + ($("#statsbox").width()/2) - ($("div#preview").width()/2)) + "px")
					.fadeIn("slow");
				if ($("#preview").width() > 200) {
					$("#preview").width("200px");
				}
            });
        }
        
        var smallImageWrap = findParent($(event.target), "div#preview");
        if (smallImageWrap.length) {
        	if (smallImageWrap.attr("_timer")) {
            	clearTimeout(smallImageWrap.attr("_timer"));
                smallImageWrap.attr("_timer", "");
            }
        }

        var commentTrigger = findParent($(event.target), "a.comment_trigger");
        if (commentTrigger.length) {
            var comment = $("div.comment_popup");
            function commentPositioning() {
                if (($(window).width()-5-event.clientX) < comment.width())
                    comment.css({top: event.pageY + 5, left: $(window).width()-comment.width()-20});
                else
                    comment.css({top: event.pageY + 5, left: event.pageX+5});
            }
            
            TimerDelay = setTimeout(function() {
                TimerDelay = null;
                if ($(".comment_popup").length == 0) {
                    comment = $("<div class=\"comment_popup\"></div>").html(editor_comments);
                    $("body").append(comment.css({visibility: 'hidden'}));
                    commentPositioning();
                    comment.css({visibility: 'visible'}).fadeIn();
                } else {
                    commentPositioning();
                    comment.fadeIn();
   
                }
            }, delay);
        }
    }).mouseout(function(event) {
        var noteTrigger = findParent($(event.target), "a.note_trigger");
        if (noteTrigger.length) {
            if (showPopupDelay) {
                clearTimeout(showPopupDelay);
                showPopupDelay = null;
            }
            var id = $(event.target).attr("id").substr(1);
            var notePopupWrap1 = $("div#np" + id);
            var hidePopupDelay = setTimeout(function() {
                notePopupWrap1.attr("_timer", "");
                notePopupWrap1.fadeOut("slow");
            }, delay);
            notePopupWrap1.attr("_timer", hidePopupDelay);
        }
        
        var verseTrigger = findParent($(event.target), "a.verse_trigger");
        if (verseTrigger.length) {
            if (showPopupDelay) {
                clearTimeout(showPopupDelay);
                showPopupDelay = null;
            }
            var id = $(event.target).attr("id");
            var versePopupWrap1 = $("div#vp" + id.replace(new RegExp(",", "g"), "_"));
            var hidePopupDelay = setTimeout(function() {
                versePopupWrap1.attr("_timer", "");
                versePopupWrap1.fadeOut("slow");
            }, delay);
            versePopupWrap1.attr("_timer", hidePopupDelay);
        }
        
        var strongTrigger = findParent($(event.target), "a.strong_trigger");
        if (strongTrigger.length) {
            if (showPopupDelay) {
                clearTimeout(showPopupDelay);
                showPopupDelay = null;
            }
            var id = $(event.target).attr("id");
            var strongPopupWrap1 = $("div#sp" + id);
            var hidePopupDelay = setTimeout(function() {
                strongPopupWrap1.attr("_timer", "");
                strongPopupWrap1.fadeOut("slow");
            }, delay);
            strongPopupWrap1.attr("_timer", hidePopupDelay);
        }
        
		var imageTrigger = findParent($(event.target), "img.image_art");
        if (imageTrigger.length) {
            if (showPopupDelay) {
                clearTimeout(showPopupDelay);
                showPopupDelay = null;
            }
            var id = $(event.target).attr("id");
            var imagePopupWrap1 = $("div#image_zoom");
            var hidePopupDelay = setTimeout(function() {
                imagePopupWrap1.attr("_timer", "");
                imagePopupWrap1.fadeOut("fast");
            }, delay/4);
            imagePopupWrap1.attr("_timer", hidePopupDelay);
        }
        
        var smallImageTrigger = findParent($(event.target), "img.image_art_dict");
        if (smallImageTrigger.length) {
            if (showPopupDelay) {
                clearTimeout(showPopupDelay);
                showPopupDelay = null;
            }
            var smallImageWrap1 = $("div#preview");
            var hidePopupDelay = setTimeout(function() {
                smallImageWrap1.attr("_timer", "");
                smallImageWrap1.fadeOut("slow");
            }, delay);
            smallImageWrap1.attr("_timer", hidePopupDelay);
        }
        
        var smallImageTrigger2 = findParent($(event.target), "img.image_art_disc");
        if (smallImageTrigger2.length) {
            if (showPopupDelay) {
                clearTimeout(showPopupDelay);
                showPopupDelay = null;
            }
            var smallImageWrap1 = $("div#preview");
            var hidePopupDelay = setTimeout(function() {
                smallImageWrap1.attr("_timer", "");
                smallImageWrap1.fadeOut("slow");
            }, delay);
            smallImageWrap1.attr("_timer", hidePopupDelay);
        }

        var notePopupWrap = findParent($(event.target), "div.note_popup");
        if (notePopupWrap.length && !notePopupWrap.is(".note_sticky")) {
            var hidePopupDelay = setTimeout(function() {
                notePopupWrap.attr("_timer", "");
                var id = notePopupWrap.attr("id").substr(1);
                notePopupWrap.fadeOut("slow");
            }, delay);
            notePopupWrap.attr("_timer", hidePopupDelay);
        }
        
        var versePopupWrap = findParent($(event.target), "div.verse_popup");
        if (versePopupWrap.length && !versePopupWrap.is(".verse_sticky")) {
            var hidePopupDelay = setTimeout(function() {
                versePopupWrap.attr("_timer", "");
                var id = versePopupWrap.attr("id").substr(1);
                versePopupWrap.fadeOut("slow");
            }, delay);
            versePopupWrap.attr("_timer", hidePopupDelay);
        }
             
        var strongPopupWrap = findParent($(event.target), "div.strong_popup");
        var strongPopupWrap_id = strongPopupWrap.attr("id");
        var strongPopupClass = $("." + strongPopupWrap_id + "");
        if (strongPopupWrap.length)
        	strongPopupWrap.removeClass("strong_on");
        if (strongPopupWrap.length && !strongPopupWrap.is(".strong_sticky") && (!isElementsDisplayed(strongPopupClass))) {
            var hidePopupDelay = setTimeout(function() {
                strongPopupWrap.attr("_timer", "");
                var id = strongPopupWrap.attr("id");
                strongPopupWrap.fadeOut("slow", function() {
                	closeParent($(this));
                });
            }, delay);
            strongPopupWrap.attr("_timer", hidePopupDelay);
        }
        
		var imagePopupWrap = findParent($(event.target), "div#image_zoom");
        if (imagePopupWrap.length) {
            var hidePopupDelay = setTimeout(function() {
                imagePopupWrap.attr("_timer", "");
                imagePopupWrap.css("display","none");
            }, delay/4);
            imagePopupWrap.attr("_timer", hidePopupDelay);
        }
        
        var smallImageWrap = findParent($(event.target), "div#preview");
        if (smallImageWrap.length) {
            var hidePopupDelay = setTimeout(function() {
                smallImageWrap.attr("_timer", "");
                smallImageWrap.css("display","none");
            }, delay);
            smallImageWrap.attr("_timer", hidePopupDelay);
        }

        var commentTrigger = findParent($(event.target), "a.comment_trigger");
        if (commentTrigger.length) {
            $(".comment_popup").fadeOut();
            if (TimerDelay) {
                clearTimeout(TimerDelay);
                TimerDelay = null;
            }
        }
    }).click(function(event) {
        var notePopupWrap = findParent($(event.target), "div.note_popup");
        var versePopupWrap = findParent($(event.target), "div.verse_popup");
        var strongPopupWrap = findParent($(event.target), "div.strong_popup");
        
        if (notePopupWrap.length) {
            $("div.note_popup").css({zIndex: 90});
            notePopupWrap.css({zIndex: 95});
        }
        if ($(event.target).is("div.note_button_close")) {
            $(event.target).parent().removeClass("note_sticky").fadeOut("fast");
            $(event.target).siblings("div.note_button_sticky").removeClass("note_button_sticky_on").addClass("note_button_sticky_off");
        }
        if ($(event.target).is("div.note_button_sticky")) {
            if ($(event.target).parent().is(".note_sticky")) {
                $(event.target).parent().removeClass("note_sticky");
                $(event.target).removeClass("note_button_sticky_on").addClass("note_button_sticky_off");
            } else {
                $(event.target).parent().addClass("note_sticky");
                $(event.target).removeClass("note_button_sticky_off").addClass("note_button_sticky_on");
            }
        }
        
        if (versePopupWrap.length) {
            $("div.verse_popup").css({zIndex: 90});
            versePopupWrap.css({zIndex: 95});
        }
        if ($(event.target).is("div.verse_button_close")) {
            $(event.target).parent().removeClass("verse_sticky").fadeOut("fast");
            $(event.target).siblings("div.verse_button_sticky").removeClass("verse_button_sticky_on").addClass("verse_button_sticky_off");
        }
        if ($(event.target).is("div.verse_button_sticky")) {
            if ($(event.target).parent().is(".verse_sticky")) {
                $(event.target).parent().removeClass("verse_sticky");
                $(event.target).removeClass("verse_button_sticky_on").addClass("verse_button_sticky_off");
            } else {
                $(event.target).parent().addClass("verse_sticky");
                $(event.target).removeClass("verse_button_sticky_off").addClass("verse_button_sticky_on");
            }
        }
        
        if (strongPopupWrap.length) {
            $("div.strong_popup").css({zIndex: 90});
            strongPopupWrap.css({zIndex: 95});
        }
        if ($(event.target).is("div.strong_button_close")) {
            $(event.target).parent().removeClass("strong_sticky").fadeOut("fast");
            $(event.target).siblings("div.strong_button_sticky").removeClass("strong_button_sticky_on").addClass("strong_button_sticky_off");
        }
        if ($(event.target).is("div.strong_button_sticky")) {
            if ($(event.target).parent().is(".strong_sticky")) {
                $(event.target).parent().removeClass("strong_sticky");
                $(event.target).removeClass("strong_button_sticky_on").addClass("strong_button_sticky_off");
            } else {
                $(event.target).parent().addClass("strong_sticky");
    	        $(event.target).removeClass("strong_button_sticky_off").addClass("strong_button_sticky_on");
        	}
        }
        
        var contextButton = findParent($(event.target), "#context_form");        
    	contextButton.ajaxForm(function() {
            contextButton.parent().parent().html("<div style=\"position:relative; top:8px;\">" + thanks_correct + "</div>");
        });
        
        var correctButton = findParent($(event.target), "#correct_form");
        correctButton.ajaxForm(function() {
            versePopupContext.html("<div style=\"position:relative; top:3px;\">" + suggest + ": " + " <form id='context_form' action='ajax/context.php' method='post'><input class='input_small' type='text' name='suggestion'><input type='hidden' name='table' value=" + context_split[0] + "><input type='hidden' name='id' value=" + context_split[1] + "><input type='hidden' name='vref' value=" + escape(context_split[2]) + "><input type='hidden' name='url' value=" + context_split[3] + ">&nbsp;<input type='submit' value='" + "Send" + "' height='15px' style='font-size:100%'/></form>" + "</div>" + "<div style=\"position:relative; left:75px;\">" + suggest_ex + "</div>");
        });
        
    });
	
	var autoPlay = $("#autoplay");
	if (autoPlay.length) {
		//var autoPlayStat = $.cookie("autoplay_hymn");
		var imgSpeaker = $(".img_speaker");
		/*
		if (autoPlayStat == 'on') {
			imgSpeaker.show();
			autoPlay.children().eq(0).attr("id","autoplayon");
			autoPlay.children().eq(0).attr("src","images/autoplayon.gif");
			autoPlay.children().eq(0).attr("title",autoplayoff_text);
		} else {
			*/
			imgSpeaker.hide();
			autoPlay.children().eq(0).attr("id","autoplayoff");
			autoPlay.children().eq(0).attr("src","images/autoplayoff.gif");
			autoPlay.children().eq(0).attr("title",autoplayon_text);
		//}
		autoPlay.click(function(){
			if (autoPlay.children().eq(0).attr("id") == 'autoplayoff') {
				imgSpeaker.show();
				//$.cookie("autoplay_hymn", "on", { expires: 10 });
				autoPlay.children().eq(0).attr("id","autoplayon");
				autoPlay.children().eq(0).attr("src","images/autoplayon.gif");
				autoPlay.children().eq(0).attr("title",autoplayoff_text);
			} else {
				imgSpeaker.hide();
				//$.cookie("autoplay_hymn", "off", { expires: 10 });
				autoPlay.children().eq(0).attr("id","autoplayoff");
				autoPlay.children().eq(0).attr("src","images/autoplayoff.gif");
				autoPlay.children().eq(0).attr("title",autoplayon_text);
			}
		})
	}
	
	$(".art_other").click(function(){
   		var art_other = $(this);
		var titlePos = $(this).attr("id").split("_");
		var titleId = titlePos[2];
		var art_wrap = $(this).parent();
		var show = (4 + (((art_wrap.parent().children(".art_wrap_in").children(".art_wrap_divide").size())-1)*20)) + ",20";
		var a=0;
		var pic_count = $(this).attr("_count");
		//console.log(pic_count);
		
		var image_result = "";
		var art_table = $("#art_table_" + titleId);
		var last = $(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide[style!='display :none;']:last");
		$(this).siblings(".art_min").css("display","block");
		
		if (last.next().length) {
		    last.next().slideDown(function(){
		        $(this).removeClass('art_wrap_divide_none').addClass('art_wrap_divide');
		    });
		    //console.log($(".td_" + titleId).size());
		    //console.log(pic_count);
		    if (!last.next().next().length && $(".td_" + titleId).size() >= pic_count){
				$(this).css("display","none");
				$(this).siblings(".art_all").css("display","none");
			}
		}
		else {
			art_other.children("img").attr("src","images/spin.gif");
    		$.getJSON("ajax/ajax_art.php", {title: titleId, show:show}, function(data) {
    			var table_wrap = "<div class=\"art_wrap_divide\"><table>";
    			var image_gabung = "<tr class=\"art_row\">";
    			var title_gabung = "<tr>";
    			$.each(data, function(i, item) {
    				a++;
    				image_wrap = "<td class=\"td_" + item['id'] + "\" width=\"160px\" valign=\"bottom\" align=\"center\"><div><a href=\"http://www.biblical-art.com/artwork.asp?id_artwork=" + item['art_nbr'] + "&showmode=Full\"><img class=\"image_art\" align=\"center\" src=\"" + arts_url + "/" + item['path'] + "\"></a></div></td>";
    				image_title_wrap = "<td width=\"160px\" valign=\"top\" align=\"center\"><div class=\"image_title\"><b>" + item['image_title'] + "</b></div><div class=\"image_title\"><a href=\"http://www.biblical-art.com/artist_artwork.asp?id_artist=" + item['artist_nbr'] + "&alt=2&pagenum=1\">" + item['image_artist'] + "</a></div></td>";
    				if (a != 5) {
    				    image_gabung += image_wrap;
    				    title_gabung += image_title_wrap;
    				} else {
    					image_gabung += "</tr>";
    					title_gabung += "</tr>";
    				    table_wrap += image_gabung + title_gabung;
    				    image_gabung = "<tr class=\"art_row\">" + image_wrap;
    				    title_gabung = "<tr>" + image_title_wrap;
    					a = 1;
    				}
    			});
    			image_gabung += "</tr>";
    			title_gabung += "</tr>";
    			table_wrap += image_gabung + title_gabung + "</table></div>";
    			art_wrap.parent().children(".art_wrap_in").append(table_wrap);
    			//console.log($(".td_" + titleId).size());
				//console.log(pic_count);    			
    			if ($(".td_" + titleId).size() >= pic_count){
    				art_other.siblings(".art_all").css("display","none");
    				art_other.css("display","none");
    			}
    			art_other.children("img").attr("src","images/maximize.gif");	
    			//!!!!cek jumlah images yang sudah muncul, dibandingkan dengan jumlah data images per title
				//!!!!kalau lebih sedikit atau sama dengan, maka hide $(this) and hide Show All
    		});
    	}
    	
		return false;
	});
	
	$(".art_all").click(function(){
		var art_all = $(this);
		var titlePos = $(this).attr("id").split("_");
		var titleId = titlePos[2];
		var a=0;
		var grup=0;
		var art_wrap = $(this).parent();
		var pic_count = $(this).attr("_count");
		$(this).siblings(".art_min").css("display","block");
		$(this).siblings(".art_other").css("display","none");
		$(this).parent().prev().children().removeClass("art_wrap_divide_none").addClass("art_wrap_divide");
		$(this).parent().prev().children(".art_wrap_divide").css("display","block");
		var rowCount = $(this).parent().prev().children().children().children().children(".art_row").size();
		var showCount = 0;
		if (rowCount == 1)
			show = "4,500"
		else if (rowCount > 1) {
			showCount = (4 * rowCount);
			show = showCount + ",500";
		}
		if (pic_count > showCount) {
			var image_result = "";
			art_all.children("img").attr("src","images/spin.gif");
			$.getJSON("ajax/ajax_art.php", {title: titleId, show:show}, function(data) {
				var table_wrap = "<div class=\"art_wrap_divide\"><table>";
				var image_gabung = "<tr class=\"art_row\">";
				var title_gabung = "<tr>";
				$.each(data, function(i, item) {
					a++;
					grup++;
					image_wrap = "<td class=\"td_" + item['id'] + "\" width=\"160px\" valign=\"bottom\" align=\"center\"><div><a href=\"http://www.biblical-art.com/artwork.asp?id_artwork=" + item['art_nbr'] + "&showmode=Full\"><img class=\"image_art\" align=\"center\" src=\"" + arts_url +"/" + item['path'] + "\"></a></div></td>";
					image_title_wrap = "<td width=\"160px\" valign=\"top\" align=\"center\"><div class=\"image_title\"><b>" + item['image_title'] + "</b></div><div class=\"image_title\"><a href=\"http://www.biblical-art.com/artist_artwork.asp?id_artist=" + item['artist_nbr'] + "&alt=2&pagenum=1\">" + item['image_artist'] + "</a></div></td>";
					if (a != 5) {
						image_gabung += image_wrap;
						title_gabung += image_title_wrap;
					} else {
						image_gabung += "</tr>";
						title_gabung += "</tr>";
					    table_wrap += image_gabung + title_gabung;
					    image_gabung = "<tr class=\"art_row\">" + image_wrap;
					    title_gabung = "<tr>" + image_title_wrap;
						a = 1;
					}
					
					if (grup == 20){
						table_wrap += "</table></div><div class=\"art_wrap_divide\"><table>";
						grup = 0;
					}
						
				});
				image_gabung += "</tr>";
				title_gabung += "</tr>";
				table_wrap += image_gabung + title_gabung + "</table></div>";
	    		art_wrap.parent().children(".art_wrap_in").append(table_wrap);
	    		art_all.children("img").attr("src","images/disc_arrow.gif");
	    		art_all.css("display","none");
			});
		} else
			art_all.css("display","none");
		return false;
	});
	
	$(".art_min").click(function(){
		$(this).siblings().css("display","block");
		if ($(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide").size() == 2) {
			$(this).css("display","none");
			$(this).siblings().css("display","block");
		}
		if ($(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide").size() > 1) {
			$(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide[style!='display :none;']:last").slideUp();
			$(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide[style!='display :none;']:last").removeClass('art_wrap_divide').addClass('art_wrap_divide_none');	
		}
		return false;
	});
	
	$(".photos_other").click(function(){
   		var art_other = $(this);
		var titleId = $(this).attr("word")
		var resource_temp = $(this).attr("id").split("_");
	    var resource = resource_temp[2];
		var art_wrap = $(this).parent();
		var show = (4 + (((art_wrap.parent().children(".art_wrap_in").children(".art_wrap_divide").size())-1)*20)) + ",20";
		var a=0;
		var pic_count = $(this).attr("_count");
		
		var image_result = "";
		var last = $(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide[style!='display :none;']:last");
		$(this).siblings(".photos_min").css("display","block");
		$(this).parent().parent().prev().children(".photos_min_top").css("display","block");
		if (last.next().length) {
		    last.next().slideDown(function(){
		        $(this).removeClass('art_wrap_divide_none').addClass('art_wrap_divide');
		    });
		    if (!last.next().next().length && ($(".td_" + resource).size() >= pic_count)) {
				$(this).css("display","none");
				$(this).siblings(".photos_all").css("display","none");
				$(this).parent().parent().prev().children(".photos_all_top").css("display","none");
		    }
		}
		else {
			art_other.children("img").attr("src","images/spin.gif");
    		$.getJSON("ajax/ajax_photos.php", {title: titleId, show:show, res:resource}, function(data) {
    			var table_wrap = "<div class=\"art_wrap_divide\"><table>";
    			var image_gabung = "<tr class=\"art_row\">";
    			var title_gabung = "<tr>";
    			$.each(data, function(i, item) {
    				a++;
    				image_wrap = "<td class=\"td_" + resource + "\" width=\"160px\" valign=\"bottom\" align=\"center\"><div><img class=\"image_art\" align=\"center\" src=\"" + photos_url + "/" + item['filename'] + "\"></div></td>";
    				image_title_wrap = "<td width=\"160px\" valign=\"top\" align=\"center\"><div class=\"image_title\" style=\"font-size:11px;\"><b>" + item['title'] + "</b></div></td>";
    				if (a != 5) {
    				    image_gabung += image_wrap;
    				    title_gabung += image_title_wrap;
    				} else {
    					image_gabung += "</tr>";
    					title_gabung += "</tr>";
    				    table_wrap += image_gabung + title_gabung;
    				    image_gabung = "<tr class=\"art_row\">" + image_wrap;
    				    title_gabung = "<tr>" + image_title_wrap;
    					a = 1;
    				}
    			});
    			image_gabung += "</tr>";
    			title_gabung += "</tr>";
    			table_wrap += image_gabung + title_gabung + "</table></div>";
    			art_wrap.parent().children(".art_wrap_in").append(table_wrap);
    			if ($(".td_" + resource).size() >= pic_count){
    				art_other.siblings(".photos_all").css("display","none");
    				art_other.parent().parent().prev().children(".photos_all_top").css("display","none");
    				art_other.css("display","none");
    			}
    			art_other.children("img").attr("src","images/maximize.gif");
    		});
    	}
    	
		return false;
	});
	
	$(".photos_all").click(function(){
		var art_all = $(this);
		var titleId = $(this).attr("word");
		var a=0;
		var grup=0;
		var art_wrap = $(this).parent();
		var pic_count = $(this).attr("_count");
		var resource_temp = $(this).attr("id").split("_");
	    var resource = resource_temp[2];
		$(this).siblings(".photos_min").css("display","block");
		$(this).parent().parent().prev().children(".photos_min_top").css("display","block");
		$(this).siblings(".photos_other").css("display","none");
		$(this).parent().prev().children().removeClass("art_wrap_divide_none").addClass("art_wrap_divide");
		$(this).parent().prev().children(".art_wrap_divide").css("display","block");
		var rowCount = $(this).parent().prev().children().children().children().children(".art_row").size();
		var showCount = 4 * rowCount;
		if (rowCount == 1) 
			show = "4,700";
		else if (rowCount > 1) 
			show = showCount + ",700";
		
		if (pic_count > showCount) {
			var image_result = "";
			art_all.children("img").attr("src","images/spin.gif");
			$.getJSON("ajax/ajax_photos.php", {title: titleId, show:show, res:resource}, function(data) {
				var table_wrap = "<div class=\"art_wrap_divide\"><table>";
				var image_gabung = "<tr class=\"art_row\">";
				var title_gabung = "<tr>";
				$.each(data, function(i, item) {
					a++;
					grup++;
					image_wrap = "<td class=\"td_" + resource + "\" width=\"160px\" valign=\"bottom\" align=\"center\"><div><img class=\"image_art\" align=\"center\" src=\"" + photos_url + "/" + item['filename'] + "\"></div></td>";
					image_title_wrap = "<td width=\"160px\" valign=\"top\" align=\"center\"><div class=\"image_title\" style=\"font-size:11px;\"><b>" + item['title'] + "</b></div></td>";
					if (a != 5) {
						image_gabung += image_wrap;
						title_gabung += image_title_wrap;
					} else {
						image_gabung += "</tr>";
						title_gabung += "</tr>";
					    table_wrap += image_gabung + title_gabung;
					    image_gabung = "<tr class=\"art_row\">" + image_wrap;
					    title_gabung = "<tr>" + image_title_wrap;
						a = 1;
					}
					
					if (grup == 20){
						table_wrap += "</table></div><div class=\"art_wrap_divide\"><table>";
						grup = 0;
					}
						
				});
				image_gabung += "</tr>";
				title_gabung += "</tr>";
				table_wrap += image_gabung + title_gabung + "</table></div>";
	    		art_wrap.parent().children(".art_wrap_in").append(table_wrap);
	    		art_all.children("img").attr("src","images/disc_arrow.gif");
	    		art_all.parent().parent().prev().children(".photos_all_top").css("display","none");
	    		art_all.css("display","none");
			});
		} else {
			art_all.css("display","none");
			art_all.parent().parent().prev().children(".photos_all_top").css("display","none");
		}
		return false;
	});
	
	$(".photos_min").click(function(){
		$(this).siblings().css("display","block");
		if ($(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide").size() == 2) {
			$(this).css("display","none");
			$(this).parent().parent().prev().children(".photos_min_top").css("display","none");
			$(this).parent().parent().prev().children(".photos_all_top").css("display","block");
			$(this).siblings().css("display","block");
		}
		if ($(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide").size() > 1) {
			$(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide[style!='display :none;']:last").slideUp();
			$(this).parent().parent().children(".art_wrap_in").children(".art_wrap_divide[style!='display :none;']:last").removeClass('art_wrap_divide').addClass('art_wrap_divide_none');	
		}
		return false;
	});
	
	$(".photos_min_top").click(function(){
		$(this).siblings(".photos_all_top").css("display","block");
		$(this).parent().next().children(".other_all").children(".photos_all").css("display","block");
		$(this).parent().next().children(".other_all").children(".photos_other").css("display","block");
		$(this).parent().next().children(".other_all").children(".photos_min").css("display","none");
		$(this).css("display","none");
		$(this).parent().next().children(".art_wrap_in").children().removeClass("art_wrap_divide").addClass("art_wrap_divide_none").css("display","none");
		$(this).parent().next().children(".art_wrap_in").children().eq(0).removeClass("art_wrap_divide_none").addClass("art_wrap_divide").css("display","block");
		return false;
	});
	
	$(".photos_all_top").click(function(){
		$(this).parent().next().children(".other_all").children(".photos_all").click();
	});	
	
	$("a.midi_hymn").hover(function(){
		if ($("img#autoplayon").length) {
			var midi_path = $(this).attr("path");
			var embed_midi = $("<embed src=\"" + midi_path + "\" hidden=\"TRUE\" AUTOSTART=\"TRUE\" LOOP=\"TRUE\">"); 
			$("body").append(embed_midi);
		}
	},function(){
		$("embed").remove();
	});
	
	$(".hymn_text").click(function(){
		if ($(this).parent().children(".hymn_lyrics").eq(0).css("display") == "none") {
			$(this).parent().children(".hymn_lyrics").eq(0).slideDown("fast");
			$(this).attr("src","images/minus.gif");
			$(this).siblings(".hymn_text").attr("src","images/minus.gif"); //untuk jika klik ke titlenya, bukan pada gambar
		}
		else {
			$(this).parent().children(".hymn_lyrics").eq(0).slideUp("fast");
			$(this).attr("src","images/advanced.gif");
			$(this).attr("src","images/advanced.gif");
			$(this).siblings(".hymn_text").attr("src","images/advanced.gif"); //untuk jika klik ke titlenya, bukan pada gambar
		}
	})


});

