function isQuicktimeAvailableJS() {
    var retval = false;
    if (navigator.plugins) {
        for (var i = 0; i < navigator.plugins.length; i++) {
            if (navigator.plugins[i].name.indexOf("QuickTime") >= 0) {
                retval = true;
            }
        }
    }
    return retval;
}

function getUrlParam(paramName){
	var results = new RegExp('[\\?&]' + paramName + '=([^&#]*)').exec(window.location.href);
	if (!results) { return 0; }
	return results[1] || 0;
}

function onDocumentReady() {
	$.fx.off = false;	
	$.urlParam = getUrlParam;
	var useLocalDB = $.urlParam("local") == "true" ? "true" : "false";
	$.getJSON("content.php", { local:useLocalDB, cat:"mograph", fmt:"json" }, function(data1) {
        displayThumbnails("images/graphicsthatmove.png", "graphics that move", data1, $("#hapLeftBlock"));
        $.getJSON("content.php", { local:useLocalDB, cat:"video", fmt:"json" }, function(data2) {
            displayThumbnails("images/videoproduction.png", "video", data2, $("#hapLeftBlock"));
		    $.getJSON("content.php", { local:useLocalDB, cat:"print", fmt:"json"}, function(dataA) {
		        displayThumbnails("images/print_adv.png", "print + advertising", dataA, $("#hapLeftBlock"));
				fillRightBlock(useLocalDB);
	        });
        });
	});
    $("#hapImage").load(showImage);
    $("#hapWebImage").load(showWebImage);

    if (qtReady) {
        var qt = document.getElementById('movie1');

        if (document.addEventListener) {
            // For every browser except IE.
            qt.addEventListener('qt_play', playMovie, false);
        } else {
            // For IE.
            qt.attachEvent('on' + 'qt_play', playMovie);
        }
    } else {
		// alert("The Apple Quicktime plug-in does not appear to be installed, or may be disabled. Video content will not be available.");
	}
	
	$.curCat = "logos";
	$("#hapImage").attr("src", "content/logos/hap.jpg");
}

function fillRightBlock(localDB) {
    $.getJSON("content.php", { local:localDB, cat: "web", fmt: "json" }, function (dataB) {
        displayThumbnails("images/web.png", "web", dataB, $("#hapRightBlock"));
        $.getJSON("content.php", { local:localDB, cat: "logos", fmt: "json" }, function (dataC) {
            displayThumbnails("images/logos.png", "logos", dataC, $("#hapRightBlock"));
            $.getJSON("content.php", { local:localDB, cat: "photos", fmt: "json" }, function (data3) {
                displayThumbnails("images/photography.png", "photography", data3, $("#hapRightBlock"));
            });
        });
    });
} 

function displayThumbnails(header_img, header_alt, data, container) {
	container.append($.sprintf("<img class='hapSectionHeader' src='%s' alt='%s'>", header_img, header_alt));
	container.append("<br>");

    for (var i = 0; i < data.length; i++) {
        var txt = $.sprintf("<img class='hapThumbnail' src='%s' id='%s' title='%s' url='%s' media_type='%s' category='%s' alt='%s'>",
							data[i].thumbnail, data[i].id, data[i].name, data[i].url, data[i].media_type, data[i].category, data[i].name);
        container.append(txt);
        var thumb = $("#" + data[i].id);
		if (data[i].category == "mograph" || data[i].category == "video") {
			//if (qtReady) {
				thumb.hover(onThumbMouseIn, onThumbMouseOut);
				thumb.click(onClickThumb);
				thumb.fadeIn('slow');
			/*} else {
				thumb.attr('title', "Quicktime plug-in unavailable");
				thumb.fadeTo('slow', 0.5);
			}*/
		} else {
			thumb.hover(onThumbMouseIn, onThumbMouseOut);
        	thumb.click(onClickThumb);
			thumb.fadeIn('slow');
		}
        if (0 == (i + 1) % 8) {
            container.append("<br>");
        }
    }
}

function onThumbMouseIn(event) {
    $(this).css("border", "1px solid #7C885B");
}

function onThumbMouseOut(event) {
    $(this).css("border", "1px solid #DDD2C7");
}

function getTopPad(container, containee) {
    var h1 = container.height();
    var h2 = containee.height();
    var result = (h1 / 2) - (h2 / 2);
    return result;
}

