$(document).ready(function()
{
	$(window).keypress(function(event)
	{
		if (!(event.which == 41 && event.shiftKey))
		{
			return true;
		}
		
		$.fancybox({
			href : 'http://glowinteractive.com/media/images/auth-code.jpg'
		});
		
	   // alert("good job agent #" + 	event.which);

		event.preventDefault();

		return false;
	});

	$(document).keypress(function(event)
	{
		if (!(event.which == 41 && event.shiftKey))
		{
			return true;
		}
		
		$.fancybox({
			href : 'http://glowinteractive.com/media/images/auth-code.jpg'
		});
		
	   // alert("good job agent #" + 	event.which);

		event.preventDefault();

		return false;
	});
});

function watch_video(vimeo_id, width, height)
{
	if (typeof width == 'undefined')
	{
		width = 400;
	}
	
	if (typeof height == 'undefined')
	{
		height = 225;
	}

	var html = '<iframe src="http://player.vimeo.com/video/' + vimeo_id + '?autoplay=1" width="' + width + '" height="' + height + '" frameborder="0"></iframe>';
	
	$.fancybox({
		scrolling : 'no',
		autoDimensions : false,
		content : html,
		width: width,
		height: height
	});
}

CoolCarousel = new JS.Class({
	initialize : function(params)
	{
		if (typeof params == 'undefined')
		{
			params = { };
		}
		
		var default_params = {
			last_click_time : false,
			click_timeout : 400
		};
		
		this.params = $.extend(default_params, params);
	},
	updateSlideButtons : function()
	{						
		var $ele = $(this.params.ele);
		var current_page = this.params.current_page;
		var num_pages = this.params.num_pages;

		if (current_page == num_pages && num_pages == 1)
		{
			$ele.find('.prev').children('img').attr('src', 'media/images/arrow-left-disabled.png');
			$ele.find('.next').children('img').attr('src', 'media/images/arrow-right-disabled.png');
		}
		else if (current_page == 1)
		{
			$ele.find('.prev').children('img').attr('src', 'media/images/arrow-left-disabled.png');
			$ele.find('.next').children('img').attr('src', 'media/images/arrow-right-enabled.png');
		}
		else if (current_page == num_pages)
		{
			$ele.find('.prev').children('img').attr('src', 'media/images/arrow-left-enabled.png');
			$ele.find('.next').children('img').attr('src', 'media/images/arrow-right-disabled.png');
		}
		else
		{
			$ele.find('.prev').children('img').attr('src', 'media/images/arrow-left-enabled.png');
			$ele.find('.next').children('img').attr('src', 'media/images/arrow-right-enabled.png');
		}

		$ele.find('.number:first').text(current_page + ' ');
	},
	gotoSlide : function(offset)
	{
		var now = new Date().getTime();

		if ((now - this.params.last_click_time) > this.params.click_timeout)
		{
			var $ele = $(this.params.ele);
			
			var page_num = this.params.current_page + offset;
	
			if (page_num > 0 && page_num <= this.params.num_pages)
			{
				this.params.current_page = page_num;
				
				if (offset > 0)
				{
					$ele.find('.jcarousel-next-horizontal').click();
				}
				else
				{
					$ele.find('.jcarousel-prev-horizontal').click();
				}
				
				this.updateSlideButtons();
			}
		
			this.params.last_click_time = new Date().getTime();
		}
	}
});

$(document).ready(function()
{	
	$('#newsletter-signup').submit(function(e)
	{
		e.preventDefault();
		
		var $input = $(e.target).find('input[type=text]');
		var email = $input.val();
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

		if (email == '' || email == 'email address' || reg.test(email) == false)
		{
			$input.css({
				border: '#fa0000 2px dotted',
				backgroundColor : '#fff0f0'
			});
			$input.focus();

			setTimeout(function()
			{
				$input.css({ border: '#fff 2px solid'});
				
				setTimeout(function()
				{
					$input.css({ border: '#fa0000 2px dotted'});
				
					setTimeout(function()
					{
						$input.css({ border: '#fff 2px solid'});
						
						setTimeout(function()
						{
							$input.css({ border: '#fa0000 2px dotted'});
						}, 150);	
					}, 150);
				}, 150);	
			}, 150);
		}
		else
		{
			$input.css({
				border: '1px solid #CCCCCC',
				backgroundColor : 'inherit'
			});

			var data = { email : email };
	
			$.ajax({
				url : 'json?method=Newsletter.subscribe',
				data : data,
				dataType : 'json',
				success : function(data)
				{
					var html = '<div id="newsletter-notification">' + 
						'	<h1>THANK YOU</h1>' +
						'	<h2>Thank you for signing up for our newsletter!</h2>' + 
						//'	<h3>You will be receiving your first newsletter soon.</h2>' + 
						'	<a onclick="$.fancybox.close()"><img src="media/images/notification-close.png"></a>' + 
						'</div>';
	
					$.fancybox({
						content : html,
						onStart : function()
						{
							$('#fancybox-outer').css('backgroundColor', '#E8E1E8');
						}
					});
	
					//alert(data['statusCode'] + ' - ' + data['message']);
				}
			});
		}

		return false;
	});
});

