﻿
(function ($) {
	$.extend($, {
		cacheImage: function (src, options) {
			if (typeof src === 'object') {
				$.each(src, function () {
					$.cacheImage(String(document), options);
				});
			}
			var image = new Image();
			options = options || {};
			$.each(['load', 'error', 'abort'], function () { // Callbacks
				var e = String(document);
				if (typeof options[e] === 'function') { $(image)[e](options[e]); }
			});
			image.src = src;
			return image;
		}
	});
	$.extend($.fn, {
		cacheImage: function (options) {
			return this.each(function () {
				$.cacheImage(this.src, options);
			});
		}
	});
})(jQuery);



//Begin generic function for populating select boxes
$(document).ready(function () {
	$.fn.addItems = function (dropdown, data) {
		return this.each(function () {
			var list = this;
			$.each(data, function (index, itemData) {
				$("#" + dropdown).addOption(itemData.Value, itemData.Name);
			});
		});
	};
});

function populateDropDown(dropdown, url) {
	$.getJSON(url, null, function (data) {
		$('#' + dropdown).children().remove().end().append("<option value=" + "" + ">" + "Please Select" + "</option>");
		$("#" + dropdown).addItems(dropdown, data);
	});
}
//End generic function for populating select boxes
var oldWidth = 0;


function initVolumeSlider() {
	var length = $("#volume-bar").slider("#volume-bar-value").length;

	if (length != 2)
		$(window).ready(function () {
			$("#volume-bar").slider({
				orientation: "vertical",
				handle: "#volume-bar-value",
				range: "min",
				min: 0,
				max: 100,
				value: 26,
				stop: function (event, ui) {
					setVolume2(ui.value);
				},
				slide: function (event, ui) {
					setVolume(event);
				},
				change: function (event, ui) {
					setVolume2(ui.value);
				}
			})
		});	
}



function initSlider() {

	$(window).ready(function () {
		$("#slider").slider("destroy");

		var container = $('#gallery');
		var ul = $('ul#imageLibrary', container);
		var itemsWidth = ul.innerWidth() - container.outerWidth() + 16;
		var totalWidth = $("ul#imageLibrary").css("width");

		$('#slider').slider({
			min: 0,
			max: itemsWidth,
			handle: '#handle',
			stop: function (event, ui) {
				ul.animate({ 'left': '-' + ui.value + 'px' }, 10);
			},
			slide: function (event, ui) {
				ul.css('left', '-' + ui.value + 'px');
			}
		});
	});
}

var _control = null;
function sortmyway(data_A, data_B) {
	return 0.5 - Math.random(); //random gives us result between 0 and 1
}

function partialLoad(control) {
	_control = control;

	var randomValue = (Math.round(Math.random()) - 0.5);
	var customDivs = $(".random");

	imageArray = new Array();
	$.each(customDivs, function (key, value) {
		imageArray.push(value);
	});
	imageArray = imageArray.sort(sortmyway);

	jQuery.noConflict();

	jQuery(function () {
		jQuery.blockUI({ message: imageArray[0].innerHTML });
		jQuery('a.menu, .imgPartialLoad, #logo').live('click', getPartialControl(false));
	});

}

function getPartialControl(initial, func) {
	jQuery.get("/Helper/GetPartial", { partialViewName: _control },
				function (data) {
					preCleanUI();
					jQuery('#MidContent').html(data);
					jQuery('img').imgpreload(function () {
						postCleanUI();
						initSlider();
						if (initial == false) {
							window.setTimeout(function () { jQuery.unblockUI(); }, 100);
						}
						if (func != null) {
							window[func]();
						}
					});
				},
		"html");
}

function preCleanUI() {
	$("#Pager1").css("display", "none");
	$("#Pager1").attr("alt", "");
	$("#Pager2").css("display", "none");
	$("#Pager2").attr("alt", "");
	$("#Pager3").css("display", "none");
	$("#Pager3").attr("alt", "");
}

