var base_url = 'http://gtechprinting.com/';
var hero_rotate;

var home = {
	init: function() {
		$('#virtual_tour').click(function() {
			flowplayer("player", "http://releases.flowplayer.org/swf/flowplayer.commercial-3.1.5.swf", {
				key: '#@5e4561882e2a8aefeb2'
			});
			$('#overlay').fadeIn();
			$('#video_overlay').fadeIn();
			
			return false;
		});
		
		$('#virtual_tour2').click(function() {
			flowplayer("player2", "http://releases.flowplayer.org/swf/flowplayer.commercial-3.1.5.swf", {
				key: '#@5e4561882e2a8aefeb2'
			});
			$('#overlay').fadeIn();
			$('#video_overlay2').fadeIn();
			
			return false;
		});
		
		$('#video_overlay .close_button, #video_overlay2 .close_button').click(function() {
			$('#video_overlay, #video_overlay2').fadeOut();
			$('#overlay').fadeOut();
			return false;
		});
	}
}

var gallery = {
	//count: -4,
	count: 0,
	left_limit: null,
	init: function() {
		if ($('body').hasClass('login')) return false;
		if ($('body').hasClass('cms')) return false;
			
		gallery.left_limit = ($('#carousel_inner img').length - 4)  * -1;
		
		//$('#carousel_inner').animate({'left':'-=' + 596}, 'slow');

		$('#home_gallery_right_arrow').click(function(e) {
			if (gallery.count == gallery.left_limit) return false;
			$('#carousel_inner').animate({'left':'-=' + 149}, 'medium');
			gallery.count -= 1;
			e.preventDefault();
		});

		$('#home_gallery_left_arrow').click(function(e) {
			if (gallery.count == 0) return false;
			$('#carousel_inner').animate({'left':'+=' + 149}, 'medium');
			gallery.count += 1;
			e.preventDefault();
		});
	}
}

var license = {
	init: function() {
		$('#license_overlay .close_button').click(function(e) {
			$('#license_overlay').fadeOut('medium');
			e.preventDefault();
		});
	}
}

var sort = {
	advanced: 0,
	init: function() {
		$('#sort_link').click(function(e) {
			if (sort.advanced == 0) {
				$('#advanced_sort').show('blind', {direction: 'vertical', duration: 400});
				$('#sort_link').text('return to basic sort');
				sort.advanced = 1;
			} else {
				$('#advanced_sort').hide('blind', {direction: 'vertical', duration: 400});
				$('#sort_link').text('advanced sort');
				sort.advanced = 0;
			}
			e.preventDefault();
		});
		
		$('#price_sort_container').hide();
		
		$('#price_region_sort').change(function() {
			if ($("#price_region_sort option:selected").val() == 'null') {
				$('#price_sort_container').hide('fast', function() {
					$('#price_sort').html('');
				});
			} else {
				$.post(base_url+'index.php/main/getPricesInRegion', {id: $("#price_region_sort option:selected").val()}, function(data){
					$('#price_sort').html(data);
					$('#price_sort_container').fadeIn('fast');
				});
			}
		});
		
		$('#customer_sort_container').hide();
		
		$('#customer_region_sort').change(function() {
			if ($("#customer_region_sort option:selected").val() == 'null') {
				$('#customer_sort_container').hide('fast', function() {
					$('#customer_sort').html('');
				});
			} else {
				$.post(base_url+'index.php/main/getCustomersInRegion', {id: $("#customer_region_sort option:selected").val()}, function(data){
					$('#customer_sort').html(data);
					$('#customer_sort_container').fadeIn('fast');
				});
			}
		});
	}
}

var hero = {
	init: function() {
		$('#hero_thumbs').sortable({ 
			handle: $('.hero_handle'),
			update: function() {
				var data = $('#hero_thumbs').sortable('serialize');
				$.post(base_url+'index.php/admin/reorderHero', data, function(data) {
					hero.update_display(data)
				});
			}
		});
		
		$('#add_hero').click(function() {
			$('#overlay').fadeIn();
			$('#add_hero_form').fadeIn();
			return false;
		});
		
		$('#add_hero_form .close_button').click(function() {
			$('#add_hero_form').fadeOut('medium');
			$('#overlay').fadeOut('medium');
			return false;
		});
	},
	update_display: function() {
		clearInterval(hero_rotate);
		
		$.get(base_url+'index.php/admin/getOrder', function(data) {
			var images = data.split(',');
			var i = 0;
			
			$('#hero').css('backgroundImage', 'url('+base_url+'images/hero/'+images[0]+')');
			
			hero_rotate = setInterval(function() {
				$('<img />')
				.attr('src', base_url+'images/hero/'+images[i])
				.load(function() {
					$('#hero').fadeOut('slow', function() {
						$('#hero').css('backgroundImage', 'url('+base_url+'images/hero/'+images[i]+')');
						$('#hero').fadeIn('slow');
					});
				});
				(i == images.length-1) ? i = 0 : i++;
			}, 4000);
		});
	}
}

