// COPYRIGHT 2008 MagicLamp Networks & PosePrints.com -- ALL RIGHTS RESERVED
// ver 2008.11.17
var src



$(document).ready(function() {
	//$('#dialog').jqm().jqmShow();
	
	$(window).load(function() { 
	//$('#dialog').jqmHide(); 
	
	});
	
	
	if ($.browser.msie && jQuery.browser.version=='6.0'){
	$('#posebuilder').ajaxComplete(function(){
	//alert('complete');
	$('#posebuilder img[@src$=.png]').ifixpng();
	});
	
	}
	
	$('#dialogcart').jqm({modal: false});
	$('#dialogpress').jqm({modal: false});





	//
	// model1: woman
	// model2: man
	// model3: little boy
	// model4: little girl
	// model5: silhouette
	// model6: baby
  
  //$(this).oneTime("120s", function(){	$('#dialog').jqmHide();	});

  if (PoseMode=='Preview' || PoseMode == 'Email' || PoseMode == 'ViewPoseCard' || PoseMode == 'CartPreview'){
  

	$.get('/pose.asp',{pose:PoseID,nt: (new Date().getTime())},function(poseHTML){
		$('#posebuilder').html();
		$('#posecontainer').append(poseHTML);
		//$('#posecontainer img[@src$=.png]').ifixpng();
		
		switch (ProductType)
		{
		case "1":

			$('.model2').css({position:'absolute',top:'11px',left:'200px'});
			$('.model1').css({position:'absolute',top:'12px',left:'195px'});
			$('.model3').css({position:'absolute',top:'14px',left:'175px'});
			$('.model4').css({position:'absolute',top:'12px',left:'175px'});
			$('.model5').css({position:'absolute',top:'12px',left:'200px'});
		  	if ($('.model6').is('div')){
				$('.asset').attr({height:'745',width:'745'});
				$('#posecontainer').css({height:'745px'});
				$('.model6').css({position:'absolute',top:'-157px',left:'40px'});
			}
			$('#punch-h').css({position:'absolute',top:'50px',left:'48px',zIndex:'2000',height:'452px',width:'552px',background:'url(/webimg/punch-h-drop.png) no-repeat'});
			$('#punch-magnet').css({position:'absolute',top:'49px',left:'47px',zIndex:'2000',height:'452px',width:'552px',background:'url(/webimg/punch-magnet.png) no-repeat'});
			$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});
			break;
		
		case "2": //notepads
			
			$('#posecontainer').css({position:'absolute',top:'170px'});
			$('#footer').css('margin','150px 0 0 0');
			$('#SubTitleRow').hide();
			$('#MessageRow').hide();
			
			if (FirstPerson =='' || FirstPerson==0) {
				$('.model2').css({position:'absolute',top:'30px',left:'25px'});
				$('.model1').css({position:'absolute',top:'22px',left:'50px'});
				$('.model3').css({position:'absolute',top:'24px',left:'32px',zIndex:'1550'});
				$('.model4').css({position:'absolute',top:'20px',left:'32px'});
				$('.model5').css({position:'absolute',top:'22px',left:'60px'});
				$('.asset').attr('src',function(src){
				    src = buildIMGURL($(this).attr('src'),'/img/86percent-')
				    $(this).attr('src',src);
				});
				$('.asset').attr({height:'361',width:'361'});
				$('h1.Template').css({position: 'absolute',top: '20px',width:'358px',textAlign:'center'})
				$('#dialog').css('height','550px');
			}
					
			break;
		
		
		case "3": //business cards
			$('#posecontainer').css({position:'absolute',top:'45px',height:'292px',overflow:'hidden'});
			$('.model1').css({position:'absolute',top:'0px',left:'220px'});
			$('.asset').attr('src',function(src){
			    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/86percent-'));
			});
			$('.asset').attr({height:'361',width:'361'});
			$("#SubTitleRow").hide();
		break;
		
		}

		
		
		if ($('#posecontainer div').is('.model5')==true) {
			//alert('silhouette!');
			if ($('.model5 img').is('.Clothes')==true) {
				//alert('couples silhouette!');
				$('.Template').css({width: '170px'});
				$('#preview-wrapper-h div').css({top: '10px', left:'25px'});
			}else{
				//alert('not couples silhouette!');
			}
		}
		

		if (FirstPerson!='' && FirstPerson!=0) {
			$.get('/pose.asp',{pose:FirstPerson,nt: (new Date().getTime())},function(poseHTML){
				$('#posecontainer').append(poseHTML);
				//$('#posecontainer').iunfixpng();
				
				
				// Two People Mode, get busy with placement
				
				if ($('.model1').eq(0).is('div') && $('.model2').eq(0).is('div')) { // Man & Woman
				
					if (ProductType == 2) {
					
						$('#posecontainer').css({position:'absolute',top:'165px',left:'-18px'});
						$('.model1').attr({height:'361px',width:'361px'});
						$('.model1').css({position:'absolute',top:'44px',left:'36px', zIndex:'1009'});
						$('.model2').attr({height:'380',width:'380'});
						$('.model2').css({position:'absolute',top:'5px',left:'-114px'});
						
						$('.model1 .asset').attr('src',function(src){
						    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/86percent-'));
						});
						$('.model1 .asset').attr({height:'361px',width:'361px'});
	
						$('.model2 .asset').attr('src',function(src){
						    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/95percent-'));
						    $('#Title').val($('#Title').val()+'/img/95percent-'+src);
						});
						$('.model2 .asset').attr({height:'399px',width:'399px'});
					
					
					} else {
				
						$('.model2').css({position:'absolute',top:'10px',left:'-140px'});
						$('.model1').css({position:'absolute',top:'50px',left:'15px', zIndex:'1009'});
						$('.Template').css({width: '170px',textAlign:'center', margin: '0 0 11px 0'});
						$('#preview-wrapper-h div').css({top:'44px',left:'295px', zIndex:'1010'});
						$('.model1 img').attr('src',function(src){
						    
						    //alert(buildIMGURL($(this).attr('src'),'/img/95percent-'));
						    $(this).attr({height:'399',width:'399'});
						    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/95percent-'));
						    //$('#Body').html($('#Body').html()+'<br />/img/95percent-'+src);
						    
						});
						//$('.model1 .img').attr({height:'399',width:'399'});
						//alert('here');
					
					}
				}
				if ($('.model1').eq(1).is('div')) { // Two Women
					$('.model1').eq(0).css({position:'absolute',top:'6px',left:'94px', zIndex:'1008'});
					$('.model1').eq(1).css({position:'absolute',top:'6px',left:'207px', zIndex:'1009'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'30px', zIndex:'1010'});
					$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});
				}
				if ($('.model2').eq(1).is('div')) { // Two Men
					$('.model2').eq(1).css({position:'absolute',top:'18px',left:'-138px', zIndex:'1008'});
					$('.model2').eq(0).css({position:'absolute',top:'18px',left:'207px', zIndex:'1009'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'163px', zIndex:'1010'});	
					$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});		
				}
				if ($('.model4').eq(1).is('div')) { // Two Little Girls
					$('.model4').eq(1).css({position:'absolute',top:'61px',left:'-85px', zIndex:'1008'});
					$('.model4').eq(0).css({position:'absolute',top:'61px',left:'212px', zIndex:'1009'});
					$('.model4 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/86percent-'));
					});
					$('.model4 img').attr({height:'361',width:'361'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'163px', zIndex:'1010'});
					$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});
				}
				
				if ($('.model3').eq(1).is('div')) { // Two Little Boys
					$('.model3').eq(1).css({position:'absolute',top:'61px',left:'-85px', zIndex:'1008'});
					$('.model3').eq(0).css({position:'absolute',top:'61px',left:'212px', zIndex:'1009'});
					$('.model3 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/86percent-'));
					});
					$('.model3 img').attr({height:'361',width:'361'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'163px', zIndex:'1010'});
					$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});
				}
				
				if ($('.model2').eq(0).is('div') && $('.model4').eq(0).is('div')) { // Man & Girl
					$('.model2').css({position:'absolute',top:'10px',left:'205px'});
					$('.model4').css({position:'absolute',top:'61px',left:'95px', zIndex:'1009'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'25px', zIndex:'1010'});
					$('.model4 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/86percent-'));
					});
					$('.model4 img').attr({height:'361',width:'361'});
					$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});
				}

				if ($('.model2').eq(0).is('div') && $('.model3').eq(0).is('div')) { // Man & Boy
					$('.model2').css({position:'absolute',top:'10px',left:'205px'});
					$('.model3').css({position:'absolute',top:'61px',left:'95px', zIndex:'1009'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'25px', zIndex:'1010'});
					$('.model3 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/86percent-'));
					});
					$('.model3 img').attr({height:'361',width:'361'});
					$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});
				}
				

				if ($('.model1').eq(0).is('div') && $('.model4').eq(0).is('div')) { // Woman & Girl
					$('.model1').css({position:'absolute',top:'30px',left:'216px', zIndex:'1200'});
					$('.model4').css({position:'absolute',top:'61px',left:'95px', zIndex:'999'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'25px', zIndex:'1010'});
					$('.model4 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/86percent-'));
					});
					$('.model4 img').attr({height:'361',width:'361'});
					$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});
				}			

				if ($('.model1').eq(0).is('div') && $('.model3').eq(0).is('div')) { // Woman & Boy
					$('.model1').css({position:'absolute',top:'30px',left:'216px', zIndex:'1100'});
					$('.model3').css({position:'absolute',top:'64px',left:'95px', zIndex:'1200'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'25px', zIndex:'1010'});
					$('.model3 img').attr('src',function(src){
						$(this).attr('src',buildIMGURL($(this).attr('src'),'/img/86percent-'));
					});
					$('.model3 img').attr({height:'361',width:'361'});
					$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});
				}

				if ($('.model1').eq(0).is('div') && $('.model6').eq(0).is('div')) { // Woman & Baby
					$('#posecontainer').css({height:'375px'});
					$('.model1').css({position:'absolute',top:'36px',left:'216px', zIndex:'1200'});
					$('.model6').css({position:'absolute',top:'5px',left:'56px', zIndex:'1201'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'25px', zIndex:'1010'});
					$('.model6 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/520px-'));
					});
					$('.model6 img[@src$=.png]').attr({height:'520',width:'520'});
				}

				if ($('.model2').eq(0).is('div') && $('.model6').eq(0).is('div')) { // Man & Baby
					$('#posecontainer').css({height:'375px',top:'51px',left:'61px'});
					$('.model2').css({position:'absolute',top:'10px',left:'205px'});
					$('.model6').css({position:'absolute',top:'3px',left:'46px', zIndex:'999'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'25px', zIndex:'1010'});
					$('.model6 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/520px-'));
					});
					$('.model6 img').attr({height:'520',width:'520'});
				}

				if ($('.model4').eq(0).is('div') && $('.model6').eq(0).is('div')) { // Girl & Baby
					$('#posecontainer').css({height:'375px',top:'51px',left:'61px'});
					$('.model4').css({position:'absolute',top:'14px',left:'195px'});
					$('.model6').css({position:'absolute',top:'3px',left:'46px', zIndex:'999'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'25px', zIndex:'1010'});
					$('.model6 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/520px-'));
					});
					$('.model6 img').attr({height:'520',width:'520'});
				}

				if ($('.model3').eq(0).is('div') && $('.model6').eq(0).is('div')) { // Boy & Baby
					$('#posecontainer').css({height:'375px',top:'51px',left:'61px'});
					$('.model3').css({position:'absolute',top:'17px',left:'195px'});
					$('.model6').css({position:'absolute',top:'3px',left:'26px', zIndex:'999'});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'25px', zIndex:'1010'});
					$('.model6 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/520px-'));
					});
					$('.model6 img').attr({height:'520',width:'520'});
				}

				if ($('.model3').eq(0).is('div') && $('.model4').eq(0).is('div')) { // Boy & Girl
					$('.model3').eq(0).css({position:'absolute',top:'11px',left:'-135px', zIndex:'1008'});
					$('.model4').eq(0).css({position:'absolute',top:'11px',left:'207px', zIndex:'1009'});
					$('.model4 img, .model3 img').attr('src',function(src){
					    //$(this).attr('src',buildIMGURL($(this).attr('src'),'/img/86percent-'));
					});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'163px', zIndex:'1010'});
					$('#posecontainer').css({position:'absolute',top:'56px',left:'54px'});
				}

				if ($('.model6').eq(0).is('div') && $('.model6').eq(1).is('div')) { // Two Babys
					$('#posecontainer').css({position:'absolute',height:'375px',width:'500px',top:'50px',left:'55px'});
					$('.model6').eq(1).css({position:'absolute',top:'-77px',left:'108px', zIndex:'1009'});
					$('.model6').eq(0).css({position:'absolute',top:'-77px',left:'-218px', zIndex:'1009'});
					$('.model6 img').attr('src',function(src){
					    $(this).attr('src',buildIMGURL($(this).attr('src'),'/img/635px-'));
					});
					$('.Template').css({width: '170px'});
					$('p.Template').css({width: '170px'});
					$('#preview-wrapper-h div').css({top:'10px',left:'163px', zIndex:'1010'});
					$('.model6 img').attr({height:'635',width:'635'});
					//alert('1');
				}
				
				$('#posecontainer img[@src$=.png]').ifixpng();
				//$('#posecontainer img[@src$=.png]').css('border','1px solid #0ee');
			});
	
		}
		else // one person mode
		{
			$('#posecontainer img[@src$=.png]').ifixpng();
		}
		
		  if (PoseMode='CartPreview') {
		  	$('#punch-h').css('top','48px');
		  	positionText ();
		  }
		  
		//$('#posecontainer img[@src$=.png]').css('border','1px solid #0ee');
		//alert($('#posecontainer img[@src$=.png]').eq(0).dimension.position());
	});
	
	
	
  }

  if (PoseMode == 'Email' || PoseMode == 'ViewPoseCard'){
  	$("#Copy").html($("#Copy").html().replace(/[\r\n]/g,'<br>'));
  }

  if (PoseMode=='BuildAvatar'){
	$.get('/pose.asp',{pose:PoseID,nt: (new Date().getTime())},function(poseHTML){
		$('#posebuilder').html(poseHTML);
		
	  	if (Model=='BB'){
	  	// enlarge baby in solitary mode
			//alert($('.asset').length);
			$('.asset').attr({height:'745',width:'745'});
			$('#posebuilder').css({width:'745px',height:'745px',top:'-150px',left:'370px'});
		}
		$('#posebuilder img[@src$=.png]').ifixpng();
	});
  }




  // Bind Save Model
  $("#Save").click(function() {
	//resetCookie();
	//collfig();
	if ($.cookie('MyPose') != '1' ) {
	$("#Preview").show();
	}
	$("#savemypose").show();
	//alert($.cookie('MyPose'));
	if (FirstPerson == '0' && Model !='S' && $.cookie('MyPose') != '1' ) {
		$("#secondperson").show();
		alert("your pose has been saved.  please click on the personalize message button or make a second person.");
	}
	else
	{
		if ($.cookie('MyPose') == '1' ) {
		}
		else
		{
		alert("your pose has been saved.  please click on the personalize message button.");
		}
	}
	
	// new code
	$.get('/input.asp',{Type:'SavePose', CartID: $.cookie('CartID'), PoseID: PoseID,nt: (new Date().getTime())});	
	
	
  });
  
  // Bind Reset
  $("#Reset").click(function() {
		$.get('/input.asp',{Type:'ClearLayer', PoseID: PoseID,nt: (new Date().getTime())});	
  	//$.cookie('CartID', '', { expires: -1 });
  	document.location.href='builder.asp?B1=Change+Model&ProductID=0&Action=BuildAvatar&cg='+$.cookie("cg")+'&cs='+$.cookie("cs");
  });
  

  
  // Bind Retrieve
  $("#Retrieve").click(function() {

  });
  
  

  // Bind Preview
	$("#Print").hide();
	$("#Email").hide();
	$("#secondperson").hide();
  
	$("#PreviewCard").click(function() {
		$("#Heading").text($("#Title").val().ltrim());
		$("#SubHead").text($("#SubTitle").val());
		$("#Copy").html($("#Message").val().replace(/[\r\n]/g,'<br>'));
		//alert($("#Message").val());

		$("input#CartHeading").val($("#Title").val().ltrim());
		$("input#CartSubHead").val($("#SubTitle").val());
		$("input#CartMessage").val($("#Message").val());

		$("input#CardHeading").val($("#Title").val().ltrim());
		$("input#CardSubHead").val($("#SubTitle").val());
		$("input#CardMessage").val($("#Message").val());

		positionText();
		$("#Print").show();
		$("#Email").show();
		//$("#previewActions").css({position:'absolute',zIndex:'5000',top:'300px',left:'0px'});
	});


	$('.productpreview').click( function () {
		
		$('#icartpreview').attr('src','/cartpreview.asp?cartid=c'+$(this).attr('id'));
		
		//$.get('/preview.asp',{CartID:$(this).attr('id'),nt: (new Date().getTime())},function(poseHTML){
			//$('#loadingpreview').html(poseHTML);
			//replaceNewLines();
			//$('#posebuilder img[@src$=.png]').ifixpng();
		//});
		//$('#loading').html('<p>here</p><img src="'+$(this).attr('src').replace('thumb','med')+'">');
		//$('#dialogcart').bgiframe();
		//$('#dialogcart').css({position:'absolute',top:'300px',bottom:'300px',left:'100px',right:'100px',zIndex:'11111'});
		
		$('#dialogcart').jqm().jqmShow();
	});
	
	$('.closedialog').click( function () {
		$('.jqmWindow').jqmHide();
	});
	
	$("#preview-wrapper-v").dropShadow({left: 3, top: 3, blur: 3, opacity: 1});

});