function postCleanUI() {
	$("#imageLibrary").css("left", "0px");
	$("#handle").css("left", "0px");
	$("#handle").css("visibility", "visible");
	$("#navMenu").css("visibility", "visible");
	disableImageDragging();
    if (isIOS)
    {
        registerSwipe();
    }
}
function playMusic() {

	var firstTime = true;
	jQuery.noConflict();
	jQuery(window).ready(function () {
		jQuery("#jquery_jplayer").jPlayer({
			ready: function () {
				playListInit(true); // Parameter is a boolean for autoplay.
			},
			oggSupport: false,
			preload: 'none'
		})
  .jPlayer("onSoundComplete", function () {
	  playListNext();
  })
	});

	jQuery(document).ready(function () {
		jQuery("#pause").click(function () {
			jQuery("#jquery_jplayer").jPlayer("pause");
			jQuery("#pause").css("display", "none");
			jQuery("#play").css("display", "block");
			jQuery("#play").css("line-height", "0");
		})

		jQuery("#play").click(function () {
			jQuery("#jquery_jplayer").jPlayer("play");
			jQuery("#play").css("display", "none");
			jQuery("#pause").css("display", "block");
			jQuery("#pause").css("line-height", "0");
		})
	});

	var playItem = 0;

	var myPlayList = [
		{ name: "GirlFriend Experience", mp3: "/moviesandmusic/The Girlfriend Experience.mp3" },
	    { name: "Fuckin in the bushes", mp3: "/moviesandmusic/F In The Bushes Edit.mp3" },
	    { name: "The Chase", mp3: "/moviesandmusic/07 The Chase 1.mp3" },
	    { name: "Liberation Front", mp3: "/moviesandmusic/12 Liberation Front.mp3" },
	    { name: "Kangaroo Rat", mp3: "/moviesandmusic/12 The Kangaroo Rat 1.mp3" },
		{ name: "Angel", mp3: "/moviesandmusic/17 Angel.mp3" },
		{ name: "Diamond", mp3: "/moviesandmusic/02 Diamond.mp3" },
		{ name: "Machete", mp3: "/moviesandmusic/16MacheteTheme.mp3" }
  ];

	$("#prev").click(function () {
		jQuery("#play").css("display", "none");
		jQuery("#pause").css("display", "block");
		playListPrev();
		$(document).blur();
		return false;
	});

	$("#next").click(function () {
		jQuery("#play").css("display", "none");
		jQuery("#pause").css("display", "block");
		playListNext();
		$(document).blur();
		return false;
	});

	function playListInit(autoplay) {
		if (autoplay) {
			playListChange(playItem);
		} else {
			playListConfig(playItem);
		}
	}

	function playListConfig(index) {
		playItem = index;
		jQuery("#jquery_jplayer").jPlayer("setFile", myPlayList[playItem].mp3, myPlayList[playItem].mp3);

	}

	function playListChange(index) {
		playListConfig(index);
		
		if (firstTime == true) {
			jQuery("#jquery_jplayer").jPlayer("setFile", "/moviesandmusic/FirstTrackMachete2.mp3").jPlayer("play");
			initVolumeSlider();
			playItem--;
			firstTime = false;
		}
		else {
			jQuery("#jquery_jplayer").jPlayer("play");
		}
	}

	function playListNext() {
		var index = (playItem + 1 < myPlayList.length) ? playItem + 1 : 0;
		playListChange(index);
	}

	function playListPrev() {
		var index = (playItem - 1 >= 0) ? playItem - 1 : myPlayList.length - 1;
		playListChange(index);
	}
}

$(document).ready(function () {
	$(window).resize(function () {
		centerContent();
		initSlider();
	})
});

$(document).ready(function () {
	centerContent();

	if (!$.browser.msie)
		$("#Left").click(function () {
			moveLeft();
		})

	if (!$.browser.msie)
		$("#Right").click(function () {
			moveRight();
		})

	if ($.browser.msie)
		$("#DivLeft").click(function () {
			moveLeft();
		});
	if ($.browser.msie)
		$("#DivRight").click(function () {
			moveRight();
		});
});