var game = {
	init: function() {
		$('#add_game_form #customer_container').hide();
		$('#add_game_form #price_container').hide();
		
		$('#region').change(function() {
			if ($("#region option:selected").val() == 'null') {
				$('#customer_container').hide('fast', function() {
					$('#customer').html('');
				});
			} else {
				$.post(base_url+'index.php/admin/getCustomersInRegion', {id: $("#region option:selected").val()}, function(data){
					$('#customer').html(data);
					$('#customer_container').show('fast');
				});
			}
			$('#price_container').hide('fast', function() {
				$('#price').html('');
			});
		});
		
		$('#customer').change(function() {
			if ($("#customer option:selected").val() == 'null') {
				$('#price_container').hide('fast', function() {
					$('#price').html('');
				});
			} else {
				$.post(base_url+'index.php/admin/getPricesInRegion', {id: $("#region option:selected").val()}, function(data){
					$('#price').html(data);
					$('#price_container').show('fast');
				});
			}
		});
		
		// ajax upload for full size image
		if ($('#fullsize_img1').length > 0) {
			new AjaxUpload('#fullsize_img1', {
				action: base_url+'index.php/admin/addAjaxImage',
				name: 'upload_img',
				onChange: function() {
					$('#jcrop_loading').text('loading...');
				},
				onComplete : function(file) {
					$('#jcrop_loading').text('');
				
					var path = base_url+'images/games/'+file;
				
					$('#fullsize_preview1').html('<img style="width: 475px" src="'+path+'" alt="" />');
					$('#fullsize_file_name1').val(file);
					
					$('#large_preview1').html('<p><label>Large Image: </label></p><img style="width: 380px" src="'+path+'" alt="" />');
					$('#large_file_name1').val('380_'+file);
					
					$('#small_preview1').html('<div style="width: 200px; height: 100px; overflow: hidden"><img style="width: 200px" src="'+path+'" alt="" /></div>');
					$('#small_file_name1').val('200_'+file);
					
					$('#scroll_preview1').html('<div style="width: 125px; height: 125px; overflow: hidden"><img style="height: 125px" src="'+path+'" alt="" /></div>');
					$('#scroll_file_name1').val('125_'+file);
				}
			});
		}
		
		// ajax upload and jcrop for 125 size
		if ($('#scroll_img1').length > 0) {
			new AjaxUpload('#scroll_img1', {
				action: base_url+'index.php/admin/addAjaxImage',
				name: 'upload_img',
				onChange: function() {
					$('#jcrop_loading').text('loading...');
				},
				onComplete : function(file) {
					var path = base_url+'images/games/'+file;
					$('#file_name').val(path);
				
					var d = new Date();
					var t = d.getTime();
					$('<img />')
						.attr('src', path+'?t='+t)
						.load(function() {
							$('#jcrop_loading').text('');
						
							var img2 = $(this).clone();
						
							$('#jcrop_container p:first').append($(this));
							$('#jcrop_container div:first').append(img2);
							$('#jcrop_container div:first').css('width', 125);
							$('#jcrop_container div:first').css('height', 125);
						
							$('#jcrop_container h3:first').text('Fullsize Image');
							$('#jcrop_container h3:last').text('Scrolling Image');
							$('#game_overlay form:first').attr('id', 'jcrop_form_125');
						
							$('#game_overlay').show();

							$('#game_overlay').css('width', $('#jcrop_container img:first').width());
							$('#game_overlay').css('marginLeft', -$('#jcrop_container img:first').width()/2);
						
							$('#game_overlay').hide();

							$('#jcrop_container img:first').Jcrop({
								onChange: showPreview125,
								onSelect: showPreview125,
								aspectRatio: 1,
								minSize: [125,125]
							});
						
							$('#overlay').fadeIn();
							$('#game_overlay').fadeIn();
						});
				
					$('#jcrop_form_125').live('submit', function() {
						$.post(base_url+'index.php/admin/resizeImage/125', $(this).serialize(), function(data) {
							var d = new Date();
							var t = d.getTime();
							$('<img />')
								.attr('src', base_url+'images/games/125_'+file+'?t='+t)
								.load(function() {
									$('#scroll_preview1').html($(this));
									$('#scroll_file_name1').val('125_'+file);
								});
	
							$('#game_overlay').fadeOut(function() {
								$('#jcrop_container').html('<h3></h3><p></p><br /><h3></h3><div style="overflow: hidden;"></div>');
								$('#file_name').val(file);
								$('#x1').val('');
								$('#y1').val('');
								$('#x2').val('');
								$('#y2').val('');
								$('#w').val('');
								$('#h').val('');
							});
							$('#overlay').fadeOut();
						});
					
						return false;
					});
				}
			});
		}
		// end 125 size
		
		// ajax upload and jcrop for 200 size
		if ($('#small_img1').length > 0) {
				new AjaxUpload('#small_img1', {
				action: base_url+'index.php/admin/addAjaxImage',
				name: 'upload_img',
				onChange: function() {
					$('#jcrop_loading').text('loading...');
				},
				onComplete : function(file) {
					var path = base_url+'images/games/'+file;
					$('#file_name').val(path);
			
					var d = new Date();
					var t = d.getTime();
					$('<img />')
						.attr('src', path+'?t='+t)
						.load(function() {
							$('#jcrop_loading').text('');
					
							var img2 = $(this).clone();
					
							$('#jcrop_container p:first').append($(this));
							$('#jcrop_container div:first').append(img2);
							$('#jcrop_container div:first').css('width', 200);
							$('#jcrop_container div:first').css('height', 100);
					
							$('#jcrop_container h3:first').text('Fullsize Image');
							$('#jcrop_container h3:last').text('Small Image');
							$('#game_overlay form:first').attr('id', 'jcrop_form_200');
					
							$('#game_overlay').show();

							$('#game_overlay').css('width', $('#jcrop_container img:first').width());
							$('#game_overlay').css('marginLeft', -$('#jcrop_container img:first').width()/2);
					
							$('#game_overlay').hide();

							$('#jcrop_container img:first').Jcrop({
								onChange: showPreview200,
								onSelect: showPreview200,
								aspectRatio: 2/1,
								minSize: [200,100]
							});
					
							$('#overlay').fadeIn();
							$('#game_overlay').fadeIn();
						});
			
					$('#jcrop_form_200').live('submit', function() {
						$.post(base_url+'index.php/admin/resizeImage/200/100', $(this).serialize(), function(data) {
							var d = new Date();
							var t = d.getTime();
							$('<img />')
								.attr('src', base_url+'images/games/200_'+file+'?t='+t)
								.load(function() {
									$('#small_preview1').html($(this));
									$('#small_file_name1').val('200_'+file);
								});

							$('#game_overlay').fadeOut(function() {
								$('#jcrop_container').html('<h3></h3><p></p><br /><h3></h3><div style="overflow: hidden;"></div>');
								$('#file_name').val(file);
								$('#x1').val('');
								$('#y1').val('');
								$('#x2').val('');
								$('#y2').val('');
								$('#w').val('');
								$('#h').val('');
							});
							$('#overlay').fadeOut();
						});
				
						return false;
					});
				}
			});
		}
		// end 200 size
		
		$('#game_overlay .close_button').click(function() {
			$('#game_overlay').fadeOut(function() {
				$('#jcrop_container').html('<h3></h3><p></p><br /><h3></h3><div style="overflow: hidden;"></div>');
				$('#file_name').val('');
				$('#x1').val('');
				$('#y1').val('');
				$('#x2').val('');
				$('#y2').val('');
				$('#w').val('');
				$('#h').val('');
			});
			$('#overlay').fadeOut();
			return false;
		});
		
		var count = 2;
		$('#add_image').click(function() {
			$('#option_images_container').append('<br /><h3><a href="#">Image '+count+'</a></h3><div class="optional_image"><p><label for="fullsize_img'+count+'">Fullsize Image: </label><input type="file" id="fullsize_img'+count+'" name="fullsize_img'+count+'" /></p><p id="fullsize_preview'+count+'"></p><input type="hidden" id="fullsize_file_name'+count+'" name="fullsize_file_name'+count+'" value="" /><p><label for="small_img'+count+'">Small Image: </label><input type="file" id="small_img'+count+'" name="small_img'+count+'" /></p><p id="small_preview'+count+'"></p><input type="hidden" id="small_file_name'+count+'" name="small_file_name'+count+'" value="" />');
			$('#game_right div:last').hide().show();
			
			var p = $('#fullsize_preview'+count);
			var n = $('#fullsize_file_name'+count);
			var p2 = $('#small_preview'+count);
			var n2 = $('#small_file_name'+count);
			// ajax upload for optional full size image
			if ($('#fullsize_img'+count).length > 0) {
				new AjaxUpload('#fullsize_img'+count, {
					action: base_url+'index.php/admin/addAjaxImageOptional',
					name: 'upload_img',
					onChange: function() {
						$('#jcrop_loading').text('loading...');
					},
					onComplete : function(file) {
						$('#jcrop_loading').text('');

						var path = base_url+'images/games/'+file;
 
						p.html('<img style="width: 475px" src="'+path+'" alt="" />');
						n.val(file);
						
						p2.html('<div style="width: 200px; height: 100px; overflow: hidden"><img style="width: 200px" src="'+path+'" alt="" /></div>');
						n2.val('200_'+file);
					}
				});
			}
			
			// ajax upload and jcrop for optional 200 size
			var m = $('#small_preview'+count);
			var o = $('#small_file_name'+count);
			if ($('#small_img'+count).length > 0) {
				new AjaxUpload('#small_img'+count, {
					action: base_url+'index.php/admin/addAjaxImageOptional',
					name: 'upload_img',
					onChange: function() {
						$('#jcrop_loading').text('loading...');
					},
					onComplete : function(file) {
						var path = base_url+'images/games/'+file;
						$('#file_name').val(path);

						var d = new Date();
						var t = d.getTime();
						$('<img />')
							.attr('src', path+'?t='+t)
							.load(function() {
								$('#jcrop_loading').text('');

								var img2 = $(this).clone();

								$('#jcrop_container p:first').append($(this));
								$('#jcrop_container div:first').append(img2);
								$('#jcrop_container div:first').css('width', 200);
								$('#jcrop_container div:first').css('height', 100);

								$('#jcrop_container h3:first').text('Fullsize Image');
								$('#jcrop_container h3:last').text('Small Image');
								$('#game_overlay form:first').attr('id', 'jcrop_form_380_'+count);

								$('#game_overlay').show();

								$('#game_overlay').css('width', $('#jcrop_container img:first').width());
								$('#game_overlay').css('marginLeft', -$('#jcrop_container img:first').width()/2);

								$('#game_overlay').hide();

								$('#jcrop_container img:first').Jcrop({
									onChange: showPreview200,
									onSelect: showPreview200,
									aspectRatio: 2/1,
									minSize: [200,100]
								});

								$('#overlay').fadeIn();
								$('#game_overlay').fadeIn();
							});
								
						$('#jcrop_form_380_'+count).live('submit', function() {
							$.post(base_url+'index.php/admin/resizeImage/200/100', $(this).serialize(), function(data) {
								var d = new Date();
								var t = d.getTime();
								$('<img />')
									.attr('src', base_url+'images/games/200_'+file+'?t='+t)
									.load(function() {
										m.html($(this));
										o.val('200_'+file);
									});

								$('#game_overlay').fadeOut(function() {
									$('#jcrop_container').html('<h3></h3><p></p><br /><h3></h3><div style="overflow: hidden;"></div>');
									$('#file_name').val(file);
									$('#x1').val('');
									$('#y1').val('');
									$('#x2').val('');
									$('#y2').val('');
									$('#w').val('');
									$('#h').val('');
								});
								$('#overlay').fadeOut();
							});

							return false;
						});
					}
				});
			}
			// end 380 size
			
			
			count++;
		});
		
		$('#add_game_form').submit(function() {
			var valid = true;
			
			if ($('#region').val() == 'null') {
				valid = false;
			}
			
			if ($('#customer').val() == 'null') {
				valid = false;
			}
			
			if ($('#price').val() == 'null') {
				valid = false;
			}
			
			if (valid) {
				return true;
			} else {
				alert('Please fill out all required (*) fields!');
				return false;
			}
		});
		
		// zoom
		$('.zoom_image').click(function() {
			var path = $(this).attr('href');
			
			$('<img />')
				.attr('src', path)
				.load(function() {
					$('#zoom_overlay').prepend($(this));
					
					$('#zoom_overlay').show();

					$('#zoom_overlay').css('width', $(this).width());
					$('#zoom_overlay').css('marginLeft', -$(this).width()/2);

					$('#zoom_overlay').hide();
					
					$('#overlay').fadeIn();
					$('#zoom_overlay').fadeIn();
				});
			
			return false;
		});
		
		$('#zoom_overlay .close_button').click(function() {
			$('#zoom_overlay').fadeOut(function() {
				$('#zoom_overlay img:first').remove();
			});
			$('#overlay').fadeOut();
			return false;
		});
	}
}