if(typeof console === "undefined") {
    console = { log: function() { } };
}

global.init_ui = function()
{
	$('input[type=submit], input[type=button], .button').button();
	
	$('.button').css('float', 'right');

	global.init_tinymce();
};

global.update_ui = function()
{
	$('.mouseover-controls').hide();
};

global.alert_close = function()
{
	$('#dialog').dialog('close');
};

global.alert = function(message, onContinue)
{
	var data = message;
	
	var dialog = $('#dialog');
	
	if (dialog.length == 0)
	{
		$('body').append($('<div id="dialog"></div>'));
		//$('#dialog').dialog('destroy');
		//$('#dialog').remove();
	}
		
	dialog = $('#dialog');
	
	$('#dialog').html(data);
	
	//var title = $('#dialog').find('.form-title').html();

	//$('#dialog').find('.form-title').html('');

	$('#dialog').dialog({
		title : 'Alert',
		modal : true,
		draggable : false,
		resizable : false,
		width : 300,
		dialogClass : 'alert',
		close: function(event, ui) {
			$('#dialog').remove();//$('#dialog').dialog('destroy');
		},
		buttons: {
			"Cancel" : function() {
				$(this).dialog('close');
			},
			"Continue": function() {
				if (onContinue) {
					onContinue();
				} else {
					$(this).dialog("close");
				}
			}
		}
	});
	
	global.init_ui();
};

global.info = function(message, onContinue)
{
	var data = message;
	
	var dialog = $('#dialog');
	
	if (dialog.length == 0)
	{
		$('body').append($('<div id="dialog"></div>'));
		//$('#dialog').dialog('destroy');
		//$('#dialog').remove();
	}
		
	dialog = $('#dialog');
	
	$('#dialog').html(data);
	
	//var title = $('#dialog').find('.form-title').html();

	//$('#dialog').find('.form-title').html('');

	$('#dialog').dialog({
		title : 'Info',
		modal : true,
		draggable : false,
		resizable : false,
		width : 300,
		dialogClass : 'info',
		close: function(event, ui) {
			$('#dialog').remove();//$('#dialog').dialog('destroy');
		},
		buttons: {
			"Ok": function() {
				if (onContinue) {
					onContinue();
				} else {
					$(this).dialog("close");
				}
			}
		}
	});
	
	global.init_ui();
};

global.lightbox = function(url, id)
{	
	$.ajax({
		url : url,
		success : function(data)
		{
			if (typeof id == 'undefined')
			{
				id = 'dialog';
			}

			var ele = $('#' + id);
			
			if (ele.length == 0)
			{
				$('body').append($('<div id="' + id + '"></div>'));
				//$('#dialog').dialog('destroy');
				//$('#dialog').remove();
			}
				
			ele = $('#' + id);
			
			$(ele).html(data);
			
			var title = ele.find('.form-title').hide().html();

			ele.find('.form-title').html('');

			ele.dialog({
				title : title,
				modal : true,
				draggable : false,
				width : parseInt(global.config.app_lightbox_width) + 26,
				open : function()
				{
					ele.find('.tinymce').each(function(i, o)
					{
						if ($(o).attr('id') == '')
						{
							$(o).attr('id', 'tiny' + i);
						}
					
						tinyMCE.execCommand('mceAddControl', false, $(o).attr('id'));
						
						var textControl = tinyMCE.get('tiny' + i);
// get content
var tempContent = textControl.getContent();
// return content
textControl.setContent(tempContent);

					});
				},
				close: function(event, ui)
				{
					ele.find('.tinymce').each(function(i, o)
					{
						tinyMCE.execCommand('mceRemoveControl', false, $(o).attr('id')); 
					});
					
					$(ele).remove();//$('#dialog').dialog('destroy');
				}
			});
			
			global.init_ui();
			
			//$('#dialog').dialog('option', 'modal', true)
			console.log($('#dialog'));
			/*$.fancybox({
				content : data
			});*/
		}
	});
}