function centerContent() {
	var contentHeight = $('#mainContent').height();
	var bodyHeight = $(window).height();
	var differenceInHeight = (bodyHeight - contentHeight);
	var topMargin = differenceInHeight / 2;
	var bottomMargin = differenceInHeight / 2;

	$('#container').css('margin-top', topMargin + 'px');
	$('#container').css('margin-bottom', bottomMargin + 'px');
	$('body').css('overflow', 'hidden');
	$('#mainContent').css('visibility', 'visible');
}

$(document).ready(function () {
	$("a.menu").click(function () {
		partialLoad(this.rel);
	})
	$(".imgPartialLoad").click(function () {
		partialLoad(this.alt);
	})
});

function loadArtwillcontinue() {
    setTimeout(function () {
        _control = "Artwillcontinue";
        getPartialControl(true, null);
    }, 100);

}

function loadInitialControl(control, func) {
	setTimeout(function () {
		_control = control;
		getPartialControl(true, func);
	}, 100);
}

$(document).ready(function () {

	$("#DivLeft").mouseover(function () { canMoveLeft() });
	$("#DivRight").mouseover(function () { canMoveRight() });

	if (!$.browser.msie) {
		$("#DivLeft").mouseout(function () { $("#Left").css("visibility", "hidden") });
		$("#DivRight").mouseout(function () { $("#Right").css("visibility", "hidden") });
	}
	else {
		$("#DivLeft").mouseout(function () {
			$("#DivLeft").css("opacity", "0.05");
			$("#DivLeft").css("filter", "alpha(opacity = 0.05)");
		});
		$("#DivRight").mouseout(function () {
			$("#DivRight").css("opacity", "0.05");
			$("#DivRight").css("filter", "alpha(opacity = 0.05)");
		});
	}

	//Initially hide both
	$("#Left").css("visibility", "hidden");
	$("#Right").css("visibility", "hidden");
})

function canMoveLeft() {
	var container = $('#gallery');
	var ul = $('ul#imageLibrary', container);

	var itemsWidth = ul.innerWidth() - container.outerWidth() + 16;
	var marginLeft = ul.css("left").replace("px", "");

	if ((Math.abs(marginLeft) > 108)) {
		if (!$.browser.msie) {
			$("#Left").css("visibility", "visible");
		}
		else {
			$("#DivLeft").css("opacity", "100");
			$("#DivLeft").css("filter", "alpha(opacity = 100)");
		}
		return true;
	}
	else {
		if (!$.browser.msie) {
			$("#Left").css("visibility", "hidden");
		}
		else {
			$("#DivLeft").css("opacity", "0.05");
			$("#DivLeft").css("filter", "alpha(opacity = 0.05)");
		}
		return false;
	}
}

function canMoveRight() {
	var container = $('#gallery');
	var ul = $('ul#imageLibrary', container);

	var itemsWidth = ul.innerWidth() - container.outerWidth() + 16;

	var marginLeft = ul.css("left").replace("px", "");

	if (((Math.abs(marginLeft) + 500) <= itemsWidth)) {

		if (!$.browser.msie) {
			$("#Right").css("visibility", "visible");
		}
		else {
			$("#DivRight").css("opacity", "100");
			$("#DivRight").css("filter", "alpha(opacity = 100)");
		}
		return true;
	}
	else {
		if (!$.browser.msie) {
			$("#Right").css("visibility", "hidden");
		}
		else {
			$("#DivRight").css("opacity", "0.05");
			$("#DivRight").css("filter", "alpha(opacity = 0.05)");
		}
	}
	return false;
}


jQuery(document).keypress(function (e) {
	if (e.keyCode == 37) {
		e.preventDefault();
		moveLeft();
	}
});

jQuery(document).keydown(function (e) {
	if (e.keyCode == 37) {
		e.preventDefault();
		moveLeft();
	}
});

jQuery(document).keypress(function (e) {
	if (e.keyCode == 39) {
		e.preventDefault();
		moveRight();
	}
});

jQuery(document).keydown(function (e) {
	if (e.keyCode == 39) {
		e.preventDefault();
		moveRight();
	}
});

