/*
 * jQuery extensions for Thinking Pictures
 * http://www.thinkingpictures.com/
 *
 * Copyright 2011, Thinking Pictures
 */





/* Global variables
------------------------------------------------- */
var hash = hash ? hash : window.location.hash;
/* -------------------------------------------------
                             end global variables */





/* Custom functions
------------------------------------------------- */

// capitalize
String.prototype.capitalize = function() {return this.replace(/(^|\s)([a-z])/g, function(m,p1,p2) {return p1+p2.toUpperCase();});};

// add commas
jQuery.addCommas = function(int) { int += ''; x  = int.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d+)(\d{3})/; while(rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; };

jQuery.array_to_object = function(array) {
	var object = {};
	for(var i=0;i<array.length;i++) {object[array[i]]='';}
	return object;
};

// jQuery Cookie plugin; https://github.com/carhartl/jquery-cookie
jQuery.cookie = function(key, value, options) {
	if(arguments.length > 1 && (value === null || typeof value !== "object")) {
		options = jQuery.extend({}, options);
		
		if(value === null) { options.expires = -1; }
		
		if(typeof options.expires === 'number') {
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}
		
		return (document.cookie = [
			encodeURIComponent(key), '=',
			options.raw ? String(value) : encodeURIComponent(String(value)),
			options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
			options.path ? '; path=' + options.path : '',
			options.domain ? '; domain=' + options.domain : '',
			options.secure ? '; secure' : ''
		].join(""));
	}
	
	options = value || {};
	
	var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

jQuery.rtrim = function(str, chars) {
    chars = chars || "\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
};

jQuery.initialize = function() {
	$("body").find(":first-child").addClass("first-child");
	$("body").find(":last-child").addClass("last-child");
	
	$("select#type").change(function() {
		
		switch($(this).val()) {
			case "audio":
				$("a#file_upload").attr("href","/uploader/upload/" + $(this).val()).text("Upload MP3");
				$(".embed").slideUp();
				$(".flash").show();
				$("#file_preview div.flash").length == 1 ? $("dt.file").hide() : $("dt.file").show();
				$("#file_preview div.flash").length == 1 ? $("#the_file").show().parent().show() : $("#the_file").hide().parent().show();
				$("#file_preview a.pdf").hide().next().hide();
			break;
			case "photo":
				$(".embed").slideDown();
				$("dt.file").slideUp();
				$(".flash").hide();
			break;
			case "pdf":
				$("a#file_upload").attr("href","/uploader/upload/" + $(this).val()).text("Upload PDF");
				$(".embed").slideUp();
				$(".flash").hide();
				$("#file_preview a.pdf").length == 1 ? $("dt.file").hide() : $("dt.file").show();
				$("#file_preview a.pdf").length == 1 ? $("#the_file").show().parent().show() : $("#the_file").hide().parent().show();
				$("#file_preview a.pdf").show().next().show();
			break;
			case "video":
				$(".embed").slideDown();
				$("dt.file").slideUp();
				$(".flash").hide();
			break;
			default: $(".embed").slideUp(); $(".file").slideUp();
		}
		
	});
	
	$("#include_thumbnail").click(function() {
		
		if($(this).is(":checked")) {
			$("dd.thumb").slideDown();
		}
		else {
			$("dd.thumb").slideUp();
			$("#image_preview").html("");
		}
		
	});
	
	$("a.delete").click(function() {
		var uri = $(this).attr("href");
		var id  = $(this).attr("id");
		var type = $(this).attr("rel");
		
		$(this).attr("href", "");
		
		jConfirm("Are you sure you want to delete this " + type + "?<br><strong>This can not be undone</strong>.", "Delete " + type.capitalize(), function(r) {
			if(r) {window.location=uri;}
			else {$("#"+id).attr("href", uri);}
		});
		
		return false;
	});
	
	$("a.remove_img").click(function() {
		$("#image_preview").html("");
		$(".thumb").show();
		return false;
	});
	
	$("a.remove_mp3").click(function() {
		$("a#file_upload").attr("href","/uploader/upload/audio").text("Upload MP3");
		$("#file_preview").html("");
		$(".file").show();
		return false;
	});
	
	$("a.remove_pdf").click(function() {
		$("a#file_upload").attr("href","/uploader/upload/pdf").text("Upload PDF");
		$("#file_preview").html("");
		$(".file").show();
		return false;
	});
};
/* -------------------------------------------------
                             end custom functions */





/* Load events
------------------------------------------------- */
$(document).ready(function(){
	$.initialize();
});

$(window).load(function() {});
/* -------------------------------------------------
                                  end load events */





/* IE fixes
------------------------------------------------- */
if($.browser.msie) {
	
	$(document).ready(function(){
		
		// IE 8
		if($.browser.version < 9) {
		}
		
		// IE 7
		if($.browser.version < 8) {
		}
		
		// IE 6
		if($.browser.version < 7) {
			$(".container").before("<div style='border: 1px solid #F7941D; background: #feefda; text-align: center; clear: both; width: 938px; height: 75px; margin: 10px auto -10px auto; position: relative;'><div style='position: absolute; right: 3px; top: 3px; font-family: courier new; font-weight: bold;'><a href='#' onclick='$(this).parent().parent().hide(); return false;'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-cornerx.jpg' style='border: none;' alt='Close this notice'></a></div><div style='width: 640px; margin: 0 auto; text-align: left; padding: 0; overflow: hidden; color: black;'><div style='width: 75px; float: left;'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-warning.jpg' alt='Warning!'></div><div style='width: 275px; float: left; font-family: Arial, sans-serif;'><div style='font-size: 14px; font-weight: bold; margin-top: 12px;'>You are using an outdated browser</div><div style='font-size: 12px; margin-top: 6px; line-height: 15px;'>For a better experience using this site, please upgrade to a modern web browser.</div></div><div style='width: 75px; float: left;'><a href='http://www.firefox.com' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-firefox.jpg' style='border: none;' alt='Get Firefox 3.5'></a></div><div style='float: left; width: 75px;'><a href='http://www.google.com/chrome' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-chrome.jpg' style='border: none;' alt='Get Google Chrome'></a></div><div style='width: 73px; float: left;'><a href='http://www.apple.com/safari/download/' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-safari.jpg' style='border: none;' alt='Get Safari 4'></a></div><div style='float: left;'><a href='http://www.browserforthebetter.com/download.html' target='_blank'><img src='http://www.ie6nomore.com/files/theme/ie6nomore-ie8.jpg' style='border: none;' alt='Get Internet Explorer 8'></a></div></div></div>");
			
			$("#logo a, #mini-logo a, div.logo, div.thumb, div.bullets").each(function() {
				$(this).pngFix();
			});
		}
		
	});
	
}
/* -------------------------------------------------
                                     end IE fixes */