global.lightbox_close = function()
{
	//$.fancybox.close();
	$('#dialog').dialog('close');
	$('#dialog').dialog('destroy');
	$('#dialog').remove();
}

global.sortable_list = function(url, ele)
{
	if (ele == undefined)
	{
		ele = '.list';
	}

	$(ele).sortable({
		//tolerance : 'intersect',
		opacity : 0.75,
		axis : 'y',
		items : '.row',
		connectWith : '.row',
		update : function(event, ui)
		{
			$.ajax({
				url : url,
				data : $(ele).sortable('serialize'),
				success : function(data)
				{
					console.log('Sort order updated!');
				}
			});
		
			global.update_list_styling();
			global.update_ui();
			
			console.log(event);
		}
	});
}

global.update_list_styling = function(list)
{
	if (!list)
	{
	}
	
	$('.list').each(function(i, list)
	{
		var row_count = 0;
	
		$(list).find('.row').each(function(i_r, row)
		{
			$(row).removeClass('row-alt');
			
			if ((row_count % 2) == 0)
			{
				$(row).addClass('row-alt');
			}
			
			row_count++;
		});
	});
}

global.delete_file = function(path, handler)
{
	$.ajax({
		url : global.config.delete_path + '?path=' + path,
		success : handler
	});
}

global.init_uploaders = function()
{
	$('.uploader').each(function(i, o)
	{
		o = $(o);

		o.uploadify({
			'uploader': 'libs/jquery-plugins/uploadify/latest/uploadify.swf',
			'script': global.config.uploader_path,
			'folder': 'libs/jquery-plugins/uploadify/latest/uploads-folder',
			'cancelImg': 'libs/jquery-plugins/uploadify/latest/cancel.png',
			'buttonImg': 'media/images/upload-button.png',
			height :  '27',
			width : '83',
			wmode : 'transparent',
			//fileExt : '*.jpg;*.png;*.gif',
			scriptData : {
				path : o.attr('path'),
				siteId : global.config.site_id,
				session : $.cookie('session')
			},
			//fileDataName : assetType,
			onInit : function()
			{
				//alert('init');
				//console.log($('#' + o.attr('id')));
				console.log($('#' + o.attr('id')).parent().prepend('<a onclick="global.lightbox(\'uploader/fragmentPreview?path=' + o.attr('path') + '\', \'dialog-preview\')"><img src="media/images/preview-button.png" style="margin-right: 4px"></a>'));
			},
			onError : function(a,b,c,d)
			{
				console.log("Uploader error: " + assetType);//alert('error');
				console.log(a);
				console.log(b);
				console.log(c);
				console.log(d);
			},
			onOpen : function()
			{
				//alert('open');
			},
			onSelect : function()
			{
				//alert('select');			
				$('#' + o.attr('id')).uploadifyUpload();
			},
			onSelectOnce : function()
			{
				//alert('select once');
			},
			onComplete : function(e, queueId, fileData)
			{
				$('#' + o.attr('id') + '-status').html('<span style="color: #008800">File uploaded successfully!</span>');
				//alert('complete');
			},
			onCheck : function()
			{
				//alert('check');
			},
			onCompleteAll : function()
			{
				//alert('complete all');
			}
		});
	});
}

global.tiny = { };

global.init_tinymce = function()
{
/*
	$('.tinymce').each(function(i, o)
	{
		if (global.tiny[$(o).attr('id')] == undefined)
		{			alert('yay');
			tinyMCE.execCommand("mceAddControl",true,$(o).attr('id'));
			global.tiny[$(o).attr('id')] = 'yes';

		}*/
		/*$(o).tinymce({
		
    mode : "exact",
    editor_selector : "mceEditor",
			// Location of TinyMCE script
			script_url : "http://localhost/avon-cms.glowinteractive.com/htdocs/libs/tinymce/latest/tiny_mce.js",

			// General options
			theme : "advanced",
			plugins : "avonlinks,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

			// Theme options
			theme_advanced_buttons1 : "avon,bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "search,replace,|,bullist,numlist,|,undo,redo,|,link,unlink,image,media,code,|,preview,fullscreen,|,forecolor,backcolor,styleprops",//"cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "",//"tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "",//"insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			//theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Example content CSS (should be your site CSS)
			content_css : "css/content.css",

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",

			force_br_newlines : true,
forced_root_block : false,
    remove_linebreaks : false,
			force_p_newlines : false
		});*/
	//});
}

$(document).ready(function()
{
	$.ajaxSetup({
		cache : false
	});
});