function showPreview125(coords) {
	var rx = 125 / coords.w;
	var ry = 125 / coords.h;

	$('#jcrop_container img:last').css({
		width: Math.round(rx * $('#jcrop_container img:first').width()) + 'px',
		height: Math.round(ry * $('#jcrop_container img:first').height()) + 'px',
		marginLeft: '-' + Math.round(rx * coords.x) + 'px',
		marginTop: '-' + Math.round(ry * coords.y) + 'px'
	});
	
	$('#x1').val(coords.x);
	$('#y1').val(coords.y);
	$('#x2').val(coords.x2);
	$('#y2').val(coords.y2);
	$('#w').val(coords.w);
	$('#h').val(coords.h);
}

function showPreview200(coords) {
	var rx = 200 / coords.w;
	var ry = 100 / coords.h;

	$('#jcrop_container img:last').css({
		width: Math.round(rx * $('#jcrop_container img:first').width()) + 'px',
		height: Math.round(ry * $('#jcrop_container img:first').height()) + 'px',
		marginLeft: '-' + Math.round(rx * coords.x) + 'px',
		marginTop: '-' + Math.round(ry * coords.y) + 'px'
	});
	
	$('#x1').val(coords.x);
	$('#y1').val(coords.y);
	$('#x2').val(coords.x2);
	$('#y2').val(coords.y2);
	$('#w').val(coords.w);
	$('#h').val(coords.h);
}