function moveLeft() {
	var container = jQuery('#gallery');
	var ul = jQuery('ul#imageLibrary', container);

	var itemsWidth = ul.innerWidth() - container.outerWidth() + 16;
	var marginLeft = ul.css("left").replace("px", "");
	if ($("ul#imageLibrary:animated").length == 0) {

		if ((marginLeft + 500) <= 108) {
			$("#slider").slider("moveTo", Math.abs(marginLeft) - 500, null, false);
		}
		else {
			if (Math.abs(marginLeft) != 0) {
				$("#slider").slider("moveTo", 0, null, false);
				canMoveLeft();
			}
		}
	}
}

function moveRight() {
	var container = $('#gallery');
	var ul = $('ul#imageLibrary', container);

	var itemsWidth = ul.innerWidth() - container.outerWidth() + 16;
	var marginLeft = ul.css("left").replace("px", "");

	if (marginLeft == "auto" || marginLeft == null)
		marginLeft = 0;


	if ($("ul#imageLibrary:animated").length == 0) {
		if ((Math.abs(marginLeft) + 500) <= itemsWidth) {
			$("#slider").slider("moveTo", (Math.abs(marginLeft) + 500), null, false);
		}
		else {
			if (Math.abs(marginLeft) != Math.abs(itemsWidth)) {
				$("#slider").slider("moveTo", (Math.abs(itemsWidth)), null, false);
				canMoveRight();
			}
		}
	}
}

function movieHasEnded() {
	jQuery.blockUI({ message: $("#makeYourMark").html() });
	window.setTimeout(function () { ended(); }, 500);
}

function iPadIntro() {
	jQuery.blockUI({ message: $("#loading").html() });
	window.setTimeout(function () { ended(); }, 500);
}


function ended() {
	loadInitialControl('home', "loadHome");
	$('.sf-menu').css('visibility', 'visible');
}

function loadHome() {
	window.setTimeout(function () { jQuery.unblockUI(); }, 100);
}

function isIOS() {
	if ((navigator.userAgent.toLowerCase().indexOf("iphone") != -1) || (navigator.userAgent.indexOf("iPad") != -1)) {
		return true;
	}
	else {
		return false;
	}
}

function registerSwipe() {
    $(document).ready(function () {
        if (isIOS()) {
            jQuery.noConflict();
           jQuery('#MidContent, #galleryItems, #galleryItems img').swipe(
           {
             swipeLeft: function()  { moveRight() },
             swipeRight: function() {  moveLeft() }
         });
        }
    });
}

$(document).ready(function () {
	setInitialVolume();
});


function setInitialVolume() {
	$("#volume-bar-value").css("top", 76);
}
function setVolume(e) {
	if (e == null) return;
	var offset = $("#volume-bar").offset();
	var objheight = $("#volume-bar").css("height").replace("px", "");
	var val = parseFloat(offset.top) + parseFloat(objheight) - parseFloat(e.pageY);
	var value = objheight - val;


	if (val > 9 && val < 100) {
		jQuery("#jquery_jplayer").jPlayer("volume", val);
		jQuery("#volume-bar-value").css("top", value - 5);
	}
	else if (val >= 100) {
		jQuery("#jquery_jplayer").jPlayer("volume", 100);
		jQuery("#volume-bar-value").css("top", "5px");
	}
	else if (val <= 20) {
		jQuery("#jquery_jplayer").jPlayer("volume", 0);
		jQuery("#volume-bar-value").css("top", "97px");
	}
}

function setVolume2(e) {

	var objheight = $("#volume-bar").css("height").replace("px", "");
	var value = e - objheight;

	if (Math.abs(value) >= 100) {
		jQuery("#jquery_jplayer").jPlayer("volume", 100);
	}
	else if (Math.abs(value) <= 15) {
		jQuery("#jquery_jplayer").jPlayer("volume", 0);
	}
	else {
		jQuery("#jquery_jplayer").jPlayer("volume", Math.abs(value));
	}
}

function disableImageDragging() {
	$(document).ready(function () {
		$("img").bind("mousedown",
			function (e) {
				e.preventDefault();
			});
	});
}

window.onerror = function () {
	return true;
}