function getLeftPad(container, containee) {
    var w1 = container.width();
    var w2 = containee.width();
    var result = (w1 / 2) - (w2 / 2);
    return result;
}

function showImage(event) {
	$("#hapPreloader").hide();
    $(this).css("top", getTopPad($("#hapImageContainer"), $(this)) + "px");
    $(this).fadeIn('fast');
}

function showWebImage(event) {
	$(this).show();
}

function playMovie(event) {
    try {
        // This jQuery call fails on FireFox ('not a function')
        // so catch error & retry with straight JavaScript.
        $("#movie1").get(0).Play();
    } catch(error) {
        document.movie1.Play();
    }
}

function stopMovie() {
    try {
        // This jQuery call fails on FireFox ('not a function')
        // so catch error & retry with straight JavaScript.	
        $("#movie1").get(0).Stop();
    } catch(error) {
        document.movie1.Stop();
    }
}

function setMovieURL(url) {     
	// Set the url of the Quicktime player only if the
	// incoming url is different than the current one.	
    try {
        // This jQuery call fails on FireFox ('not a function')
        // so catch error & retry with straight JavaScript.	
		if (url != $("#movie1").get(0).GetURL()) {
        	$("#movie1").get(0).SetURL(url);
		}
    } catch(error) {
		if (url != document.movie1.GetURL()) {
			document.movie1.SetURL(url)
		}
    }
}

function onClickThumb(event) {
    var thumb = $("#" + event.target.id);
	var newUrl = thumb.attr("url");
	var newCat = thumb.attr("category");
			
	switch (newCat) {
		case "mograph":
		case "video":
			$("#hapPreloader").hide('fast');
			if ($.curCat == "mograph" || $.curCat == "video") {
				setMovieURL(newUrl);
			}
			else if ($.curCat == "print" || $.curCat == "logos" || $.curCat == "photos") {
				$("#hapImage").fadeOut('fast', function() {
					$("#hapQTContainer").fadeIn('fast', function() {
						setMovieURL(newUrl);   
					});
				});
 			} else { // $.curCat == "web"
				$("#hapWebImage").fadeOut('fast', function() {
 					$("#hapWebImageContainer").fadeOut('slow', function() {
						$("#hapQTContainer").fadeIn('fast', function() {
							setMovieURL(newUrl);
						});
					});
				});
			}
			break;
		case "print":
		case "logos":
		case "photos":
			if ($.curCat == "print" || $.curCat == "logos" || $.curCat == "photos") {
				if (newUrl != $("#hapImage").attr("src")) {
					$("#hapImage").fadeOut(100, function() {
						$("#hapPreloader").show();
						$("#hapImage").attr("src", newUrl);
						$("#hapImage").attr("title", thumb.attr("title"));
					});
				}				
			} else if ($.curCat == "mograph" || $.curCat == "video") {
				stopMovie();
				$("#hapQTContainer").fadeOut('slow', function() {
					$("#hapPreloader").show();										  
					$("#hapImage").attr("src", newUrl);
					$("#hapImage").attr("title", thumb.attr("title"));
				});
			} else { // $.curCat == "web"
				$("#hapWebImageContainer").fadeOut('slow', function() {
					$("#hapPreloader").show();												
					$("#hapImage").attr("src", newUrl);
					$("#hapImage").attr("title", thumb.attr("title"));
				});				
			}
			break;
		case "web":
			if ($.curCat == "web") {
				//$("#hapPreloader").show();
				$("#hapWebImage").attr("src", newUrl);
				$("#hapWebImage").attr("title", thumb.attr("title"));
			} else if ($.curCat == "mograph" || $.curCat == "video") {
				stopMovie();
				$("#hapQTContainer").fadeOut('slow', function() {
					//$("#hapPreloader").show();										  
					$("#hapWebImage").attr("src", newUrl);
					$("#hapWebImage").attr("title", thumb.attr("title"));
					$("#hapWebImageContainer").fadeIn('slow');				
				});
			} else { // $.curCat == "print" || "logos" || "photos"
				$("#hapImage").fadeOut('fast', function() {
					//$("#hapPreloader").show();									
					$("#hapWebImage").attr("src", newUrl);
					$("#hapWebImage").attr("title", thumb.attr("title"));
					$("#hapWebImageContainer").fadeIn('slow');				
				});
			}
			break;
		default:
			break;
	}
	
	$.curCat = newCat;
}