var news = {
	init: function() {
		$('p.archive_year').nextUntil('p').hide();
		$('p.archive_year:first').nextUntil('p').show();
		
		$('p.archive_year a').css('background-image', 'url('+base_url+'images/news_arrow2.gif)');
		$('p.archive_year:first a').css('background-image', 'url('+base_url+'images/news_arrow.gif)');
		
		$('p.archive_year a').toggle(function() {
			$(this).css('background-image', 'url('+base_url+'images/news_arrow.gif)');
			$(this).parent().nextUntil('p').fadeIn();
		}, function() {
			$(this).css('background-image', 'url('+base_url+'images/news_arrow2.gif)');
			$(this).parent().nextUntil('p').fadeOut();
		});
		
		$('p.archive_year:first a').toggle(function() {
			$(this).css('background-image', 'url('+base_url+'images/news_arrow2.gif)');
			$(this).parent().nextUntil('p').fadeOut();
		}, function() {
			$(this).css('background-image', 'url('+base_url+'images/news_arrow.gif)');
			$(this).parent().nextUntil('p').fadeIn();
		});
	}
}

product_innovation = {
	init: function() {
		$('.innovation_thumb a').click(function() {
			
			$('html, body').animate({
				scrollTop: 0
			}, 100);
			
			var path = 'http://gtechprinting.com/index.php/product_innovation/'+$(this).attr('href');
			
			$('#innovation_overlay_content').load(path, function() {
				$('#overlay').fadeIn();
				$('#innovation_overlay').fadeIn();
			});
			
			return false;
		});
		
		$('#innovation_overlay .close_button').click(function() {
			$('#innovation_overlay').fadeOut(function() {
				$('#innovation_overlay_content').html('');
			});
			$('#overlay').fadeOut();
			return false;
		});
	}
}

$(document).ready(function() {
	home.init();
	gallery.init();
	//license.init();
	sort.init();
	hero.init();
	game.init();
	news.init();
	product_innovation.init();
	
	$('#header_search').focus(function() {
		$(this).attr('value', '');
	});
	
	$('.datepicker').datepicker();
});