function layerClick (inputElementID) {
	$.get('/input.asp',{Type:'SaveElement', PoseID: PoseID, ElementID: inputElementID,nt: (new Date().getTime())});

	$.get('/pose.asp',{pose:PoseID,nt: (new Date().getTime())},function(poseHTML){
	

		$('#posebuilder').html(poseHTML);
	  	if ($('.model6').is('div')){
			$('.asset').attr({height:'745',width:'745'});
			}
		$.cookie('PoseID',PoseID);
	});
	
	
	
	
}

function clearLayers (inputAssetID) {
	if($("#isSilhouettes").length > 0)
	{$.get('/input.asp',{Type:'ClearLayer', PoseID: PoseID, AssetID: inputAssetID, isSilhouettes: "1", nt: (new Date().getTime())});}
	else
	{$.get('/input.asp',{Type:'ClearLayer', PoseID: PoseID, AssetID: inputAssetID,nt: (new Date().getTime())});}

	$.get('/pose.asp',{pose:PoseID,nt: (new Date().getTime())},function(poseHTML){
		$('#posebuilder').html(poseHTML);
		$('#posebuilder img[@src$=.png]').ifixpng();
		
	  	if ($('.model6').is('div')){
			$('.asset').attr({height:'745',width:'745'});
			}
		
	});

	
}

function replaceNewLines () {
	$("#Copy").html($("#Copy").html().replace(/[\r\n]/g,'<br />'));
}

function buildIMGURL (src,prefix) {
	// this removes absolute & relative paths from images
	var str = src;
	var pre = prefix;
	if (str.indexOf('1px')==-1){
	str = str.replace('86percent-','');
	str = str.replace('95percent-','');
	str = str.replace('http://beta.poseprints.com','');
	str = str.replace('http://www.poseprints.com','');
	str = str.replace('http://poseprints.com','');
	str = str.replace('http://cdn1.poseprints.com','');
	str = str.replace('http://poseprints.s3.amazonaws.com/','');
	str = str.replace('/img/','');
	str = pre+str
	}
	return str
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function positionText () {
	if ($("#Heading").html().length == 0 || $("#Heading").html==' '){$("#Heading").html('&nbsp;')}
}