var Pyra={
	initProduct:function(){
		var okToGo=available=false;
		var opsLen=$('.productOptions').length;
		if(opsLen == 0){
			okToGo=true;
		}
		$('.productOptions select').change(function(){
			var sel=$(this).attr('name').substr(14,1);
			for(var i=0;i<opsLen;i++){
				if(sel==i||sel>i){
					continue;
				}else{
					$("select[name='productOption["+i+"]']").get(0).selectedIndex=0;
				}
			}
			if(sel==opsLen-1){
				var fm=$(this).get(0).form;
				var ops={id:$('#product_id').val()};
				var send=true;
				for(var i=0;i<opsLen;i++){
					ops['po['+i+']']=fm.elements["productOption["+i+"]"].value;
					if(fm.elements["productOption["+i+"]"].value==''){
						send=false;
					}
				}
				if(send){
					$.post('/xml_http_req/product_option',ops,
						function(data){
							okToGo=true;
							var ac=data.documentElement.childNodes[0].firstChild.nodeValue;
							var cd=data.documentElement.childNodes[1].firstChild.nodeValue;
							var price=data.documentElement.childNodes[2].firstChild.nodeValue;
							var save=data.documentElement.childNodes[3].firstChild.nodeValue;
							var weight=data.documentElement.childNodes[4].firstChild.nodeValue;
							var av=data.documentElement.childNodes[5].firstChild.nodeValue;
							if(av>0){
								available=true;
							}
							$('.pdPrice').html(price);
							$('.saveTxt').html(save);
						}
					);
				}
			}
		});
		$('#pForm').submit(function(){
			
			reqCk = true;
			$('.requiredField').each(function() {
				 if($(this).val() == ''){
					reqCk = false;
				 }
			});
			if(!reqCk){ alert('Please fill in all required (*) fields!'); return false; }
			
			if($('#optionsRequired').val()==0){
				okToGo=true;
				available=true;
			}
			if(okToGo){
				if(available){
					return true;
				}else{
					if(opsLen==0){
						return true;
					}else{
						alert('Item out of stock');
						return false;
					}
				}
			}else{
				alert('Please choose all options');
				return false;
			}
		});
		$('#loginForm').submit(function(){
			if($('#email').val() != '' && $('#psswrd').val() != ''){
				$.post('/xml_http_req/auth_user',{e:$('#email').val(),p:$('#psswrd').val()},
					function(data){
						if(data.documentElement.firstChild.nodeValue == 1){
							$('#postComment').show();
							$('#commentSignin').hide();
							$('#commentSignup').hide();
						}else{
							$('#postComment').hide();
						}
					}
				);
			}else{
				alert('Please complete all form fields.');
			}
			return false;
		});
		$('#postBtn').submit(function(){
			if($('#comment').val() != ''){
				$.post('/xml_http_req/post_comment',{c:$('#comment').val(),i:$('#product_id').val(),r:1},
					function(data){
						if(data.documentElement.firstChild.nodeValue == 1){
							alert('Your comment has been posted successfully, and will be visible once reviewed.');
							$('#comment').val('');
						}else{
							alert('Comment post unsuccessful.');
						}
					}
				);
			}else{
				alert('Please type your comment in the box provided.');
			}
			return false;
		});
		$('.image-show').mouseover(function(){
			$('.p-image').hide();
			$('#'+$(this).get(0).id+'-lg').fadeIn();
		});
		$("#preview-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width: 500,
			height: 530,
			draggable: false,
			modal: true
		});
	},
	newsletterFields:function(){
		$("select[name='list_id']").change(function(){
			var sel=$(this);
			$.post('/xml_http_req/custom_fields',{l:sel.val()},
				function(data){
					$('#newsletterFields').html(data.documentElement.firstChild.nodeValue);
				}
			);
		});		
	},
	vmjMenu:function(){
		$.post('/xml_http_req/vmj_menu',{},
			function(data){
				$('#vmj-menu').html(data.documentElement.firstChild.nodeValue);
				$('#vmjsrcfld').addClass('vmj-search-off');
				$('#vmjsrcfld').val('Enter a word or phrase...');
				$('#vmjsrcfld').blur(function(){
					if($(this).val()==''){
						$(this).val('Enter a word or phrase...').addClass('vmj-search-off');
					}
				});
				$('#vmjsrcfld').focus(function(){
					if($(this).val()=='Enter a word or phrase...'){
						$(this).val('');
					}
					$(this).removeClass('vmj-search-off');
				});
				$('#vmjsrcf').submit(function(){
					if($('#vmjsrcfld').val()==''||$('#vmjsrcfld').val()=='Enter a word or phrase...'){
						return false;
					}else{
						return true;
					}
				});
			}
		);
	},
	previewImg:function(f,i){
		var i=typeof(i)!='undefined'?i:'';
		var lnk='';
		var lg='';
		if(i!=''){
			lnk='/uploads/images/'+i;
			lg='<br />Click image to enlarge';
		}
		$('#preview-dialog').html('<div style="text-align:center;"><a href="'+lnk+'" target="_blank"><img src="'+f+'" /></a>'+lg+'</div>');
		$('#preview-dialog').dialog('open');
	}
};
$(document).ready(function(){
	Pyra.vmjMenu();
});
