/*
custom object orientated javascript built alongside Jquery. 
Jquery 3.1.6 is required at least for this to work.

/*begin `balloon` class*/

var api, aspect_ratio = 0.746, initial_slider_pos = 33;

jQuery.b = {
	
	VR:'0.7.6 281009',
	Developer:'David Mackintosh',
	
	orientation:function(opt) {
		opt = eval(opt);
		switch (opt.read) {
		case 'write':
			$('#orientation').html(opt.or);
			$('#shape').html(opt);
			$('#ccrop').html(opt.or);
		break;

		case 'read':
		default:
			return $('#orientation').html();
		break;
		}
	}, //function orientation
	
	design:function(opt) {
		opt = eval(opt);
		switch (opt.read) {
		case 'write':
			$('#design').html(opt.obj.attr('id'));
			$('#cdes').html(opt.obj.attr('id'));
		break;

		case 'read':
		default:
			return $('#design').html();
		break;
		}
	}, //function design
	
	slide:function(opt) {
		opt = eval(opt);
		opt.dir = (opt.dir == 'left') ? '-': ''; //if its left it has to animate minus figures;
		$('#appC').animate({
			left: opt.dir + opt.pos
		},
		600);//dont you just love Jquery
		$.b.uTop();
	}, //function slide
	
	checkUploadField: function(){
		if($('#fakeField').val()){
			window.file =true;
			$('.relative').removeClass('relative').addClass('tempRel');
			$('#er').show().css({left:940}).html('<p class="clear" style="margin-top:250px; margin-left:40px; text-align:center;">Loading time is normally 10 secs-2 mins. However it can be longer and is determined by your connection and photo-size.</p><img class="loading" src="/_images/application/loading.gif" style="margin-left:440px;" />');
		}
		else{
			return false;
		}
	},
	
	cread:function(opt) {
		opt = eval(opt);
		$.post('/_core/cookies.app.php', {
			action: 'read',
			name: 'nuser'
		},
		function(data) {
			if (data == 'set') {
				$('#appC').animate({
					top: 300
				});
			} else {
				$.post('/_core/cookies.app.php', {
					action: 'set',
					name: 'nuser'
				});
			} //else
		}); //post
	}, //cookies
	
	uTop:function(){
		$('.topbanner').html('<img src="/_images/ex1.png" alt="" /><img src="/_images/ex2.png" alt="" /><img src="/_images/ex3.png" alt="" /><img src="/_images/ex4.png" alt="" />');
		$('#app').css({top:30});
		$('.footer').css({marginTop:30});
	},
	
	cCropD:function(opt){
		
		opt=eval(opt);		
		var max_w = $('#imgPhoto').width(); 
		var max_h = $('#imgPhoto').height();
		if ($('#orientation').html() == 'portrait')
		{
			var new_h = Math.round(window.initial_slider_pos / 100 * max_h);
			var new_w = Math.round(new_h * window.aspect_ratio);
			//var setS =[ 104, 102, 169, 192 ];
		} 
		else
		{
			if($('#orientation').html() == 'landscape')
			{
				var new_w = Math.round(window.initial_slider_pos / 100 * max_w);
				var new_h = Math.round(new_w * window.aspect_ratio);
				//var setS =[ 92, 114, 192, 179 ];
			}
			else
			{
				window.aspect_ratio = 1;
				var new_w = Math.round(window.initial_slider_pos / 100 * max_w);
				var new_h = Math.round(new_w);
				//var setS =[ 92, 114, 192, 179 ];
			}
		}		
		
		var x1 = Math.floor( ( max_w - new_w ) / 2);
		var y1 = Math.floor( ( max_h - new_h ) / 2);
		var x2 = x1 + new_w;
		var y2 = y1 + new_h;
		var setS =[ x1, y1, x2, y2 ];
		
		window.api = $.Jcrop('#imgPhoto',{
	            bgOpacity:   .4,
	            setSelect:   setS,
	            onChange: function (c){
								$('#x').html(c.x);
								$('#y').html(c.y);
								$('#x2').html(c.x2);
								$('#y2').html(c.y2);
								$('#w').html(c.w);
								$('#h').html(c.h);
							},
				onSelect: function (c){
								$('#x').html(c.x);
								$('#y').html(c.y);
								$('#x2').html(c.x2);
								$('#y2').html(c.y2);
								$('#w').html(c.w);
								$('#h').html(c.h);
							},
				allowResize: false,
				allowSelect: false,
				drawBorders: false,
				addClass:'background-shape'
		});

	},
	
	Errors:function(opt){
		opt=eval(opt);
		if(!opt.pos){opt.pos='0';}
		$('#er').css({left:(960*opt.pos)}).fadeIn('slow').html('<div>'+opt.msg+'</div>');
	},
		
	save:function(cb){
		$.post(
			   '/_core/_func/images.php',
			   {
				   action:'save',
				   shape:$('#shape').html(),
				   orientation:$('#orientation').html(),
				   design:$('#design').html(),
				   id:$('#pid').html(),
				   x:$('#x').html(),
				   y:$('#y').html(),
				   w:$('#w').html(),
				   h:$('#h').html()
			   },
			   function(data){
					return cb(data);
				}//function(data)
		);//post
	},//save
	
	updateView:function(id,warnWhat,wi,he){
		$('#iw',window.parent.document).html(wi);
		$('#ih',window.parent.document).html(he);
		//$('#imgPhoto',window.parent.document).attr({src:'/_images/pictures/'+id+'/orig.png'});
		$('#pid',window.parent.document).html(id);
	
		var w = $('#imgPhoto',window.parent.document);
		
		if(wi > he){
			w.width(273);
		}
		else if(he > wi){
			w.height(273);
		}
		else{
			w.width(273);
			w.height(273);
		}
		
		$($('#imgPhoto',window.parent.document).attr({src:'/_images/pictures/'+id+'/orig.png'})).load(function(){
	
			if(!warnWhat){
				$('#er',window.parent.document).hide();
			}
			else{
				$('#er .close',window.parent.document).hide();
				$('#er .loading, #er .clear',window.parent.document).hide();
				$('#er',window.parent.document).append('<div class="Warning">Print quality is defined by the image uploaded.<br clear="all" /><br clear="all" />Enlarged to the size of the balloon the print sharpness may be reduced.<br clear="all" /><p style="font-size:11px;">For more information please see FAQ\'s</p><br clear="all" /><img onclick="$(\'#er\').hide();" src="/_images/application/APPROVE.png" /><img src="/_images/browseagain.png" onclick="$.b.reBrowse();" class="rb" /></div>');
			}
			return true;
		});
	
	},
	
	reBrowse:function (){
		$('#home_page #wholePage .prev',window.parent.document).attr({src:'#'});
		$('#er .close',window.parent.document).hide();
		$('#er .loading, #er .clear',window.parent.document).hide();
		$('#er').hide();
		$('#imgPhoto').attr({'src':'/_images/application/default.jpg'}).css({'height':292,'width':273});
		$('#fileU,#fakeField').val('');
	},

	check_cart_status:function(){
			$.post(
					'/_includes/cart/todo.php',
					{name:'cart'},
					function(data){
						$('.todopoints').html(data);
					}//function
			);//post
	},

	GreenTick:function(where){
		$('#greentick_'+where).attr({src:'/_images/shoppingcart/greentick.jpg'});
		return true;
	},
	
	ieseven:function(){
		//
		
		/*var $source = $("#crop-shape-tool").attr("src");
		var $styles = $("#crop-shape-tool").attr("style");
		$('#crop-shape-tool').remove()
		$('.jcrop-tracker')
		.html($('<div />')
				.attr({'id':'crop-shape-tool'})
				.css({
					'backgroundImage': 'url(' + $source +')',
					'backgroundRepeat': 'no-repeat',
					'backgroundPosition': 'top left',
					'display':'block',
					'opacity':1
				}));
		alert($('.jcrop-tracker').html());*/
	}
	
}; //b
